fix corner case in merge_vars (#5320)

fixes #5319
This commit is contained in:
Alex Lam S.L
2022-01-28 08:05:57 +00:00
committed by GitHub
parent e4a91a89e0
commit 18f00457f6
4 changed files with 76 additions and 0 deletions

View File

@@ -5927,6 +5927,16 @@ Compressor.prototype.compress = function(node) {
var references = Object.create(null);
var prev = Object.create(null);
var tw = new TreeWalker(function(node, descend) {
if (node.variables) {
if (node instanceof AST_BlockStatement) {
var save_scope = segment.scope;
segment.scope = node;
descend();
segment.scope = save_scope;
return true;
}
segment.scope = node;
}
if (node instanceof AST_Assign) {
var lhs = node.left;
var rhs = node.right;
@@ -6298,6 +6308,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;
sym.scope = seg.scope;
refs.push(sym);
refs.end = seg;
if (def.id in prev) {
@@ -6312,6 +6323,7 @@ Compressor.prototype.compress = function(node) {
return references[def.id] = false;
} else {
var refs = declarations.get(def.id) || [];
sym.scope = seg.scope;
refs.push(sym);
references[def.id] = refs;
if (!read) {

View File

@@ -103,6 +103,7 @@ function OutputStream(options) {
function make_indent(value) {
if (typeof value == "number") return new Array(value + 1).join(" ");
if (!value) return "";
if (!/^\s*$/.test(value)) throw new Error("unsupported indentation: " + JSON.stringify("" + value));
return value;
}

View File

@@ -1813,3 +1813,33 @@ issue_5260: {
]
node_version: ">=4"
}
issue_5319: {
options = {
collapse_vars: true,
merge_vars: true,
}
input: {
(function(a, c) {
var b = a, c = b;
{
const a = c;
console.log(c());
}
})(function() {
return "PASS";
});
}
expect: {
(function(a, c) {
var b = a, c;
{
const a = c = b;
console.log(c());
}
})(function() {
return "PASS";
});
}
expect_stdout: true
}

View File

@@ -1973,3 +1973,36 @@ issue_5260: {
]
node_version: ">=4"
}
issue_5319: {
options = {
collapse_vars: true,
merge_vars: true,
}
input: {
"use strict";
(function(a, c) {
var b = a, c = b;
{
let a = c;
console.log(c());
}
})(function() {
return "PASS";
});
}
expect: {
"use strict";
(function(a, c) {
var b = a, c;
{
let a = c = b;
console.log(c());
}
})(function() {
return "PASS";
});
}
expect_stdout: "PASS"
node_version: ">=4"
}