From: Mihai Bazon Date: Wed, 8 May 2013 13:22:39 +0000 (+0300) Subject: Better fix for equality of typeof ... against "undefined" X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=a6ed2c84ac65f68dc6cf102dffb44f3b00fd0277;p=UglifyJS.git Better fix for equality of typeof ... against "undefined" --- diff --git a/lib/compress.js b/lib/compress.js index c994a3ab..c94af7d0 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1726,8 +1726,8 @@ merge(Compressor.prototype, { var commutativeOperators = makePredicate("== === != !== * & | ^"); OPT(AST_Binary, function(self, compressor){ - function reverse(op) { - if (!(self.left.has_side_effects() || self.right.has_side_effects())) { + function reverse(op, force) { + if (force || !(self.left.has_side_effects() || self.right.has_side_effects())) { if (op) self.operator = op; var tmp = self.left; self.left = self.right; @@ -1737,7 +1737,10 @@ merge(Compressor.prototype, { if (commutativeOperators(self.operator)) { if (self.right instanceof AST_Constant && !(self.left instanceof AST_Constant)) { - reverse(); + // if right is a constant, whatever side effects the + // left side might have could not influence the + // result. hence, force switch. + reverse(null, true); } } self = self.lift_sequences(compressor); @@ -1751,15 +1754,15 @@ merge(Compressor.prototype, { // XXX: intentionally falling down to the next case case "==": case "!=": - if (compressor.option("unsafe") - && self.left instanceof AST_UnaryPrefix - && self.left.operator == "typeof" - && self.right instanceof AST_String - && self.right.value == "undefined") { - if (!(self.left.expression instanceof AST_SymbolRef) - || !self.left.expression.undeclared()) { - self.left = self.left.expression; - self.right = make_node(AST_Undefined, self.left).optimize(compressor); + if (self.left instanceof AST_String + && self.left.value == "undefined" + && self.right instanceof AST_UnaryPrefix + && self.right.operator == "typeof" + && compressor.option("unsafe")) { + if (!(self.right.expression instanceof AST_SymbolRef) + || !self.right.expression.undeclared()) { + self.right = self.right.expression; + self.left = make_node(AST_Undefined, self.left).optimize(compressor); if (self.operator.length == 2) self.operator += "="; } } diff --git a/test/compress/issue-105.js b/test/compress/issue-105.js index 0c37eb82..ca17adbf 100644 --- a/test/compress/issue-105.js +++ b/test/compress/issue-105.js @@ -3,7 +3,7 @@ typeof_eq_undefined: { comparisons: true }; input: { a = typeof b.c != "undefined" } - expect: { a = typeof b.c != "undefined" } + expect: { a = "undefined" != typeof b.c } } typeof_eq_undefined_unsafe: { @@ -12,5 +12,14 @@ typeof_eq_undefined_unsafe: { unsafe: true }; input: { a = typeof b.c != "undefined" } - expect: { a = b.c !== void 0 } + expect: { a = void 0 !== b.c } +} + +typeof_eq_undefined_unsafe2: { + options = { + comparisons: true, + unsafe: true + }; + input: { a = "undefined" != typeof b.c } + expect: { a = void 0 !== b.c } }