Skip to content

Commit

Permalink
fix corner case with optional chain operator (#4927)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed May 12, 2021
1 parent 689f8f5 commit 60f3b55
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,23 @@ function OutputStream(options) {
if (p instanceof AST_Unary) return true;
});

function lhs_has_optional(node, output) {
var p = output.parent();
if (p instanceof AST_PropAccess && p.expression === node && is_lhs(p, output.parent(1))) {
// ++(foo?.bar).baz
// (foo?.()).bar = baz
do {
if (node.optional) return true;
node = node.expression;
} while (node.TYPE == "Call" || node instanceof AST_PropAccess);
}
}

PARENS(AST_PropAccess, function(output) {
var node = this;
var p = output.parent();
if (p instanceof AST_New && p.expression === node) {
if (p instanceof AST_New) {
if (p.expression !== node) return false;
// i.e. new (foo().bar)
//
// if there's one call into this subtree, then we need
Expand All @@ -805,20 +818,22 @@ function OutputStream(options) {
} while (node instanceof AST_PropAccess);
return node.TYPE == "Call";
}
return lhs_has_optional(node, output);
});

PARENS(AST_Call, function(output) {
var node = this;
var p = output.parent();
if (p instanceof AST_New) return p.expression === this;
if (p instanceof AST_New) return p.expression === node;
// https://bugs.webkit.org/show_bug.cgi?id=123506
if (output.option("webkit")) {
if (output.option("webkit")
&& node.expression instanceof AST_Function
&& p instanceof AST_PropAccess
&& p.expression === node) {
var g = output.parent(1);
return this.expression instanceof AST_Function
&& p instanceof AST_PropAccess
&& p.expression === this
&& g instanceof AST_Assign
&& g.left === p;
if (g instanceof AST_Assign && g.left === p) return true;
}
return lhs_has_optional(node, output);
});

PARENS(AST_New, function(output) {
Expand Down
42 changes: 42 additions & 0 deletions test/compress/optional-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,48 @@ ternary_decimal: {
expect_stdout: "PASS"
}

assign_parentheses_call: {
input: {
var o = {};
((() => o)?.()).p = "PASS";
console.log(o.p);
}
expect_exact: 'var o={};((()=>o)?.()).p="PASS";console.log(o.p);'
expect_stdout: "PASS"
node_version: ">=14"
}

assign_parentheses_dot: {
input: {
(console?.log).name.p = console.log("PASS");
}
expect_exact: '(console?.log.name).p=console.log("PASS");'
expect_stdout: "PASS"
node_version: ">=14"
}

assign_no_parentheses: {
input: {
console[console.log?.("PASS")] = 42;
}
expect_exact: 'console[console.log?.("PASS")]=42;'
expect_stdout: "PASS"
node_version: ">=14"
}

unary_parentheses: {
input: {
var o = { p: 41 };
(function() {
return o;
}?.()).p++;
console.log(o.p);
}
expect_exact: "var o={p:41};(function(){return o}?.()).p++;console.log(o.p);"
expect_stdout: "42"
node_version: ">=14"
}

collapse_vars_1: {
options = {
collapse_vars: true,
Expand Down

0 comments on commit 60f3b55

Please sign in to comment.