fix corner case in `dead_code` (#4304)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 19 Nov 2020 00:34:55 +0000 (00:34 +0000)
committerGitHub <noreply@github.com>
Thu, 19 Nov 2020 00:34:55 +0000 (08:34 +0800)
lib/compress.js
test/compress/const.js
test/compress/let.js

index acd15f5..f5dd73c 100644 (file)
@@ -7027,7 +7027,8 @@ merge(Compressor.prototype, {
     OPT(AST_Try, function(self, compressor) {
         self.body = tighten_body(self.body, compressor);
         if (compressor.option("dead_code")) {
-            if (has_declarations_only(self)) {
+            if (has_declarations_only(self)
+                && !(self.bcatch && self.bcatch.argname && !can_drop_symbol(self.bcatch.argname))) {
                 var body = [];
                 if (self.bcatch) {
                     extract_declarations_from_unreachable_code(compressor, self.bcatch, body);
index 0f5e3fd..7e393d4 100644 (file)
@@ -57,6 +57,23 @@ retain_block: {
     expect_stdout: true
 }
 
+retain_catch: {
+    options = {
+        dead_code: true,
+    }
+    input: {
+        try {} catch (a) {
+            const a = "aa";
+        }
+    }
+    expect: {
+        try {} catch (a) {
+            const a = "aa";
+        }
+    }
+    expect_stdout: true
+}
+
 if_dead_branch: {
     options = {
         conditionals: true,
index 0476c43..0669b36 100644 (file)
@@ -20,6 +20,26 @@ retain_block: {
     node_version: ">=4"
 }
 
+retain_catch: {
+    options = {
+        dead_code: true,
+    }
+    input: {
+        "use strict";
+        try {} catch (a) {
+            let a = "aa";
+        }
+    }
+    expect: {
+        "use strict";
+        try {} catch (a) {
+            let a = "aa";
+        }
+    }
+    expect_stdout: true
+    node_version: ">=4"
+}
+
 if_dead_branch: {
     options = {
         conditionals: true,