From b54f298ce9a722bb419ecdad56aed45d5f5ad4c4 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L." Date: Thu, 5 Sep 2024 15:33:38 +0300 Subject: [PATCH] fix corner case in `merge_vars` (#5931) fixes #5930 --- lib/compress.js | 20 +++++++------- test/compress/const.js | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index f0d470fa706..831050c9ef2 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6791,7 +6791,7 @@ Compressor.prototype.compress = function(node) { var id = node.definition().id; var refs = references[id]; if (refs) { - refs.push(node); + push_ref(refs, node); } else if (!(id in references)) { declarations.add(id, node); } @@ -6936,6 +6936,13 @@ Compressor.prototype.compress = function(node) { segment = Object.getPrototypeOf(segment); } + function push_ref(refs, sym) { + refs.push(sym); + push_uniq(refs.scopes, sym.scope); + var scope = find_scope(tw); + if (scope !== sym.scope) push_uniq(refs.scopes, scope); + } + function walk_destructured(symbol_type, mark, lhs) { var marker = new TreeWalker(function(node) { if (node instanceof AST_Destructured) return; @@ -6987,7 +6994,7 @@ Compressor.prototype.compress = function(node) { var refs = references[def.id]; if (!refs) return; if (refs.start.block !== seg.block) return references[def.id] = false; - push_ref(sym); + push_ref(refs, sym); refs.end = seg; if (def.id in prev) { last[prev[def.id]] = null; @@ -7002,7 +7009,7 @@ Compressor.prototype.compress = function(node) { } else { var refs = declarations.get(def.id) || []; refs.scopes = []; - push_ref(sym); + push_ref(refs, sym); references[def.id] = refs; if (!read) { refs.start = seg; @@ -7019,13 +7026,6 @@ Compressor.prototype.compress = function(node) { index: index++, definition: def, }); - - function push_ref(sym) { - refs.push(sym); - push_uniq(refs.scopes, sym.scope); - var scope = find_scope(tw); - if (scope !== sym.scope) push_uniq(refs.scopes, scope); - } } function insert(target) { diff --git a/test/compress/const.js b/test/compress/const.js index df948ac4d9e..d991f1b4a81 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -2231,3 +2231,66 @@ issue_5787: { } expect_stdout: true } + +issue_5930_1: { + options = { + merge_vars: true, + } + input: { + console.log(function() { + var a; + (f = a) && f(); + { + const a = 42; + var f; + } + }()); + } + expect: { + console.log(function() { + var a; + (f = a) && f(); + { + const a = 42; + var f; + } + }()); + } + expect_stdout: true +} + +issue_5930_2: { + options = { + collapse_vars: true, + inline: true, + merge_vars: true, + unused: true, + } + input: { + "use strict"; + (function() { + f = function g(a) { + a.p; + }(); + f && f(); + { + const a = 42; + var b = false; + var f; + } + })(); + } + expect: { + "use strict"; + (function() { + var a; + (f = void a.p) && f(); + { + const a = 42; + var f; + } + })(); + } + expect_stdout: TypeError("Cannot read properties of undefined") + node_version: ">=4" +}