case expression with side effects can only be de-duplicated at the tail

This commit is contained in:
alexlamsl 2017-03-26 15:48:10 +08:00
parent 4ce5c784d6
commit 4291ecc77c
2 changed files with 44 additions and 8 deletions

View File

@ -2542,22 +2542,23 @@ merge(Compressor.prototype, {
continue;
}
}
var case_side_effects = branch instanceof AST_Case && branch.expression.has_side_effects(compressor);
if (aborts(branch)) {
var key = make_node(AST_BlockStatement, branch, branch).print_to_string();
var block;
if (!fallthrough && (block = blocks[key])) {
block.body = [];
body.splice(body.indexOf(block) + 1, 0, branch);
} else {
body.push(branch);
}
var insert = body.indexOf(block) + 1;
if (insert == body.length || !case_side_effects) {
block.body = [];
body.splice(insert, 0, branch);
} else body.push(branch);
} else body.push(branch);
fallthrough = false;
} else {
body.push(branch);
fallthrough = true;
}
if (branch instanceof AST_Case && branch.expression.has_side_effects(compressor))
blocks = Object.create(null);
if (case_side_effects) blocks = Object.create(null);
if (!fallthrough) blocks[key] = branch;
}
for (; i < len && fallthrough; i++) {

View File

@ -478,7 +478,7 @@ issue_1679: {
expect_stdout: true
}
issue_1680: {
issue_1680_1: {
options = {
dead_code: true,
evaluate: true,
@ -518,3 +518,38 @@ issue_1680: {
"5",
]
}
issue_1680_2: {
options = {
dead_code: true,
}
input: {
var a = 100, b = 10;
switch (b) {
case a--:
break;
case b:
var c;
break;
case a:
break;
case a--:
break;
}
console.log(a, b);
}
expect: {
var a = 100, b = 10;
switch (b) {
case a--:
case a:
break;
case b:
var c;
break;
case a--:
}
console.log(a, b);
}
expect_stdout: true
}