enhance `reduce_vars` (#5171)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 13 Nov 2021 14:18:56 +0000 (14:18 +0000)
committerGitHub <noreply@github.com>
Sat, 13 Nov 2021 14:18:56 +0000 (22:18 +0800)
lib/compress.js
test/compress/drop-unused.js
test/compress/hoist_vars.js
test/compress/reduce_vars.js

index 351ec2f..ab3d9df 100644 (file)
@@ -613,12 +613,14 @@ merge(Compressor.prototype, {
             if (def.single_use == "m") return false;
             var safe = tw.safe_ids[def.id];
             if (safe) {
-                if (!HOP(tw.safe_ids, def.id)) safe.read = safe.read && safe.read !== tw.safe_ids ? true : tw.safe_ids;
+                var in_order = HOP(tw.safe_ids, def.id);
+                if (!in_order) safe.read = safe.read && safe.read !== tw.safe_ids ? true : tw.safe_ids;
                 if (def.fixed == null) {
                     if (is_arguments(def)) return false;
                     if (def.global && def.name == "arguments") return false;
                     tw.loop_ids[def.id] = null;
                     def.fixed = make_node(AST_Undefined, def.orig[0]);
+                    if (in_order) delete def.safe_ids;
                     return true;
                 }
                 return !safe.assign || safe.assign === tw.safe_ids;
index dc934c9..eae53cd 100644 (file)
@@ -3062,7 +3062,7 @@ issue_4184: {
     expect_stdout: "42"
 }
 
-issue_4235: {
+issue_4235_1: {
     options = {
         inline: true,
         reduce_vars: true,
@@ -3081,13 +3081,37 @@ issue_4235: {
     }
     expect: {
         void function() {
-            var f;
-            console.log(f);
+            var f = console.log(f);
         }();
     }
     expect_stdout: "undefined"
 }
 
+issue_4235_2: {
+    options = {
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        side_effects: true,
+        unused: true,
+        varify: true,
+    }
+    input: {
+        (function() {
+            {
+                const f = 0;
+            }
+            (function f() {
+                var f = console.log(f);
+            })();
+        })();
+    }
+    expect: {
+        console.log(void 0);
+    }
+    expect_stdout: "undefined"
+}
+
 issue_4404: {
     options = {
         pure_getters: "strict",
index 39a2727..983b47e 100644 (file)
@@ -135,7 +135,7 @@ issue_2295: {
     }
 }
 
-issue_4487: {
+issue_4487_1: {
     options = {
         functions: true,
         hoist_vars: true,
@@ -152,14 +152,37 @@ issue_4487: {
     }
     expect: {
         function a() {
-            var f;
-            console.log(typeof f);
+            var f = console.log(typeof f);
         }
         a();
     }
     expect_stdout: "undefined"
 }
 
+issue_4487_2: {
+    options = {
+        functions: true,
+        hoist_vars: true,
+        keep_fnames: true,
+        passes: 2,
+        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);
+        })();
+    }
+    expect_stdout: "undefined"
+}
+
 issue_4489: {
     options = {
         collapse_vars: true,
index 43e1ff1..f98f88e 100644 (file)
@@ -7340,6 +7340,24 @@ local_assignment_modified: {
     expect_stdout: "42"
 }
 
+local_declaration: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+    }
+    input: {
+        var a;
+        a || console.log(a = "PASS");
+    }
+    expect: {
+        var a;
+        console.log("PASS");
+    }
+    expect_stdout: "PASS"
+}
+
 local_definition_modified: {
     options = {
         evaluate: true,