[Pkg-javascript-commits] [uglifyjs] 22/228: improve string concatenation shuffle associative operations to minimise parentheses and aid other uglification efforts

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:13 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit 6b3c49e45837e8e1b32b60fe3b217b965ac16efd
Author: alexlamsl <alexlamsl at gmail.com>
Date:   Sat Feb 18 19:07:52 2017 +0800

    improve string concatenation
    shuffle associative operations to minimise parentheses and aid other uglification efforts
    
    closes #1454
---
 lib/compress.js                 |   9 ++-
 test/compress/arrays.js         |   8 +--
 test/compress/concat-strings.js | 140 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 152 insertions(+), 5 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index e8b271f..536b751 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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,
diff --git a/test/compress/arrays.js b/test/compress/arrays.js
index 2e1f86e..f0ded06 100644
--- a/test/compress/arrays.js
+++ b/test/compress/arrays.js
@@ -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");
     }
 }
diff --git a/test/compress/concat-strings.js b/test/compress/concat-strings.js
index 50eef8b..d2503c6 100644
--- a/test/compress/concat-strings.js
+++ b/test/compress/concat-strings.js
@@ -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"
+        );
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list