fix collapse_vars on switch (#2578)

This commit is contained in:
Alex Lam S.L
2017-12-11 18:11:09 +08:00
committed by GitHub
parent c43118be4f
commit f2ad542679
2 changed files with 61 additions and 7 deletions

View File

@@ -873,14 +873,12 @@ merge(Compressor.prototype, {
node.expression = node.expression.transform(scanner);
for (var i = 0, len = node.body.length; !abort && i < len; i++) {
var branch = node.body[i];
if (branch instanceof AST_Case)
if (branch instanceof AST_Case) {
branch.expression = branch.expression.transform(scanner);
if (side_effects || !replace_all) break;
}
for (i = 0; !abort && i < len; i++) {
var branch = node.body[i];
for (var j = 0, len2 = branch.body.length; j < len2; j++)
branch.body[j] = branch.body[j].transform(scanner);
}
abort = true;
return node;
}
// Skip nodes before `candidate` as quickly as possible

View File

@@ -1749,7 +1749,7 @@ for_init: {
}
}
switch_case: {
switch_case_1: {
options = {
collapse_vars: true,
unused: true,
@@ -1777,6 +1777,62 @@ switch_case: {
}
}
switch_case_2: {
options = {
collapse_vars: true,
}
input: {
var a = 1, b = 2;
switch (b++) {
case b:
var c = a;
var a;
break;
}
console.log(a);
}
expect: {
var a = 1, b = 2;
switch (b++) {
case b:
var c = a;
var a;
break;
}
console.log(a);
}
expect_stdout: "1"
}
switch_case_3: {
options = {
collapse_vars: true,
}
input: {
var a = 1, b = 2;
switch (a) {
case a:
var b;
break;
case b:
break;
}
console.log(b);
}
expect: {
var a = 1, b = 2;
switch (a) {
case a:
var b;
break;
case b:
break;
}
console.log(b);
}
expect_stdout: "2"
}
issue_27: {
options = {
collapse_vars: true,