From 10648c9af66364697f0f51219b0497fdfcb6dd34 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 8 Nov 2019 13:45:28 +0800 Subject: enhance `dead_code` (#3575) --- lib/compress.js | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/compress.js b/lib/compress.js index 0d19640d..43ee1126 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6543,23 +6543,28 @@ merge(Compressor.prototype, { } } } else if (self.left instanceof AST_SymbolRef) { + if (self.left.is_immutable()) return strip_assignment(); var def = self.left.definition(); - if (def.scope === compressor.find_parent(AST_Lambda)) { - if (self.left.is_immutable()) return strip_assignment(); - var level = 0, node, parent = self; - do { - node = parent; - parent = compressor.parent(level++); - if (parent instanceof AST_Exit) { - if (in_try(level, parent)) break; - if (is_reachable(def.scope, [ def ])) break; - def.fixed = false; - return strip_assignment(); - } - } while (parent instanceof AST_Binary && parent.right === node - || parent instanceof AST_Sequence && parent.tail_node() === node - || parent instanceof AST_UnaryPrefix); - } + var local = def.scope.resolve() === compressor.find_parent(AST_Lambda); + var level = 0, node, parent = self; + do { + node = parent; + parent = compressor.parent(level++); + if (parent instanceof AST_Assign) { + if (!(parent.left instanceof AST_SymbolRef)) continue; + if (parent.left.definition() !== def) continue; + def.fixed = false; + return strip_assignment(); + } else if (parent instanceof AST_Exit) { + if (!local) break; + if (in_try(level, parent)) break; + if (is_reachable(def.scope, [ def ])) break; + def.fixed = false; + return strip_assignment(); + } + } while (parent instanceof AST_Binary && parent.right === node + || parent instanceof AST_Sequence && parent.tail_node() === node + || parent instanceof AST_UnaryPrefix); } } self = self.lift_sequences(compressor); @@ -6599,7 +6604,7 @@ merge(Compressor.prototype, { self.right = make_node(AST_Null, right); var may_throw = node.may_throw(compressor); self.right = right; - var scope = self.left.definition().scope; + var scope = self.left.definition().scope.resolve(); var parent; while ((parent = compressor.parent(level++)) !== scope) { if (parent instanceof AST_Try) { -- cgit v1.2.3