Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Add type check to & and | #8965

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading