diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-06-10 19:01:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-11 02:01:23 +0800 |
commit | 596fad182e853f83960b6b659b8093aa8ad09fc6 (patch) | |
tree | cdc89e0ddc233419971e01c6d6fb4789816a24e3 | |
parent | ed69adedcd7140fd93d888d6b9dd46a6153adc7f (diff) | |
download | tracifyjs-596fad182e853f83960b6b659b8093aa8ad09fc6.tar.gz tracifyjs-596fad182e853f83960b6b659b8093aa8ad09fc6.zip |
fix corner case in `unused` (#3987)
fixes #3986
-rw-r--r-- | lib/compress.js | 10 | ||||
-rw-r--r-- | test/compress/drop-unused.js | 34 |
2 files changed, 41 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js index 03432bee..0eaa3a4d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4674,9 +4674,13 @@ merge(Compressor.prototype, { var rhs = assign.right; if (!assign.write_only) return rhs; if (!(rhs instanceof AST_Binary && lazy_op[rhs.operator])) return rhs; - var sym = assign.left; - if (!(sym instanceof AST_SymbolRef) || sym.name != rhs.left.name) return rhs; - return rhs.right.has_side_effects(compressor) ? rhs : rhs.right; + if (!(rhs.left instanceof AST_SymbolRef)) return rhs; + if (!(assign.left instanceof AST_SymbolRef)) return rhs; + var def = assign.left.definition(); + if (rhs.left.definition() !== def) return rhs; + if (rhs.right.has_side_effects(compressor)) return rhs; + if (track_assigns(def, rhs.left)) add_assigns(def, rhs.left); + return rhs.right; } function scan_ref_scoped(node, descend, init) { diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 8c912a57..b66320ad 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -2755,3 +2755,37 @@ issue_3962_2: { } expect_stdout: "PASS" } + +issue_3986: { + options = { + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + var a = 0, b = 0; + (function() { + try { + throw 42; + } catch (e) { + a++; + } + b = b && 0; + })(b *= a); + console.log(b); + } + expect: { + var a = 0, b = 0; + (function() { + try { + throw 42; + } catch (e) { + a++; + } + b = b && 0; + })(b *= a); + console.log(b); + } + expect_stdout: "0" +} |