diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-09-14 19:56:59 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-09-14 19:56:59 +0300 |
commit | 43fd45154bac89b89330e13bd1f167e2a927a23c (patch) | |
tree | c66863f1c3e891be2dc2bb068e9939fe70a9af2c /lib/compress.js | |
parent | 50d1670e4266b7cefaccb4eef779ee6e08c980e1 (diff) | |
download | tracifyjs-43fd45154bac89b89330e13bd1f167e2a927a23c.tar.gz tracifyjs-43fd45154bac89b89330e13bd1f167e2a927a23c.zip |
compress typeof x == "undefined" to x === undefined, which further gets
shortened to x === void 0 (or x === [][0] in unsafe mode)
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 20 |
1 files changed, 20 insertions, 0 deletions
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) { |