[Pkg-javascript-commits] [uglifyjs] 185/228: combine rules for binary boolean operations (#1744)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:28 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 1ddc05725d078ccf73d711e376c3c530cd517cdb
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Fri Mar 31 18:47:44 2017 +0800

    combine rules for binary boolean operations (#1744)
---
 lib/compress.js             | 90 ++++++++++++++++++++-------------------------
 test/compress/issue-1261.js |  4 +-
 2 files changed, 41 insertions(+), 53 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 55f6d79..a2332a9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3140,42 +3140,7 @@ merge(Compressor.prototype, {
             }
             break;
         }
-        if (compressor.option("booleans") && compressor.in_boolean_context()) switch (self.operator) {
-          case "&&":
-            var ll = self.left.evaluate(compressor);
-            var rr = self.right.evaluate(compressor);
-            if (!ll || !rr) {
-                compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
-                return make_node(AST_Seq, self, {
-                    car: self.left,
-                    cdr: make_node(AST_False, self)
-                }).optimize(compressor);
-            }
-            if (ll !== self.left && ll) {
-                return self.right.optimize(compressor);
-            }
-            if (rr !== self.right && rr) {
-                return self.left.optimize(compressor);
-            }
-            break;
-          case "||":
-            var ll = self.left.evaluate(compressor);
-            var rr = self.right.evaluate(compressor);
-            if (ll !== self.left && ll || rr !== self.right && rr) {
-                compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
-                return make_node(AST_Seq, self, {
-                    car: self.left,
-                    cdr: make_node(AST_True, self)
-                }).optimize(compressor);
-            }
-            if (!ll) {
-                return self.right.optimize(compressor);
-            }
-            if (!rr) {
-                return self.left.optimize(compressor);
-            }
-            break;
-          case "+":
+        if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {
             var ll = self.left.evaluate(compressor);
             var rr = self.right.evaluate(compressor);
             if (ll && typeof ll == "string") {
@@ -3192,7 +3157,6 @@ merge(Compressor.prototype, {
                     cdr: make_node(AST_True, self)
                 }).optimize(compressor);
             }
-            break;
         }
         if (compressor.option("comparisons") && self.is_boolean()) {
             if (!(compressor.parent() instanceof AST_Binary)
@@ -3233,24 +3197,48 @@ merge(Compressor.prototype, {
         if (compressor.option("evaluate")) {
             switch (self.operator) {
               case "&&":
-                if (self.left.is_constant()) {
-                    if (self.left.constant_value(compressor)) {
-                        compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
-                        return maintain_this_binding(compressor.parent(), self, self.right);
-                    } else {
-                        compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
-                        return maintain_this_binding(compressor.parent(), self, self.left);
+                var ll = self.left.evaluate(compressor);
+                if (!ll) {
+                    compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
+                    return maintain_this_binding(compressor.parent(), self, self.left).optimize(compressor);
+                } else if (ll !== self.left) {
+                    compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
+                    return maintain_this_binding(compressor.parent(), self, self.right).optimize(compressor);
+                }
+                if (compressor.option("booleans") && compressor.in_boolean_context()) {
+                    var rr = self.right.evaluate(compressor);
+                    if (!rr) {
+                        compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
+                        return make_node(AST_Seq, self, {
+                            car: self.left,
+                            cdr: make_node(AST_False, self)
+                        }).optimize(compressor);
+                    } else if (rr !== self.right) {
+                        compressor.warn("Dropping side-effect-free && in boolean context [{file}:{line},{col}]", self.start);
+                        return self.left.optimize(compressor);
                     }
                 }
                 break;
               case "||":
-                if (self.left.is_constant()) {
-                    if (self.left.constant_value(compressor)) {
-                        compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
-                        return maintain_this_binding(compressor.parent(), self, self.left);
-                    } else {
-                        compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start);
-                        return maintain_this_binding(compressor.parent(), self, self.right);
+                var ll = self.left.evaluate(compressor);
+                if (!ll) {
+                    compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start);
+                    return maintain_this_binding(compressor.parent(), self, self.right).optimize(compressor);
+                } else if (ll !== self.left) {
+                    compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
+                    return maintain_this_binding(compressor.parent(), self, self.left).optimize(compressor);
+                }
+                if (compressor.option("booleans") && compressor.in_boolean_context()) {
+                    var rr = self.right.evaluate(compressor);
+                    if (!rr) {
+                        compressor.warn("Dropping side-effect-free || in boolean context [{file}:{line},{col}]", self.start);
+                        return self.left.optimize(compressor);
+                    } else if (rr !== self.right) {
+                        compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
+                        return make_node(AST_Seq, self, {
+                            car: self.left,
+                            cdr: make_node(AST_True, self)
+                        }).optimize(compressor);
                     }
                 }
                 break;
diff --git a/test/compress/issue-1261.js b/test/compress/issue-1261.js
index a872c57..343c175 100644
--- a/test/compress/issue-1261.js
+++ b/test/compress/issue-1261.js
@@ -154,12 +154,12 @@ should_warn: {
         "WARN: Boolean || always true [test/compress/issue-1261.js:129,23]",
         "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:129,23]",
         "WARN: Condition always true [test/compress/issue-1261.js:129,23]",
-        "WARN: Boolean || always true [test/compress/issue-1261.js:130,8]",
+        "WARN: Condition left of || always true [test/compress/issue-1261.js:130,8]",
         "WARN: Condition always true [test/compress/issue-1261.js:130,8]",
         "WARN: Boolean && always false [test/compress/issue-1261.js:131,23]",
         "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:131,23]",
         "WARN: Condition always false [test/compress/issue-1261.js:131,23]",
-        "WARN: Boolean && always false [test/compress/issue-1261.js:132,8]",
+        "WARN: Condition left of && always false [test/compress/issue-1261.js:132,8]",
         "WARN: Condition always false [test/compress/issue-1261.js:132,8]",
         "WARN: + in boolean context always true [test/compress/issue-1261.js:133,23]",
         "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:133,23]",

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