fix corner case in `loops` (#4565)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 17 Jan 2021 22:36:59 +0000 (22:36 +0000)
committerGitHub <noreply@github.com>
Sun, 17 Jan 2021 22:36:59 +0000 (06:36 +0800)
fixes #4564

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

index d5334a9..8151153 100644 (file)
@@ -6169,7 +6169,7 @@ merge(Compressor.prototype, {
                 var def = sym.definition();
                 if (def.scope !== self) {
                     var d = find_variable(sym.name);
-                    if ((d && d.redefined() || d) === def) return;
+                    if (d === def || d && d.redefined() === def) return;
                 }
                 node.object.walk(tw);
                 return true;
index d76e7f7..a6d0277 100644 (file)
@@ -1280,3 +1280,33 @@ issue_4355: {
     }
     expect_stdout: "PASS"
 }
+
+issue_4564: {
+    options = {
+        loops: true,
+        unused: true,
+    }
+    input: {
+        try {
+            throw null;
+        } catch (a) {
+            var a;
+            (function() {
+                for (a in "foo");
+            })();
+            console.log(a);
+        }
+    }
+    expect: {
+        try {
+            throw null;
+        } catch (a) {
+            var a;
+            (function() {
+                for (a in "foo");
+            })();
+            console.log(a);
+        }
+    }
+    expect_stdout: "2"
+}
index c478da5..91df8bc 100644 (file)
@@ -666,7 +666,7 @@ function has_loopcontrol(body, loop, label) {
 }
 
 function is_error(result) {
-    return typeof result == "object" && typeof result.name == "string" && typeof result.message == "string";
+    return result && typeof result.name == "string" && typeof result.message == "string";
 }
 
 function is_timed_out(result) {