improve string concatenation
authoralexlamsl <alexlamsl@gmail.com>
Sat, 18 Feb 2017 11:07:52 +0000 (19:07 +0800)
committeralexlamsl <alexlamsl@gmail.com>
Tue, 21 Feb 2017 05:29:57 +0000 (13:29 +0800)
shuffle associative operations to minimise parentheses and aid other uglification efforts

closes #1454

lib/compress.js
test/compress/arrays.js
test/compress/concat-strings.js

index e8b271f..536b751 100644 (file)
@@ -2751,9 +2751,16 @@ merge(Compressor.prototype, {
         }
         // x && (y && z)  ==>  x && y && z
         // x || (y || z)  ==>  x || y || z
+        // x + ("y" + z)  ==>  x + "y" + z
+        // "x" + (y + "z")==>  "x" + y + "z"
         if (self.right instanceof AST_Binary
             && self.right.operator == self.operator
-            && (self.operator == "&&" || self.operator == "||"))
+            && (self.operator == "&&"
+                || self.operator == "||"
+                || (self.operator == "+"
+                    && (self.right.left.is_string(compressor)
+                        || (self.left.is_string(compressor)
+                            && self.right.right.is_string(compressor))))))
         {
             self.left = make_node(AST_Binary, self.left, {
                 operator : self.operator,
index 2e1f86e..f0ded06 100644 (file)
@@ -51,7 +51,7 @@ constant_join: {
         var c = boo() + "foo123bar" + bar();
         var c1 = "" + boo() + bar() + "foo123bar" + bar();
         var c2 = "12foobar" + baz();
-        var c3 = boo() + bar() + "foo123bar" + (bar() + "foo");
+        var c3 = boo() + bar() + "foo123bar" + bar() + "foo";
         var c4 = "12foobar" + baz();
         var c5 = [ boo() + bar() + "foo", 1, 2, 3, "bar", bar() + "foo" ].join();
         var c6 = [ "1,2,,,foo,bar", baz() ].join();
@@ -117,11 +117,11 @@ constant_join_3: {
         var d = "" + foo;
         var e = [ foo, "-", bar ].join("-");
         var f = "" + foo + bar;
-        var g = "foo" + (bar + "baz");
+        var g = "foo" + bar + "baz";
         var h = [ "-foo-", bar + "baz" ].join("-");
-        var i = "foo" + bar + (baz + "moo");
+        var i = "foo" + bar + baz + "moo";
         var j = foo + "bar" + baz;
-        var k = foo + ("bar" + baz);
+        var k = foo + "bar" + baz;
         var l = foo + (bar + "baz");
     }
 }
index 50eef8b..d2503c6 100644 (file)
@@ -24,3 +24,143 @@ concat_1: {
         var f = "\x00360\08\0";
     }
 }
+
+concat_2: {
+    options = {};
+    input: {
+        console.log(
+            1 + (2 + 3),
+            1 + (2 + "3"),
+            1 + ("2" + 3),
+            1 + ("2" + "3"),
+            "1" + (2 + 3),
+            "1" + (2 + "3"),
+            "1" + ("2" + 3),
+            "1" + ("2" + "3")
+        );
+    }
+    expect: {
+        console.log(
+            1 + (2 + 3),
+            1 + (2 + "3"),
+            1 + "2" + 3,
+            1 + "2" + "3",
+            "1" + (2 + 3),
+            "1" + 2 + "3",
+            "1" + "2" + 3,
+            "1" + "2" + "3"
+        );
+    }
+}
+
+concat_3: {
+    options = {};
+    input: {
+        console.log(
+            1 + 2 + (3 + 4 + 5),
+            1 + 2 + (3 + 4 + "5"),
+            1 + 2 + (3 + "4" + 5),
+            1 + 2 + (3 + "4" + "5"),
+            1 + 2 + ("3" + 4 + 5),
+            1 + 2 + ("3" + 4 + "5"),
+            1 + 2 + ("3" + "4" + 5),
+            1 + 2 + ("3" + "4" + "5")
+        );
+    }
+    expect: {
+        console.log(
+            1 + 2 + (3 + 4 + 5),
+            1 + 2 + (3 + 4 + "5"),
+            1 + 2 + (3 + "4") + 5,
+            1 + 2 + (3 + "4") + "5",
+            1 + 2 + "3" + 4 + 5,
+            1 + 2 + "3" + 4 + "5",
+            1 + 2 + "3" + "4" + 5,
+            1 + 2 + "3" + "4" + "5"
+        );
+    }
+}
+
+concat_4: {
+    options = {};
+    input: {
+        console.log(
+            1 + "2" + (3 + 4 + 5),
+            1 + "2" + (3 + 4 + "5"),
+            1 + "2" + (3 + "4" + 5),
+            1 + "2" + (3 + "4" + "5"),
+            1 + "2" + ("3" + 4 + 5),
+            1 + "2" + ("3" + 4 + "5"),
+            1 + "2" + ("3" + "4" + 5),
+            1 + "2" + ("3" + "4" + "5")
+        );
+    }
+    expect: {
+        console.log(
+            1 + "2" + (3 + 4 + 5),
+            1 + "2" + (3 + 4) + "5",
+            1 + "2" + 3 + "4" + 5,
+            1 + "2" + 3 + "4" + "5",
+            1 + "2" + "3" + 4 + 5,
+            1 + "2" + "3" + 4 + "5",
+            1 + "2" + "3" + "4" + 5,
+            1 + "2" + "3" + "4" + "5"
+        );
+    }
+}
+
+concat_5: {
+    options = {};
+    input: {
+        console.log(
+            "1" + 2 + (3 + 4 + 5),
+            "1" + 2 + (3 + 4 + "5"),
+            "1" + 2 + (3 + "4" + 5),
+            "1" + 2 + (3 + "4" + "5"),
+            "1" + 2 + ("3" + 4 + 5),
+            "1" + 2 + ("3" + 4 + "5"),
+            "1" + 2 + ("3" + "4" + 5),
+            "1" + 2 + ("3" + "4" + "5")
+        );
+    }
+    expect: {
+        console.log(
+            "1" + 2 + (3 + 4 + 5),
+            "1" + 2 + (3 + 4) + "5",
+            "1" + 2 + 3 + "4" + 5,
+            "1" + 2 + 3 + "4" + "5",
+            "1" + 2 + "3" + 4 + 5,
+            "1" + 2 + "3" + 4 + "5",
+            "1" + 2 + "3" + "4" + 5,
+            "1" + 2 + "3" + "4" + "5"
+        );
+    }
+}
+
+concat_6: {
+    options = {};
+    input: {
+        console.log(
+            "1" + "2" + (3 + 4 + 5),
+            "1" + "2" + (3 + 4 + "5"),
+            "1" + "2" + (3 + "4" + 5),
+            "1" + "2" + (3 + "4" + "5"),
+            "1" + "2" + ("3" + 4 + 5),
+            "1" + "2" + ("3" + 4 + "5"),
+            "1" + "2" + ("3" + "4" + 5),
+            "1" + "2" + ("3" + "4" + "5")
+        );
+    }
+    expect: {
+        console.log(
+            "1" + "2" + (3 + 4 + 5),
+            "1" + "2" + (3 + 4) + "5",
+            "1" + "2" + 3 + "4" + 5,
+            "1" + "2" + 3 + "4" + "5",
+            "1" + "2" + "3" + 4 + 5,
+            "1" + "2" + "3" + 4 + "5",
+            "1" + "2" + "3" + "4" + 5,
+            "1" + "2" + "3" + "4" + "5"
+        );
+    }
+}