fix corner case in switches (#5009)

fixes #5008
This commit is contained in:
Alex Lam S.L
2021-06-15 11:27:55 +01:00
committed by GitHub
parent ac1262dc97
commit bf76e35772
2 changed files with 128 additions and 26 deletions

View File

@@ -8108,10 +8108,6 @@ merge(Compressor.prototype, {
cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true); cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
} else if (cond) { } else if (cond) {
self.condition = null; self.condition = null;
} else if (!compressor.option("dead_code")) {
var orig = self.condition;
self.condition = make_node_from_constant(cond, self.condition);
self.condition = best_of_expression(self.condition.transform(compressor), orig);
} }
if (!cond) { if (!cond) {
if (compressor.option("dead_code")) { if (compressor.option("dead_code")) {
@@ -8226,13 +8222,8 @@ merge(Compressor.prototype, {
// one of the blocks. note, statically determined implies // one of the blocks. note, statically determined implies
// “has no side effects”; also it doesn't work for cases like // “has no side effects”; also it doesn't work for cases like
// `x && true`, though it probably should. // `x && true`, though it probably should.
var cond = self.condition.evaluate(compressor);
if (!compressor.option("dead_code") && !(cond instanceof AST_Node)) {
var orig = self.condition;
self.condition = make_node_from_constant(cond, orig);
self.condition = best_of_expression(self.condition.transform(compressor), orig);
}
if (compressor.option("dead_code")) { if (compressor.option("dead_code")) {
var cond = self.condition.evaluate(compressor);
if (cond instanceof AST_Node) { if (cond instanceof AST_Node) {
cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true); cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
} }
@@ -8424,16 +8415,7 @@ merge(Compressor.prototype, {
OPT(AST_Switch, function(self, compressor) { OPT(AST_Switch, function(self, compressor) {
if (!compressor.option("switches")) return self; if (!compressor.option("switches")) return self;
var value = self.expression.evaluate(compressor);
if (!(value instanceof AST_Node)) {
var orig = self.expression;
self.expression = make_node_from_constant(value, orig);
self.expression = best_of_expression(self.expression.transform(compressor), orig);
}
if (!compressor.option("dead_code")) return self; if (!compressor.option("dead_code")) return self;
if (value instanceof AST_Node) {
value = self.expression.evaluate(compressor, true);
}
var body = []; var body = [];
var branch; var branch;
var decl = []; var decl = [];
@@ -8450,10 +8432,19 @@ merge(Compressor.prototype, {
} else { } else {
default_branch = branch; default_branch = branch;
} }
} else if (!(value instanceof AST_Node)) { } else {
var exp = branch.expression; var exp = branch.expression;
var val = exp.evaluate(compressor, true); var equals = make_node(AST_Binary, self, {
if (val === value) { operator: "===",
left: self.expression,
right: exp,
}).evaluate(compressor, true);
if (!equals) {
if (exp.has_side_effects(compressor)) side_effects.push(exp);
eliminate_branch(branch, body[body.length - 1]);
continue;
}
if (!(equals instanceof AST_Node)) {
exact_match = branch; exact_match = branch;
if (default_branch) { if (default_branch) {
var default_index = body.indexOf(default_branch); var default_index = body.indexOf(default_branch);
@@ -8461,10 +8452,6 @@ merge(Compressor.prototype, {
eliminate_branch(default_branch, body[default_index - 1]); eliminate_branch(default_branch, body[default_index - 1]);
default_branch = null; default_branch = null;
} }
} else if (!(val instanceof AST_Node)) {
if (exp.has_side_effects(compressor)) side_effects.push(exp);
eliminate_branch(branch, body[body.length - 1]);
continue;
} }
} }
if (exact_match || i == len - 1 || aborts(branch)) { if (exact_match || i == len - 1 || aborts(branch)) {

View File

@@ -1441,3 +1441,118 @@ issue_4059: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
issue_5008_1: {
options = {
dead_code: true,
evaluate: true,
reduce_vars: true,
switches: true,
unsafe: true,
}
input: {
console.log(function f() {
switch (f) {
case f:
return "PASS";
default:
return "FAIL";
}
}());
}
expect: {
console.log(function f() {
switch (f) {
case f:
return "PASS";
}
}());
}
expect_stdout: "PASS"
}
issue_5008_2: {
options = {
dead_code: true,
evaluate: true,
reduce_vars: true,
switches: true,
unsafe: true,
}
input: {
console.log(function(a) {
switch (a) {
case a:
return "PASS";
default:
return "FAIL";
}
}([]));
}
expect: {
console.log(function(a) {
switch (a) {
case a:
return "PASS";
}
}([]));
}
expect_stdout: "PASS"
}
issue_5008_3: {
options = {
dead_code: true,
evaluate: true,
reduce_vars: true,
switches: true,
unsafe: true,
}
input: {
console.log(function(a) {
switch (a) {
case a:
return "PASS";
default:
return "FAIL";
}
}({}));
}
expect: {
console.log(function(a) {
switch (a) {
case a:
return "PASS";
}
}({}));
}
expect_stdout: "PASS"
}
issue_5008_4: {
options = {
dead_code: true,
evaluate: true,
reduce_vars: true,
switches: true,
}
input: {
console.log(function(a) {
switch (a) {
case a:
return "PASS";
default:
return "FAIL";
}
}(/foo/));
}
expect: {
console.log(function(a) {
switch (a) {
case a:
return "PASS";
}
}(/foo/));
}
expect_stdout: "PASS"
}