Skip to content

Commit 952f7cb

Browse files
authored
Fix member chain when nullsafe operator is used (#1912)
This makes multiline method chaining like: ```php $a ->bbbbbbbbbbbbbbbbb() ->cccccccccccccccccccccc() ->ddddddddddddddddd(); ``` Work correctly when the nullsafe operator `?->` is used somewhere in the chain.
1 parent a7e5469 commit 952f7cb

5 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/needs-parens.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function needsParens(path) {
5252
(node.type === "+" || node.type === "-")
5353
);
5454
case "propertylookup":
55+
case "nullsafepropertylookup":
5556
case "staticlookup":
5657
case "offsetlookup":
5758
case "call":
@@ -76,6 +77,7 @@ function needsParens(path) {
7677
return true;
7778
case "call":
7879
case "propertylookup":
80+
case "nullsafepropertylookup":
7981
case "staticlookup":
8082
case "offsetlookup":
8183
return name === "what" && parent.what === node;
@@ -121,6 +123,7 @@ function needsParens(path) {
121123
}
122124
}
123125
case "propertylookup":
126+
case "nullsafepropertylookup":
124127
case "staticlookup": {
125128
switch (parent.kind) {
126129
case "call":
@@ -138,6 +141,7 @@ function needsParens(path) {
138141
case "new": {
139142
switch (parent.kind) {
140143
case "propertylookup":
144+
case "nullsafepropertylookup":
141145
case "staticlookup":
142146
case "offsetlookup":
143147
case "call":
@@ -149,6 +153,7 @@ function needsParens(path) {
149153
case "yield": {
150154
switch (parent.kind) {
151155
case "propertylookup":
156+
case "nullsafepropertylookup":
152157
case "staticlookup":
153158
case "offsetlookup":
154159
case "call":
@@ -196,6 +201,7 @@ function needsParens(path) {
196201

197202
return true;
198203
case "propertylookup":
204+
case "nullsafepropertylookup":
199205
case "staticlookup":
200206
case "offsetlookup":
201207
case "call":
@@ -211,6 +217,7 @@ function needsParens(path) {
211217

212218
// https://github.com/prettier/plugin-php/issues/1675
213219
case "propertylookup":
220+
case "nullsafepropertylookup":
214221
return true;
215222

216223
default:
@@ -225,6 +232,7 @@ function needsParens(path) {
225232
case "array":
226233
switch (parent.kind) {
227234
case "propertylookup":
235+
case "nullsafepropertylookup":
228236
case "staticlookup":
229237
case "offsetlookup":
230238
case "call":

src/printer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function printMemberChain(path, options, print) {
288288
if (
289289
printedNodes[i].node.kind === "call" &&
290290
printedNodes[i - 1] &&
291-
["propertylookup", "staticlookup"].includes(
291+
["propertylookup", "nullsafepropertylookup", "staticlookup"].includes(
292292
printedNodes[i - 1].node.kind
293293
) &&
294294
printedNodes[i - 1].needsParens
@@ -2822,7 +2822,11 @@ function printNode(path, options, print) {
28222822
const parentParent = path.getParentNode(1);
28232823
const pureParent =
28242824
parent.kind === "cast" && parentParent ? parentParent : parent;
2825-
const breakLookupNodes = ["propertylookup", "staticlookup"];
2825+
const breakLookupNodes = [
2826+
"propertylookup",
2827+
"nullsafepropertylookup",
2828+
"staticlookup",
2829+
];
28262830
const breakClosingParens = breakLookupNodes.includes(pureParent.kind);
28272831

28282832
const printedTest = path.call(print, "test");

src/util.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ function hasTrailingComment(node) {
485485
function isLookupNode(node) {
486486
return (
487487
node.kind === "propertylookup" ||
488+
node.kind === "nullsafepropertylookup" ||
488489
node.kind === "staticlookup" ||
489490
node.kind === "offsetlookup"
490491
);

tests/nullsafepropertylookup/__snapshots__/jsfmt.spec.js.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ $obj?->foo_{'test' . 'bar'};
2626
// variable with literal with call
2727
$obj?->\${call()};
2828
29+
// long methods names chaining
30+
// all nullsafe
31+
$obj?->aaaaaaaaaaaaaaaaaaaaaaaa()?->ccccccccccccccccccccccccccccc()?->ccccccccccccccccccccc();
32+
// first nullsafe
33+
$obj?->aaaaaaaaaaaaaaaaaaaaaaaa()->ccccccccccccccccccccccccccccc()->ccccccccccccccccccccc();
34+
// middle nullsafe
35+
$obj->aaaaaaaaaaaaaaaaaaaaaaaa()?->ccccccccccccccccccccccccccccc()->ccccccccccccccccccccc();
36+
// last nullsafe
37+
$obj->aaaaaaaaaaaaaaaaaaaaaaaa()->ccccccccccccccccccccccccccccc()?->ccccccccccccccccccccc();
38+
2939
=====================================output=====================================
3040
<?php
3141
@@ -46,5 +56,23 @@ $obj?->foo_["test" . "bar"];
4656
// variable with literal with call
4757
$obj?->\${call()};
4858
59+
// long methods names chaining
60+
// all nullsafe
61+
$obj?->aaaaaaaaaaaaaaaaaaaaaaaa()
62+
?->ccccccccccccccccccccccccccccc()
63+
?->ccccccccccccccccccccc();
64+
// first nullsafe
65+
$obj?->aaaaaaaaaaaaaaaaaaaaaaaa()
66+
->ccccccccccccccccccccccccccccc()
67+
->ccccccccccccccccccccc();
68+
// middle nullsafe
69+
$obj->aaaaaaaaaaaaaaaaaaaaaaaa()
70+
?->ccccccccccccccccccccccccccccc()
71+
->ccccccccccccccccccccc();
72+
// last nullsafe
73+
$obj->aaaaaaaaaaaaaaaaaaaaaaaa()
74+
->ccccccccccccccccccccccccccccc()
75+
?->ccccccccccccccccccccc();
76+
4977
================================================================================
5078
`;

tests/nullsafepropertylookup/offsets.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@
1616
$obj?->foo_{'test' . 'bar'};
1717
// variable with literal with call
1818
$obj?->${call()};
19+
20+
// long methods names chaining
21+
// all nullsafe
22+
$obj?->aaaaaaaaaaaaaaaaaaaaaaaa()?->ccccccccccccccccccccccccccccc()?->ccccccccccccccccccccc();
23+
// first nullsafe
24+
$obj?->aaaaaaaaaaaaaaaaaaaaaaaa()->ccccccccccccccccccccccccccccc()->ccccccccccccccccccccc();
25+
// middle nullsafe
26+
$obj->aaaaaaaaaaaaaaaaaaaaaaaa()?->ccccccccccccccccccccccccccccc()->ccccccccccccccccccccc();
27+
// last nullsafe
28+
$obj->aaaaaaaaaaaaaaaaaaaaaaaa()->ccccccccccccccccccccccccccccc()?->ccccccccccccccccccccc();

0 commit comments

Comments
 (0)