Skip to content

Commit

Permalink
fix corner case in merge_vars (#5092)
Browse files Browse the repository at this point in the history
fixes #5091
  • Loading branch information
alexlamsl committed Jul 20, 2021
1 parent 8926a2f commit 7fac839
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5672,7 +5672,10 @@ merge(Compressor.prototype, {
if (node instanceof AST_Call) {
var exp = node.expression;
var tail = exp.tail_node();
if (!(tail instanceof AST_LambdaExpression)) return walk_node_with_expr(node);
if (!(tail instanceof AST_LambdaExpression)) {
descend();
return mark_expression(exp);
}
if (exp !== tail) exp.expressions.slice(0, -1).forEach(function(node) {
node.walk(tw);
});
Expand Down Expand Up @@ -5788,7 +5791,18 @@ merge(Compressor.prototype, {
pop();
return true;
}
if (node instanceof AST_Sub) return walk_node_with_expr(node);
if (node instanceof AST_Sub) {
var exp = node.expression;
if (node.optional) {
exp.walk(tw);
push();
node.property.walk(tw);
pop();
} else {
descend();
}
return mark_expression(exp);
}
if (node instanceof AST_Switch) {
node.expression.walk(tw);
var save = segment;
Expand Down Expand Up @@ -5888,10 +5902,9 @@ merge(Compressor.prototype, {
return true;
}

function walk_node_with_expr(node) {
descend();
function mark_expression(exp) {
if (compressor.option("ie")) {
var sym = root_expr(node.expression);
var sym = root_expr(exp);
if (sym instanceof AST_SymbolRef) sym.walk(tw);
}
return true;
Expand Down
26 changes: 26 additions & 0 deletions test/compress/optional-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,29 @@ issue_5039: {
expect_stdout: "PASS"
node_version: ">=14"
}

issue_5091: {
options = {
merge_vars: true,
}
input: {
function f(a) {
var b = a.p;
var c;
b?.[c = "FAIL 2"];
return b || c;
}
console.log(f("FAIL 1") || "PASS");
}
expect: {
function f(b) {
var b = b.p;
var c;
b?.[c = "FAIL 2"];
return b || c;
}
console.log(f("FAIL 1") || "PASS");
}
expect_stdout: "PASS"
node_version: ">=14"
}

0 comments on commit 7fac839

Please sign in to comment.