[Pkg-javascript-commits] [uglifyjs] 121/228: fix commit 88fb83a (#1622)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:22 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 96f8befdd7dbc3be9f5991f805ad8d60dfb3e6d6
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Sun Mar 19 11:59:42 2017 +0800

    fix commit 88fb83a (#1622)
    
    The following is wrong:
        `a == (b ? a : c)` => `b`
    Because:
    - `b` may not be boolean
    - `a` might have side effects
    - `a == a` is not always `true` (think `NaN`)
    - `a == c` is not always `false`
---
 lib/compress.js               | 26 ------------
 test/compress/conditionals.js | 96 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 26 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 53412a3..41612ad 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3057,32 +3057,6 @@ merge(Compressor.prototype, {
                     reverse();
                 }
             }
-            if (/^[!=]==?$/.test(self.operator)) {
-                if (self.left instanceof AST_SymbolRef && self.right instanceof AST_Conditional) {
-                    if (self.right.consequent instanceof AST_SymbolRef
-                        && self.right.consequent.definition() === self.left.definition()) {
-                        if (/^==/.test(self.operator)) return self.right.condition;
-                        if (/^!=/.test(self.operator)) return self.right.condition.negate(compressor);
-                    }
-                    if (self.right.alternative instanceof AST_SymbolRef
-                        && self.right.alternative.definition() === self.left.definition()) {
-                        if (/^==/.test(self.operator)) return self.right.condition.negate(compressor);
-                        if (/^!=/.test(self.operator)) return self.right.condition;
-                    }
-                }
-                if (self.right instanceof AST_SymbolRef && self.left instanceof AST_Conditional) {
-                    if (self.left.consequent instanceof AST_SymbolRef
-                        && self.left.consequent.definition() === self.right.definition()) {
-                        if (/^==/.test(self.operator)) return self.left.condition;
-                        if (/^!=/.test(self.operator)) return self.left.condition.negate(compressor);
-                    }
-                    if (self.left.alternative instanceof AST_SymbolRef
-                        && self.left.alternative.definition() === self.right.definition()) {
-                        if (/^==/.test(self.operator)) return self.left.condition.negate(compressor);
-                        if (/^!=/.test(self.operator)) return self.left.condition;
-                    }
-                }
-            }
         }
         self = self.lift_sequences(compressor);
         if (compressor.option("comparisons")) switch (self.operator) {
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index 074d2a6..7c81cc8 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -797,3 +797,99 @@ no_evaluate: {
         }
     }
 }
+
+equality_conditionals_false: {
+    options = {
+        conditionals: false,
+        sequences: true,
+    }
+    input: {
+        function f(a, b, c) {
+            console.log(
+                a == (b ? a : a),
+                a == (b ? a : c),
+                a != (b ? a : a),
+                a != (b ? a : c),
+                a === (b ? a : a),
+                a === (b ? a : c),
+                a !== (b ? a : a),
+                a !== (b ? a : c)
+            );
+        }
+        f(0, 0, 0);
+        f(0, true, 0);
+        f(1, 2, 3);
+        f(1, null, 3);
+        f(NaN);
+        f(NaN, "foo");
+    }
+    expect: {
+        function f(a, b, c) {
+            console.log(
+                a == (b ? a : a),
+                a == (b ? a : c),
+                a != (b ? a : a),
+                a != (b ? a : c),
+                a === (b ? a : a),
+                a === (b ? a : c),
+                a !== (b ? a : a),
+                a !== (b ? a : c)
+            );
+        }
+        f(0, 0, 0),
+        f(0, true, 0),
+        f(1, 2, 3),
+        f(1, null, 3),
+        f(NaN),
+        f(NaN, "foo");
+    }
+    expect_stdout: true
+}
+
+equality_conditionals_true: {
+    options = {
+        conditionals: true,
+        sequences: true,
+    }
+    input: {
+        function f(a, b, c) {
+            console.log(
+                a == (b ? a : a),
+                a == (b ? a : c),
+                a != (b ? a : a),
+                a != (b ? a : c),
+                a === (b ? a : a),
+                a === (b ? a : c),
+                a !== (b ? a : a),
+                a !== (b ? a : c)
+            );
+        }
+        f(0, 0, 0);
+        f(0, true, 0);
+        f(1, 2, 3);
+        f(1, null, 3);
+        f(NaN);
+        f(NaN, "foo");
+    }
+    expect: {
+        function f(a, b, c) {
+            console.log(
+                (b, a == a),
+                a == (b ? a : c),
+                (b, a != a),
+                a != (b ? a : c),
+                (b, a === a),
+                a === (b ? a : c),
+                (b, a !== a),
+                a !== (b ? a : c)
+            );
+        }
+        f(0, 0, 0),
+        f(0, true, 0),
+        f(1, 2, 3),
+        f(1, null, 3),
+        f(NaN),
+        f(NaN, "foo");
+    }
+    expect_stdout: true
+}

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