Skip to content

Commit 6f9790e

Browse files
authored
Adjustment for overlooked nested lookups in chain breaks (#2322)
* Fix for lookups * Further adjustments --------- Co-authored-by: Kristijan Novaković <[email protected]>
1 parent 26cf2a8 commit 6f9790e

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

src/printer.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,8 @@ function printMemberChain(path, options, print) {
509509
printIndentedGroup(groups.slice(shouldMerge ? 2 : 1)),
510510
];
511511

512-
const callExpressions = printedNodes.filter((tuple) =>
513-
["call", "new"].includes(tuple.node.kind)
512+
const callExpressions = printedNodes.filter(
513+
(tuple) => tuple.node.kind === "call"
514514
);
515515

516516
// We don't want to print in one line if there's:

src/util.mjs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -622,27 +622,35 @@ function createTypeCheckFunction(kindsArray) {
622622
}
623623

624624
const isSingleWordType = createTypeCheckFunction([
625-
"variable",
625+
"variadicplaceholder",
626+
"namedargument",
627+
"nullkeyword",
628+
"identifier",
626629
"parameter",
630+
"variable",
627631
"variadic",
628-
"clone",
629-
"cast",
630632
"boolean",
633+
"literal",
631634
"number",
632635
"string",
633-
"literal",
634-
"nullkeyword",
635-
"namedargument",
636-
"variadicplaceholder",
636+
"clone",
637+
"cast",
637638
]);
638639

639640
const isArrayExpression = createTypeCheckFunction(["array"]);
640-
const isCallOrNewExpression = createTypeCheckFunction(["call", "new"]);
641+
const isCallLikeExpression = createTypeCheckFunction([
642+
"nullsafepropertylookup",
643+
"propertylookup",
644+
"staticlookup",
645+
"offsetlookup",
646+
"call",
647+
"new",
648+
]);
641649
const isArrowFuncExpression = createTypeCheckFunction(["arrowfunc"]);
642650

643651
function getChainParts(node, prev = []) {
644652
const parts = prev;
645-
if (isCallOrNewExpression(node)) {
653+
if (isCallLikeExpression(node)) {
646654
parts.push(node);
647655
}
648656

@@ -668,11 +676,17 @@ function isSimpleCallArgument(node, depth = 2) {
668676
return node.items.every((x) => x === null || isChildSimple(x));
669677
}
670678

671-
if (isCallOrNewExpression(node)) {
679+
if (isCallLikeExpression(node)) {
672680
const parts = getChainParts(node);
681+
parts.unshift();
682+
673683
return (
674-
parts.filter((node) => node.kind === "call").length <= depth &&
675-
parts.every((node) => node.arguments.every(isChildSimple))
684+
parts.length <= depth &&
685+
parts.every((node) =>
686+
isLookupNode(node)
687+
? isChildSimple(node.offset)
688+
: node.arguments.every(isChildSimple)
689+
)
676690
);
677691
}
678692

tests/member_chain/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,9 @@ $var = Foo::keys($items)
583583
return $x * 2;
584584
});
585585
586-
(new static(func_get_args()))
587-
->push($this)
588-
->each(function ($item) {
589-
VarDumper::dump($item);
590-
});
586+
(new static(func_get_args()))->push($this)->each(function ($item) {
587+
VarDumper::dump($item);
588+
});
591589
(new static(func_get_args()))
592590
->offset(10)
593591
->push($this)
@@ -721,6 +719,16 @@ $window->{call()}
721719
return $b;
722720
});
723721
722+
723+
$window->call($foo->bar->baz)->first()->second();
724+
$window->call($foo->bar->baz->foo())->first()->second();
725+
726+
(new Foo())->call($foo->bar->baz)->first()->second();
727+
(new Foo())->call($foo->bar->baz->foo())->first()->second();
728+
729+
Foo::call($foo->bar->baz)->first()->second();
730+
Foo::call($foo->bar->baz->foo())->first()->second();
731+
724732
=====================================output=====================================
725733
<?php
726734
@@ -798,6 +806,23 @@ $window->{call()}
798806
return $b;
799807
});
800808
809+
$window->call($foo->bar->baz)->first()->second();
810+
$window
811+
->call($foo->bar->baz->foo())
812+
->first()
813+
->second();
814+
815+
(new Foo())->call($foo->bar->baz)->first()->second();
816+
(new Foo())
817+
->call($foo->bar->baz->foo())
818+
->first()
819+
->second();
820+
821+
Foo::call($foo->bar->baz)->first()->second();
822+
Foo::call($foo->bar->baz->foo())
823+
->first()
824+
->second();
825+
801826
================================================================================
802827
`;
803828

tests/member_chain/offsetlookup.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@
5353
->catch(function () {
5454
return $b;
5555
});
56+
57+
58+
$window->call($foo->bar->baz)->first()->second();
59+
$window->call($foo->bar->baz->foo())->first()->second();
60+
61+
(new Foo())->call($foo->bar->baz)->first()->second();
62+
(new Foo())->call($foo->bar->baz->foo())->first()->second();
63+
64+
Foo::call($foo->bar->baz)->first()->second();
65+
Foo::call($foo->bar->baz->foo())->first()->second();

0 commit comments

Comments
 (0)