fallthrough should not execute case expression (#1683)

- de-duplicate trailing cases only, avoid all potential side-effects
- enable switch statement fuzzing

fixes #1680
This commit is contained in:
Alex Lam S.L
2017-03-26 16:52:38 +08:00
committed by GitHub
parent 5509e51098
commit 3276740779
3 changed files with 83 additions and 22 deletions

View File

@@ -2515,7 +2515,7 @@ merge(Compressor.prototype, {
self.expression = best_of_expression(expression, self.expression);
}
if (compressor.option("dead_code")) {
var blocks = Object.create(null);
var prev_block;
var decl = [];
var body = [];
var default_branch;
@@ -2542,31 +2542,21 @@ 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 block = make_node(AST_BlockStatement, branch, branch).print_to_string();
if (!fallthrough && prev_block === block) body[body.length - 1].body = [];
body.push(branch);
prev_block = block;
fallthrough = false;
} else {
body.push(branch);
prev_block = null;
fallthrough = true;
}
if (branch instanceof AST_Case && branch.expression.has_side_effects(compressor))
blocks = Object.create(null);
if (!fallthrough) blocks[key] = branch;
}
for (; i < len && fallthrough; i++) {
branch = self.body[i];
if (branch instanceof AST_Case) {
exact_match.body.push(make_node(AST_SimpleStatement, branch.expression, {
body: branch.expression
}));
}
exact_match.body = exact_match.body.concat(branch.body);
fallthrough = !aborts(exact_match);
}