From 9e40abededaae3568e6cd931268cfaa0bd0ffbc0 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 22 Oct 2020 15:19:47 +0100 Subject: [PATCH] fix corner case in `unused` (#4236) fixes #4235 --- lib/compress.js | 8 +++++--- test/compress/drop-unused.js | 27 +++++++++++++++++++++++++++ test/compress/ie8.js | 18 ++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index ec9fd2a1..e4479ca8 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4774,9 +4774,11 @@ merge(Compressor.prototype, { node.definitions.forEach(function(defn) { var def = defn.name.definition(); var_defs_by_id.add(def.id, defn); - var redef = def.redefined(); - if (redef && node instanceof AST_Var) var_defs_by_id.add(redef.id, defn); - if ((!drop_vars || (node instanceof AST_Const ? redef : def.const_redefs)) + if (node instanceof AST_Var && def.orig[0] instanceof AST_SymbolCatch) { + var redef = def.redefined(); + if (redef) var_defs_by_id.add(redef.id, defn); + } + if ((!drop_vars || (node instanceof AST_Const ? def.redefined() : def.const_redefs)) && !(def.id in in_use_ids)) { in_use_ids[def.id] = true; in_use.push(def); diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 257d7a76..4448bdd8 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -3085,3 +3085,30 @@ issue_4184: { } expect_stdout: "42" } + +issue_4235: { + options = { + inline: true, + reduce_vars: true, + unused: true, + varify: true, + } + input: { + (function() { + { + const f = 0; + } + (function f() { + var f = console.log(f); + })(); + })(); + } + expect: { + (function() { + f = console.log(f), + void 0; + var f; + })(); + } + expect_stdout: "undefined" +} diff --git a/test/compress/ie8.js b/test/compress/ie8.js index f2132579..2a7dace3 100644 --- a/test/compress/ie8.js +++ b/test/compress/ie8.js @@ -2859,3 +2859,21 @@ issue_4186: { } expect_stdout: "NaN" } + +issue_4235: { + options = { + ie8: true, + unused: true, + } + input: { + try {} catch (e) {} + console.log(function e() { + var e = 0; + }()); + } + expect: { + try {} catch (e) {} + console.log(function e() {}()); + } + expect_stdout: "undefined" +}