compress typeof x == "undefined" to x === undefined, which further gets

shortened to x === void 0 (or x === [][0] in unsafe mode)
This commit is contained in:
Mihai Bazon
2012-09-14 19:56:59 +03:00
parent 50d1670e42
commit 43fd45154b

View File

@@ -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) {