Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjustment for overlooked nested lookups in chain breaks #2322

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Loading