fix corner case in merge_vars (#5466)

fixes #5465
This commit is contained in:
Alex Lam S.L
2022-05-23 22:45:07 +01:00
committed by GitHub
parent d4caa97b88
commit 740f93f5a9
3 changed files with 64 additions and 33 deletions

View File

@@ -6044,31 +6044,7 @@ Compressor.prototype.compress = function(node) {
var rhs = node.right;
if (lhs instanceof AST_Destructured) {
rhs.walk(tw);
var marker = new TreeWalker(function(node) {
if (node instanceof AST_Destructured) return;
if (node instanceof AST_DefaultValue) {
push();
node.value.walk(tw);
pop();
node.name.walk(marker);
} else if (node instanceof AST_DestructuredKeyVal) {
if (node.key instanceof AST_Node) {
push();
segment.block = node;
node.key.walk(tw);
node.value.walk(marker);
pop();
} else {
node.value.walk(marker);
}
} else if (node instanceof AST_SymbolRef) {
mark(node);
} else {
node.walk(tw);
}
return true;
});
lhs.walk(marker);
walk_destructured(AST_SymbolRef, mark, lhs);
return true;
}
if (lazy_op[node.operator.slice(0, -1)]) {
@@ -6196,15 +6172,15 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_Lambda) {
if (node.name) references[node.name.definition().id] = false;
var marker = node.uses_arguments && !tw.has_directive("use strict") ? function(node) {
if (node instanceof AST_SymbolFunarg) references[node.definition().id] = false;
references[node.definition().id] = false;
} : function(node) {
if (node instanceof AST_SymbolFunarg) mark(node);
mark(node);
};
in_arg.push(node);
node.argnames.forEach(function(argname) {
argname.mark_symbol(marker, tw);
walk_destructured(AST_SymbolFunarg, marker, argname);
});
if (node.rest) node.rest.mark_symbol(marker, tw);
if (node.rest) walk_destructured(AST_SymbolFunarg, marker, node.rest);
in_arg.pop();
}
walk_lambda(node, tw);
@@ -6419,6 +6395,34 @@ Compressor.prototype.compress = function(node) {
segment = Object.getPrototypeOf(segment);
}
function walk_destructured(symbol_type, mark, lhs) {
var marker = new TreeWalker(function(node) {
if (node instanceof AST_Destructured) return;
if (node instanceof AST_DefaultValue) {
push();
node.value.walk(tw);
pop();
node.name.walk(marker);
} else if (node instanceof AST_DestructuredKeyVal) {
if (node.key instanceof AST_Node) {
push();
segment.block = node;
node.key.walk(tw);
node.value.walk(marker);
pop();
} else {
node.value.walk(marker);
}
} else if (node instanceof symbol_type) {
mark(node);
} else {
node.walk(tw);
}
return true;
});
lhs.walk(marker);
}
function mark(sym, read) {
var def = sym.definition(), ldef;
if (read && !all(in_arg, function(fn) {

View File

@@ -2421,3 +2421,30 @@ issue_5463: {
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5465: {
options = {
inline: true,
merge_vars: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f(a, b) {
(function(c = b = "FAIL 2") {
this && console.log(b || "PASS");
})(42 - a && a);
}
f("FAIL 1");
}
expect: {
a = "FAIL 1",
void function(c = b = "FAIL 2") {
this && console.log(b || "PASS");
}(42 - a && a);
var a, b;
}
expect_stdout: "PASS"
node_version: ">=6"
}

View File

@@ -1815,8 +1815,8 @@ issue_4288: {
console.log(typeof b);
}()]: a,
}) {
var a = a;
a++;
var b = a;
b++;
}
f(0);
}
@@ -1848,8 +1848,8 @@ issue_4294: {
}) {}({
[a]: 0,
});
var a = A;
console.log(a);
var b = A;
console.log(b);
})();
}
expect_stdout: "PASS"