Skip to content

Commit a7c82bd

Browse files
authored
fix(es/minifier): Fix variable declaration in default branch (#9220)
**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
1 parent daf1025 commit a7c82bd

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

crates/swc_ecma_minifier/src/compress/optimize/switches.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ impl Optimizer<'_> {
8686

8787
if !may_match_other_than_exact {
8888
// remove default if there's an exact match
89-
cases.retain(|case| case.test.is_some());
89+
cases.retain(|case| {
90+
if case.test.is_some() {
91+
true
92+
} else {
93+
var_ids.extend(case.cons.extract_var_ids());
94+
false
95+
}
96+
});
9097
}
9198

9299
if cases.len() == 2 {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
const k = (() => {
3+
switch ("") {
4+
default:
5+
var x;
6+
case "":
7+
x;
8+
}
9+
return x;
10+
})();
11+
console.log(k);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
console.log(void 0);

0 commit comments

Comments
 (0)