[Pkg-javascript-commits] [uglifyjs] 306/491: improve boolean compression (#2548)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:47 UTC 2018


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

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit 9a6b11f8e628c66731c4037ff408bb969003e6f7
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Fri Dec 1 22:41:35 2017 +0800

    improve boolean compression (#2548)
    
    fixes #2535
---
 lib/compress.js               | 70 +++++++++++++++++++++++++++++------------
 test/compress/conditionals.js | 73 ++++++++++++++++++++++++++++++++++++++++++-
 test/compress/evaluate.js     | 67 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 186 insertions(+), 24 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index adfbb79..5301d48 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -158,6 +158,7 @@ merge(Compressor.prototype, {
                 return true;
             }
             if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")
+                || p instanceof AST_Conditional
                 || p.tail_node() === self) {
                 self = p;
             } else {
@@ -4090,49 +4091,72 @@ merge(Compressor.prototype, {
         if (compressor.option("evaluate")) {
             switch (self.operator) {
               case "&&":
-                var ll = self.left.evaluate(compressor);
+                var ll = self.left.truthy ? true : self.left.falsy ? false : self.left.evaluate(compressor);
                 if (!ll) {
                     compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
                     return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor);
-                } else if (ll !== self.left) {
+                } else if (!(ll instanceof AST_Node)) {
                     compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
-                    return maintain_this_binding(compressor.parent(), compressor.self(), self.right).optimize(compressor);
+                    return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
                 }
-                if (compressor.in_boolean_context()) {
-                    var rr = self.right.evaluate(compressor);
-                    if (!rr) {
+                var rr = self.right.evaluate(compressor);
+                if (!rr) {
+                    if (compressor.in_boolean_context()) {
                         compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
                         return make_sequence(self, [
                             self.left,
                             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);
+                    } else self.falsy = true;
+                } else if (!(rr instanceof AST_Node)) {
+                    var parent = compressor.parent();
+                    if (parent.operator == "&&" && parent.left === compressor.self() || compressor.in_boolean_context()) {
+                        compressor.warn("Dropping side-effect-free && [{file}:{line},{col}]", self.start);
                         return self.left.optimize(compressor);
                     }
                 }
+                // x || false && y ---> x ? y : false
+                if (self.left.operator == "||") {
+                    var lr = self.left.right.evaluate(compressor);
+                    if (!lr) return make_node(AST_Conditional, self, {
+                        condition: self.left.left,
+                        consequent: self.right,
+                        alternative: self.left.right
+                    }).optimize(compressor);
+                }
                 break;
               case "||":
-                var ll = self.left.evaluate(compressor);
+                var ll = self.left.truthy ? true : self.left.falsy ? false : self.left.evaluate(compressor);
                 if (!ll) {
                     compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start);
-                    return maintain_this_binding(compressor.parent(), compressor.self(), self.right).optimize(compressor);
-                } else if (ll !== self.left) {
+                    return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
+                } else if (!(ll instanceof AST_Node)) {
                     compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
                     return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor);
                 }
-                if (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);
+                var rr = self.right.evaluate(compressor);
+                if (!rr) {
+                    var parent = compressor.parent();
+                    if (parent.operator == "||" && parent.left === compressor.self() || compressor.in_boolean_context()) {
+                        compressor.warn("Dropping side-effect-free || [{file}:{line},{col}]", self.start);
                         return self.left.optimize(compressor);
-                    } else if (rr !== self.right) {
+                    }
+                } else if (!(rr instanceof AST_Node)) {
+                    if (compressor.in_boolean_context()) {
                         compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
                         return make_sequence(self, [
                             self.left,
                             make_node(AST_True, self)
                         ]).optimize(compressor);
-                    }
+                    } else self.truthy = true;
+                }
+                if (self.left.operator == "&&") {
+                    var lr = self.left.right.evaluate(compressor);
+                    if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, {
+                        condition: self.left.left,
+                        consequent: self.left.right,
+                        alternative: self.right
+                    }).optimize(compressor);
                 }
                 break;
             }
@@ -4640,7 +4664,7 @@ merge(Compressor.prototype, {
                 consequent
             ]).optimize(compressor);
         }
-
+        var in_bool = compressor.in_boolean_context();
         if (is_true(self.consequent)) {
             if (is_false(self.alternative)) {
                 // c ? true : false ---> !!c
@@ -4696,18 +4720,24 @@ merge(Compressor.prototype, {
         // AST_True or !0
         function is_true(node) {
             return node instanceof AST_True
+                || in_bool
+                    && node instanceof AST_Constant
+                    && node.getValue()
                 || (node instanceof AST_UnaryPrefix
                     && node.operator == "!"
                     && node.expression instanceof AST_Constant
-                    && !node.expression.value);
+                    && !node.expression.getValue());
         }
         // AST_False or !1
         function is_false(node) {
             return node instanceof AST_False
+                || in_bool
+                    && node instanceof AST_Constant
+                    && !node.getValue()
                 || (node instanceof AST_UnaryPrefix
                     && node.operator == "!"
                     && node.expression instanceof AST_Constant
-                    && !!node.expression.value);
+                    && node.expression.getValue());
         }
     });
 
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index 22947d8..89c0526 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -1016,7 +1016,7 @@ delete_conditional_2: {
     expect_stdout: true
 }
 
-issue_2535: {
+issue_2535_1: {
     options = {
         booleans: true,
         conditionals: true,
@@ -1044,3 +1044,74 @@ issue_2535: {
         (x(), 0) && y();
     }
 }
+
+issue_2535_2: {
+    options = {
+        booleans: true,
+        conditionals: true,
+        evaluate: true,
+        side_effects: true,
+    }
+    input: {
+        function x() {}
+        function y() {
+            return "foo";
+        }
+        console.log((x() || true) || y());
+        console.log((y() || true) || x());
+        console.log((x() || true) && y());
+        console.log((y() || true) && x());
+        console.log((x() && true) || y());
+        console.log((y() && true) || x());
+        console.log((x() && true) && y());
+        console.log((y() && true) && x());
+        console.log((x() || false) || y());
+        console.log((y() || false) || x());
+        console.log((x() || false) && y());
+        console.log((y() || false) && x());
+        console.log((x() && false) || y());
+        console.log((y() && false) || x());
+        console.log((x() && false) && y());
+        console.log((y() && false) && x());
+    }
+    expect: {
+        function x() {}
+        function y() {
+            return "foo";
+        }
+        console.log(x() || !0);
+        console.log(y() || !0);
+        console.log((x(), y()));
+        console.log((y(), x()));
+        console.log(!!x() || y());
+        console.log(!!y() || x());
+        console.log(x() && y());
+        console.log(y() && x());
+        console.log(x() || y());
+        console.log(y() || x());
+        console.log(!!x() && y());
+        console.log(!!y() && x());
+        console.log((x(), y()));
+        console.log((y(), x()));
+        console.log(x() && !1);
+        console.log(y() && !1);
+    }
+    expect_stdout: [
+        "true",
+        "foo",
+        "foo",
+        "undefined",
+        "foo",
+        "true",
+        "undefined",
+        "undefined",
+        "foo",
+        "foo",
+        "false",
+        "undefined",
+        "foo",
+        "undefined",
+        "undefined",
+        "false",
+    ]
+}
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index 04b15a8..8e030d2 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -1,6 +1,7 @@
 and: {
     options = {
-        evaluate: true
+        evaluate: true,
+        side_effects: true,
     }
     input: {
         var a;
@@ -76,7 +77,8 @@ and: {
 
 or: {
     options = {
-        evaluate: true
+        evaluate: true,
+        side_effects: true,
     }
     input: {
         var a;
@@ -158,7 +160,8 @@ or: {
 
 unary_prefix: {
     options = {
-        evaluate: true
+        evaluate: true,
+        side_effects: true,
     }
     input: {
         a = !0 && b;
@@ -1245,3 +1248,61 @@ self_comparison_2: {
     }
     expect_stdout: "false false true true 'number'"
 }
+
+issue_2535_1: {
+    options = {
+        booleans: true,
+        evaluate: true,
+        sequences: true,
+        side_effects: true,
+    }
+    input: {
+        if ((x() || true) || y()) z();
+        if ((x() || true) && y()) z();
+        if ((x() && true) || y()) z();
+        if ((x() && true) && y()) z();
+        if ((x() || false) || y()) z();
+        if ((x() || false) && y()) z();
+        if ((x() && false) || y()) z();
+        if ((x() && false) && y()) z();
+    }
+    expect: {
+        if (x(), 1) z();
+        if (x(), y()) z();
+        if (x() || y()) z();
+        if (x() && y()) z();
+        if (x() || y()) z();
+        if (x() && y()) z();
+        if (x(), y()) z();
+        if (x(), 0) z();
+    }
+}
+
+issue_2535_2: {
+    options = {
+        booleans: true,
+        evaluate: true,
+        sequences: true,
+        side_effects: true,
+    }
+    input: {
+        (x() || true) || y();
+        (x() || true) && y();
+        (x() && true) || y();
+        (x() && true) && y();
+        (x() || false) || y();
+        (x() || false) && y();
+        (x() && false) || y();
+        (x() && false) && y();
+    }
+    expect: {
+        x(),
+        x(), y(),
+        x() || y(),
+        x() && y(),
+        x() || y(),
+        x() && y(),
+        x(), y(),
+        x();
+    }
+}

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