compress typeof x == "undefined" to x === undefined, which further gets
authorMihai Bazon <mihai@bazon.net>
Fri, 14 Sep 2012 16:56:59 +0000 (19:56 +0300)
committerMihai Bazon <mihai@bazon.net>
Fri, 14 Sep 2012 16:56:59 +0000 (19:56 +0300)
shortened to x === void 0 (or x === [][0] in unsafe mode)

lib/compress.js

index b37dcfe..369094e 100644 (file)
@@ -473,6 +473,7 @@ function Compressor(options, false_by_default) {
             switch (this.operator) {
               case "!": return !ev(e);
               case "typeof": return typeof ev(e);
+              case "void": return void ev(e);
               case "~": return ~ev(e);
               case "-": return -ev(e);
               case "+": return +ev(e);
@@ -1251,6 +1252,25 @@ function Compressor(options, false_by_default) {
                 (this.left.is_boolean() && this.right.is_boolean())) {
                 this.operator = this.operator.substr(0, 2);
             }
+            // XXX: intentionally falling down to the next case
+          case "==":
+          case "!=":
+            if (this.left instanceof AST_UnaryPrefix
+                && this.left.operator == "typeof"
+                && this.right instanceof AST_String
+                && this.right.value == "undefined") {
+                this.left = this.left.expression;
+                this.right = make_node(AST_Undefined, this.right).optimize(compressor);
+                if (this.operator.length == 2) this.operator += "=";
+            }
+            else if (this.left instanceof AST_String
+                     && this.left.value == "undefined"
+                     && this.right instanceof AST_UnaryPrefix
+                     && this.right.operator == "typeof") {
+                this.left = this.right.expression;
+                this.right = make_node(AST_Undefined, this.left).optimize(compressor);
+                if (this.operator.length == 2) this.operator += "=";
+            }
             break;
         }
         if (compressor.option("booleans") && compressor.in_boolean_context()) switch (this.operator) {