improve `if_return` (#2727)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 5 Jan 2018 12:24:30 +0000 (20:24 +0800)
committerGitHub <noreply@github.com>
Fri, 5 Jan 2018 12:24:30 +0000 (20:24 +0800)
lib/compress.js
test/compress/if_return.js

index e824d6f..7f33b53 100644 (file)
@@ -1436,8 +1436,9 @@ merge(Compressor.prototype, {
                     }
                     //---
                     // if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;
-                    if (multiple_if_returns && in_lambda && value && !stat.alternative
-                        && (!next || next instanceof AST_Return)) {
+                    if (value && !stat.alternative
+                        && (!next && in_lambda && multiple_if_returns
+                            || next instanceof AST_Return)) {
                         CHANGED = true;
                         stat = stat.clone();
                         stat.alternative = next || make_node(AST_Return, stat, {
index a0dfdc9..981b437 100644 (file)
@@ -372,3 +372,27 @@ if_var_return: {
         }
     }
 }
+
+if_if_return_return: {
+    options = {
+        conditionals: true,
+        if_return: true,
+    }
+    input: {
+        function f(a, b) {
+            if (a) {
+                if (b)
+                    return b;
+                return;
+            }
+            g();
+        }
+    }
+    expect: {
+        function f(a, b) {
+            if (a)
+                return b || void 0;
+            g();
+        }
+    }
+}