fix corner case in `loops` & `unused` (#4241)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 24 Oct 2020 05:33:48 +0000 (06:33 +0100)
committerGitHub <noreply@github.com>
Sat, 24 Oct 2020 05:33:48 +0000 (13:33 +0800)
fixes #4240

lib/compress.js
test/compress/loops.js

index c6e27e0..216ca92 100644 (file)
@@ -5182,7 +5182,7 @@ merge(Compressor.prototype, {
                 var def = sym.definition();
                 if (!def) return;
                 if (def.id in in_use_ids) return;
-                if (def.scope !== self && member(def, self.enclosed)) return;
+                if (def.scope !== self && self.find_variable(sym) === def) return;
                 log(sym, "Dropping unused loop variable {name}");
                 if (for_ins[def.id] === node) delete for_ins[def.id];
                 var body = [];
index cfabe6f..6915082 100644 (file)
@@ -1222,3 +1222,35 @@ do_continue: {
     }
     expect_stdout: "PASS"
 }
+
+issue_4240: {
+    options = {
+        loops: true,
+        reduce_funcs: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function(a) {
+            function f() {
+                var o = { PASS: 42 };
+                for (a in o);
+            }
+            (function() {
+                if (f());
+            })();
+            console.log(a);
+        })();
+    }
+    expect: {
+        (function(a) {
+            (function() {
+                if (function() {
+                    for (a in { PASS: 42 });
+                }());
+            })();
+            console.log(a);
+        })();
+    }
+    expect_stdout: "PASS"
+}