enhance `comparisons` (#3347)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 18 Mar 2019 17:34:25 +0000 (01:34 +0800)
committerGitHub <noreply@github.com>
Mon, 18 Mar 2019 17:34:25 +0000 (01:34 +0800)
lib/compress.js
test/compress/comparisons.js [moved from test/compress/comparing.js with 93% similarity]

index a31ce86..69a900c 100644 (file)
@@ -2252,45 +2252,38 @@ merge(Compressor.prototype, {
             return !compressor.option("pure_getters")
                 || this._dot_throw(compressor);
         });
-
         function is_strict(compressor) {
             return /strict/.test(compressor.option("pure_getters"));
         }
-
         def(AST_Node, is_strict);
-        def(AST_Null, return_true);
-        def(AST_Undefined, return_true);
-        def(AST_Constant, return_false);
         def(AST_Array, return_false);
-        def(AST_Object, function(compressor) {
-            if (!is_strict(compressor)) return false;
-            for (var i = this.properties.length; --i >=0;)
-                if (this.properties[i].value instanceof AST_Accessor) return true;
-            return false;
-        });
-        def(AST_Lambda, return_false);
-        def(AST_UnaryPostfix, return_false);
-        def(AST_UnaryPrefix, function() {
-            return this.operator == "void";
-        });
-        def(AST_Binary, function(compressor) {
-            return (this.operator == "&&" || this.operator == "||")
-                && (this.left._dot_throw(compressor) || this.right._dot_throw(compressor));
-        })
         def(AST_Assign, function(compressor) {
             return this.operator == "="
                 && this.right._dot_throw(compressor);
         })
+        def(AST_Binary, function(compressor) {
+            return (this.operator == "&&" || this.operator == "||")
+                && (this.left._dot_throw(compressor) || this.right._dot_throw(compressor));
+        })
         def(AST_Conditional, function(compressor) {
             return this.consequent._dot_throw(compressor)
                 || this.alternative._dot_throw(compressor);
         })
+        def(AST_Constant, return_false);
         def(AST_Dot, function(compressor) {
             if (!is_strict(compressor)) return false;
             var exp = this.expression;
             if (exp instanceof AST_SymbolRef) exp = exp.fixed_value();
             return !(exp instanceof AST_Lambda && this.property == "prototype");
         });
+        def(AST_Lambda, return_false);
+        def(AST_Null, return_true);
+        def(AST_Object, function(compressor) {
+            if (!is_strict(compressor)) return false;
+            for (var i = this.properties.length; --i >=0;)
+                if (this.properties[i].value instanceof AST_Accessor) return true;
+            return false;
+        });
         def(AST_Sequence, function(compressor) {
             return this.tail_node()._dot_throw(compressor);
         });
@@ -2302,6 +2295,11 @@ merge(Compressor.prototype, {
             var fixed = this.fixed_value();
             return !fixed || fixed._dot_throw(compressor);
         });
+        def(AST_UnaryPrefix, function() {
+            return this.operator == "void";
+        });
+        def(AST_UnaryPostfix, return_false);
+        def(AST_Undefined, return_true);
     })(function(node, func) {
         node.DEFMETHOD("_dot_throw", func);
     });
@@ -2335,6 +2333,10 @@ merge(Compressor.prototype, {
         def(AST_Sequence, function(compressor) {
             return this.tail_node().is_boolean(compressor);
         });
+        def(AST_SymbolRef, function(compressor) {
+            var fixed = this.fixed_value();
+            return fixed && fixed.is_boolean(compressor);
+        });
         var unary = makePredicate("! delete");
         def(AST_UnaryPrefix, function() {
             return unary[this.operator];
@@ -3249,6 +3251,12 @@ merge(Compressor.prototype, {
             return true;
         }
         def(AST_Node, return_false);
+        def(AST_Array, function() {
+            return all(this.elements);
+        });
+        def(AST_Binary, function() {
+            return this.left.is_constant_expression() && this.right.is_constant_expression();
+        });
         def(AST_Constant, return_true);
         def(AST_Lambda, function(scope) {
             var self = this;
@@ -3277,21 +3285,15 @@ merge(Compressor.prototype, {
             }));
             return result;
         });
-        def(AST_Unary, function() {
-            return this.expression.is_constant_expression();
-        });
-        def(AST_Binary, function() {
-            return this.left.is_constant_expression() && this.right.is_constant_expression();
-        });
-        def(AST_Array, function() {
-            return all(this.elements);
-        });
         def(AST_Object, function() {
             return all(this.properties);
         });
         def(AST_ObjectProperty, function() {
             return this.value.is_constant_expression();
         });
+        def(AST_Unary, function() {
+            return this.expression.is_constant_expression();
+        });
     })(function(node, func) {
         node.DEFMETHOD("is_constant_expression", func);
     });
similarity index 93%
rename from test/compress/comparing.js
rename to test/compress/comparisons.js
index 1ff2956..434f09c 100644 (file)
@@ -323,3 +323,25 @@ is_number_unsafe: {
     }
     expect_stdout: "true"
 }
+
+is_boolean_var: {
+    options = {
+        comparisons: true,
+        reduce_vars: true,
+    }
+    input: {
+        console.log(function(a, b) {
+            for (var i = 0, c = !b; i < a.length; i++)
+                if (!a[i] === c)
+                    return i;
+        }([ false, true ], 42));
+    }
+    expect: {
+        console.log(function(a, b) {
+            for (var i = 0, c = !b; i < a.length; i++)
+                if (!a[i] == c)
+                    return i;
+        }([ false, true ], 42));
+    }
+    expect_stdout: "1"
+}