Skip to content

Commit

Permalink
fix corner case in switches (#5013)
Browse files Browse the repository at this point in the history
fixes #5012
  • Loading branch information
alexlamsl committed Jun 15, 2021
1 parent 21fc8f4 commit 7880568
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
38 changes: 18 additions & 20 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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) {
Expand All @@ -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: [] }));
}
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/compress/merge_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -3075,7 +3075,7 @@ issue_4237_2: {
console.log(function(a) {
do {
switch (0) {
case 0:
default:
var b = a++;
if (b)
return "FAIL";
Expand Down
46 changes: 36 additions & 10 deletions test/compress/switches.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ constant_switch_5: {
// the break inside the if ruins our job
// we can still get rid of irrelevant cases.
switch (1) {
case 1:
default:
x();
if (foo) break;
y();
Expand Down Expand Up @@ -501,7 +501,8 @@ drop_case_5: {
}
expect: {
switch (42) {
case (void console.log("PASS 1"), 42):
default:
void console.log("PASS 1");
console.log("PASS 2");
}
}
Expand Down Expand Up @@ -554,7 +555,8 @@ drop_case_7: {
}
expect: {
switch (2) {
case (console.log("PASS 1"), 1, 2):
default:
console.log("PASS 1"), 1;
console.log("PASS 2");
}
}
Expand Down Expand Up @@ -920,7 +922,7 @@ issue_1680_1: {
case f(0):
case f(1):
f(2);
case 2:
default:
f(5);
}
}
Expand Down Expand Up @@ -1183,7 +1185,6 @@ issue_2535: {
}
expect: {
w(), 42;
42;
y();
z();
}
Expand All @@ -1209,7 +1210,6 @@ issue_1750: {
expect: {
var a = 0, b = 1;
true;
a, true;
b = 2;
console.log(a, b);
}
Expand Down Expand Up @@ -1463,7 +1463,7 @@ issue_5008_1: {
expect: {
console.log(function f() {
switch (f) {
case f:
default:
return "PASS";
}
}());
Expand Down Expand Up @@ -1492,7 +1492,7 @@ issue_5008_2: {
expect: {
console.log(function(a) {
switch (a) {
case a:
default:
return "PASS";
}
}([]));
Expand Down Expand Up @@ -1521,7 +1521,7 @@ issue_5008_3: {
expect: {
console.log(function(a) {
switch (a) {
case a:
default:
return "PASS";
}
}({}));
Expand Down Expand Up @@ -1549,7 +1549,7 @@ issue_5008_4: {
expect: {
console.log(function(a) {
switch (a) {
case a:
default:
return "PASS";
}
}(/foo/));
Expand Down Expand Up @@ -1582,3 +1582,29 @@ issue_5010: {
}
expect_stdout: "PASS"
}

issue_5012: {
options = {
dead_code: true,
evaluate: true,
switches: true,
}
input: {
switch (void 0) {
case console.log("PASS"):
break;
case void 0:
case 42:
console.log("FAIL");
}
}
expect: {
switch (void 0) {
case console.log("PASS"):
break;
default:
console.log("FAIL");
}
}
expect_stdout: "PASS"
}

0 comments on commit 7880568

Please sign in to comment.