[Pkg-javascript-commits] [uglifyjs] 59/228: minor improvement to string optimisation (#1514)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:16 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 fdc9b9413bfddc711fe6195bd4fd408ab1dfa95e
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Mar 2 11:31:39 2017 +0800

    minor improvement to string optimisation (#1514)
    
    - "" + "a"     => "a"
    - "" + a + "b" => a + "b"
    - "a" + ""     => "a" (improving on #45)
---
 lib/compress.js                 | 29 +++++++++++++++---------
 test/compress/concat-strings.js | 50 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 10 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 2455097..1d4e719 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1054,12 +1054,6 @@ merge(Compressor.prototype, {
         def(AST_Conditional, function(compressor){
             return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
         });
-        def(AST_Call, function(compressor){
-            return compressor.option("unsafe")
-                && this.expression instanceof AST_SymbolRef
-                && this.expression.name == "String"
-                && this.expression.undeclared();
-        });
     })(function(node, func){
         node.DEFMETHOD("is_string", func);
     });
@@ -2985,10 +2979,25 @@ merge(Compressor.prototype, {
                 }
             }
         }
-        if (self.operator == "+" && self.right instanceof AST_String
-            && self.right.getValue() === "" && self.left instanceof AST_Binary
-            && self.left.operator == "+" && self.left.is_string(compressor)) {
-            return self.left;
+        if (self.operator == "+") {
+            if (self.right instanceof AST_String
+                && self.right.getValue() == ""
+                && self.left.is_string(compressor)) {
+                return self.left;
+            }
+            if (self.left instanceof AST_String
+                && self.left.getValue() == ""
+                && self.right.is_string(compressor)) {
+                return self.right;
+            }
+            if (self.left instanceof AST_Binary
+                && self.left.operator == "+"
+                && self.left.left instanceof AST_String
+                && self.left.left.getValue() == ""
+                && self.right.is_string(compressor)) {
+                self.left = self.left.right;
+                return self.transform(compressor);
+            }
         }
         if (compressor.option("evaluate")) {
             switch (self.operator) {
diff --git a/test/compress/concat-strings.js b/test/compress/concat-strings.js
index d2503c6..2f99375 100644
--- a/test/compress/concat-strings.js
+++ b/test/compress/concat-strings.js
@@ -164,3 +164,53 @@ concat_6: {
         );
     }
 }
+
+concat_7: {
+    input: {
+        console.log(
+            "" + 1,
+            "" + "1",
+            "" + 1 + 2,
+            "" + 1 + "2",
+            "" + "1" + 2,
+            "" + "1" + "2",
+            "" + (x += "foo")
+        );
+    }
+    expect: {
+        console.log(
+            "" + 1,
+            "1",
+            "" + 1 + 2,
+            1 + "2",
+            "1" + 2,
+            "1" + "2",
+            x += "foo"
+        );
+    }
+}
+
+concat_8: {
+    input: {
+        console.log(
+            1 + "",
+            "1" + "",
+            1 + 2 + "",
+            1 + "2" + "",
+            "1" + 2 + "",
+            "1" + "2" + "",
+            (x += "foo") + ""
+        );
+    }
+    expect: {
+        console.log(
+            1 + "",
+            "1",
+            1 + 2 + "",
+            1 + "2",
+            "1" + 2,
+            "1" + "2",
+            x += "foo"
+        );
+    }
+}

-- 
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