Skip to content

Commit

Permalink
fix corner case in switch (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Apr 2, 2017
1 parent d575276 commit 9469c03
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,7 @@ merge(Compressor.prototype, {
if (!default_branch) {
default_branch = branch;
} else {
eliminate_branch(branch);
eliminate_branch(branch, body[body.length - 1]);
}
} else if (value !== self.expression) {
var exp = branch.expression.evaluate(compressor);
Expand All @@ -2570,7 +2570,7 @@ merge(Compressor.prototype, {
default_branch = null;
}
} else if (exp !== branch.expression) {
eliminate_branch(branch);
eliminate_branch(branch, body[body.length - 1]);
continue;
}
}
Expand All @@ -2583,7 +2583,7 @@ merge(Compressor.prototype, {
}
body.push(branch);
}
while (i < len) eliminate_branch(self.body[i++]);
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
if (body.length > 0) {
body[0].body = decl.concat(body[0].body);
}
Expand Down Expand Up @@ -2626,7 +2626,6 @@ merge(Compressor.prototype, {
return self;

function eliminate_branch(branch, prev) {
if (!prev) prev = body[body.length - 1];
if (prev && !aborts(prev)) {
prev.body = prev.body.concat(branch.body);
} else {
Expand Down
28 changes: 28 additions & 0 deletions test/compress/issue-1750.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,31 @@ case_1: {
}
expect_stdout: "0 2"
}

case_2: {
options = {
dead_code: true,
evaluate: true,
switches: true,
}
input: {
var a = 0, b = 1;
switch (0) {
default:
b = 2;
case a:
a = 3;
case 0:
}
console.log(a, b);
}
expect: {
var a = 0, b = 1;
switch (0) {
case a:
a = 3;
}
console.log(a, b);
}
expect_stdout: "3 1"
}

0 comments on commit 9469c03

Please sign in to comment.