diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-11-17 10:03:31 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-17 18:03:31 +0800 |
commit | 383163afa67c5726e18da145d6eb012ee1b064e3 (patch) | |
tree | 67a17555d89272b845e5ff291a42a666e87a14dc | |
parent | 8a83c8dd46dc8e446cd7231116a06c8b217291b7 (diff) | |
download | tracifyjs-383163afa67c5726e18da145d6eb012ee1b064e3.tar.gz tracifyjs-383163afa67c5726e18da145d6eb012ee1b064e3.zip |
fix corner case in `collapse_vars` (#4287)
fixes #4286
-rw-r--r-- | lib/compress.js | 21 | ||||
-rw-r--r-- | test/compress/destructured.js | 37 |
2 files changed, 43 insertions, 15 deletions
diff --git a/lib/compress.js b/lib/compress.js index e4426fa3..fe2e6242 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1972,15 +1972,9 @@ merge(Compressor.prototype, { function find_stop_value(node, level) { var parent = scanner.parent(level); if (parent instanceof AST_Array) return find_stop_value(parent, level + 1); - if (parent instanceof AST_Assign) { - if (may_throw(parent)) return node; - if (parent.left instanceof AST_SymbolRef) { - var name = parent.left.name; - if (lhs.name == name) return node; - if (value_def.name == name) return node; - } - return find_stop_value(parent, level + 1); - } + if (parent instanceof AST_Assign) return may_throw(parent) || parent.left.match_symbol(function(ref) { + return ref instanceof AST_SymbolRef && (lhs.name == ref.name || value_def.name == ref.name); + }) ? node : find_stop_value(parent, level + 1); if (parent instanceof AST_Binary) { if (lazy_op[parent.operator] && parent.left !== node) { do { @@ -2034,12 +2028,9 @@ merge(Compressor.prototype, { if (parent.operator == "delete") return node; return find_stop_value(parent, level + 1); } - if (parent instanceof AST_VarDef) { - var name = parent.name.name; - if (lhs.name == name) return node; - if (value_def.name == name) return node; - return find_stop_value(parent, level + 1); - } + if (parent instanceof AST_VarDef) return parent.name.match_symbol(function(sym) { + return sym instanceof AST_SymbolDeclaration && (lhs.name == sym.name || value_def.name == sym.name); + }) ? node : find_stop_value(parent, level + 1); if (parent instanceof AST_While) { if (parent.condition !== node) return node; return find_stop_value(parent, level + 1); diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 1555e3e3..6feb5bd6 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -1403,3 +1403,40 @@ issue_4284_3: { expect_stdout: "PASS" node_version: ">=6" } + +issue_4286_1: { + options = { + collapse_vars: true, + toplevel: true, + } + input: { + var a = "PASS", b; + (0 && a)[{ a } = b = a]; + console.log(b); + } + expect: { + var a = "PASS", b; + (0 && a)[{ a } = b = a]; + console.log(b); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_4286_2: { + options = { + collapse_vars: true, + toplevel: true, + } + input: { + a = [ "PASS" ]; + var b, { a } = b = a; + console.log(b[0]); + } + expect: { + var b, { a } = b = a = [ "PASS" ]; + console.log(b[0]); + } + expect_stdout: "PASS" + node_version: ">=6" +} |