From 368ac8f93ca7ea704ee0eabd419cd083c6b843aa Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Tue, 25 Sep 2012 10:31:03 +0300 Subject: [PATCH] some boolean cleanup --- lib/compress.js | 75 ++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 51 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index a93b39b9..c6524e09 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1573,35 +1573,6 @@ function Compressor(options, false_by_default) { self.right = tmp; }; switch (self.operator) { - case "==": - case "!=": - var ll = self.left.evaluate(compressor); - var rr = self.right.evaluate(compressor); - if (ll.length == 2 && typeof ll[1] == "boolean") { - compressor.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", { - operator : self.operator, - value : ll[1], - file : self.start.file, - line : self.start.line, - col : self.start.col - }); - self.left = make_node(AST_Number, self.left, { - value: +ll[1] - }); - } - if (rr.length == 2 && typeof rr[1] == "boolean") { - compressor.warn("Non-strict equality against boolean {operator} {value} [{file}:{line},{col}]", { - operator : self.operator, - value : rr[1], - file : self.start.file, - line : self.start.line, - col : self.start.col - }); - self.right = make_node(AST_Number, self.right, { - value: +rr[1] - }); - } - break; case "<": reverse(">"); break; case "<=": reverse(">="); break; } @@ -1743,31 +1714,33 @@ function Compressor(options, false_by_default) { return self; }); - SQUEEZE(AST_True, function(self, compressor){ + SQUEEZE(AST_Boolean, function(self, compressor){ return self.optimize(compressor); }); - AST_True.DEFMETHOD("optimize", function(compressor){ - if (compressor.option("booleans")) return make_node(AST_UnaryPrefix, this, { - operator: "!", - expression: make_node(AST_Number, this, { - value: 0 - }) - }); - return this; - }); - - SQUEEZE(AST_False, function(self, compressor){ - return self.optimize(compressor); - }); - - AST_False.DEFMETHOD("optimize", function(compressor){ - if (compressor.option("booleans")) return make_node(AST_UnaryPrefix, this, { - operator: "!", - expression: make_node(AST_Number, this, { - value: 1 - }) - }); + AST_Boolean.DEFMETHOD("optimize", function(compressor){ + if (compressor.option("booleans")) { + var p = compressor.parent(); + if (p instanceof AST_Binary && (p.operator == "==" + || p.operator == "!=")) { + compressor.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", { + operator : p.operator, + value : this.value, + file : p.start.file, + line : p.start.line, + col : p.start.col, + }); + return make_node(AST_Number, this, { + value: +this.value + }); + } + return make_node(AST_UnaryPrefix, this, { + operator: "!", + expression: make_node(AST_Number, this, { + value: 1 - this.value + }) + }); + } return this; }); -- 2.34.1