fix `unused` on var of the same name within catch (#1716)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 28 Mar 2017 13:25:49 +0000 (21:25 +0800)
committerGitHub <noreply@github.com>
Tue, 28 Mar 2017 13:25:49 +0000 (21:25 +0800)
fixes #1715

lib/compress.js
test/compress/drop-unused.js

index c60f863..64c654d 100644 (file)
@@ -1894,7 +1894,13 @@ merge(Compressor.prototype, {
                     if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
                         var def = node.definitions.filter(function(def){
                             if (def.value) def.value = def.value.transform(tt);
-                            if (def.name.definition().id in in_use_ids) return true;
+                            var sym = def.name.definition();
+                            if (sym.id in in_use_ids) return true;
+                            if (sym.orig[0] instanceof AST_SymbolCatch
+                                && sym.scope.parent_scope.find_variable(def.name).orig[0] === def.name) {
+                                def.value = def.value && def.value.drop_side_effect_free(compressor);
+                                return true;
+                            }
                             var w = {
                                 name : def.name.name,
                                 file : def.name.start.file,
index 10ca549..aa875ec 100644 (file)
@@ -844,3 +844,90 @@ issue_1709: {
     }
     expect_stdout: true
 }
+
+issue_1715_1: {
+    options = {
+        unused: true,
+    }
+    input: {
+        var a = 1;
+        function f() {
+            a++;
+            try {} catch (a) {
+                var a;
+            }
+        }
+        f();
+        console.log(a);
+    }
+    expect: {
+        var a = 1;
+        function f() {
+            a++;
+            try {} catch (a) {
+                var a;
+            }
+        }
+        f();
+        console.log(a);
+    }
+    expect_stdout: "1"
+}
+
+issue_1715_2: {
+    options = {
+        unused: true,
+    }
+    input: {
+        var a = 1;
+        function f() {
+            a++;
+            try {} catch (a) {
+                var a = 2;
+            }
+        }
+        f();
+        console.log(a);
+    }
+    expect: {
+        var a = 1;
+        function f() {
+            a++;
+            try {} catch (a) {
+                var a;
+            }
+        }
+        f();
+        console.log(a);
+    }
+    expect_stdout: "1"
+}
+
+issue_1715_3: {
+    options = {
+        unused: true,
+    }
+    input: {
+        var a = 1;
+        function f() {
+            a++;
+            try {} catch (a) {
+                var a = 2 + x();
+            }
+        }
+        f();
+        console.log(a);
+    }
+    expect: {
+        var a = 1;
+        function f() {
+            a++;
+            try {} catch (a) {
+                var a = x();
+            }
+        }
+        f();
+        console.log(a);
+    }
+    expect_stdout: "1"
+}