aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-06-15 16:51:53 +0100
committerGitHub <noreply@github.com>2021-06-15 23:51:53 +0800
commit7880568d157fcd1c15c0f392e4646e01de363afd (patch)
tree83167c0baafa2dc84ac0e0abdc5c986d55b74bba /lib
parent21fc8f4630bccdb8b0a33747c2de8fd377e83138 (diff)
downloadtracifyjs-7880568d157fcd1c15c0f392e4646e01de363afd.tar.gz
tracifyjs-7880568d157fcd1c15c0f392e4646e01de363afd.zip
fix corner case in `switches` (#5013)
fixes #5012
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js38
1 files changed, 18 insertions, 20 deletions
diff --git a/lib/compress.js b/lib/compress.js
index ed3b5a39..22434f62 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -8422,7 +8422,7 @@ merge(Compressor.prototype, {
var default_branch;
var exact_match;
var side_effects = [];
- for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
+ for (var i = 0, len = self.body.length; i < len; i++) {
branch = self.body[i];
if (branch instanceof AST_Default) {
var prev = body[body.length - 1];
@@ -8445,16 +8445,21 @@ merge(Compressor.prototype, {
continue;
}
if (!(equals instanceof AST_Node)) {
- exact_match = branch;
if (default_branch) {
var default_index = body.indexOf(default_branch);
body.splice(default_index, 1);
eliminate_branch(default_branch, body[default_index - 1]);
default_branch = null;
}
+ if (exp.has_side_effects(compressor)) {
+ exact_match = branch;
+ } else {
+ default_branch = branch = make_node(AST_Default, branch, branch);
+ }
+ while (++i < len) eliminate_branch(self.body[i], branch);
}
}
- if (exact_match || i == len - 1 || aborts(branch)) {
+ if (i + 1 >= len || aborts(branch)) {
var prev = body[body.length - 1];
var statements = branch.body;
if (aborts(prev)) switch (prev.body.length - statements.length) {
@@ -8479,7 +8484,6 @@ merge(Compressor.prototype, {
}
body.push(branch);
}
- while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
if (side_effects.length && !exact_match) {
body.push(make_node(AST_Case, self, { expression: make_sequence(self, side_effects), body: [] }));
}
@@ -8506,23 +8510,17 @@ merge(Compressor.prototype, {
}));
return make_node(AST_BlockStatement, self, { body: decl }).optimize(compressor);
}
- if (branch === default_branch || branch === exact_match && !branch.expression.has_side_effects(compressor)) {
- while (branch = body[body.length - 2]) {
- if (branch instanceof AST_Default) break;
- if (!has_declarations_only(branch)) break;
- var exp = branch.expression;
- if (exp.has_side_effects(compressor)) {
- var prev = body[body.length - 3];
- if (prev && !aborts(prev)) break;
- if (exact_match) {
- exact_match.expression = make_sequence(self, [ exp, exact_match.expression ]);
- } else {
- default_branch.body.unshift(make_node(AST_SimpleStatement, self, { body: exp }));
- }
- }
- eliminate_branch(branch);
- body.splice(-2, 1);
+ if (branch === default_branch) while (branch = body[body.length - 2]) {
+ if (branch instanceof AST_Default) break;
+ if (!has_declarations_only(branch)) break;
+ var exp = branch.expression;
+ if (exp.has_side_effects(compressor)) {
+ var prev = body[body.length - 3];
+ if (prev && !aborts(prev)) break;
+ default_branch.body.unshift(make_node(AST_SimpleStatement, self, { body: exp }));
}
+ eliminate_branch(branch);
+ body.splice(-2, 1);
}
body[0].body = decl.concat(body[0].body);
self.body = body;