fix corner cases in `reduce_vars` & `unused` (#4306)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 19 Nov 2020 03:25:36 +0000 (03:25 +0000)
committerGitHub <noreply@github.com>
Thu, 19 Nov 2020 03:25:36 +0000 (11:25 +0800)
lib/compress.js
test/compress/const.js
test/compress/let.js

index f5dd73c..23eb5e4 100644 (file)
@@ -1085,7 +1085,10 @@ merge(Compressor.prototype, {
                     tw.loop_ids[d.id] = tw.in_loop;
                     d.fixed = fixed;
                     d.fixed.assigns = [ node ];
-                    if (name instanceof AST_SymbolConst && d.redefined()) d.single_use = false;
+                    if (name instanceof AST_SymbolConst && d.redefined()
+                        || !(can_drop_symbol(name) || is_safe_lexical(d))) {
+                        d.single_use = false;
+                    }
                 } else {
                     d.fixed = false;
                 }
@@ -5352,7 +5355,7 @@ merge(Compressor.prototype, {
                         return;
                     }
                     var sym = def.name.definition();
-                    var drop_sym = !is_var || can_drop_symbol(def.name);
+                    var drop_sym = is_var ? can_drop_symbol(def.name) : is_safe_lexical(sym);
                     if (!drop_sym || !drop_vars || sym.id in in_use_ids) {
                         if (def.value && indexOf_assign(sym, def) < 0) {
                             var write_only = def.value.write_only;
index 7e393d4..8dbe74d 100644 (file)
@@ -1257,3 +1257,51 @@ issue_4290_1: {
     }
     expect_stdout: true
 }
+
+issue_4305_1: {
+    options = {
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            const arguments = function() {
+                while (console.log("PASS"));
+            };
+            arguments();
+        })();
+    }
+    expect: {
+        (function() {
+            const arguments = function() {
+                while (console.log("PASS"));
+            };
+            arguments();
+        })();
+    }
+    expect_stdout: true
+}
+
+issue_4305_2: {
+    options = {
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function(a) {
+            const a = function() {
+                while (console.log("aaaaa"));
+            };
+            a();
+        })();
+    }
+    expect: {
+        (function(a) {
+            const a = function() {
+                while (console.log("aaaaa"));
+            };
+            a();
+        })();
+    }
+    expect_stdout: true
+}
index 0669b36..76641c3 100644 (file)
@@ -1134,3 +1134,55 @@ issue_4290_2: {
     expect_stdout: "PASS"
     node_version: ">=4"
 }
+
+issue_4305_1: {
+    options = {
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        (function() {
+            let arguments = function() {
+                while (console.log("PASS"));
+            };
+            arguments();
+        })();
+    }
+    expect: {
+        (function() {
+            let arguments = function() {
+                while (console.log("PASS"));
+            };
+            arguments();
+        })();
+    }
+    expect_stdout: true
+    node_version: ">=6"
+}
+
+issue_4305_2: {
+    options = {
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        "use strict";
+        (function(a) {
+            let a = function() {
+                while (console.log("aaaaa"));
+            };
+            a();
+        })();
+    }
+    expect: {
+        "use strict";
+        (function(a) {
+            let a = function() {
+                while (console.log("aaaaa"));
+            };
+            a();
+        })();
+    }
+    expect_stdout: true
+    node_version: ">=4"
+}