fix typeof side-effects (#1669)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 25 Mar 2017 09:40:18 +0000 (17:40 +0800)
committerGitHub <noreply@github.com>
Sat, 25 Mar 2017 09:40:18 +0000 (17:40 +0800)
`has_side_effects()` does not take `typeof`'s magical power of not tripping over undeclared variable into account.

fixes #1668

lib/compress.js
test/compress/typeof.js

index 47eb4d7..e12c5eb 100644 (file)
@@ -3013,10 +3013,10 @@ merge(Compressor.prototype, {
                 // typeof always returns a non-empty string, thus it's
                 // always true in booleans
                 compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
-                return make_node(AST_Seq, self, {
+                return (e instanceof AST_SymbolRef ? make_node(AST_True, self) : make_node(AST_Seq, self, {
                     car: e,
                     cdr: make_node(AST_True, self)
-                }).optimize(compressor);
+                })).optimize(compressor);
             }
         }
         // avoids infinite recursion of numerals
index 7bf8e5e..60f3d1d 100644 (file)
@@ -48,3 +48,15 @@ typeof_in_boolean_context: {
         foo();
     }
 }
+
+issue_1668: {
+    options = {
+        booleans: true,
+    }
+    input: {
+        if (typeof bar);
+    }
+    expect: {
+        if (!0);
+    }
+}