[Pkg-javascript-commits] [uglifyjs] 471/491: change `undefined == x` to `null == x` (#2882)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:52:05 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 9637f51b6865d0987dcd950bc7113c871ca6cb3c
Author: Dan <dan.d.wolff at gmail.com>
Date:   Mon Feb 5 08:00:23 2018 +0100

    change `undefined == x` to `null == x` (#2882)
    
    fixes #2871
---
 lib/compress.js             |  7 ++++++-
 test/compress/issue-2871.js | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/lib/compress.js b/lib/compress.js
index 321ee5f..a48d0c9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -4790,6 +4790,7 @@ merge(Compressor.prototype, {
         if (compressor.option("comparisons")) switch (self.operator) {
           case "===":
           case "!==":
+            var is_strict_comparison = true;
             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()) ||
@@ -4799,8 +4800,12 @@ merge(Compressor.prototype, {
             // XXX: intentionally falling down to the next case
           case "==":
           case "!=":
+            // void 0 == x => null == x
+            if (!is_strict_comparison && is_undefined(self.left, compressor)) {
+                self.left = make_node(AST_Null, self.left);
+            }
             // "undefined" == typeof x => undefined === x
-            if (compressor.option("typeofs")
+            else if (compressor.option("typeofs")
                 && self.left instanceof AST_String
                 && self.left.value == "undefined"
                 && self.right instanceof AST_UnaryPrefix
diff --git a/test/compress/issue-2871.js b/test/compress/issue-2871.js
new file mode 100644
index 0000000..43c8352
--- /dev/null
+++ b/test/compress/issue-2871.js
@@ -0,0 +1,37 @@
+comparison_with_undefined: {
+    options = {
+        comparisons: true,
+    }
+    input: {
+        a == undefined;
+        a != undefined;
+        a === undefined;
+        a !== undefined;
+
+        undefined == a;
+        undefined != a;
+        undefined === a;
+        undefined !== a;
+
+        void 0 == a;
+        void 0 != a;
+        void 0 === a;
+        void 0 !== a;
+    }
+    expect: {
+        null == a;
+        null != a;
+        void 0 === a;
+        void 0 !== a;
+
+        null == a;
+        null != a;
+        void 0 === a;
+        void 0 !== a;
+
+        null == a;
+        null != a;
+        void 0 === a;
+        void 0 !== a;
+    }
+}

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