aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-11-14 02:29:55 +0800
committerGitHub <noreply@github.com>2019-11-14 02:29:55 +0800
commit910799ca9921ed435df3fd149a1fb2ab77dedbc5 (patch)
tree9197b7f7f2249962483aae6ac8fe0322a08fb3a1 /lib
parent4bd36dc8da23ad31eba45376e861784ba8568379 (diff)
downloadtracifyjs-910799ca9921ed435df3fd149a1fb2ab77dedbc5.tar.gz
tracifyjs-910799ca9921ed435df3fd149a1fb2ab77dedbc5.zip
fix corner case in `switches` (#3585)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js38
1 files changed, 24 insertions, 14 deletions
diff --git a/lib/compress.js b/lib/compress.js
index e97427bc..668293d2 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -4872,25 +4872,23 @@ merge(Compressor.prototype, {
prev.body = [];
}
}
- if (default_branch
- && default_branch.body.length == 0
- && body[body.length - 1] === default_branch
- && !branch.expression.has_side_effects(compressor)) {
- default_branch.body = branch.body.slice();
- } else {
- body.push(branch);
- }
+ body.push(branch);
}
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
- 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 (is_break(stat, compressor)) branch.body.pop();
- if (branch.body.length || branch instanceof AST_Case
- && (default_branch || branch.expression.has_side_effects(compressor))) break;
+ if (branch === default_branch) {
+ if (!is_body_empty(branch)) break;
+ } else if (branch.expression.has_side_effects(compressor)) {
+ break;
+ } else if (default_branch) {
+ if (!is_body_empty(default_branch)) break;
+ if (body[body.length - 2] !== default_branch) break;
+ default_branch.body = default_branch.body.concat(branch.body);
+ branch.body = [];
+ } else if (!is_body_empty(branch)) break;
+ eliminate_branch(branch);
if (body.pop() === default_branch) default_branch = null;
}
if (body.length == 0) {
@@ -4900,6 +4898,8 @@ merge(Compressor.prototype, {
}))
}).optimize(compressor);
}
+ body[0].body = decl.concat(body[0].body);
+ self.body = body;
if (body.length == 1 && (body[0] === exact_match || body[0] === default_branch)) {
var has_break = false;
var tw = new TreeWalker(function(node) {
@@ -4929,6 +4929,16 @@ merge(Compressor.prototype, {
return node instanceof AST_Break && tw.loopcontrol_target(node) === self;
}
+ function is_body_empty(branch) {
+ return all(branch.body, function(stat) {
+ return is_empty(stat)
+ || stat instanceof AST_Defun
+ || stat instanceof AST_Var && all(stat.definitions, function(var_def) {
+ return !var_def.value;
+ });
+ });
+ }
+
function eliminate_branch(branch, prev) {
if (prev && !aborts(prev)) {
prev.body = prev.body.concat(branch.body);