feat(es/minifier): Optimize switches more correctly (#4180)

This commit is contained in:
Donny/강동윤 2022-03-29 13:25:15 +09:00 committed by GitHub
parent 783904917d
commit 6d3ea17aa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 13 deletions

View File

@ -34,6 +34,14 @@ where
return;
}
if stmt
.cases
.iter()
.any(|case| matches!(case.test.as_deref(), Some(Expr::Update(..))))
{
return;
}
let matching_case = stmt.cases.iter_mut().position(|case| {
case.test
.as_ref()
@ -117,7 +125,16 @@ where
_ => !found_break,
});
stmts.append(&mut case.cons);
for case_stmt in case.cons.take() {
match case_stmt {
Stmt::Decl(Decl::Var(v)) if v.decls.iter().all(|v| v.init.is_none()) => {
var_ids.extend(v.decls)
}
_ => {
stmts.push(case_stmt);
}
}
}
if found_break {
break;
}
@ -235,7 +252,7 @@ where
/// If a case ends with break but content is same with the consequtive case
/// except the break statement, we merge them.
fn merge_cases_with_same_cons(&mut self, cases: &mut Vec<SwitchCase>) {
let stop_pos = cases
let mut stop_pos_opt = cases
.iter()
.position(|case| matches!(case.test.as_deref(), Some(Expr::Update(..))));
@ -245,8 +262,14 @@ where
continue;
}
if let Some(stop_pos) = stop_pos {
if li > stop_pos {
if let Some(stop_pos) = stop_pos_opt {
if li == stop_pos {
// Look for next stop position
stop_pos_opt = cases
.iter()
.skip(li)
.position(|case| matches!(case.test.as_deref(), Some(Expr::Update(..))))
.map(|v| v + li);
continue;
}
}

View File

@ -749,8 +749,6 @@ sequences/issue_1758/input.js
sequences/lift_sequences_2/input.js
sequences/lift_sequences_5/input.js
sequences/side_effects_cascade_3/input.js
switch/issue_1663/input.js
switch/issue_1680_2/input.js
template_string/array_join/input.js
template_string/coerce_to_string/input.js
template_string/evaluate_nested_templates/input.js

View File

@ -1257,9 +1257,11 @@ switch/drop_case/input.js
switch/drop_default_1/input.js
switch/drop_default_2/input.js
switch/if_switch_typeof/input.js
switch/issue_1663/input.js
switch/issue_1674/input.js
switch/issue_1679/input.js
switch/issue_1680_1/input.js
switch/issue_1680_2/input.js
switch/issue_1690_1/input.js
switch/issue_1690_2/input.js
switch/issue_1698/input.js

View File

@ -1,9 +1,8 @@
var a = 100,
b = 10;
var a = 100, b = 10;
function f() {
var b;
b = a++;
return ++b;
var b1;
b1 = a++;
return ++b1;
}
f();
console.log(a, b);

View File

@ -1079,6 +1079,8 @@ impl Remover {
where
T: StmtLike + VisitWith<Hoister> + VisitMutWith<Self>,
{
let orig_len = stmts.len();
let is_block_stmt = self.normal_block;
self.normal_block = false;
@ -1142,10 +1144,14 @@ impl Remover {
new_stmts.extend(hoisted_fns);
*stmts = new_stmts;
if stmts.len() != orig_len {
self.changed = true;
if cfg!(feature = "debug") {
debug!("Dropping statements after a control keyword");
if cfg!(feature = "debug") {
debug!("Dropping statements after a control keyword");
}
}
return;
}