Skip to content

Commit

Permalink
fix(es/minifier): Fix variable declaration in default branch (#9220)
Browse files Browse the repository at this point in the history
**Description:**

When a default branch appears before an exact match, the variable declaration should be recorded before deleting the default branch.

**Related issue:**

 - Closes #8919
  • Loading branch information
CPunisher committed Jul 12, 2024
1 parent daf1025 commit a7c82bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/switches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ impl Optimizer<'_> {

if !may_match_other_than_exact {
// remove default if there's an exact match
cases.retain(|case| case.test.is_some());
cases.retain(|case| {
if case.test.is_some() {
true
} else {
var_ids.extend(case.cons.extract_var_ids());
false
}
});
}

if cases.len() == 2 {
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8919/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
const k = (() => {
switch ("") {
default:
var x;
case "":
x;
}
return x;
})();
console.log(k);
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8919/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
console.log(void 0);

0 comments on commit a7c82bd

Please sign in to comment.