Fixed typeof undefined optimization and updated related test case to

accomodates the sort behaviour changes made in commit
mishoo/UglifyJS2@aebafad41e.
Signed-off-by: Justin Lau <justin@tclau.com>
This commit is contained in:
Justin Lau
2013-05-05 20:38:32 +08:00
committed by Mihai Bazon
parent 672699613e
commit a1958aad56
2 changed files with 10 additions and 11 deletions

View File

@@ -1751,14 +1751,14 @@ merge(Compressor.prototype, {
// XXX: intentionally falling down to the next case // XXX: intentionally falling down to the next case
case "==": case "==":
case "!=": case "!=":
if (self.left instanceof AST_String if (compressor.option("unsafe")
&& self.left.value == "undefined" && self.left instanceof AST_UnaryPrefix
&& self.right instanceof AST_UnaryPrefix && self.left.operator == "typeof"
&& self.right.operator == "typeof" && self.right instanceof AST_String
&& compressor.option("unsafe")) { && self.right.value == "undefined") {
if (!(self.right.expression instanceof AST_SymbolRef) if (!(self.left.expression instanceof AST_SymbolRef)
|| !self.right.expression.undeclared()) { || !self.left.expression.undeclared()) {
self.left = self.right.expression; self.left = self.left.expression;
self.right = make_node(AST_Undefined, self.left).optimize(compressor); self.right = make_node(AST_Undefined, self.left).optimize(compressor);
if (self.operator.length == 2) self.operator += "="; if (self.operator.length == 2) self.operator += "=";
} }

View File

@@ -1,10 +1,9 @@
typeof_eq_undefined: { typeof_eq_undefined: {
options = { options = {
comparisons: true, comparisons: true
unsafe: false
}; };
input: { a = typeof b.c != "undefined" } input: { a = typeof b.c != "undefined" }
expect: { a = "undefined" != typeof b.c } expect: { a = typeof b.c != "undefined" }
} }
typeof_eq_undefined_unsafe: { typeof_eq_undefined_unsafe: {