fix(es/minifier): Handle switch cases (#8854)

**Related issue:**

- Closes #8813
This commit is contained in:
magic-akari 2024-04-13 10:11:32 +08:00 committed by GitHub
parent fcc82a5a5b
commit 7a89e5da3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 161 additions and 28 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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()
@ -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);
//
@ -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![];

View File

@ -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!(