convert to number under boolean context (#2545)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 1 Dec 2017 04:52:36 +0000 (12:52 +0800)
committerGitHub <noreply@github.com>
Fri, 1 Dec 2017 04:52:36 +0000 (12:52 +0800)
lib/compress.js
test/compress/conditionals.js
test/compress/issue-1639.js
test/compress/issue-637.js
test/compress/issue-640.js
test/compress/reduce_vars.js
test/compress/transform.js
test/compress/typeof.js

index 395c4ed..8b2951d 100644 (file)
@@ -4699,6 +4699,9 @@ merge(Compressor.prototype, {
     });
 
     OPT(AST_Boolean, function(self, compressor){
+        if (compressor.in_boolean_context()) return make_node(AST_Number, self, {
+            value: +self.value
+        });
         if (compressor.option("booleans")) {
             var p = compressor.parent();
             if (p instanceof AST_Binary && (p.operator == "=="
index 9cb27fa..22947d8 100644 (file)
@@ -1037,10 +1037,10 @@ issue_2535: {
     expect: {
         y();
         x() && y();
-        (x(), 0) || y();
+        (x(), 1) && y();
         x() && y();
         x() && y();
         x() && y();
-        (x(), 1) || y();
+        (x(), 0) && y();
     }
 }
index b6a9647..fc3db98 100644 (file)
@@ -26,7 +26,7 @@ issue_1639_1: {
     }
     expect: {
         for (var a = 100, b = 10, L1 = 5; --L1 > 0;)
-            if (--b, !1) var ignore = 0;
+            if (--b, 0) var ignore = 0;
         console.log(a, b);
     }
     expect_stdout: true
@@ -57,7 +57,7 @@ issue_1639_2: {
     expect: {
         var a = 100, b = 10;
         function f19() {
-            ++a, 1;
+            ++a, 0;
         }
         f19(),
         console.log(a, b);
index 45fd248..ed42cb8 100644 (file)
@@ -17,6 +17,6 @@ wrongly_optimized: {
             foo();
         }
         // TODO: optimize to `func(), bar()`
-        (func(), 0) || bar();
+        (func(), 1) && bar();
     }
 }
index c9a68dc..784092c 100644 (file)
@@ -100,7 +100,7 @@ wrongly_optimized: {
             foo();
         }
         // TODO: optimize to `func(), bar()`
-        if (func(), !0) bar();
+        if (func(), 1) bar();
     }
 }
 
index bcfa7b3..4a098f7 100644 (file)
@@ -2347,7 +2347,7 @@ booleans: {
     }
     expect: {
         console.log(function(a) {
-            if (!1);
+            if (0);
             switch (!1) {
               case 0:
                 return "FAIL";
index 1e21da5..68d3384 100644 (file)
@@ -45,9 +45,9 @@ condition_evaluate: {
         if (void 0 == null);
     }
     expect: {
-        while (!1);
-        for (; !0;);
-        if (!0);
+        while (0);
+        for (; 1;);
+        if (1);
     }
 }
 
index 60f3d1d..34949fb 100644 (file)
@@ -44,7 +44,7 @@ typeof_in_boolean_context: {
         function f2() { return g(), "Yes"; }
         foo();
         console.log(1);
-        var a = !(console.log(2), !0);
+        var a = !(console.log(2), 1);
         foo();
     }
 }
@@ -57,6 +57,6 @@ issue_1668: {
         if (typeof bar);
     }
     expect: {
-        if (!0);
+        if (1);
     }
 }