Skip to content

Commit

Permalink
Fix invalid mangling of zero and negative zero (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsao authored Apr 16, 2021
1 parent e2af734 commit 06452f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,15 @@ func valuesLookTheSame(left js_ast.E, right js_ast.E) bool {
}
return true
}

// Special-case to distinguish between negative an non-negative zero when mangling
// "a ? -0 : 0" => "a ? -0 : 0"
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
case *js_ast.ENumber:
b, ok := right.(*js_ast.ENumber)
if ok && a.Value == 0 && b.Value == 0 && math.Signbit(a.Value) != math.Signbit(b.Value) {
return false
}
}

equal, ok := checkEqualityIfNoSideEffects(left, right)
Expand Down
7 changes: 7 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,13 @@ func TestMangleIf(t *testing.T) {
expectPrintedMangle(t, "let b; a = null === b || b === undefined ? c : b", "let b;\na = b ?? c;\n")
expectPrintedMangle(t, "let b; a = b !== undefined && b !== null ? b : c", "let b;\na = b ?? c;\n")

// Distinguish between negative an non-negative zero (i.e. Object.is)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
expectPrintedMangle(t, "a(b ? 0 : 0)", "a((b, 0));\n")
expectPrintedMangle(t, "a(b ? +0 : -0)", "a(b ? 0 : -0);\n")
expectPrintedMangle(t, "a(b ? +0 : 0)", "a((b, 0));\n")
expectPrintedMangle(t, "a(b ? -0 : 0)", "a(b ? -0 : 0);\n")

expectPrintedMangle(t, "a ? b : b", "a, b;\n")
expectPrintedMangle(t, "let a; a ? b : b", "let a;\nb;\n")

Expand Down

0 comments on commit 06452f9

Please sign in to comment.