aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMihai Bazon <mihai@bazon.net>2013-05-08 16:22:39 +0300
committerMihai Bazon <mihai@bazon.net>2013-05-08 16:22:48 +0300
commita6ed2c84ac65f68dc6cf102dffb44f3b00fd0277 (patch)
tree45d365d3f4cce1f6c5fa7785670fd005dd0a0b8f
parenta1958aad563030b76bf4d9af7bf80a160c625e27 (diff)
downloadtracifyjs-a6ed2c84ac65f68dc6cf102dffb44f3b00fd0277.tar.gz
tracifyjs-a6ed2c84ac65f68dc6cf102dffb44f3b00fd0277.zip
Better fix for equality of typeof ... against "undefined"
-rw-r--r--lib/compress.js27
-rw-r--r--test/compress/issue-105.js13
2 files changed, 26 insertions, 14 deletions
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 }
}