Skip to content

Commit

Permalink
fix(es/minifier): Handle switch cases (#8854)
Browse files Browse the repository at this point in the history
**Related issue:**

- Closes #8813
  • Loading branch information
magic-akari committed Apr 13, 2024
1 parent fcc82a5 commit 7a89e5d
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 28 deletions.
25 changes: 23 additions & 2 deletions crates/swc/tests/tsc-references/switchStatements.2.minified.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _type_of } from "@swc/helpers/_/_type_of";
import { _ as _create_super } from "@swc/helpers/_/_create_super";
(M || (M = {})).fn = function(x) {
switch((M || (M = {})).fn = function(x) {
return "";
}, void 0 === x || _type_of(x), void 0 === M || _type_of(M), M.fn(1);
}, x){
case "":
case 12:
case !0:
case null:
case void 0:
case new Date(12):
case {}:
case /[a-z]/:
case []:
case {}:
case {
id: 12
}:
case [
"a"
]:
case void 0 === x ? "undefined" : _type_of(x):
case void 0 === M ? "undefined" : _type_of(M):
case M.fn(1):
default:
}
var M, x, C = function C() {
_class_call_check(this, C);
}, D = function(C) {
Expand Down
74 changes: 74 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8813/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const k1 = (() => {
const x = 'asdf';
let y = "PASS 1";
switch (x) {
case x:
default:
case y = "FAIL":
}
console.log(y);
})();

const k2 = (() => {
const x = 'asdf';
let y = "PASS 2";
switch (x) {
case x:
case y = "FAIL":
default:
}
console.log(y);
})();

const k3 = (() => {
const x = 'asdf';
let y = "FAIL";
switch (x) {
case (y = "PASS 3", x):
default:
}
console.log(y);
})();

const k4 = (() => {
const x = 'asdf';
let y = "FAIL";
switch (x) {
case y = "PASS 4":
case x:
default:
}
console.log(y);
})();

const k5 = (() => {
const x = 'asdf';
let y = "FAIL";
let z = "FAIL";
switch (x) {
case y = "PASS 5":
case z = "PASS 5":
case x:
default:
}
console.log(y, z);
})();


var c = "FAIL";
(function () {
function f(a, NaN) {
function g() {
switch (a) {
case a:
break;
case ((c = "PASS"), NaN):
c = "FAIL"
break;
}
}
g();
}
f(0 / 0);
})();
console.log(c);
36 changes: 36 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8813/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(()=>{
const x = 'asdf';
let y = "PASS 1";
switch(x){
case x:
default:
case y = "FAIL":
}
console.log(y);
})(), (()=>{
const x = 'asdf';
let y = "PASS 2";
switch(x){
case x:
case y = "FAIL":
default:
}
console.log(y);
})(), console.log("PASS 3"), console.log("PASS 4"), (()=>{
let y = "FAIL", z = "FAIL";
switch('asdf'){
case y = "PASS 5":
case z = "PASS 5":
default:
}
console.log(y, z);
})();
var a, c = "FAIL";
a = 0 / 0, function() {
switch(a){
case a:
break;
case void (c = "PASS"):
c = "FAIL";
}
}(), console.log(c);
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
var c = "FAIL";
var a, NaN;
a = 0, c = "PASS";
(function(a, NaN) {
(function() {
switch(a){
case a:
case void (c = "PASS"):
}
})();
})(0 / 0);
console.log(c);
21 changes: 10 additions & 11 deletions crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,6 @@ impl VisitMut for Remover {
&Expr::Lit(Lit::Bool(Bool { value: d, .. })),
) => test == d,
(&Expr::Lit(Lit::Null(..)), &Expr::Lit(Lit::Null(..))) => true,
(Expr::Ident(test), Expr::Ident(d)) => {
test.sym == d.sym && test.span.ctxt() == d.span.ctxt()
}

_ => {
if !test.is_nan()
Expand Down Expand Up @@ -767,16 +764,10 @@ impl VisitMut for Remover {
ignore_result(*s.discriminant, true, &self.expr_ctx).map(Box::new),
);

let mut stmts = s.cases.remove(i).cons;
let mut cases = s.cases.drain(i..);
let mut stmts = s.cases[i].cons.take();
let mut cases = s.cases.drain((i + 1)..);

for case in cases.by_ref() {
exprs.extend(
case.test
.and_then(|e| ignore_result(*e, true, &self.expr_ctx))
.map(Box::new),
);

let should_stop = has_unconditional_stopper(&case.cons);
stmts.extend(case.cons);
//
Expand Down Expand Up @@ -968,8 +959,16 @@ impl VisitMut for Remover {
.iter()
.all(|case| case.test.is_none() || case.cons.is_empty());

let is_all_case_side_effect_free = s.cases.iter().all(|case| {
case.test
.as_ref()
.map(|e| e.is_ident() || !e.may_have_side_effects(&self.expr_ctx))
.unwrap_or(true)
});

if is_default_last
&& is_all_case_empty
&& is_all_case_side_effect_free
&& !has_conditional_stopper(&s.cases.last().unwrap().cons)
{
let mut exprs = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -867,19 +867,16 @@ fn test_optimize_switch_with_default_case() {
test_same("switch (x) { default: if (a) break; bar(); }");

// Potentially foldable
test(
concat!(
"switch (x) {",
" case x:",
" foo();",
" break;",
" default:",
" if (a) { break; }",
" bar();",
"}",
),
"x; foo()",
);
test_same(concat!(
"switch (x) {",
" case x:",
" foo();",
" break;",
" default:",
" if (a) break;",
" bar();",
"}",
));

test(
concat!(
Expand Down

0 comments on commit 7a89e5d

Please sign in to comment.