Skip to content

Commit

Permalink
minify: more constant folding for strict equality
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 6, 2025
1 parent 4cdf03c commit 22ecd30
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 24 additions & 0 deletions internal/js_ast/js_ast_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,12 @@ func CheckEqualityIfNoSideEffects(left E, right E, kind EqualityKind) (equal boo
case *ENull, *EUndefined:
// "(not null or undefined) == undefined" is false
return false, true

default:
if kind == StrictEquality && IsPrimitiveLiteral(right) {
// "boolean === (not boolean)" is false
return false, true
}
}

case *ENumber:
Expand Down Expand Up @@ -1523,6 +1529,12 @@ func CheckEqualityIfNoSideEffects(left E, right E, kind EqualityKind) (equal boo
case *ENull, *EUndefined:
// "(not null or undefined) == undefined" is false
return false, true

default:
if kind == StrictEquality && IsPrimitiveLiteral(right) {
// "number === (not number)" is false
return false, true
}
}

case *EBigInt:
Expand All @@ -1535,6 +1547,12 @@ func CheckEqualityIfNoSideEffects(left E, right E, kind EqualityKind) (equal boo
case *ENull, *EUndefined:
// "(not null or undefined) == undefined" is false
return false, true

default:
if kind == StrictEquality && IsPrimitiveLiteral(right) {
// "bigint === (not bigint)" is false
return false, true
}
}

case *EString:
Expand All @@ -1547,6 +1565,12 @@ func CheckEqualityIfNoSideEffects(left E, right E, kind EqualityKind) (equal boo
case *ENull, *EUndefined:
// "(not null or undefined) == undefined" is false
return false, true

default:
if kind == StrictEquality && IsPrimitiveLiteral(right) {
// "string === (not string)" is false
return false, true
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2860,14 +2860,14 @@ func TestConstantFolding(t *testing.T) {

expectPrinted(t, "x = 1 === 1", "x = true;\n")
expectPrinted(t, "x = 1 === 2", "x = false;\n")
expectPrinted(t, "x = 1 === '1'", "x = 1 === \"1\";\n")
expectPrinted(t, "x = 1 === '1'", "x = false;\n")
expectPrinted(t, "x = 1 == 1", "x = true;\n")
expectPrinted(t, "x = 1 == 2", "x = false;\n")
expectPrinted(t, "x = 1 == '1'", "x = 1 == \"1\";\n")

expectPrinted(t, "x = 1 !== 1", "x = false;\n")
expectPrinted(t, "x = 1 !== 2", "x = true;\n")
expectPrinted(t, "x = 1 !== '1'", "x = 1 !== \"1\";\n")
expectPrinted(t, "x = 1 !== '1'", "x = true;\n")
expectPrinted(t, "x = 1 != 1", "x = false;\n")
expectPrinted(t, "x = 1 != 2", "x = true;\n")
expectPrinted(t, "x = 1 != '1'", "x = 1 != \"1\";\n")
Expand Down Expand Up @@ -2932,6 +2932,8 @@ func TestConstantFolding(t *testing.T) {
expectPrinted(t, "x = 0n !== 1n", "x = true;\n")
expectPrinted(t, "x = 0n !== 0n", "x = false;\n")
expectPrinted(t, "x = 123n === 1_2_3n", "x = true;\n")
expectPrinted(t, "x = 0n === '1n'", "x = false;\n")
expectPrinted(t, "x = 0n !== '1n'", "x = true;\n")

expectPrinted(t, "x = 0n === 0b0n", "x = 0n === 0b0n;\n")
expectPrinted(t, "x = 0n === 0o0n", "x = 0n === 0o0n;\n")
Expand Down

0 comments on commit 22ecd30

Please sign in to comment.