try negating AST_Binary
authorMihai Bazon <mihai@bazon.net>
Mon, 17 Sep 2012 08:16:44 +0000 (11:16 +0300)
committerMihai Bazon <mihai@bazon.net>
Mon, 17 Sep 2012 08:16:44 +0000 (11:16 +0300)
lib/compress.js

index 67f3726..09dc180 100644 (file)
@@ -187,12 +187,12 @@ function Compressor(options, false_by_default) {
             if (compressor.option("dead_code")) {
                 statements = eliminate_dead_code(statements, compressor);
             }
-            if (compressor.option("sequences")) {
-                statements = sequencesize(statements, compressor);
-            }
             if (compressor.option("if_return")) {
                 statements = handle_if_return(statements, compressor);
             }
+            if (compressor.option("sequences")) {
+                statements = sequencesize(statements, compressor);
+            }
             if (compressor.option("join_vars")) {
                 statements = join_consecutive_vars(statements, compressor);
             }
@@ -620,6 +620,9 @@ function Compressor(options, false_by_default) {
         def(AST_Statement, function(){
             throw new Error("Cannot negate a statement");
         });
+        def(AST_Function, function(){
+            return basic_negation(this);
+        });
         def(AST_UnaryPrefix, function(){
             if (this.operator == "!")
                 return this.expression;
@@ -664,7 +667,7 @@ function Compressor(options, false_by_default) {
         });
     })(function(node, func){
         node.DEFMETHOD("negate", function(compressor){
-            return func.call(this, compressor).optimize(compressor);
+            return func.call(this, compressor);
         });
     });
 
@@ -1372,19 +1375,6 @@ function Compressor(options, false_by_default) {
                 if (this.operator.length == 2) this.operator += "=";
             }
             break;
-          case "&&":
-          case "||":
-            if (this.left instanceof AST_UnaryPrefix && this.left.operator == "!"
-                && this.right instanceof AST_UnaryPrefix && this.right.operator == "!") {
-                this.left = this.left.expression;
-                this.right = this.right.expression;
-                this.operator = this.operator == "&&" ? "||" : "&&";
-                return make_node(AST_UnaryPrefix, this, {
-                    operator: "!",
-                    expression: this
-                }).optimize(compressor);
-            }
-            break;
         }
         if (compressor.option("booleans") && compressor.in_boolean_context()) switch (this.operator) {
           case "&&":
@@ -1469,6 +1459,13 @@ function Compressor(options, false_by_default) {
               case "<": reverse(">"); break;
               case "<=": reverse(">="); break;
             }
+            if (!(compressor.parent() instanceof AST_Binary)) {
+                var negated = make_node(AST_UnaryPrefix, self, {
+                    operator: "!",
+                    expression: self.negate(compressor)
+                });
+                self = best_of(self, negated);
+            }
         }
         return self;
     });