enhance switches (#3583)

This commit is contained in:
Alex Lam S.L
2019-11-13 20:03:48 +08:00
committed by GitHub
parent fe65ce9658
commit ab15c40770
2 changed files with 168 additions and 23 deletions

View File

@@ -4828,10 +4828,12 @@ merge(Compressor.prototype, {
for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
branch = self.body[i];
if (branch instanceof AST_Default) {
if (!default_branch) {
default_branch = branch;
var prev = body[body.length - 1];
if (default_branch || is_break(branch.body[0], compressor) && (!prev || aborts(prev))) {
eliminate_branch(branch, prev);
continue;
} else {
eliminate_branch(branch, body[body.length - 1]);
default_branch = branch;
}
} else if (!(value instanceof AST_Node)) {
var exp = branch.expression.evaluate(compressor);
@@ -4857,7 +4859,14 @@ merge(Compressor.prototype, {
prev.body = [];
}
}
body.push(branch);
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);
}
}
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
if (body.length > 0) {
@@ -4866,8 +4875,7 @@ merge(Compressor.prototype, {
self.body = body;
while (branch = body[body.length - 1]) {
var stat = branch.body[branch.body.length - 1];
if (stat instanceof AST_Break && compressor.loopcontrol_target(stat) === self)
branch.body.pop();
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 (body.pop() === default_branch) default_branch = null;
@@ -4885,8 +4893,7 @@ merge(Compressor.prototype, {
if (has_break
|| node instanceof AST_Lambda
|| node instanceof AST_SimpleStatement) return true;
if (node instanceof AST_Break && tw.loopcontrol_target(node) === self)
has_break = true;
if (is_break(node, tw)) has_break = true;
});
self.walk(tw);
if (!has_break) {
@@ -4905,6 +4912,10 @@ merge(Compressor.prototype, {
}
return self;
function is_break(node, tw) {
return node instanceof AST_Break && tw.loopcontrol_target(node) === self;
}
function eliminate_branch(branch, prev) {
if (prev && !aborts(prev)) {
prev.body = prev.body.concat(branch.body);