Skip to content

Commit

Permalink
fix(es/minifier): Add type check to & and | (#8965)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8964
  • Loading branch information
kdy1 authored May 22, 2024
1 parent 59e310d commit 545ec51
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
8 changes: 7 additions & 1 deletion crates/swc_ecma_minifier/src/compress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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()
);
}
}

Expand Down
15 changes: 15 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
",
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var a = 0;
++a, a++;
++a % (1 / 0) | 1 / 0 ? a++ : 0;
console.log(a);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
a(), b();
a(), b();
a();
1 | a() ? b() : c();
1 | a() && b();
1 | a() || c();
a();
b();
b();
Original file line number Diff line number Diff line change
Expand Up @@ -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;");
}

Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 545ec51

Please sign in to comment.