From 8195a664fd83e7381d0823e01a4ea30dc28e9f0d Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 19 Jun 2024 05:15:03 +0300 Subject: [PATCH] fix corner case in `collapse_vars` (#5855) fixes #5854 --- lib/compress.js | 6 +++-- test/compress/destructured.js | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 02d68e2d384..ff5652e4ba9 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2722,8 +2722,10 @@ Compressor.prototype.compress = function(node) { if (sym instanceof AST_PropAccess) return true; if (check_destructured(sym)) return true; return sym.match_symbol(function(node) { - return node instanceof AST_SymbolRef - && (lvalues.has(node.name) || read_toplevel && compressor.exposed(node.definition())); + if (node instanceof AST_PropAccess) return true; + if (node instanceof AST_SymbolRef) { + return lvalues.has(node.name) || read_toplevel && compressor.exposed(node.definition()); + } }, true); function reject(node) { diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 8b394b0ed65..a8a194d3c5c 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -3959,3 +3959,51 @@ issue_5844: { expect_stdout: "PASS" node_version: ">=6" } + +issue_5854_1: { + options = { + collapse_vars: true, + } + input: { + console.log(function(a) { + var b = a; + a++; + [ b[0] ] = [ "foo" ]; + return a; + }([]) ? "PASS" : "FAIL"); + } + expect: { + console.log(function(a) { + var b = a; + a++; + [ b[0] ] = [ "foo" ]; + return a; + }([]) ? "PASS" : "FAIL"); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_5854_2: { + options = { + collapse_vars: true, + } + input: { + console.log(function(a) { + var b = a; + a++; + ({ p: b[0] } = { p: "foo" }); + return a; + }([]) ? "PASS" : "FAIL"); + } + expect: { + console.log(function(a) { + var b = a; + a++; + ({ p: b[0] } = { p: "foo" }); + return a; + }([]) ? "PASS" : "FAIL"); + } + expect_stdout: "PASS" + node_version: ">=6" +}