diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-11-17 20:03:20 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-18 04:03:20 +0800 |
commit | caa92aea5d6ff4f0ac053df3163a6bc2266002ec (patch) | |
tree | 3f1cc64312599e71c23bc26218c283e51d9bb93c | |
parent | 383163afa67c5726e18da145d6eb012ee1b064e3 (diff) | |
download | tracifyjs-caa92aea5d6ff4f0ac053df3163a6bc2266002ec.tar.gz tracifyjs-caa92aea5d6ff4f0ac053df3163a6bc2266002ec.zip |
fix corner case in `merge_vars` (#4289)
fixes #4288
-rw-r--r-- | lib/ast.js | 6 | ||||
-rw-r--r-- | lib/compress.js | 20 | ||||
-rw-r--r-- | test/compress/destructured.js | 30 |
3 files changed, 48 insertions, 8 deletions
@@ -509,10 +509,8 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames length_read uses_arguments", { }, each_argname: function(visit) { var tw = new TreeWalker(function(node) { - if (node instanceof AST_DestructuredObject) { - node.properties.forEach(function(prop) { - prop.value.walk(tw); - }); + if (node instanceof AST_DestructuredKeyVal) { + node.value.walk(tw); return true; } if (node instanceof AST_SymbolFunarg) visit(node); diff --git a/lib/compress.js b/lib/compress.js index fe2e6242..c86bb5f0 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4739,13 +4739,25 @@ merge(Compressor.prototype, { if (node === self) root = segment; if (node instanceof AST_Lambda) { if (node.name) references[node.name.definition().id] = false; - node.each_argname(node.uses_arguments && !tw.has_directive("use strict") ? function(node) { - references[node.definition().id] = false; + var marker = node.uses_arguments && !tw.has_directive("use strict") ? function(node) { + if (node instanceof AST_SymbolFunarg) references[node.definition().id] = false; } : function(node) { - mark(node, false, true); + if (node instanceof AST_SymbolFunarg) mark(node, false, true); + }; + var scanner = new TreeWalker(function(ref) { + if (!(ref instanceof AST_SymbolRef)) return; + var def = ref.definition(); + var ldef = node.variables.get(ref.name); + if (ldef && (ldef === def || def.undeclared || node.parent_scope.find_variable(ref) === def)) { + references[ldef.id] = false; + } + return true; + }); + node.argnames.forEach(function(argname) { + argname.mark_symbol(marker, scanner); }); } - descend(); + walk_body(node, tw); pop(); return true; } diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 6feb5bd6..7eaad226 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -1440,3 +1440,33 @@ issue_4286_2: { expect_stdout: "PASS" node_version: ">=6" } + +issue_4288: { + options = { + merge_vars: true, + } + input: { + function f({ + [new function() { + console.log(typeof b); + }()]: a, + }) { + var b = a; + b++; + } + f(0); + } + expect: { + function f({ + [new function() { + console.log(typeof b); + }()]: a, + }) { + var b = a; + b++; + } + f(0); + } + expect_stdout: "undefined" + node_version: ">=6" +} |