From 5b20bad4b360dc58d66be0143d739ac3cfad9bfe Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 12 Nov 2019 05:16:14 +0800 Subject: fix corner case in `dead_code` (#3579) fixes #3578 --- lib/compress.js | 10 ++++++---- test/compress/dead-code.js | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 26d17295..08728bb4 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6545,7 +6545,8 @@ merge(Compressor.prototype, { } else if (self.left instanceof AST_SymbolRef) { if (self.left.is_immutable()) return strip_assignment(); var def = self.left.definition(); - var local = def.scope.resolve() === compressor.find_parent(AST_Lambda); + var scope = def.scope.resolve(); + var local = scope === compressor.find_parent(AST_Lambda); var level = 0, node, parent = self; do { node = parent; @@ -6553,12 +6554,13 @@ merge(Compressor.prototype, { if (parent instanceof AST_Assign) { if (!(parent.left instanceof AST_SymbolRef)) continue; if (parent.left.definition() !== def) continue; + if (in_try(level, parent)) break; 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; + if (is_reachable(scope, [ def ])) break; def.fixed = false; return strip_assignment(); } @@ -6604,9 +6606,9 @@ 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.resolve(); var parent; - while ((parent = compressor.parent(level++)) !== scope) { + while (parent = compressor.parent(level++)) { + if (parent === scope) return false; if (parent instanceof AST_Try) { if (parent.bfinally) return true; if (may_throw && parent.bcatch) return true; diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js index abc7384d..d4832c73 100644 --- a/test/compress/dead-code.js +++ b/test/compress/dead-code.js @@ -1102,3 +1102,30 @@ catch_return_assign: { } expect_stdout: "PASS" } + +issue_3578: { + options = { + dead_code: true, + } + input: { + var a = "FAIL", b, c; + try { + b = c.p = b = 0; + } catch (e) { + b += 42; + b && (a = "PASS"); + } + console.log(a); + } + expect: { + var a = "FAIL", b, c; + try { + b = c.p = b = 0; + } catch (e) { + b += 42; + b && (a = "PASS"); + } + console.log(a); + } + expect_stdout: "PASS" +} -- cgit v1.2.3