[Pkg-javascript-commits] [uglifyjs] 26/49: Account for side effects in `string + expr` optimization

Jonas Smedegaard dr at jones.dk
Fri Dec 9 11:43:26 UTC 2016


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

js pushed a commit to branch master
in repository uglifyjs.

commit 25fc02743af0e3fec6b10ffdb5cee3b7f22b9889
Author: kzc <zaxxon2011 at gmail.com>
Date:   Thu Sep 1 09:24:56 2016 -0400

    Account for side effects in `string + expr` optimization
---
 lib/compress.js             |  4 ++--
 test/compress/issue-1275.js | 49 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 8f786d7..8a08572 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2499,8 +2499,8 @@ merge(Compressor.prototype, {
           case "+":
             var ll = self.left.evaluate(compressor);
             var rr = self.right.evaluate(compressor);
-            if ((ll.length > 1 && ll[0] instanceof AST_String && ll[1]) ||
-                (rr.length > 1 && rr[0] instanceof AST_String && rr[1])) {
+            if ((ll.length > 1 && ll[0] instanceof AST_String && ll[1] && !self.right.has_side_effects(compressor)) ||
+                (rr.length > 1 && rr[0] instanceof AST_String && rr[1] && !self.left.has_side_effects(compressor))) {
                 compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
                 return make_node(AST_True, self);
             }
diff --git a/test/compress/issue-1275.js b/test/compress/issue-1275.js
new file mode 100644
index 0000000..e88e284
--- /dev/null
+++ b/test/compress/issue-1275.js
@@ -0,0 +1,49 @@
+string_plus_optimization: {
+    options = {
+        side_effects  : true,
+        evaluate      : true,
+        conditionals  : true,
+        comparisons   : true,
+        dead_code     : true,
+        booleans      : true,
+        unused        : true,
+        if_return     : true,
+        join_vars     : true,
+        cascade       : true,
+        hoist_funs    : true,
+    };
+    input: {
+        function foo(anything) {
+            function throwing_function() {
+                throw "nope";
+            }
+            try {
+                console.log('0' + throwing_function() ? "yes" : "no");
+            } catch (ex) {
+                console.log(ex);
+            }
+            console.log('0' + anything ? "yes" : "no");
+            console.log(anything + '0' ? "Yes" : "No");
+            console.log('' + anything);
+            console.log(anything + '');
+        }
+        foo();
+    }
+    expect: {
+        function foo(anything) {
+            function throwing_function() {
+                throw "nope";
+            }
+            try {
+                console.log('0' + throwing_function() ? "yes" : "no");
+            } catch (ex) {
+                console.log(ex);
+            }
+            console.log("yes");
+            console.log("Yes");
+            console.log('' + anything);
+            console.log(anything + '');
+        }
+        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