fix corner case in switches (#3585)

This commit is contained in:
Alex Lam S.L
2019-11-14 02:29:55 +08:00
committed by GitHub
parent 4bd36dc8da
commit 910799ca99
2 changed files with 100 additions and 60 deletions

View File

@@ -4872,25 +4872,23 @@ merge(Compressor.prototype, {
prev.body = [];
}
}
if (default_branch
&& default_branch.body.length == 0
&& body[body.length - 1] === default_branch
&& !branch.expression.has_side_effects(compressor)) {
default_branch.body = branch.body.slice();
} else {
body.push(branch);
}
body.push(branch);
}
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
if (body.length > 0) {
body[0].body = decl.concat(body[0].body);
}
self.body = body;
while (branch = body[body.length - 1]) {
var stat = branch.body[branch.body.length - 1];
if (is_break(stat, compressor)) branch.body.pop();
if (branch.body.length || branch instanceof AST_Case
&& (default_branch || branch.expression.has_side_effects(compressor))) break;
if (branch === default_branch) {
if (!is_body_empty(branch)) break;
} else if (branch.expression.has_side_effects(compressor)) {
break;
} else if (default_branch) {
if (!is_body_empty(default_branch)) break;
if (body[body.length - 2] !== default_branch) break;
default_branch.body = default_branch.body.concat(branch.body);
branch.body = [];
} else if (!is_body_empty(branch)) break;
eliminate_branch(branch);
if (body.pop() === default_branch) default_branch = null;
}
if (body.length == 0) {
@@ -4900,6 +4898,8 @@ merge(Compressor.prototype, {
}))
}).optimize(compressor);
}
body[0].body = decl.concat(body[0].body);
self.body = body;
if (body.length == 1 && (body[0] === exact_match || body[0] === default_branch)) {
var has_break = false;
var tw = new TreeWalker(function(node) {
@@ -4929,6 +4929,16 @@ merge(Compressor.prototype, {
return node instanceof AST_Break && tw.loopcontrol_target(node) === self;
}
function is_body_empty(branch) {
return all(branch.body, function(stat) {
return is_empty(stat)
|| stat instanceof AST_Defun
|| stat instanceof AST_Var && all(stat.definitions, function(var_def) {
return !var_def.value;
});
});
}
function eliminate_branch(branch, prev) {
if (prev && !aborts(prev)) {
prev.body = prev.body.concat(branch.body);