fix corner case in `booleans` (#4375)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 12 Dec 2020 21:01:38 +0000 (21:01 +0000)
committerGitHub <noreply@github.com>
Sat, 12 Dec 2020 21:01:38 +0000 (05:01 +0800)
fixes #4374

lib/ast.js
test/compress/booleans.js

index 262372e..31281c1 100644 (file)
@@ -1451,14 +1451,13 @@ TreeWalker.prototype = {
                 || p.tail_node() === self) {
                 self = p;
             } else if (p instanceof AST_Return) {
-                var fn;
-                do {
-                    fn = this.parent(++i);
-                    if (!fn) return false;
-                } while (!(fn instanceof AST_Lambda));
-                if (fn.name) return false;
-                self = this.parent(++i);
-                if (!self || self.TYPE != "Call" || self.expression !== fn) return false;
+                for (var call, fn = p; call = this.parent(++i); fn = call) {
+                    if (call.TYPE == "Call") {
+                        if (!(fn instanceof AST_Lambda) || fn.name) return false;
+                    } else if (fn instanceof AST_Lambda) {
+                        return false;
+                    }
+                }
             } else {
                 return false;
             }
index 6e7988b..779f8d8 100644 (file)
@@ -153,3 +153,31 @@ issue_3690: {
     }
     expect_stdout: "PASS"
 }
+
+issue_4374: {
+    options = {
+        booleans: true,
+        conditionals: true,
+        if_return: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            console.log(f());
+            function f(a) {
+                if (null) return 0;
+                if (a) return 1;
+                return 0;
+            }
+        })();
+    }
+    expect: {
+        (function() {
+            console.log(function(a) {
+                return !null && a ? 1 : 0;
+            }());
+        })();
+    }
+    expect_stdout: "0"
+}