-
Notifications
You must be signed in to change notification settings - Fork 0
fix: preserve multiline style of the modified returned array #84
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| namespace RonasIT\Larabuilder; | ||
|
|
||
| use Illuminate\Support\Arr; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Array_; | ||
| use PhpParser\Node\PropertyItem; | ||
|
|
@@ -40,13 +41,59 @@ protected function removeDuplicateEmptyLines(string $code): string | |
| return preg_replace("/(\r?\n){3,}/", "\n\n", $code); | ||
| } | ||
|
|
||
| protected function pArray( | ||
| array $nodes, | ||
| array $origNodes, | ||
| int &$pos, | ||
| int $indentAdjustment, | ||
| string $parentNodeClass, | ||
| string $subNodeName, | ||
| ?int $fixup, | ||
| ): ?string { | ||
| $modifiedArray = ($parentNodeClass === Array_::class && $subNodeName === 'items') | ||
| ? $this->findModifiedArray($nodes) | ||
| : null; | ||
|
|
||
| if (!is_null($modifiedArray)) { | ||
| $isMultiline = $this->wasMultiline($origNodes, $pos) || count($nodes) > 2; | ||
|
|
||
| $modifiedArray->setAttribute(StatementAttributeEnum::Multiline->value, $isMultiline); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| return parent::pArray($nodes, $origNodes, $pos, $indentAdjustment, $parentNodeClass, $subNodeName, $fixup); | ||
| } | ||
|
|
||
| protected function pExpr_Array(Array_ $node): string | ||
| { | ||
| if ($this->hasParentOfType($node, PropertyItem::class)) { | ||
| $isMultiline = $node->getAttribute(StatementAttributeEnum::Multiline->value); | ||
|
|
||
| if ($this->hasParentOfType($node, PropertyItem::class) || $isMultiline === true) { | ||
| return '[' . $this->pCommaSeparatedMultiline($node->items, true) . $this->nl . ']'; | ||
| } | ||
|
|
||
| return parent::pExpr_Array($node); | ||
| return ($isMultiline === false) | ||
| ? '[' . $this->pCommaSeparated($node->items) . ']' | ||
| : parent::pExpr_Array($node); | ||
| } | ||
|
|
||
| protected function findModifiedArray(array $items): ?Array_ | ||
| { | ||
| $modifiedItem = Arr::first( | ||
| $items, | ||
| fn (?Node $item) => $item?->getAttribute(StatementAttributeEnum::Modified->value) === true, | ||
| ); | ||
|
|
||
| return $modifiedItem?->getAttribute(StatementAttributeEnum::Parent->value); | ||
| } | ||
|
|
||
| protected function wasMultiline(array $origNodes, int $pos): bool | ||
| { | ||
| $firstItem = $origNodes[0] ?? null; | ||
| $endPos = $firstItem?->getStartTokenPos() ?? $this->origTokens->findRight($pos, ']'); | ||
|
artengin marked this conversation as resolved.
|
||
|
|
||
| return str_contains($this->origTokens->getTokenCode($pos, $endPos, 0), "\n"); | ||
|
Comment on lines
+93
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a one-item array places its first item on the opening line but its closing bracket on a later line, such as Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @artengin I noticed this case as well. I don't think it's critical, though. Looks good for me protected function getSortableFields(): array
{
return ['id',
];
}VS protected function getSortableFields(): array
{
return ['id', 'name'];
} |
||
| } | ||
|
|
||
| protected function hasParentOfType(Node $node, string $type): bool | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,18 @@ public function method3() | |
| ]); | ||
| } | ||
|
|
||
| protected function getInlineOptions(): array | ||
| { | ||
| return ['first' => 1, 'second' => 2]; | ||
| } | ||
|
|
||
| protected function getDefaultFilters(): array | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @artengin It might be worth adding some of the test cases below to explicitly confirm/document the existing behavior. Please take a look. I added these locally, and everything worked as expected for me.
Before: protected function getValidationRules(): array
{
return [
// profile
'name' => 'required|string',
'email' => 'required|email',
// address
'city' => 'nullable|string'
];
}After: protected function getValidationRules(): array
{
return [
// profile
'name' => 'required|string',
'email' => 'required|email',
// address
'city' => 'nullable|string',
'zip' => 'nullable|string',
];
}
Before: protected function getUserData(): array
{
return [
'name' => 'John',
'settings' => [
'locale' => 'en',
'timezone' => 'UTC',
],
];
}After: protected function getUserData(): array
{
return [
'name' => 'John',
'settings' => [
'locale' => 'en',
'timezone' => 'UTC',
],
'roles' => ['admin', 'editor'],
];
}
This is the only case I found where the Before: protected function getExtraFilters(): array
{
return [
];
}After: protected function getExtraFilters(): array
{
return [
'is_hidden',
];
} |
||
| { | ||
| return [ | ||
| 'is_active', | ||
| ]; | ||
| } | ||
|
|
||
| protected function getUserData(): array | ||
| { | ||
| return [ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an item is appended to a returned array written with
array(...),pArray()now forces the entire expression through this override, which always emits square brackets. Consequently, a formatting-only update unexpectedly converts long array syntax to short syntax; select delimiters based on the array'skindattribute or delegate that part to the parent printer.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short array syntax is the standard in Laravel projects, so the linter rewrites
array(...)to[...]on any file it touches anyway. Preserving long syntax here would only postpone an edit the linter makes regardless, so not supporting it is a deliberate choice.