From 43fd45154bac89b89330e13bd1f167e2a927a23c Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Fri, 14 Sep 2012 19:56:59 +0300 Subject: [PATCH] compress typeof x == "undefined" to x === undefined, which further gets shortened to x === void 0 (or x === [][0] in unsafe mode) --- lib/compress.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/compress.js b/lib/compress.js index b37dcfe7..369094ee 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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) { -- 2.34.1