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);
} else if (cond) {
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 (compressor.option("dead_code")) {
@@ -8226,13 +8222,8 @@ merge(Compressor.prototype, {
// one of the blocks. note, statically determined implies
// “has no side effects”; also it doesn't work for cases like
// `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")) {
var cond = self.condition.evaluate(compressor);
if (cond instanceof AST_Node) {
cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
}
@@ -8424,16 +8415,7 @@ merge(Compressor.prototype, {
OPT(AST_Switch, function(self, compressor) {
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 (value instanceof AST_Node) {
value = self.expression.evaluate(compressor, true);
}
var body = [];
var branch;
var decl = [];
@@ -8450,10 +8432,19 @@ merge(Compressor.prototype, {
} else {
default_branch = branch;
}
} else if (!(value instanceof AST_Node)) {
} else {
var exp = branch.expression;
var val = exp.evaluate(compressor, true);
if (val === value) {
var equals = make_node(AST_Binary, self, {
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;
if (default_branch) {
var default_index = body.indexOf(default_branch);
@@ -8461,10 +8452,6 @@ merge(Compressor.prototype, {
eliminate_branch(default_branch, body[default_index - 1]);
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)) {