[Pkg-javascript-commits] [uglifyjs] 48/77: Fix invalid removal of left side in && and || compression

Jonas Smedegaard dr at jones.dk
Tue May 19 00:02:31 UTC 2015


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

js pushed a commit to tag v2.4.18
in repository uglifyjs.

commit 992b6b9fcce47ca67ecb14675f10b172ce7a99b5
Author: Richard van Velzen <rvanvelzen1 at gmail.com>
Date:   Wed Feb 11 21:05:49 2015 +0100

    Fix invalid removal of left side in && and || compression
    
    See #637. This does not produce the optimal result, but it does prevent the removal of non-side-effect-free code.
---
 lib/compress.js            | 12 ++++++++++++
 test/compress/issue-637.js | 22 ++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index 2728598..b77b5ff 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2110,6 +2110,12 @@ merge(Compressor.prototype, {
             var rr = self.right.evaluate(compressor);
             if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
                 compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
+                if (self.left.has_side_effects(compressor)) {
+                    return make_node(AST_Seq, self, {
+                        car: self.left,
+                        cdr: make_node(AST_False)
+                    }).optimize(compressor);
+                }
                 return make_node(AST_False, self);
             }
             if (ll.length > 1 && ll[1]) {
@@ -2124,6 +2130,12 @@ merge(Compressor.prototype, {
             var rr = self.right.evaluate(compressor);
             if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
                 compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
+                if (self.left.has_side_effects(compressor)) {
+                    return make_node(AST_Seq, self, {
+                        car: self.left,
+                        cdr: make_node(AST_True)
+                    }).optimize(compressor);
+                }
                 return make_node(AST_True, self);
             }
             if (ll.length > 1 && !ll[1]) {
diff --git a/test/compress/issue-637.js b/test/compress/issue-637.js
new file mode 100644
index 0000000..45fd248
--- /dev/null
+++ b/test/compress/issue-637.js
@@ -0,0 +1,22 @@
+wrongly_optimized: {
+    options = {
+        conditionals: true,
+        booleans: true,
+        evaluate: true
+    };
+    input: {
+        function func() {
+            foo();
+        }
+        if (func() || true) {
+            bar();
+        }
+    }
+    expect: {
+        function func() {
+            foo();
+        }
+        // TODO: optimize to `func(), bar()`
+        (func(), 0) || bar();
+    }
+}

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