aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-03-28 03:59:13 +0800
committerGitHub <noreply@github.com>2017-03-28 03:59:13 +0800
commit67d0237f73c3147855983edde137cd95a2cb1749 (patch)
tree88be1c764d2de5243ce210a5eef76382f511b7a2 /lib/compress.js
parent984a21704e126616a74d65a1e8790aeccd02f548 (diff)
downloadtracifyjs-67d0237f73c3147855983edde137cd95a2cb1749.tar.gz
tracifyjs-67d0237f73c3147855983edde137cd95a2cb1749.zip
fix tail trimming of switch blocks (#1707)
now guarded under `dead_code` fixes #1705
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js105
1 files changed, 52 insertions, 53 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 1146f300..e2fde883 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2509,74 +2509,73 @@ merge(Compressor.prototype, {
var expression = make_node_from_constant(value, self.expression).transform(compressor);
self.expression = best_of_expression(expression, self.expression);
}
- if (compressor.option("dead_code")) {
- var prev_block;
- var decl = [];
- var body = [];
- var default_branch;
- var exact_match;
- var fallthrough;
- for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
- branch = self.body[i];
- if (branch instanceof AST_Default) {
- if (!default_branch) default_branch = branch;
- else if (!fallthrough) {
- extract_declarations_from_unreachable_code(compressor, branch, decl);
- continue;
+ if (!compressor.option("dead_code")) return self;
+ var prev_block;
+ var decl = [];
+ var body = [];
+ var default_branch;
+ var exact_match;
+ var fallthrough;
+ for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
+ branch = self.body[i];
+ if (branch instanceof AST_Default) {
+ if (!default_branch) default_branch = branch;
+ else if (!fallthrough) {
+ extract_declarations_from_unreachable_code(compressor, branch, decl);
+ continue;
+ }
+ } else if (value !== self.expression) {
+ var exp = branch.expression.evaluate(compressor);
+ if (exp === value) {
+ exact_match = branch;
+ if (default_branch) {
+ body.splice(body.indexOf(default_branch), 1);
+ extract_declarations_from_unreachable_code(compressor, default_branch, decl);
+ default_branch = null;
}
- } else if (value !== self.expression) {
- var exp = branch.expression.evaluate(compressor);
- if (exp === value) {
- exact_match = branch;
- if (default_branch) {
- body.splice(body.indexOf(default_branch), 1);
- extract_declarations_from_unreachable_code(compressor, default_branch, decl);
- }
- } else if (exp !== branch.expression && !fallthrough) {
- extract_declarations_from_unreachable_code(compressor, branch, decl);
- continue;
- }
- }
- if (aborts(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;
+ } else if (exp !== branch.expression && !fallthrough) {
+ extract_declarations_from_unreachable_code(compressor, branch, decl);
+ continue;
}
}
- for (; i < len && fallthrough; i++) {
- branch = self.body[i];
- exact_match.body = exact_match.body.concat(branch.body);
- fallthrough = !aborts(exact_match);
- }
- while (i < len) extract_declarations_from_unreachable_code(compressor, self.body[i++], decl);
- if (body.length > 0) {
- body[0].body = decl.concat(body[0].body);
+ if (aborts(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;
}
- self.body = body;
}
- while (branch = self.body[self.body.length - 1]) {
+ for (; i < len && fallthrough; i++) {
+ branch = self.body[i];
+ exact_match.body = exact_match.body.concat(branch.body);
+ fallthrough = !aborts(exact_match);
+ }
+ while (i < len) extract_declarations_from_unreachable_code(compressor, self.body[i++], decl);
+ if (body.length > 0) {
+ body[0].body = decl.concat(body[0].body);
+ }
+ self.body = body;
+ while (branch = body[body.length - 1]) {
var stat = branch.body[branch.body.length - 1];
if (stat instanceof AST_Break && compressor.loopcontrol_target(stat.label) === self)
branch.body.pop();
- if (branch.body.length
- || branch instanceof AST_Case
- && branch.expression.has_side_effects(compressor)) break;
- self.body.pop();
+ if (branch.body.length || branch instanceof AST_Case
+ && (default_branch || branch.expression.has_side_effects(compressor))) break;
+ if (body.pop() === default_branch) default_branch = null;
}
- if (decl && self.body.length == 0) {
+ if (body.length == 0) {
return make_node(AST_BlockStatement, self, {
body: decl.concat(make_node(AST_SimpleStatement, self.expression, {
body: self.expression
}))
}).optimize(compressor);
}
- if (body && body.length == 1 && (body[0] === exact_match || body[0] === default_branch)) {
+ if (body.length == 1 && (body[0] === exact_match || body[0] === default_branch)) {
var has_break = false;
var tw = new TreeWalker(function(node) {
if (has_break