diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-11-29 02:05:48 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-29 10:05:48 +0800 |
commit | 9d34f8428bc4a5fb7d26ac953f585b2622d63171 (patch) | |
tree | fb36e0f7676354a2a5a2e7c1e4c8366894408370 /lib | |
parent | f045e2b46077e84db8b7ae787819a010077ce1fd (diff) | |
download | tracifyjs-9d34f8428bc4a5fb7d26ac953f585b2622d63171.tar.gz tracifyjs-9d34f8428bc4a5fb7d26ac953f585b2622d63171.zip |
fix corner case in `side_effects` (#4326)
fixes #4325
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/lib/compress.js b/lib/compress.js index 708b9df3..7434a20d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5592,20 +5592,10 @@ merge(Compressor.prototype, { } else if (node instanceof AST_ForIn) { if (!drop_vars || !compressor.option("loops")) return; if (!is_empty(node.body)) return; - var sym = node.init; - if (sym instanceof AST_Definitions) { - sym = sym.definitions[0].name; - } else while (sym instanceof AST_PropAccess) { - sym = sym.expression.tail_node(); - } - if (sym instanceof AST_Destructured) return; + var sym = get_init_symbol(node); + if (!sym) return; var def = sym.definition(); - if (!def) return; if (def.id in in_use_ids) return; - if (def.scope !== self) { - var d = self.find_variable(sym); - if ((d && d.redefined() || d) === def) return; - } log(sym, "Dropping unused loop variable {name}"); if (for_ins[def.id] === node) delete for_ins[def.id]; var body = []; @@ -5705,6 +5695,16 @@ merge(Compressor.prototype, { return rhs.right; } + function get_init_symbol(for_in) { + var init = for_in.init; + if (init instanceof AST_Definitions) { + init = init.definitions[0].name; + return init instanceof AST_SymbolDeclaration && init; + } + while (init instanceof AST_PropAccess) init = init.expression.tail_node(); + if (init instanceof AST_SymbolRef) return init; + } + function scan_ref_scoped(node, descend, init) { if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) { var def = node.left.definition(); @@ -5748,8 +5748,14 @@ merge(Compressor.prototype, { } if (!drop_vars || !compressor.option("loops")) return; if (!is_empty(node.body)) return; - if (node.init instanceof AST_Destructured) return; if (node.init.has_side_effects(compressor)) return; + var sym = get_init_symbol(node); + if (!sym) return; + var def = sym.definition(); + if (def.scope !== self) { + var d = self.find_variable(sym); + if ((d && d.redefined() || d) === def) return; + } node.object.walk(tw); return true; } @@ -6259,6 +6265,7 @@ merge(Compressor.prototype, { }); // always shallow clone to ensure stripping of negated IIFEs self = self.clone(); + self.expression = exp.clone(); } if (self instanceof AST_New) { var fn = exp; |