From 383163afa67c5726e18da145d6eb012ee1b064e3 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 17 Nov 2020 10:03:31 +0000 Subject: [PATCH] fix corner case in `collapse_vars` (#4287) fixes #4286 --- lib/compress.js | 21 ++++++-------------- 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" +} -- 2.34.1