diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-04-18 23:28:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-19 06:28:01 +0800 |
commit | e38754e8021c46b08595a4c71f26d20b2d538409 (patch) | |
tree | 2a2a645a086d9dcd020f873505844829e4d046df | |
parent | eb6f32bfc3c9af65f417e2e4440115cec21ac075 (diff) | |
download | tracifyjs-e38754e8021c46b08595a4c71f26d20b2d538409.tar.gz tracifyjs-e38754e8021c46b08595a4c71f26d20b2d538409.zip |
fix corner case in `functions` & `unused` (#3803)
fixes #3802
-rw-r--r-- | lib/compress.js | 19 | ||||
-rw-r--r-- | 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" +} |