@@ -82,6 +82,7 @@ private function enrichTypeOnUnionOrIntersection(TokenIterator $tokens, Ast\Type
8282 * @internal
8383 * @template T of Ast\Node
8484 * @param T $type
85+ *
8586 * @return T
8687 */
8788 public function enrichWithAttributes (TokenIterator $ tokens , Ast \Node $ type , int $ startLine , int $ startIndex ): Ast \Node
@@ -91,6 +92,11 @@ public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int
9192 $ type ->setAttribute (Ast \Attribute::END_LINE , $ tokens ->currentTokenLine ());
9293 }
9394
95+ $ comments = $ tokens ->flushComments ();
96+ if ($ this ->config ->useCommentsAttributes ) {
97+ $ type ->setAttribute (Ast \Attribute::COMMENTS , $ comments );
98+ }
99+
94100 if ($ this ->config ->useIndexAttributes ) {
95101 $ type ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
96102 $ type ->setAttribute (Ast \Attribute::END_INDEX , $ tokens ->endIndexOfLastRelevantToken ());
@@ -429,6 +435,7 @@ public function isHtml(TokenIterator $tokens): bool
429435 public function parseGeneric (TokenIterator $ tokens , Ast \Type \IdentifierTypeNode $ baseType ): Ast \Type \GenericTypeNode
430436 {
431437 $ tokens ->consumeTokenType (Lexer::TOKEN_OPEN_ANGLE_BRACKET );
438+ $ tokens ->skipNewLineTokens ();
432439
433440 $ startLine = $ baseType ->getAttribute (Ast \Attribute::START_LINE );
434441 $ startIndex = $ baseType ->getAttribute (Ast \Attribute::START_INDEX );
@@ -859,6 +866,8 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
859866 $ sealed = true ;
860867 $ unsealedType = null ;
861868
869+ $ done = false ;
870+
862871 do {
863872 $ tokens ->skipNewLineTokens ();
864873
@@ -884,9 +893,17 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
884893 }
885894
886895 $ items [] = $ this ->parseArrayShapeItem ($ tokens );
887-
888896 $ tokens ->skipNewLineTokens ();
889- } while ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_COMMA ));
897+ if (!$ tokens ->tryConsumeTokenType (Lexer::TOKEN_COMMA )) {
898+ $ done = true ;
899+ }
900+ if ($ tokens ->currentTokenType () !== Lexer::TOKEN_COMMENT ) {
901+ continue ;
902+ }
903+
904+ $ tokens ->next ();
905+
906+ } while (!$ done );
890907
891908 $ tokens ->skipNewLineTokens ();
892909 $ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_CURLY_BRACKET );
@@ -904,12 +921,17 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
904921 {
905922 $ startLine = $ tokens ->currentTokenLine ();
906923 $ startIndex = $ tokens ->currentTokenIndex ();
924+
925+ // parse any comments above the item
926+ $ tokens ->skipNewLineTokens ();
927+
907928 try {
908929 $ tokens ->pushSavePoint ();
909930 $ key = $ this ->parseArrayShapeKey ($ tokens );
910931 $ optional = $ tokens ->tryConsumeTokenType (Lexer::TOKEN_NULLABLE );
911932 $ tokens ->consumeTokenType (Lexer::TOKEN_COLON );
912933 $ value = $ this ->parse ($ tokens );
934+
913935 $ tokens ->dropSavePoint ();
914936
915937 return $ this ->enrichWithAttributes (
@@ -1056,12 +1078,19 @@ private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectSha
10561078 $ startLine = $ tokens ->currentTokenLine ();
10571079 $ startIndex = $ tokens ->currentTokenIndex ();
10581080
1081+ $ tokens ->skipNewLineTokens ();
1082+
10591083 $ key = $ this ->parseObjectShapeKey ($ tokens );
10601084 $ optional = $ tokens ->tryConsumeTokenType (Lexer::TOKEN_NULLABLE );
10611085 $ tokens ->consumeTokenType (Lexer::TOKEN_COLON );
10621086 $ value = $ this ->parse ($ tokens );
10631087
1064- return $ this ->enrichWithAttributes ($ tokens , new Ast \Type \ObjectShapeItemNode ($ key , $ optional , $ value ), $ startLine , $ startIndex );
1088+ return $ this ->enrichWithAttributes (
1089+ $ tokens ,
1090+ new Ast \Type \ObjectShapeItemNode ($ key , $ optional , $ value ),
1091+ $ startLine ,
1092+ $ startIndex ,
1093+ );
10651094 }
10661095
10671096 /**
0 commit comments