Skip to content

Commit

Permalink
Keep cases that can skip a side-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Aug 13, 2021
1 parent ae63f4f commit 3321339
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/compress/index.js
Expand Up @@ -1634,19 +1634,20 @@ def_optimize(AST_Switch, function(self, compressor) {
if (i < body.length && (!default_branch || body.indexOf(default_branch, i) >= i)) {
// The default behavior is to do nothing. We can take advantage of that to
// remove all case expressions that are side-effect free that also do
// nothing, since they'll default to doing nothing.
let pointer = i;
for (let j = pointer; j < body.length; j++) {
// nothing, since they'll default to doing nothing. But we can't remove any
// case expressions before one that would side-effect, since they may cause
// the side-effect to be skipped.
for (let j = body.length - 1; j >= i; j--) {
let branch = body[j];
if (branch === default_branch) {
default_branch = null;
continue;
body.pop();
} else if (!branch.expression.has_side_effects(compressor)) {
body.pop();
} else {
break;
}
if (!branch.expression.has_side_effects(compressor)) continue;
// preserve branches that contain a side-effect
body[pointer++] = branch;
}
body.length = pointer;
}
}

Expand Down

0 comments on commit 3321339

Please sign in to comment.