diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-04-01 17:19:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-01 17:19:57 +0800 |
commit | ee3fe0f4cd977213701579565ce68fc6a85916c3 (patch) | |
tree | baf9def4449069bc166f3c1c3dbe204a3b39223c /lib | |
parent | 87f6e1b09146607a2bf1eaa080a645277c767dda (diff) | |
download | tracifyjs-ee3fe0f4cd977213701579565ce68fc6a85916c3.tar.gz tracifyjs-ee3fe0f4cd977213701579565ce68fc6a85916c3.zip |
fix switch branch elimination (#1752)
Merge unreachable case body with previous fallthrough case
fixes #1750
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/lib/compress.js b/lib/compress.js index 1d9cd401..fc11840d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2537,49 +2537,39 @@ merge(Compressor.prototype, { 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 (!default_branch) { + default_branch = branch; + } else { + eliminate_branch(branch); } } 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); + var default_index = body.indexOf(default_branch); + body.splice(default_index, 1); + eliminate_branch(default_branch, body[default_index - 1]); default_branch = null; } - } else if (exp !== branch.expression && !fallthrough) { - extract_declarations_from_unreachable_code(compressor, branch, decl); + } else if (exp !== branch.expression) { + eliminate_branch(branch); continue; } } if (aborts(branch)) { - if (body.length > 0 && !fallthrough) { - var prev = body[body.length - 1]; - if (prev.body.length == branch.body.length - && make_node(AST_BlockStatement, prev, prev).equivalent_to(make_node(AST_BlockStatement, branch, branch))) - prev.body = []; - } - body.push(branch); - fallthrough = false; - } else { - body.push(branch); - fallthrough = true; + var prev = body[body.length - 1]; + if (aborts(prev) && prev.body.length == branch.body.length + && make_node(AST_BlockStatement, prev, prev).equivalent_to(make_node(AST_BlockStatement, branch, branch))) { + prev.body = []; + } } + body.push(branch); } - 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); + while (i < len) eliminate_branch(self.body[i++]); if (body.length > 0) { body[0].body = decl.concat(body[0].body); } @@ -2612,6 +2602,15 @@ merge(Compressor.prototype, { if (!has_break) return make_node(AST_BlockStatement, self, body[0]).optimize(compressor); } 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 { + extract_declarations_from_unreachable_code(compressor, branch, decl); + } + } }); OPT(AST_Try, function(self, compressor){ |