Skip to content

Commit

Permalink
fix(es/minifier): Fix case matching (#9208)
Browse files Browse the repository at this point in the history
**Description:**

Caused by #7112
Since swc minifier evaluates expressions in a separate pass, I make a temporary patch here.

**Related issue:**

 - Closes #9176
  • Loading branch information
CPunisher authored Jul 11, 2024
1 parent bcfc650 commit f81fa6e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/switches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ impl Optimizer<'_> {
for (idx, case) in stmt.cases.iter_mut().enumerate() {
if let Some(test) = case.test.as_ref() {
if let Some(e) = is_primitive(&self.expr_ctx, tail_expr(test)) {
if e.eq_ignore_span(tail) {
if match (e, tail) {
(Expr::Lit(Lit::Num(e)), Expr::Lit(Lit::Num(tail))) => {
e.value == tail.value
}
_ => e.eq_ignore_span(tail),
} {
cases.push(case.take());

exact = Some(idx);
Expand Down
10 changes: 10 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9176/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
const k = (function () {
switch (-0) {
case 0:
console.log("hi");
break;
default:
throw 0;
}
})();
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9176/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
console.log("hi");

0 comments on commit f81fa6e

Please sign in to comment.