diff --git a/lib/compress.js b/lib/compress.js index ce8e23283e..5fd29464e2 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3285,7 +3285,16 @@ Compressor.prototype.compress = function(node) { if (expr instanceof AST_Assign && expr.right.single_use) return; var lhs_ids = Object.create(null); var marker = new TreeWalker(function(node) { - if (node instanceof AST_SymbolRef) lhs_ids[node.definition().id] = true; + if (!(node instanceof AST_SymbolRef)) return; + for (var level = 0, parent, child = node; parent = marker.parent(level++); child = parent) { + if (is_direct_assignment(child, parent)) { + if (parent instanceof AST_DestructuredKeyVal) parent = marker.parent(level++); + continue; + } + lhs_ids[node.definition().id] = true; + return; + } + lhs_ids[node.definition().id] = "a"; }); while (expr instanceof AST_Assign && expr.operator == "=") { expr.left.walk(marker); @@ -3293,6 +3302,7 @@ Compressor.prototype.compress = function(node) { } if (expr instanceof AST_ObjectIdentity) return rhs_exact_match; if (expr instanceof AST_SymbolRef) { + if (lhs_ids[expr.definition().id] === "a") return; var value = expr.evaluate(compressor); if (value === expr) return rhs_exact_match; return rhs_fuzzy_match(value, rhs_exact_match); diff --git a/test/compress/destructured.js b/test/compress/destructured.js index a283c3ac0e..83e7392e00 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -4241,3 +4241,51 @@ issue_5866_12: { expect_stdout: "PASS" node_version: ">=6" } + +issue_5899_1: { + options = { + collapse_vars: true, + } + input: { + var log = console.log, a, b; + a = "foo"; + log(a && a); + b = { p: a } = a; + log(a); + } + expect: { + var log = console.log, a, b; + log((a = "foo") && a); + b = { p: a } = a; + log(a); + } + expect_stdout: [ + "foo", + "undefined", + ] + node_version: ">=6" +} + +issue_5899_2: { + options = { + collapse_vars: true, + } + input: { + var log = console.log, a, b; + a = "foo"; + log(a && a); + b = [ a ] = a; + log(a); + } + expect: { + var log = console.log, a, b; + log((a = "foo") && a); + b = [ a ] = a; + log(a); + } + expect_stdout: [ + "foo", + "f", + ] + node_version: ">=6" +}