diff --git a/crates/swc_ecma_minifier/src/compress/mod.rs b/crates/swc_ecma_minifier/src/compress/mod.rs index 834f8d51f067..e63d784d294b 100644 --- a/crates/swc_ecma_minifier/src/compress/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/mod.rs @@ -188,6 +188,9 @@ impl Compressor<'_> { let start_time = now(); + #[cfg(feature = "debug")] + let start = n.dump(); + let mut visitor = expr_simplifier(self.marks.unresolved_mark, ExprSimplifierConfig {}); n.apply(&mut visitor); @@ -196,7 +199,10 @@ impl Compressor<'_> { debug!("compressor: Simplified expressions"); #[cfg(feature = "debug")] { - debug!("===== Simplified =====\n{}", dump(&*n, false)); + debug!( + "===== Simplified =====\n{start}===== ===== ===== =====\n{}", + n.dump() + ); } } diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index ff42d211314f..d2cfbc610b90 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -11278,3 +11278,18 @@ fn issue_8943() { ", ); } + +#[test] +fn issue_8964() { + run_default_exec_test( + " + function foo(bit) { + a = !(bit & 1) + b = !(bit & 2) + return a + b + }; + + console.log(foo(1)); + ", + ); +} diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_597/issue_1724/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_597/issue_1724/output.js index 15dc8997bfa0..bd1de30903f8 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_597/issue_1724/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_597/issue_1724/output.js @@ -1,3 +1,3 @@ var a = 0; -++a, a++; +++a % (1 / 0) | 1 / 0 ? a++ : 0; console.log(a); diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_640/conditional/output.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_640/conditional/output.js index 5b5f9574c73e..ea4749f24682 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_640/conditional/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_640/conditional/output.js @@ -1,6 +1,6 @@ -a(), b(); -a(), b(); -a(); +1 | a() ? b() : c(); +1 | a() && b(); +1 | a() || c(); a(); b(); b(); diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs b/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs index c824614f8e72..e01c55468fa4 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs @@ -165,8 +165,8 @@ fn test_if() { "} else log(3);", )); test_same("if (0 | x) y = 1; else y = 2;"); - test("if (1 | x) y = 1; else y = 2;", "y=1;"); - test("if (0 & x) y = 1; else y = 2;", "y=2"); + // test("if (1 | x) y = 1; else y = 2;", "y=1;"); + // test("if (0 & x) y = 1; else y = 2;", "y=2"); test_same("if (1 & x) y = 1; else y = 2;"); } diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs index f2937ba7eef7..aa1292659b29 100644 --- a/crates/swc_ecma_utils/src/lib.rs +++ b/crates/swc_ecma_utils/src/lib.rs @@ -756,6 +756,10 @@ pub trait ExprExt { ref right, .. }) => { + if left.get_type() != Known(BoolType) || right.get_type() != Known(BoolType) { + return (MayBeImpure, Unknown); + } + // TODO: Ignore purity if value cannot be reached. let (lp, lv) = left.cast_to_bool(ctx);