[Pkg-javascript-commits] [uglifyjs] 250/491: compress self comparisons (#2398)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:41 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 ee082ace1b69bff228ff43065333b8703c0505dc
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Oct 26 01:16:12 2017 +0800

    compress self comparisons (#2398)
---
 lib/compress.js            | 17 +++++++++++++++--
 test/compress/comparing.js | 39 ++++++++++++++++++++++++++++++++++++++-
 test/compress/evaluate.js  | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+), 3 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 9f41071..073399b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -581,7 +581,7 @@ merge(Compressor.prototype, {
                     if (!value && props[i].key === key) value = props[i].value;
                 }
             }
-            return value instanceof AST_SymbolRef ? value.fixed_value() : value;
+            return value instanceof AST_SymbolRef && value.fixed_value() || value;
         }
 
         function is_modified(node, value, level, immutable) {
@@ -3824,6 +3824,11 @@ merge(Compressor.prototype, {
     });
 
     var commutativeOperators = makePredicate("== === != !== * & | ^");
+    function is_object(node) {
+        return node instanceof AST_Array
+            || node instanceof AST_Lambda
+            || node instanceof AST_Object;
+    }
 
     OPT(AST_Binary, function(self, compressor){
         function reversible() {
@@ -3859,7 +3864,8 @@ merge(Compressor.prototype, {
           case "!==":
             if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
                 (self.left.is_number(compressor) && self.right.is_number(compressor)) ||
-                (self.left.is_boolean() && self.right.is_boolean())) {
+                (self.left.is_boolean() && self.right.is_boolean()) ||
+                self.left.equivalent_to(self.right)) {
                 self.operator = self.operator.substr(0, 2);
             }
             // XXX: intentionally falling down to the next case
@@ -3879,6 +3885,13 @@ merge(Compressor.prototype, {
                     if (self.operator.length == 2) self.operator += "=";
                 }
             }
+            // obj !== obj => false
+            else if (self.left instanceof AST_SymbolRef
+                && self.right instanceof AST_SymbolRef
+                && self.left.definition() === self.right.definition()
+                && is_object(self.left.fixed_value())) {
+                return make_node(self.operator[0] == "=" ? AST_True : AST_False, self);
+            }
             break;
         }
         if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {
diff --git a/test/compress/comparing.js b/test/compress/comparing.js
index c51fac3..11804cb 100644
--- a/test/compress/comparing.js
+++ b/test/compress/comparing.js
@@ -73,4 +73,41 @@ dont_change_in_or_instanceof_expressions: {
         1 instanceof 1;
         null instanceof null;
     }
-}
\ No newline at end of file
+}
+
+self_comparison_1: {
+    options = {
+        comparisons: true,
+    }
+    input: {
+        a === a;
+        a !== b;
+        b.c === a.c;
+        b.c !== b.c;
+    }
+    expect: {
+        a == a;
+        a !== b;
+        b.c === a.c;
+        b.c != b.c;
+    }
+}
+
+self_comparison_2: {
+    options = {
+        comparisons: true,
+        reduce_vars: true,
+        toplevel: true,
+    }
+    input: {
+        function f() {}
+        var o = {};
+        console.log(f != f, o === o);
+    }
+    expect: {
+        function f() {}
+        var o = {};
+        console.log(false, true);
+    }
+    expect_stdout: "false true"
+}
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index fe9464b..dc8ceb6 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -1195,3 +1195,40 @@ issue_2231_2: {
     }
     expect_stdout: true
 }
+
+self_comparison_1: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+        toplevel: true,
+        unsafe: true,
+        unused: true,
+    }
+    input: {
+        var o = { n: NaN };
+        console.log(o.n == o.n, o.n === o.n, o.n != o.n, o.n !== o.n, typeof o.n);
+    }
+    expect: {
+        console.log(false, false, true, true, "number");
+    }
+    expect_stdout: "false false true true 'number'"
+}
+
+self_comparison_2: {
+    options = {
+        evaluate: true,
+        hoist_props: true,
+        passes: 2,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var o = { n: NaN };
+        console.log(o.n == o.n, o.n === o.n, o.n != o.n, o.n !== o.n, typeof o.n);
+    }
+    expect: {
+        console.log(false, false, true, true, "number");
+    }
+    expect_stdout: "false false true true 'number'"
+}

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