Fix #105: property comparison to undefined is not always safe
authorRichard Gibson <richard.gibson@gmail.com>
Thu, 24 Jan 2013 04:52:04 +0000 (23:52 -0500)
committerRichard Gibson <richard.gibson@gmail.com>
Thu, 24 Jan 2013 04:52:04 +0000 (23:52 -0500)
lib/compress.js
test/compress/issue-105.js [new file with mode: 0644]

index 982b2b1..bc0b9ee 100644 (file)
@@ -1739,7 +1739,8 @@ merge(Compressor.prototype, {
             if (self.left instanceof AST_String
                 && self.left.value == "undefined"
                 && self.right instanceof AST_UnaryPrefix
-                && self.right.operator == "typeof") {
+                && self.right.operator == "typeof"
+                && compressor.option("unsafe")) {
                 if (!(self.right.expression instanceof AST_SymbolRef)
                     || !self.right.expression.undeclared()) {
                     self.left = self.right.expression;
diff --git a/test/compress/issue-105.js b/test/compress/issue-105.js
new file mode 100644 (file)
index 0000000..349d732
--- /dev/null
@@ -0,0 +1,17 @@
+typeof_eq_undefined: {
+    options = {
+        comparisons: true,
+        unsafe: false
+    };
+    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 = b.c !== void 0 }
+}