From e38754e8021c46b08595a4c71f26d20b2d538409 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 18 Apr 2020 23:28:01 +0100 Subject: [PATCH] fix corner case in `functions` & `unused` (#3803) fixes #3802 --- lib/compress.js | 19 +++++++++---------- test/compress/drop-unused.js | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index b0e25dde..6742aef3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4151,22 +4151,17 @@ merge(Compressor.prototype, { def.value = def.value.drop_side_effect_free(compressor); } var var_defs = var_defs_by_id.get(sym.id); - if (var_defs.length > 1) { - if (!def.value) { + if (!def.value) { + if (var_defs.length > 1) { AST_Node.warn("Dropping duplicated declaration of variable {name} [{file}:{line},{col}]", template(def.name)); remove(var_defs, def); sym.eliminated++; - return; - } - if (sym.orig.indexOf(def.name) > sym.eliminated) { - remove(var_defs, def); - duplicated++; + } else { + head.push(def); } - } - if (!def.value) { - head.push(def); } else if (compressor.option("functions") && !compressor.option("ie8") + && var_defs.length == 1 && def.value === def.name.fixed_value() && def.value instanceof AST_Function && !(def.value.name && def.value.name.definition().assignments) @@ -4188,6 +4183,10 @@ merge(Compressor.prototype, { } body.push(defun); } else { + if (var_defs.length > 1 && sym.orig.indexOf(def.name) > sym.eliminated) { + remove(var_defs, def); + duplicated++; + } if (side_effects.length > 0) { if (tail.length > 0) { side_effects.push(def.value); diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 3a61f24e..a39dd480 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -2488,3 +2488,25 @@ drop_duplicated_var_catch: { } } } + +issue_3802: { + options = { + functions: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var a = 0; + a += 0; + var a = function() {}; + console.log(typeof a); + } + expect: { + var a = 0; + a += 0; + a = function() {}; + console.log(typeof a); + } + expect_stdout: "function" +} -- 2.34.1