Skip to content

Commit

Permalink
Adjustment for overlooked nested lookups in chain breaks (#2322)
Browse files Browse the repository at this point in the history
* Fix for lookups

* Further adjustments

---------

Co-authored-by: Kristijan Novaković <[email protected]>
  • Loading branch information
eldair and eldair authored May 31, 2024
1 parent 26cf2a8 commit 6f9790e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ function printMemberChain(path, options, print) {
printIndentedGroup(groups.slice(shouldMerge ? 2 : 1)),
];

const callExpressions = printedNodes.filter((tuple) =>
["call", "new"].includes(tuple.node.kind)
const callExpressions = printedNodes.filter(
(tuple) => tuple.node.kind === "call"
);

// We don't want to print in one line if there's:
Expand Down
38 changes: 26 additions & 12 deletions src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -622,27 +622,35 @@ function createTypeCheckFunction(kindsArray) {
}

const isSingleWordType = createTypeCheckFunction([
"variable",
"variadicplaceholder",
"namedargument",
"nullkeyword",
"identifier",
"parameter",
"variable",
"variadic",
"clone",
"cast",
"boolean",
"literal",
"number",
"string",
"literal",
"nullkeyword",
"namedargument",
"variadicplaceholder",
"clone",
"cast",
]);

const isArrayExpression = createTypeCheckFunction(["array"]);
const isCallOrNewExpression = createTypeCheckFunction(["call", "new"]);
const isCallLikeExpression = createTypeCheckFunction([
"nullsafepropertylookup",
"propertylookup",
"staticlookup",
"offsetlookup",
"call",
"new",
]);
const isArrowFuncExpression = createTypeCheckFunction(["arrowfunc"]);

function getChainParts(node, prev = []) {
const parts = prev;
if (isCallOrNewExpression(node)) {
if (isCallLikeExpression(node)) {
parts.push(node);
}

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

if (isCallOrNewExpression(node)) {
if (isCallLikeExpression(node)) {
const parts = getChainParts(node);
parts.unshift();

return (
parts.filter((node) => node.kind === "call").length <= depth &&
parts.every((node) => node.arguments.every(isChildSimple))
parts.length <= depth &&
parts.every((node) =>
isLookupNode(node)
? isChildSimple(node.offset)
: node.arguments.every(isChildSimple)
)
);
}

Expand Down
35 changes: 30 additions & 5 deletions tests/member_chain/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,9 @@ $var = Foo::keys($items)
return $x * 2;
});
(new static(func_get_args()))
->push($this)
->each(function ($item) {
VarDumper::dump($item);
});
(new static(func_get_args()))->push($this)->each(function ($item) {
VarDumper::dump($item);
});
(new static(func_get_args()))
->offset(10)
->push($this)
Expand Down Expand Up @@ -721,6 +719,16 @@ $window->{call()}
return $b;
});
$window->call($foo->bar->baz)->first()->second();
$window->call($foo->bar->baz->foo())->first()->second();
(new Foo())->call($foo->bar->baz)->first()->second();
(new Foo())->call($foo->bar->baz->foo())->first()->second();
Foo::call($foo->bar->baz)->first()->second();
Foo::call($foo->bar->baz->foo())->first()->second();
=====================================output=====================================
<?php
Expand Down Expand Up @@ -798,6 +806,23 @@ $window->{call()}
return $b;
});
$window->call($foo->bar->baz)->first()->second();
$window
->call($foo->bar->baz->foo())
->first()
->second();
(new Foo())->call($foo->bar->baz)->first()->second();
(new Foo())
->call($foo->bar->baz->foo())
->first()
->second();
Foo::call($foo->bar->baz)->first()->second();
Foo::call($foo->bar->baz->foo())
->first()
->second();
================================================================================
`;

Expand Down
10 changes: 10 additions & 0 deletions tests/member_chain/offsetlookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@
->catch(function () {
return $b;
});


$window->call($foo->bar->baz)->first()->second();
$window->call($foo->bar->baz->foo())->first()->second();

(new Foo())->call($foo->bar->baz)->first()->second();
(new Foo())->call($foo->bar->baz->foo())->first()->second();

Foo::call($foo->bar->baz)->first()->second();
Foo::call($foo->bar->baz->foo())->first()->second();

0 comments on commit 6f9790e

Please sign in to comment.