@@ -82,6 +82,7 @@ private function enrichTypeOnUnionOrIntersection(TokenIterator $tokens, Ast\Type
82
82
* @internal
83
83
* @template T of Ast\Node
84
84
* @param T $type
85
+ *
85
86
* @return T
86
87
*/
87
88
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
91
92
$ type ->setAttribute (Ast \Attribute::END_LINE , $ tokens ->currentTokenLine ());
92
93
}
93
94
95
+ $ comments = $ tokens ->flushComments ();
96
+ if ($ this ->config ->useCommentsAttributes ) {
97
+ $ type ->setAttribute (Ast \Attribute::COMMENTS , $ comments );
98
+ }
99
+
94
100
if ($ this ->config ->useIndexAttributes ) {
95
101
$ type ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
96
102
$ type ->setAttribute (Ast \Attribute::END_INDEX , $ tokens ->endIndexOfLastRelevantToken ());
@@ -429,6 +435,7 @@ public function isHtml(TokenIterator $tokens): bool
429
435
public function parseGeneric (TokenIterator $ tokens , Ast \Type \IdentifierTypeNode $ baseType ): Ast \Type \GenericTypeNode
430
436
{
431
437
$ tokens ->consumeTokenType (Lexer::TOKEN_OPEN_ANGLE_BRACKET );
438
+ $ tokens ->skipNewLineTokens ();
432
439
433
440
$ startLine = $ baseType ->getAttribute (Ast \Attribute::START_LINE );
434
441
$ startIndex = $ baseType ->getAttribute (Ast \Attribute::START_INDEX );
@@ -859,6 +866,8 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
859
866
$ sealed = true ;
860
867
$ unsealedType = null ;
861
868
869
+ $ done = false ;
870
+
862
871
do {
863
872
$ tokens ->skipNewLineTokens ();
864
873
@@ -884,9 +893,17 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
884
893
}
885
894
886
895
$ items [] = $ this ->parseArrayShapeItem ($ tokens );
887
-
888
896
$ 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 );
890
907
891
908
$ tokens ->skipNewLineTokens ();
892
909
$ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_CURLY_BRACKET );
@@ -904,12 +921,17 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
904
921
{
905
922
$ startLine = $ tokens ->currentTokenLine ();
906
923
$ startIndex = $ tokens ->currentTokenIndex ();
924
+
925
+ // parse any comments above the item
926
+ $ tokens ->skipNewLineTokens ();
927
+
907
928
try {
908
929
$ tokens ->pushSavePoint ();
909
930
$ key = $ this ->parseArrayShapeKey ($ tokens );
910
931
$ optional = $ tokens ->tryConsumeTokenType (Lexer::TOKEN_NULLABLE );
911
932
$ tokens ->consumeTokenType (Lexer::TOKEN_COLON );
912
933
$ value = $ this ->parse ($ tokens );
934
+
913
935
$ tokens ->dropSavePoint ();
914
936
915
937
return $ this ->enrichWithAttributes (
@@ -1056,12 +1078,19 @@ private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectSha
1056
1078
$ startLine = $ tokens ->currentTokenLine ();
1057
1079
$ startIndex = $ tokens ->currentTokenIndex ();
1058
1080
1081
+ $ tokens ->skipNewLineTokens ();
1082
+
1059
1083
$ key = $ this ->parseObjectShapeKey ($ tokens );
1060
1084
$ optional = $ tokens ->tryConsumeTokenType (Lexer::TOKEN_NULLABLE );
1061
1085
$ tokens ->consumeTokenType (Lexer::TOKEN_COLON );
1062
1086
$ value = $ this ->parse ($ tokens );
1063
1087
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
+ );
1065
1094
}
1066
1095
1067
1096
/**
0 commit comments