More attempts to determine when addition is associative
authorMihai Bazon <mihai@bazon.net>
Sun, 22 Sep 2013 12:26:10 +0000 (15:26 +0300)
committerMihai Bazon <mihai@bazon.net>
Sun, 22 Sep 2013 12:26:10 +0000 (15:26 +0300)
Somebody hit me with bug reports on this. :)

Refs #300

lib/compress.js
test/compress/arrays.js
test/compress/concat-strings.js [new file with mode: 0644]

index dafed5d..37aba41 100644 (file)
@@ -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];
index 766ec48..e636347 100644 (file)
@@ -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 (file)
index 0000000..7919298
--- /dev/null
@@ -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";
+    }
+}