[Pkg-javascript-commits] [uglifyjs] 16/228: enable typeof "undefined" for general use move out of unsafe, guard corner case with screw_id8 instead

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:12 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 e5badb954157d41dba3cc74f8813a90a145d9ca3
Author: alexlamsl <alexlamsl at gmail.com>
Date:   Sat Feb 18 19:01:42 2017 +0800

    enable typeof "undefined" for general use
    move out of unsafe, guard corner case with screw_id8 instead
    
    closes #1446
---
 lib/compress.js             | 11 +++----
 test/compress/issue-105.js  | 25 ----------------
 test/compress/issue-1446.js | 71 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 30 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index a15206e..b49ebef 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2504,14 +2504,15 @@ merge(Compressor.prototype, {
             // XXX: intentionally falling down to the next case
           case "==":
           case "!=":
+            // "undefined" == typeof x => undefined === x
             if (self.left instanceof AST_String
                 && self.left.value == "undefined"
                 && self.right instanceof AST_UnaryPrefix
-                && self.right.operator == "typeof"
-                && compressor.option("unsafe")) {
-                if (!(self.right.expression instanceof AST_SymbolRef)
-                    || !self.right.expression.undeclared()) {
-                    self.right = self.right.expression;
+                && self.right.operator == "typeof") {
+                var expr = self.right.expression;
+                if (expr instanceof AST_SymbolRef ? !expr.undeclared()
+                    : !(expr instanceof AST_PropAccess) || compressor.option("screw_ie8")) {
+                    self.right = expr;
                     self.left = make_node(AST_Undefined, self.left).optimize(compressor);
                     if (self.operator.length == 2) self.operator += "=";
                 }
diff --git a/test/compress/issue-105.js b/test/compress/issue-105.js
deleted file mode 100644
index ca17adb..0000000
--- a/test/compress/issue-105.js
+++ /dev/null
@@ -1,25 +0,0 @@
-typeof_eq_undefined: {
-    options = {
-        comparisons: true
-    };
-    input: { a = typeof b.c != "undefined" }
-    expect: { a = "undefined" != typeof b.c }
-}
-
-typeof_eq_undefined_unsafe: {
-    options = {
-        comparisons: true,
-        unsafe: true
-    };
-    input: { a = typeof b.c != "undefined" }
-    expect: { a = void 0 !== b.c }
-}
-
-typeof_eq_undefined_unsafe2: {
-    options = {
-        comparisons: true,
-        unsafe: true
-    };
-    input: { a = "undefined" != typeof b.c }
-    expect: { a = void 0 !== b.c }
-}
diff --git a/test/compress/issue-1446.js b/test/compress/issue-1446.js
new file mode 100644
index 0000000..3d69aa0
--- /dev/null
+++ b/test/compress/issue-1446.js
@@ -0,0 +1,71 @@
+typeof_eq_undefined: {
+    options = {
+        comparisons: true
+    }
+    input: {
+        var a = typeof b != "undefined";
+        b = typeof a != "undefined";
+        var c = typeof d.e !== "undefined";
+        var f = "undefined" === typeof g;
+        g = "undefined" === typeof f;
+        var h = "undefined" == typeof i.j;
+    }
+    expect: {
+        var a = "undefined" != typeof b;
+        b = void 0 !== a;
+        var c = void 0 !== d.e;
+        var f = "undefined" == typeof g;
+        g = void 0 === f;
+        var h = void 0 === i.j;
+    }
+}
+
+typeof_eq_undefined_ie8: {
+    options = {
+        comparisons: true,
+        screw_ie8: false
+    }
+    input: {
+        var a = typeof b != "undefined";
+        b = typeof a != "undefined";
+        var c = typeof d.e !== "undefined";
+        var f = "undefined" === typeof g;
+        g = "undefined" === typeof f;
+        var h = "undefined" == typeof i.j;
+    }
+    expect: {
+        var a = "undefined" != typeof b;
+        b = void 0 !== a;
+        var c = "undefined" != typeof d.e;
+        var f = "undefined" == typeof g;
+        g = void 0 === f;
+        var h = "undefined" == typeof i.j;
+    }
+}
+
+undefined_redefined: {
+    options = {
+        comparisons: true
+    }
+    input: {
+        function f(undefined) {
+            var n = 1;
+            return typeof n == "undefined";
+        }
+    }
+    expect_exact: "function f(undefined){var n=1;return void 0===n}"
+}
+
+undefined_redefined_mangle: {
+    options = {
+        comparisons: true
+    }
+    mangle = {}
+    input: {
+        function f(undefined) {
+            var n = 1;
+            return typeof n == "undefined";
+        }
+    }
+    expect_exact: "function f(n){var r=1;return void 0===r}"
+}

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