Skip to content

Commit

Permalink
Ensure switch branches reordering happens only for constant branch ex…
Browse files Browse the repository at this point in the history
…pressions (#1092)
  • Loading branch information
elad-yosifon committed Oct 19, 2021
1 parent 01f1841 commit f251cd4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/compress/index.js
Expand Up @@ -1606,7 +1606,7 @@ def_optimize(AST_Switch, function(self, compressor) {
// that way the next micro-optimization will merge them.
// ** bail micro-optimization if not a simple switch case with breaks
if (body.every((branch, i) =>
(branch === default_or_exact || !branch.expression.has_side_effects(compressor))
(branch === default_or_exact || branch.expression instanceof AST_Constant)
&& (branch.body.length === 0 || aborts(branch) || body.length - 1 === i))
) {
for (let i = 0; i < body.length; i++) {
Expand Down
36 changes: 36 additions & 0 deletions test/compress/switch.js
Expand Up @@ -2594,6 +2594,42 @@ collapse_same_branches_not_in_a_row_ensure_no_side_effects: {
}
expect_stdout: ["2"]
}

collapse_same_branches_not_in_a_row_ensure_no_evaluate_elad: {
options = {
switches: true,
dead_code: true
}
input: {
let i = 1;
switch (true) {
case 3 == i:
console.log(i);
break;
case 5 == i:
console.log(5);
break;
case 1 == i:
console.log(i);
break;
}
}
expect: {
let i = 1;
switch (true) {
case 3 == i:
console.log(i);
break;
case 5 == i:
console.log(5);
break;
case 1 == i:
console.log(i);
}
}
expect_stdout: ["1"]
}

collapse_same_branches_not_in_a_row_even_if_last_case_without_abort: {
options = {
switches: true,
Expand Down

0 comments on commit f251cd4

Please sign in to comment.