only do the typeof x == "undefined" optimization if x is a symbol reference and it...
authorMihai Bazon <mihai@bazon.net>
Mon, 17 Sep 2012 17:02:14 +0000 (20:02 +0300)
committerMihai Bazon <mihai@bazon.net>
Mon, 17 Sep 2012 17:02:57 +0000 (20:02 +0300)
lib/compress.js

index ba6f809..ca7bfe5 100644 (file)
@@ -1371,19 +1371,23 @@ function Compressor(options, false_by_default) {
             // XXX: intentionally falling down to the next case
           case "==":
           case "!=":
-            if (compressor.option("unsafe")) {
-                if (this.left instanceof AST_UnaryPrefix
-                    && this.left.operator == "typeof"
-                    && this.right instanceof AST_String
-                    && this.right.value == "undefined") {
+            if (this.left instanceof AST_UnaryPrefix
+                && this.left.operator == "typeof"
+                && this.right instanceof AST_String
+                && this.right.value == "undefined") {
+                if (!(this.left.expression instanceof AST_SymbolRef)
+                    || !this.left.expression.undeclared()) {
                     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") {
+            }
+            else if (this.left instanceof AST_String
+                     && this.left.value == "undefined"
+                     && this.right instanceof AST_UnaryPrefix
+                     && this.right.operator == "typeof") {
+                if (!(this.right.expression instanceof AST_SymbolRef)
+                    || !this.right.expression.undeclared()) {
                     this.left = this.right.expression;
                     this.right = make_node(AST_Undefined, this.left).optimize(compressor);
                     if (this.operator.length == 2) this.operator += "=";