Skip to content

Commit

Permalink
enhance side_effects & strings (#5704)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Oct 10, 2022
1 parent a391897 commit ed7051b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/compress.js
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/compress/concat-strings.js
Expand Up @@ -273,6 +273,23 @@ concat_9: {
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: {
options = {
strings: true,
Expand Down

0 comments on commit ed7051b

Please sign in to comment.