diff --git a/lib/compress/index.js b/lib/compress/index.js index 0a5f99c99..2d92a27c2 100644 --- a/lib/compress/index.js +++ b/lib/compress/index.js @@ -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; } }