From 134ef0b1eb5e7ec0fc02e3aa0f36e319b4b82ac4 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 19 Nov 2020 00:34:55 +0000 Subject: [PATCH] fix corner case in `dead_code` (#4304) --- lib/compress.js | 3 ++- test/compress/const.js | 17 +++++++++++++++++ test/compress/let.js | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index acd15f5e..f5dd73cc 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); diff --git a/test/compress/const.js b/test/compress/const.js index 0f5e3fdd..7e393d4f 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -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, diff --git a/test/compress/let.js b/test/compress/let.js index 0476c431..0669b36f 100644 --- a/test/compress/let.js +++ b/test/compress/let.js @@ -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, -- 2.34.1