Skip to content

Commit 5f93b95

Browse files
committed
support comment after comma
1 parent c5cdc3b commit 5f93b95

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/Parser/TypeParser.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use LogicException;
66
use PHPStan\PhpDocParser\Ast;
77
use PHPStan\PhpDocParser\Lexer\Lexer;
8+
use function array_merge;
89
use function in_array;
910
use function str_replace;
1011
use function strpos;
@@ -105,6 +106,18 @@ public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int
105106
return $type;
106107
}
107108

109+
/**
110+
* @param list<Ast\Comment> $comments
111+
*/
112+
public function appendComments(Ast\Node $type, array $comments = []): Ast\Node
113+
{
114+
if ($this->useCommentsAttributes) {
115+
$type->setAttribute(Ast\Attribute::COMMENTS, array_merge($comments, $type->getAttribute(Ast\Attribute::COMMENTS) ?? []));
116+
}
117+
118+
return $type;
119+
}
120+
108121
/** @phpstan-impure */
109122
private function subParse(TokenIterator $tokens): Ast\Type\TypeNode
110123
{
@@ -770,6 +783,8 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
770783
$items = [];
771784
$sealed = true;
772785

786+
$done = false;
787+
773788
do {
774789
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
775790

@@ -783,10 +798,20 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
783798
break;
784799
}
785800

786-
$items[] = $this->parseArrayShapeItem($tokens);
787-
801+
$item = $this->parseArrayShapeItem($tokens);
802+
$items[] = $item;
788803
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
789-
} while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
804+
if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
805+
$done = true;
806+
}
807+
if ($tokens->currentTokenType() !== Lexer::TOKEN_COMMENT) {
808+
continue;
809+
}
810+
811+
$this->appendComments($item, [new Ast\Comment($tokens->currentTokenValue(), $tokens->currentTokenLine(), $tokens->currentTokenIndex())]);
812+
$tokens->next();
813+
814+
} while (!$done);
790815

791816
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
792817
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);

tests/PHPStan/Parser/TypeParserTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function provideParseData(): array
164164
],
165165
[
166166
'array{
167-
a: int // a is for after,
167+
a: int, // a is for after,
168168
}',
169169
new ArrayShapeNode([
170170
new ArrayShapeItemNode(
@@ -174,6 +174,26 @@ public function provideParseData(): array
174174
),
175175
]),
176176
],
177+
[
178+
'array{
179+
a: int, // a is for after,
180+
b: string, /* b is for banana,
181+
* which is good to know
182+
*/
183+
}',
184+
new ArrayShapeNode([
185+
new ArrayShapeItemNode(
186+
new IdentifierTypeNode('a'),
187+
false,
188+
new IdentifierTypeNode('int')
189+
),
190+
new ArrayShapeItemNode(
191+
new IdentifierTypeNode('b'),
192+
false,
193+
new IdentifierTypeNode('string')
194+
),
195+
]),
196+
],
177197
[
178198
'array{
179199
/* a is also for aardvark */

0 commit comments

Comments
 (0)