From 6a570a334c787358d67ae6c590a7fd0e916f35e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 21 Mar 2023 17:46:22 +0900 Subject: [PATCH] fix(es/ast): Fix `EqIgnoreSpan` impl of `Number` (#7112) **Related issue:** - Closes https://github.com/swc-project/swc/issues/7111. --- crates/swc_ecma_ast/src/lit.rs | 2 +- crates/swc_ecma_minifier/tests/fixture/issues/7111/input.js | 1 + crates/swc_ecma_minifier/tests/fixture/issues/7111/output.js | 1 + .../tests/fixture/next/wrap-contracts/output.js | 4 ++-- 4 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7111/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7111/output.js diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index 0d40b4e2ca86..8cefaab9e492 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -350,7 +350,7 @@ impl Eq for Number {} impl EqIgnoreSpan for Number { fn eq_ignore_span(&self, other: &Self) -> bool { - self.value == other.value + self.value == other.value && self.value.is_sign_positive() == other.value.is_sign_positive() } } diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7111/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7111/input.js new file mode 100644 index 000000000000..eccb24cc3093 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7111/input.js @@ -0,0 +1 @@ +console.log(Math.random() > 0.5 ? -0 : 0) diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7111/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7111/output.js new file mode 100644 index 000000000000..9ab197546770 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7111/output.js @@ -0,0 +1 @@ +console.log(Math.random() > 0.5 ? -0 : 0); diff --git a/crates/swc_ecma_minifier/tests/fixture/next/wrap-contracts/output.js b/crates/swc_ecma_minifier/tests/fixture/next/wrap-contracts/output.js index 56b4dcd69a91..ddaeee0488bd 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/wrap-contracts/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/wrap-contracts/output.js @@ -7207,7 +7207,7 @@ if (m.c ? !m.c[0] : !m.s) return new BigNumber(NaN); (isModExp = !nIsNeg && x.isInteger() && m.isInteger()) && (x = x.mod(m)); } else { - if (n.e > 9 && (x.e > 0 || x.e < -1 || (0 == x.e ? x.c[0] > 1 || nIsBig && x.c[1] >= 24e7 : x.c[0] < 8e13 || nIsBig && x.c[0] <= 9999975e7))) return x.s < 0 && isOdd(n), k = -0, x.e > -1 && (k = 1 / k), new BigNumber(nIsNeg ? 1 / k : k); + if (n.e > 9 && (x.e > 0 || x.e < -1 || (0 == x.e ? x.c[0] > 1 || nIsBig && x.c[1] >= 24e7 : x.c[0] < 8e13 || nIsBig && x.c[0] <= 9999975e7))) return k = x.s < 0 && isOdd(n) ? -0 : 0, x.e > -1 && (k = 1 / k), new BigNumber(nIsNeg ? 1 / k : k); POW_PRECISION && (k = mathceil(POW_PRECISION / LOG_BASE + 2)); } for(nIsBig ? (half = new BigNumber(0.5), nIsNeg && (n.s = 1), nIsOdd = isOdd(n)) : nIsOdd = (i = Math.abs(+valueOf(n))) % 2, y = new BigNumber(ONE);;){ @@ -7258,7 +7258,7 @@ var xe = x.e / LOG_BASE, ye = y.e / LOG_BASE, xc = x.c, yc = y.c; if (!xe || !ye) { if (!xc || !yc) return xc ? (y.s = -b, y) : new BigNumber(yc ? x : NaN); - if (!xc[0] || !yc[0]) return yc[0] ? (y.s = -b, y) : new BigNumber(xc[0] ? x : -0); + if (!xc[0] || !yc[0]) return yc[0] ? (y.s = -b, y) : new BigNumber(xc[0] ? x : 3 == ROUNDING_MODE ? -0 : 0); } if (xe = bitFloor(xe), ye = bitFloor(ye), xc = xc.slice(), a = xe - ye) { for((xLTy = a < 0) ? (a = -a, t = xc) : (ye = xe, t = yc), t.reverse(), b = a; b--; t.push(0));