diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-03-10 15:44:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-10 23:44:49 +0800 |
commit | b4944a31a4f56ddad53046145d9d7431d5827ada (patch) | |
tree | f62a1dc93c2391b85d7fd6a8812c5c1bd8d49c90 | |
parent | 58362d5ec7bcc9c4522f185a277b2608cc99b223 (diff) | |
download | tracifyjs-b4944a31a4f56ddad53046145d9d7431d5827ada.tar.gz tracifyjs-b4944a31a4f56ddad53046145d9d7431d5827ada.zip |
fix corner case in `merge_vars` (#4760)
fixes #4759
-rw-r--r-- | lib/compress.js | 23 | ||||
-rw-r--r-- | test/compress/merge_vars.js | 46 |
2 files changed, 59 insertions, 10 deletions
diff --git a/lib/compress.js b/lib/compress.js index e207fed1..d01ebbea 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5271,7 +5271,7 @@ merge(Compressor.prototype, { var NO_MERGE = makePredicate("arguments await yield"); AST_Scope.DEFMETHOD("merge_variables", function(compressor) { if (!compressor.option("merge_vars")) return; - var self = this, segment = {}, root; + var in_try, root, segment = {}, self = this; var first = [], last = [], index = 0; var declarations = new Dictionary(); var references = Object.create(null); @@ -5476,10 +5476,11 @@ merge(Compressor.prototype, { return true; } if (node instanceof AST_Try) { - push(); - segment.block = node; + var save_try = in_try; + in_try = node; + var save = segment; walk_body(node, tw); - pop(); + segment = save; if (node.bcatch) { if (node.bcatch.argname) node.bcatch.argname.mark_symbol(function(node) { if (node instanceof AST_SymbolCatch) { @@ -5488,11 +5489,16 @@ merge(Compressor.prototype, { if (def = def.redefined()) references[def.id] = false; } }, tw); - push(); - if (node.bfinally) segment.block = node.bcatch; - walk_body(node.bcatch, tw); - pop(); + if (node.bfinally || (in_try = save_try)) { + walk_body(node.bcatch, tw); + } else { + push(); + walk_body(node.bcatch, tw); + pop(); + } } + in_try = save_try; + segment = save; if (node.bfinally) node.bfinally.walk(tw); return true; } @@ -5599,6 +5605,7 @@ merge(Compressor.prototype, { } function mark(sym, read) { + if (in_try) push(); var def = sym.definition(), ldef; if (def.id in references) { var refs = references[def.id]; diff --git a/test/compress/merge_vars.js b/test/compress/merge_vars.js index c45f8353..a26c0dd8 100644 --- a/test/compress/merge_vars.js +++ b/test/compress/merge_vars.js @@ -2620,9 +2620,9 @@ issue_4126_1: { try { console.log("PASS"); } catch (e) { - var b = a; + var c = a; } finally { - var c = b; + var c = c; } console.log(c); } @@ -3239,3 +3239,45 @@ issue_4653: { "0", ] } + +issue_4759: { + options = { + merge_vars: true, + toplevel: true, + } + input: { + var i = 2, a = 1, b, c, d; + while (i--) { + try { + if (1 != b) { + d = []; + null.p; + c = d; + } else { + b = 0; + a = c; + } + } catch (e) {} + b = a; + } + console.log(a); + } + expect: { + var i = 2, a = 1, b, c, d; + while (i--) { + try { + if (1 != b) { + d = []; + null.p; + c = d; + } else { + b = 0; + a = c; + } + } catch (e) {} + b = a; + } + console.log(a); + } + expect_stdout: "undefined" +} |