diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-04-18 11:52:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-18 18:52:44 +0800 |
commit | 60d4e7b09fb962598a9f9f0fb64f92416a9f7e9c (patch) | |
tree | cecb9e23567d6b75d97933be4002975f3ca5cc52 /lib | |
parent | b38838c6bf064bfe54f0419a5eb7cd4298bfc3a3 (diff) | |
download | tracifyjs-60d4e7b09fb962598a9f9f0fb64f92416a9f7e9c.tar.gz tracifyjs-60d4e7b09fb962598a9f9f0fb64f92416a9f7e9c.zip |
enhance `unused` (#3794)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/lib/compress.js b/lib/compress.js index d6f6a710..3aa9f0bc 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4134,6 +4134,7 @@ merge(Compressor.prototype, { // side effects, we can cascade the init. code // into the next one, or next statement. var side_effects = []; + var duplicated = 0; node.definitions.forEach(function(def) { if (def.value) def.value = def.value.transform(tt); var sym = def.name.definition(); @@ -4142,24 +4143,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 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { - AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); - if (def.value) { - var ref = make_node(AST_SymbolRef, def.name, def.name); - sym.references.push(ref); - var assign = make_node(AST_Assign, def, { - operator: "=", - left: ref, - right: def.value - }); - if (fixed_ids[sym.id] === def) { - fixed_ids[sym.id] = assign; - } - side_effects.push(assign.transform(tt)); + if (var_defs.length > 1) { + if (!def.value) { + 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++; } - remove(var_defs, def); - sym.eliminated++; - return; } if (!def.value) { head.push(def); @@ -4220,7 +4214,22 @@ merge(Compressor.prototype, { return !def || fn.name && def === fn.name.definition(); } }); - if (head.length > 0 || tail.length > 0) { + if (head.length == 0 && tail.length == duplicated) { + [].unshift.apply(side_effects, tail.map(function(def) { + AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); + var sym = def.name.definition(); + var ref = make_node(AST_SymbolRef, def.name, def.name); + sym.references.push(ref); + var assign = make_node(AST_Assign, def, { + operator: "=", + left: ref, + right: def.value + }); + if (fixed_ids[sym.id] === def) fixed_ids[sym.id] = assign; + sym.eliminated++; + return assign.transform(tt); + })); + } else if (head.length > 0 || tail.length > 0) { node.definitions = head.concat(tail); body.push(node); } |