From dac9e69f9efb491c274670f36301fd38b0ef4dd6 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 18 Apr 2020 03:06:20 +0100 Subject: enhance `collapse_vars` (#3793) --- lib/compress.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/compress.js b/lib/compress.js index 70ac9b8f..d07c2412 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1307,12 +1307,10 @@ merge(Compressor.prototype, { if (candidate instanceof AST_Assign) referenced--; if (replaced && referenced == replaced) { abort = false; - } else if (candidate instanceof AST_Assign) { + } else { candidates.push(hit_stack); force_single = true; continue; - } else { - replaced = false; } if (replaced) { hit_index = 0; @@ -1320,6 +1318,9 @@ merge(Compressor.prototype, { for (var i = stat_index; !abort && i < statements.length; i++) { if (!statements[i].transform(multi_replacer)) statements.splice(i--, 1); } + if (candidate instanceof AST_VarDef) { + replaced = !compressor.exposed(def) && def.references.length == def.replaced; + } value_def.single_use = false; } } @@ -1738,8 +1739,11 @@ merge(Compressor.prototype, { if (!member(expr.name, def.orig)) return; var referenced = def.references.length - def.replaced; var declared = def.orig.length - def.eliminated; - if (declared > 1 && !(expr.name instanceof AST_SymbolFunarg) - || (referenced > 1 ? mangleable_var(expr.value) : !compressor.exposed(def))) { + if (declared > 1 && !(expr.name instanceof AST_SymbolFunarg)) { + mangleable_var(expr.value); + return make_node(AST_SymbolRef, expr.name, expr.name); + } + if (referenced > 1 ? mangleable_var(expr.value) : !compressor.exposed(def)) { return make_node(AST_SymbolRef, expr.name, expr.name); } } else if (expr instanceof AST_Assign) { @@ -1863,6 +1867,7 @@ merge(Compressor.prototype, { found = true; if (node instanceof AST_VarDef) { node.value = null; + if (value_def) value_def.replaced++; return node; } return in_list ? List.skip : null; -- cgit v1.2.3