enhance side_effects & strings (#5704)

This commit is contained in:
Alex Lam S.L
2022-10-10 01:01:40 +01:00
committed by GitHub
parent a391897388
commit ed7051b9bb
2 changed files with 35 additions and 18 deletions

View File

@@ -8823,7 +8823,8 @@ Compressor.prototype.compress = function(node) {
var negated = node.clone();
negated.operator = op == "&&" ? "||" : "&&";
negated.left = left.negate(compressor, first_in_statement);
if (negated.operator == negated.right.operator) swap_chain(negated);
var negated_rhs = negated.right.tail_node();
if (negated_rhs instanceof AST_Binary && negated.operator == negated_rhs.operator) swap_chain(negated);
var best = first_in_statement ? best_of_statement : best_of_expression;
return op == "&&" ? best(node, negated) : best(negated, node);
}
@@ -11607,7 +11608,14 @@ Compressor.prototype.compress = function(node) {
}
function swap_chain(self, compressor) {
var rhs = self.right;
var rhs = self.right.tail_node();
if (rhs !== self.right) {
var exprs = self.right.expressions.slice(0, -1);
exprs.push(rhs.left);
rhs = rhs.clone();
rhs.left = make_sequence(self.right, exprs);
self.right = rhs;
}
self.left = make_node(AST_Binary, self, {
operator: self.operator,
left: self.left,
@@ -11808,16 +11816,7 @@ Compressor.prototype.compress = function(node) {
// x && (y && z) ---> x && y && z
// w || (x, y || z) ---> w || (x, y) || z
var rhs = self.right.tail_node();
if (rhs instanceof AST_Binary && self.operator == rhs.operator) {
if (rhs !== self.right) {
var exprs = self.right.expressions.slice(0, -1);
exprs.push(rhs.left);
rhs = rhs.clone();
rhs.left = make_sequence(self.right, exprs);
self.right = rhs;
}
swap_chain(self, compressor);
}
if (rhs instanceof AST_Binary && self.operator == rhs.operator) swap_chain(self, compressor);
}
if (compressor.option("strings") && self.operator == "+") {
// "foo" + 42 + "" ---> "foo" + 42
@@ -11843,12 +11842,13 @@ Compressor.prototype.compress = function(node) {
return self.optimize(compressor);
}
// "x" + (y + "z") ---> "x" + y + "z"
// x + ("y" + z) ---> x + "y" + z
if (self.right instanceof AST_Binary
&& self.operator == self.right.operator
&& (self.left.is_string(compressor) && self.right.is_string(compressor)
|| self.right.left.is_string(compressor)
&& (self.left.is_constant() || !self.right.right.has_side_effects(compressor)))) {
// w + (x, "y" + z) ---> w + (x, "y") + z
var rhs = self.right.tail_node();
if (rhs instanceof AST_Binary
&& self.operator == rhs.operator
&& (self.left.is_string(compressor) && rhs.is_string(compressor)
|| rhs.left.is_string(compressor)
&& (self.left.is_constant() || !rhs.right.has_side_effects(compressor)))) {
swap_chain(self, compressor);
}
}