account for declaration assignment in `collapse_vars` (#2859)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 1 Feb 2018 07:09:53 +0000 (15:09 +0800)
committerGitHub <noreply@github.com>
Thu, 1 Feb 2018 07:09:53 +0000 (15:09 +0800)
fixes #2858

lib/compress.js
test/compress/collapse_vars.js

index db6ee8b..9a3446c 100644 (file)
@@ -990,6 +990,7 @@ merge(Compressor.prototype, {
                     || node instanceof AST_Debugger
                     || node instanceof AST_IterationStatement && !(node instanceof AST_For)
                     || node instanceof AST_Try
+                    || node instanceof AST_VarDef && node.value && side_effects && !references_in_scope(node.name.definition())
                     || node instanceof AST_With
                     || parent instanceof AST_For && node !== parent.init
                     || (side_effects || !replace_all)
index b361781..f56face 100644 (file)
@@ -4190,3 +4190,37 @@ return_3: {
     }
     expect_stdout: "0"
 }
+
+issue_2858: {
+    options = {
+        collapse_vars: true,
+        unused: true,
+    }
+    input: {
+        var b;
+        (function() {
+            function f() {
+                a++;
+            }
+            f();
+            var c = f();
+            var a = void 0;
+            c || (b = a);
+        })();
+        console.log(b);
+    }
+    expect: {
+        var b;
+        (function() {
+            function f() {
+                a++;
+            }
+            f();
+            var c = f();
+            var a = void 0;
+            c || (b = a);
+        })();
+        console.log(b);
+    }
+    expect_stdout: "undefined"
+}