aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralexlamsl <alexlamsl@gmail.com>2017-02-18 19:01:42 +0800
committeralexlamsl <alexlamsl@gmail.com>2017-02-18 19:01:42 +0800
commite5badb954157d41dba3cc74f8813a90a145d9ca3 (patch)
tree4f9dd6c4aabd4ed9a82f90db9e1c32f5c79b8f76
parentfa668a28b47e06d838659d4e0910460c84ca3a61 (diff)
downloadtracifyjs-e5badb954157d41dba3cc74f8813a90a145d9ca3.tar.gz
tracifyjs-e5badb954157d41dba3cc74f8813a90a145d9ca3.zip
enable typeof "undefined" for general use
move out of unsafe, guard corner case with screw_id8 instead closes #1446
-rw-r--r--lib/compress.js11
-rw-r--r--test/compress/issue-105.js25
-rw-r--r--test/compress/issue-1446.js71
3 files changed, 77 insertions, 30 deletions
diff --git a/lib/compress.js b/lib/compress.js
index a15206e8..b49ebef2 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 ca17adbf..00000000
--- 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 00000000..3d69aa09
--- /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}"
+}