some boolean cleanup
authorMihai Bazon <mihai@bazon.net>
Tue, 25 Sep 2012 07:31:03 +0000 (10:31 +0300)
committerMihai Bazon <mihai@bazon.net>
Tue, 25 Sep 2012 07:31:03 +0000 (10:31 +0300)
lib/compress.js

index a93b39b..c6524e0 100644 (file)
@@ -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;
     });