fix corner case in switches (#5013)

fixes #5012
This commit is contained in:
Alex Lam S.L
2021-06-15 16:51:53 +01:00
committed by GitHub
parent 21fc8f4630
commit 7880568d15
3 changed files with 55 additions and 31 deletions

View File

@@ -8422,7 +8422,7 @@ merge(Compressor.prototype, {
var default_branch;
var exact_match;
var side_effects = [];
for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
for (var i = 0, len = self.body.length; i < len; i++) {
branch = self.body[i];
if (branch instanceof AST_Default) {
var prev = body[body.length - 1];
@@ -8445,16 +8445,21 @@ merge(Compressor.prototype, {
continue;
}
if (!(equals instanceof AST_Node)) {
exact_match = branch;
if (default_branch) {
var default_index = body.indexOf(default_branch);
body.splice(default_index, 1);
eliminate_branch(default_branch, body[default_index - 1]);
default_branch = null;
}
if (exp.has_side_effects(compressor)) {
exact_match = branch;
} else {
default_branch = branch = make_node(AST_Default, branch, branch);
}
while (++i < len) eliminate_branch(self.body[i], branch);
}
}
if (exact_match || i == len - 1 || aborts(branch)) {
if (i + 1 >= len || aborts(branch)) {
var prev = body[body.length - 1];
var statements = branch.body;
if (aborts(prev)) switch (prev.body.length - statements.length) {
@@ -8479,7 +8484,6 @@ merge(Compressor.prototype, {
}
body.push(branch);
}
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
if (side_effects.length && !exact_match) {
body.push(make_node(AST_Case, self, { expression: make_sequence(self, side_effects), body: [] }));
}
@@ -8506,23 +8510,17 @@ merge(Compressor.prototype, {
}));
return make_node(AST_BlockStatement, self, { body: decl }).optimize(compressor);
}
if (branch === default_branch || branch === exact_match && !branch.expression.has_side_effects(compressor)) {
while (branch = body[body.length - 2]) {
if (branch instanceof AST_Default) break;
if (!has_declarations_only(branch)) break;
var exp = branch.expression;
if (exp.has_side_effects(compressor)) {
var prev = body[body.length - 3];
if (prev && !aborts(prev)) break;
if (exact_match) {
exact_match.expression = make_sequence(self, [ exp, exact_match.expression ]);
} else {
default_branch.body.unshift(make_node(AST_SimpleStatement, self, { body: exp }));
}
}
eliminate_branch(branch);
body.splice(-2, 1);
if (branch === default_branch) while (branch = body[body.length - 2]) {
if (branch instanceof AST_Default) break;
if (!has_declarations_only(branch)) break;
var exp = branch.expression;
if (exp.has_side_effects(compressor)) {
var prev = body[body.length - 3];
if (prev && !aborts(prev)) break;
default_branch.body.unshift(make_node(AST_SimpleStatement, self, { body: exp }));
}
eliminate_branch(branch);
body.splice(-2, 1);
}
body[0].body = decl.concat(body[0].body);
self.body = body;