Skip to content

Commit

Permalink
fix corner case in merge_vars (#5931)
Browse files Browse the repository at this point in the history
fixes #5930
  • Loading branch information
alexlamsl authored Sep 5, 2024
1 parent 3ea33af commit b54f298
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
63 changes: 63 additions & 0 deletions test/compress/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit b54f298

Please sign in to comment.