From: Mihai Bazon Date: Sun, 22 Sep 2013 12:26:10 +0000 (+0300) Subject: More attempts to determine when addition is associative X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=afdaeba37d2d7541529addaefe85ed09515863af;p=UglifyJS.git More attempts to determine when addition is associative Somebody hit me with bug reports on this. :) Refs #300 --- diff --git a/lib/compress.js b/lib/compress.js index dafed5db..37aba41e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1965,7 +1965,7 @@ merge(Compressor.prototype, { self = make_node(AST_Binary, self, { operator: "+", left: make_node(AST_String, null, { - value: self.left.getValue() + self.right.left.getValue(), + value: "" + self.left.getValue() + self.right.left.getValue(), start: self.left.start, end: self.right.left.end }), @@ -1981,12 +1981,33 @@ merge(Compressor.prototype, { operator: "+", left: self.left.left, right: make_node(AST_String, null, { - value: self.left.right.getValue() + self.right.getValue(), + value: "" + self.left.right.getValue() + self.right.getValue(), start: self.left.right.start, end: self.right.end }) }); } + if (self.left instanceof AST_Binary + && self.left.operator == "+" + && self.left.is_string(compressor) + && self.left.right instanceof AST_Constant + && self.right instanceof AST_Binary + && self.right.operator == "+" + && self.right.left instanceof AST_Constant) { + self = make_node(AST_Binary, self, { + operator: "+", + left: make_node(AST_Binary, self.left, { + operator: "+", + left: self.left.left, + right: make_node(AST_String, null, { + value: "" + self.left.right.getValue() + self.right.left.getValue(), + start: self.left.right.start, + end: self.right.left.end + }) + }), + right: self.right.right + }); + } } } return self.evaluate(compressor)[0]; diff --git a/test/compress/arrays.js b/test/compress/arrays.js index 766ec484..e636347f 100644 --- a/test/compress/arrays.js +++ b/test/compress/arrays.js @@ -69,6 +69,6 @@ constant_join_2: { var e = [ "foo", "bar", boo(), "foo+1+2+3+bar", "baz", "x", "y" ].join("really-long-separator"); - var f = "strstr" + variable + "foobar" + ("moo" + foo); + var f = "strstr" + variable + "foobarmoo" + foo; } } diff --git a/test/compress/concat-strings.js b/test/compress/concat-strings.js new file mode 100644 index 00000000..79192987 --- /dev/null +++ b/test/compress/concat-strings.js @@ -0,0 +1,22 @@ +concat_1: { + options = { + evaluate: true + }; + input: { + var a = "foo" + "bar" + x() + "moo" + "foo" + y() + "x" + "y" + "z" + q(); + var b = "foo" + 1 + x() + 2 + "boo"; + var c = 1 + x() + 2 + "boo"; + + // this CAN'T safely be shortened to 1 + x() + "5boo" + var d = 1 + x() + 2 + 3 + "boo"; + + var e = 1 + x() + 2 + "X" + 3 + "boo"; + } + expect: { + var a = "foobar" + x() + "moofoo" + y() + "xyz" + q(); + var b = "foo1" + x() + "2boo"; + var c = 1 + x() + 2 + "boo"; + var d = 1 + x() + 2 + 3 + "boo"; + var e = 1 + x() + 2 + "X3boo"; + } +}