Skip to content

Commit ed7051b

Browse files
authored
enhance side_effects & strings (#5704)
1 parent a391897 commit ed7051b

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

lib/compress.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8823,7 +8823,8 @@ Compressor.prototype.compress = function(node) {
88238823
var negated = node.clone();
88248824
negated.operator = op == "&&" ? "||" : "&&";
88258825
negated.left = left.negate(compressor, first_in_statement);
8826-
if (negated.operator == negated.right.operator) swap_chain(negated);
8826+
var negated_rhs = negated.right.tail_node();
8827+
if (negated_rhs instanceof AST_Binary && negated.operator == negated_rhs.operator) swap_chain(negated);
88278828
var best = first_in_statement ? best_of_statement : best_of_expression;
88288829
return op == "&&" ? best(node, negated) : best(negated, node);
88298830
}
@@ -11607,7 +11608,14 @@ Compressor.prototype.compress = function(node) {
1160711608
}
1160811609

1160911610
function swap_chain(self, compressor) {
11610-
var rhs = self.right;
11611+
var rhs = self.right.tail_node();
11612+
if (rhs !== self.right) {
11613+
var exprs = self.right.expressions.slice(0, -1);
11614+
exprs.push(rhs.left);
11615+
rhs = rhs.clone();
11616+
rhs.left = make_sequence(self.right, exprs);
11617+
self.right = rhs;
11618+
}
1161111619
self.left = make_node(AST_Binary, self, {
1161211620
operator: self.operator,
1161311621
left: self.left,
@@ -11808,16 +11816,7 @@ Compressor.prototype.compress = function(node) {
1180811816
// x && (y && z) ---> x && y && z
1180911817
// w || (x, y || z) ---> w || (x, y) || z
1181011818
var rhs = self.right.tail_node();
11811-
if (rhs instanceof AST_Binary && self.operator == rhs.operator) {
11812-
if (rhs !== self.right) {
11813-
var exprs = self.right.expressions.slice(0, -1);
11814-
exprs.push(rhs.left);
11815-
rhs = rhs.clone();
11816-
rhs.left = make_sequence(self.right, exprs);
11817-
self.right = rhs;
11818-
}
11819-
swap_chain(self, compressor);
11820-
}
11819+
if (rhs instanceof AST_Binary && self.operator == rhs.operator) swap_chain(self, compressor);
1182111820
}
1182211821
if (compressor.option("strings") && self.operator == "+") {
1182311822
// "foo" + 42 + "" ---> "foo" + 42
@@ -11843,12 +11842,13 @@ Compressor.prototype.compress = function(node) {
1184311842
return self.optimize(compressor);
1184411843
}
1184511844
// "x" + (y + "z") ---> "x" + y + "z"
11846-
// x + ("y" + z) ---> x + "y" + z
11847-
if (self.right instanceof AST_Binary
11848-
&& self.operator == self.right.operator
11849-
&& (self.left.is_string(compressor) && self.right.is_string(compressor)
11850-
|| self.right.left.is_string(compressor)
11851-
&& (self.left.is_constant() || !self.right.right.has_side_effects(compressor)))) {
11845+
// w + (x, "y" + z) ---> w + (x, "y") + z
11846+
var rhs = self.right.tail_node();
11847+
if (rhs instanceof AST_Binary
11848+
&& self.operator == rhs.operator
11849+
&& (self.left.is_string(compressor) && rhs.is_string(compressor)
11850+
|| rhs.left.is_string(compressor)
11851+
&& (self.left.is_constant() || !rhs.right.has_side_effects(compressor)))) {
1185211852
swap_chain(self, compressor);
1185311853
}
1185411854
}

test/compress/concat-strings.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ concat_9: {
273273
expect_stdout: true
274274
}
275275

276+
concat_sequence: {
277+
options = {
278+
collapse_vars: true,
279+
strings: true,
280+
toplevel: true,
281+
unused: true,
282+
}
283+
input: {
284+
var a;
285+
console.log(12 + (a = null, "34" + a));
286+
}
287+
expect: {
288+
console.log(12 + "34" + null);
289+
}
290+
expect_stdout: "1234null"
291+
}
292+
276293
issue_3689: {
277294
options = {
278295
strings: true,

0 commit comments

Comments
 (0)