fix corner case in `hoist_vars` (#5196)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 24 Nov 2021 20:49:38 +0000 (20:49 +0000)
committerGitHub <noreply@github.com>
Wed, 24 Nov 2021 20:49:38 +0000 (04:49 +0800)
fixes #5195

lib/compress.js
test/compress/hoist_vars.js

index d17c945..ed8b94f 100644 (file)
@@ -9102,6 +9102,7 @@ merge(Compressor.prototype, {
                 });
                 def.references.push(name);
             }
+            def.assignments++;
             def.eliminated++;
             def.single_use = false;
             return a;
index 59fa6bb..3a2844a 100644 (file)
@@ -151,9 +151,9 @@ issue_4487_1: {
         var b = a();
     }
     expect: {
-        function a() {
+        var a = function f() {
             var f = console.log(typeof f);
-        }
+        };
         a();
     }
     expect_stdout: "undefined"
@@ -175,6 +175,31 @@ issue_4487_2: {
         };
         var b = a();
     }
+    expect: {
+        function a() {
+            var f = console.log(typeof f);
+        }
+        a();
+    }
+    expect_stdout: "undefined"
+}
+
+issue_4487_3: {
+    options = {
+        functions: true,
+        hoist_vars: true,
+        keep_fnames: true,
+        passes: 3,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var a = function f() {
+            var f = console.log(typeof f);
+        };
+        var b = a();
+    }
     expect: {
         (function a() {
             console.log(typeof void 0);
@@ -256,9 +281,8 @@ issue_4736: {
     expect: {
         (function() {
             (function() {
-                var b = 1 << 30;
                 0,
-                console.log(b);
+                console.log(1073741824);
             })();
         })();
     }
@@ -275,18 +299,18 @@ issue_4839: {
         unused: true,
     }
     input: {
-        var o = function(a, b) {
+        var log = console.log, o = function(a, b) {
             return b && b;
         }("foo");
         for (var k in o)
             throw "FAIL";
-        console.log("PASS");
+        log("PASS");
     }
     expect: {
-        var k, o = void 0;
-        for (k in o)
+        var k, log = console.log;
+        for (k in void 0)
             throw "FAIL";
-        console.log("PASS");
+        log("PASS");
     }
     expect_stdout: "PASS"
 }
@@ -312,8 +336,7 @@ issue_4859: {
     }
     expect: {
         (function f(a) {
-            var d = 1 / 0, d = Infinity;
-            console.log(d);
+            console.log(Infinity);
             return f;
         })();
     }
@@ -448,3 +471,32 @@ issue_5187: {
     }
     expect_stdout: "42"
 }
+
+issue_5195: {
+    options = {
+        hoist_props: true,
+        hoist_vars: true,
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            var a;
+            do {
+                var b = { p: a };
+            } while (console.log(b += ""));
+        }
+        f();
+    }
+    expect: {
+        (function() {
+            var a, b;
+            do {
+                b = { p: a };
+            } while (console.log(b += ""));
+        })();
+    }
+    expect_stdout: "[object Object]"
+}