From b757450cd8e6c5f9fc766673a4ef9f32060703d7 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 11 Jan 2018 23:13:44 +0800 Subject: [PATCH] fix nested `unused` assignments (#2769) fixes #2768 --- lib/compress.js | 22 +++++++++-------- test/compress/drop-unused.js | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 86873176..21d79b08 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2817,7 +2817,7 @@ merge(Compressor.prototype, { def.value.walk(tw); } if (def.name.fixed_value() === def.value) { - fixed_ids[node_def.id] = true; + fixed_ids[node_def.id] = def; } } }); @@ -2846,9 +2846,7 @@ merge(Compressor.prototype, { var def = sym.definition(); var in_use = def.id in in_use_ids; if (node instanceof AST_Assign) { - if (!in_use - || def.id in fixed_ids - && node.left.fixed_value() !== node.right) { + if (!in_use || def.id in fixed_ids && fixed_ids[def.id] !== node) { return maintain_this_binding(parent, node, node.right.transform(tt)); } } else if (!in_use) return make_node(AST_Number, node, { @@ -2902,25 +2900,29 @@ merge(Compressor.prototype, { if (def.value) def.value = def.value.transform(tt); var sym = def.name.definition(); if (!drop_vars || sym.id in in_use_ids) { + if (def.value && sym.id in fixed_ids && fixed_ids[sym.id] !== def) { + def.value = def.value.drop_side_effect_free(compressor); + } if (def.name instanceof AST_SymbolVar) { var var_defs = var_defs_by_id.get(sym.id); if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); if (def.value) { - side_effects.push(make_node(AST_Assign, def, { + var assign = make_node(AST_Assign, def, { operator: "=", left: make_node(AST_SymbolRef, def.name, def.name), right: def.value - }).transform(tt)); + }); + if (fixed_ids[sym.id] === def) { + fixed_ids[sym.id] = assign; + } + side_effects.push(assign.transform(tt)); } remove(var_defs, def); sym.eliminated++; return; } } - if (def.value && sym.id in fixed_ids && def.name.fixed_value() !== def.value) { - def.value = def.value.drop_side_effect_free(compressor); - } if (def.value) { if (side_effects.length > 0) { if (tail.length > 0) { @@ -3031,7 +3033,7 @@ merge(Compressor.prototype, { if (node instanceof AST_Assign) { node.right.walk(tw); if (node.left.fixed_value() === node.right) { - fixed_ids[node_def.id] = true; + fixed_ids[node_def.id] = node; } } return true; diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 5ad489b9..0ac7bb33 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -1644,3 +1644,51 @@ cascade_drop_assign: { } expect_stdout: "PASS" } + +chained_3: { + options = { + reduce_vars: true, + unused: true, + } + input: { + console.log(function(a, b) { + var c = a, c = b; + b++; + return c; + }(1, 2)); + } + expect: { + console.log(function(a, b) { + var c = b; + b++; + return c; + }(0, 2)); + } + expect_stdout: "2" +} + +issue_2768: { + options = { + inline: true, + reduce_vars: true, + sequences: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + var a = "FAIL", c = 1; + var c = function(b) { + var d = b = a; + var e = --b + (d && (a = "PASS")); + }(); + console.log(a, typeof c); + } + expect: { + var a = "FAIL"; + var c = (d = a, 0, void (d && (a = "PASS"))); + var d; + console.log(a, typeof c); + } + expect_stdout: "PASS undefined" +} -- 2.34.1