Fix typeof evaluation for regex and function
authorRoman Bataev <roman.bataev@gmail.com>
Thu, 4 Apr 2013 02:34:38 +0000 (22:34 -0400)
committerRoman Bataev <roman.bataev@gmail.com>
Thu, 4 Apr 2013 02:34:38 +0000 (22:34 -0400)
lib/compress.js

index 5a6adcd..216826d 100644 (file)
@@ -625,7 +625,18 @@ merge(Compressor.prototype, {
             var e = this.expression;
             switch (this.operator) {
               case "!": return !ev(e);
-              case "typeof": return typeof ev(e);
+              case "typeof":
+                // Function would be evaluated to an array and so typeof would
+                // incorrectly return 'object'. Hence making is a special case.
+                if (e instanceof AST_Function) return typeof function(){};
+
+                e = ev(e);
+
+                // typeof <RegExp> returns "object" or "function" on different platforms
+                // so cannot evaluate reliably
+                if (e instanceof RegExp) throw def;
+
+                return typeof e;
               case "void": return void ev(e);
               case "~": return ~ev(e);
               case "-":