enhance side_effects & strings (#5704)
This commit is contained in:
parent
a391897388
commit
ed7051b9bb
|
|
@ -8823,7 +8823,8 @@ Compressor.prototype.compress = function(node) {
|
||||||
var negated = node.clone();
|
var negated = node.clone();
|
||||||
negated.operator = op == "&&" ? "||" : "&&";
|
negated.operator = op == "&&" ? "||" : "&&";
|
||||||
negated.left = left.negate(compressor, first_in_statement);
|
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;
|
var best = first_in_statement ? best_of_statement : best_of_expression;
|
||||||
return op == "&&" ? best(node, negated) : best(negated, node);
|
return op == "&&" ? best(node, negated) : best(negated, node);
|
||||||
}
|
}
|
||||||
|
|
@ -11607,7 +11608,14 @@ Compressor.prototype.compress = function(node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function swap_chain(self, compressor) {
|
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, {
|
self.left = make_node(AST_Binary, self, {
|
||||||
operator: self.operator,
|
operator: self.operator,
|
||||||
left: self.left,
|
left: self.left,
|
||||||
|
|
@ -11808,16 +11816,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
// x && (y && z) ---> x && y && z
|
// x && (y && z) ---> x && y && z
|
||||||
// w || (x, y || z) ---> w || (x, y) || z
|
// w || (x, y || z) ---> w || (x, y) || z
|
||||||
var rhs = self.right.tail_node();
|
var rhs = self.right.tail_node();
|
||||||
if (rhs instanceof AST_Binary && self.operator == rhs.operator) {
|
if (rhs instanceof AST_Binary && self.operator == rhs.operator) swap_chain(self, compressor);
|
||||||
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 (compressor.option("strings") && self.operator == "+") {
|
if (compressor.option("strings") && self.operator == "+") {
|
||||||
// "foo" + 42 + "" ---> "foo" + 42
|
// "foo" + 42 + "" ---> "foo" + 42
|
||||||
|
|
@ -11843,12 +11842,13 @@ Compressor.prototype.compress = function(node) {
|
||||||
return self.optimize(compressor);
|
return self.optimize(compressor);
|
||||||
}
|
}
|
||||||
// "x" + (y + "z") ---> "x" + y + "z"
|
// "x" + (y + "z") ---> "x" + y + "z"
|
||||||
// x + ("y" + z) ---> x + "y" + z
|
// w + (x, "y" + z) ---> w + (x, "y") + z
|
||||||
if (self.right instanceof AST_Binary
|
var rhs = self.right.tail_node();
|
||||||
&& self.operator == self.right.operator
|
if (rhs instanceof AST_Binary
|
||||||
&& (self.left.is_string(compressor) && self.right.is_string(compressor)
|
&& self.operator == rhs.operator
|
||||||
|| self.right.left.is_string(compressor)
|
&& (self.left.is_string(compressor) && rhs.is_string(compressor)
|
||||||
&& (self.left.is_constant() || !self.right.right.has_side_effects(compressor)))) {
|
|| rhs.left.is_string(compressor)
|
||||||
|
&& (self.left.is_constant() || !rhs.right.has_side_effects(compressor)))) {
|
||||||
swap_chain(self, compressor);
|
swap_chain(self, compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -273,6 +273,23 @@ concat_9: {
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
concat_sequence: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
strings: true,
|
||||||
|
toplevel: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a;
|
||||||
|
console.log(12 + (a = null, "34" + a));
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(12 + "34" + null);
|
||||||
|
}
|
||||||
|
expect_stdout: "1234null"
|
||||||
|
}
|
||||||
|
|
||||||
issue_3689: {
|
issue_3689: {
|
||||||
options = {
|
options = {
|
||||||
strings: true,
|
strings: true,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user