From 0e7ad47c05bc00b2da5eca1abce007512d23754d Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sun, 9 Oct 2022 01:14:10 +0800 Subject: [PATCH] enhance `side_effects` & `strings` --- lib/compress.js | 36 ++++++++++++++++----------------- test/compress/concat-strings.js | 17 ++++++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 375cc5dd542..26daf3e2aa2 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); } } diff --git a/test/compress/concat-strings.js b/test/compress/concat-strings.js index d2790d08fab..ac252b06c40 100644 --- a/test/compress/concat-strings.js +++ b/test/compress/concat-strings.js @@ -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,