fix corner case in `join_vars` (#3796)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 18 Apr 2020 10:08:05 +0000 (11:08 +0100)
committerGitHub <noreply@github.com>
Sat, 18 Apr 2020 10:08:05 +0000 (18:08 +0800)
fixes #3795

lib/compress.js
test/compress/join_vars.js

index d07c241..d6f6a71 100644 (file)
@@ -2371,8 +2371,9 @@ merge(Compressor.prototype, {
                 var lhs = expr.left;
                 if (!(lhs instanceof AST_SymbolRef)) break;
                 if (is_undeclared_ref(lhs)) break;
+                if (lhs.scope !== scope) break;
                 var def = lhs.definition();
-                if (def.scope !== lhs.scope) break;
+                if (def.scope !== scope) break;
                 if (def.orig.length > def.eliminated + 1) break;
                 if (def.orig[0].TYPE != "SymbolVar") break;
                 var name = make_node(AST_SymbolVar, lhs, lhs);
index 9901c48..4e8d3b5 100644 (file)
@@ -782,3 +782,36 @@ issue_3791_2: {
     }
     expect_stdout: "function"
 }
+
+issue_3795: {
+    options = {
+        collapse_vars: true,
+        conditionals: true,
+        dead_code: true,
+        evaluate: true,
+        join_vars: true,
+        loops: true,
+        passes: 2,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var a = "FAIL";
+        function f(b) {
+            for (var i = 1; b && i; --i) return 0;
+            a = "PASS";
+        }
+        var c = f(a = "");
+        console.log(a);
+    }
+    expect: {
+        var a = "FAIL";
+        (function(b) {
+            a = "";
+            a = "PASS";
+        })();
+        console.log(a);
+    }
+    expect_stdout: "PASS"
+}