@@ -208,7 +208,7 @@ export const getTopLevelGenericType = (typeString: string) => {
208
208
export const rawTypeToTypeInformation = (
209
209
rawType : string ,
210
210
relatedDescription : string ,
211
- subTypedKeys : TypedKey [ ] | null ,
211
+ subTypedKeys : TypedKeyList | null ,
212
212
) : TypeInformation => {
213
213
// Handle the edge case of "null"
214
214
if ( rawType === 'null' || rawType === '`null`' ) {
@@ -254,42 +254,45 @@ export const rawTypeToTypeInformation = (
254
254
return {
255
255
collection,
256
256
type : 'Function' ,
257
- parameters : subTypedKeys
258
- ? subTypedKeys . map < MethodParameterDocumentation > ( typedKey => ( {
259
- name : typedKey . key ,
260
- description : typedKey . description ,
261
- required : typedKey . required ,
262
- ...typedKey . type ,
263
- } ) )
264
- : [ ] ,
257
+ parameters :
258
+ subTypedKeys && ! subTypedKeys . consumed
259
+ ? consumeTypedKeysList ( subTypedKeys ) . map < MethodParameterDocumentation > ( typedKey => ( {
260
+ name : typedKey . key ,
261
+ description : typedKey . description ,
262
+ required : typedKey . required ,
263
+ ...typedKey . type ,
264
+ } ) )
265
+ : [ ] ,
265
266
returns : null ,
266
267
} ;
267
268
} else if ( typeString === 'Object' ) {
268
269
return {
269
270
collection,
270
271
type : 'Object' ,
271
- properties : subTypedKeys
272
- ? subTypedKeys . map < PropertyDocumentationBlock > ( typedKey => ( {
273
- name : typedKey . key ,
274
- description : typedKey . description ,
275
- required : typedKey . required ,
276
- additionalTags : typedKey . additionalTags ,
277
- ...typedKey . type ,
278
- } ) )
279
- : [ ] ,
272
+ properties :
273
+ subTypedKeys && ! subTypedKeys . consumed
274
+ ? consumeTypedKeysList ( subTypedKeys ) . map < PropertyDocumentationBlock > ( typedKey => ( {
275
+ name : typedKey . key ,
276
+ description : typedKey . description ,
277
+ required : typedKey . required ,
278
+ additionalTags : typedKey . additionalTags ,
279
+ ...typedKey . type ,
280
+ } ) )
281
+ : [ ] ,
280
282
} ;
281
283
} else if ( typeString === 'String' ) {
282
284
return {
283
285
collection,
284
286
type : 'String' ,
285
- possibleValues : subTypedKeys
286
- ? subTypedKeys . map < PossibleStringValue > ( typedKey => ( {
287
- value : typedKey . key ,
288
- description : typedKey . description ,
289
- } ) )
290
- : relatedDescription
291
- ? extractStringEnum ( relatedDescription )
292
- : null ,
287
+ possibleValues :
288
+ subTypedKeys && ! subTypedKeys . consumed
289
+ ? consumeTypedKeysList ( subTypedKeys ) . map < PossibleStringValue > ( typedKey => ( {
290
+ value : typedKey . key ,
291
+ description : typedKey . description ,
292
+ } ) )
293
+ : relatedDescription
294
+ ? extractStringEnum ( relatedDescription )
295
+ : null ,
293
296
} ;
294
297
}
295
298
@@ -303,15 +306,16 @@ export const rawTypeToTypeInformation = (
303
306
return {
304
307
...info ,
305
308
type : 'Object' ,
306
- properties : subTypedKeys
307
- ? subTypedKeys . map < PropertyDocumentationBlock > ( typedKey => ( {
308
- name : typedKey . key ,
309
- description : typedKey . description ,
310
- required : typedKey . required ,
311
- additionalTags : typedKey . additionalTags ,
312
- ...typedKey . type ,
313
- } ) )
314
- : [ ] ,
309
+ properties :
310
+ subTypedKeys && ! subTypedKeys . consumed
311
+ ? consumeTypedKeysList ( subTypedKeys ) . map < PropertyDocumentationBlock > ( typedKey => ( {
312
+ name : typedKey . key ,
313
+ description : typedKey . description ,
314
+ required : typedKey . required ,
315
+ additionalTags : typedKey . additionalTags ,
316
+ ...typedKey . type ,
317
+ } ) )
318
+ : [ ] ,
315
319
} ;
316
320
}
317
321
return info ;
@@ -328,8 +332,8 @@ export const rawTypeToTypeInformation = (
328
332
parameters :
329
333
// If no param types are provided in the <A, B, C> syntax then we should fallback to the normal one
330
334
genericProvidedParams . length === 0
331
- ? subTypedKeys
332
- ? subTypedKeys . map < MethodParameterDocumentation > ( typedKey => ( {
335
+ ? subTypedKeys && ! subTypedKeys . consumed
336
+ ? consumeTypedKeysList ( subTypedKeys ) . map < MethodParameterDocumentation > ( typedKey => ( {
333
337
name : typedKey . key ,
334
338
description : typedKey . description ,
335
339
required : typedKey . required ,
@@ -428,7 +432,7 @@ export const extractReturnType = (
428
432
}
429
433
430
434
const list = findNextList ( tokens ) ;
431
- let typedKeys : null | TypedKey [ ] = null ;
435
+ let typedKeys : null | TypedKeyList = null ;
432
436
if ( list ) {
433
437
try {
434
438
typedKeys = convertListToTypedKeys ( tokens ) ;
@@ -555,6 +559,11 @@ type TypedKey = {
555
559
additionalTags : DocumentationTag [ ] ;
556
560
} ;
557
561
562
+ type TypedKeyList = {
563
+ keys : TypedKey [ ] ;
564
+ consumed : boolean ;
565
+ } ;
566
+
558
567
type List = { items : ListItem [ ] } ;
559
568
type ListItem = { tokens : Token [ ] ; nestedList : List | null } ;
560
569
@@ -592,6 +601,24 @@ const getNestedList = (rawTokens: Token[]): List => {
592
601
return rootList ;
593
602
} ;
594
603
604
+ const unconsumedTypedKeyList = < T extends TypedKey [ ] | null > (
605
+ keys : T ,
606
+ ) : T extends null ? null : TypedKeyList => {
607
+ return keys
608
+ ? {
609
+ consumed : false ,
610
+ keys,
611
+ }
612
+ : ( null as any ) ;
613
+ } ;
614
+
615
+ export const consumeTypedKeysList = ( list : TypedKeyList ) => {
616
+ if ( list . consumed )
617
+ throw new Error ( 'Attempted to consume a typed keys list that has already been consumed' ) ;
618
+ list . consumed = true ;
619
+ return list . keys ;
620
+ } ;
621
+
595
622
const convertNestedListToTypedKeys = ( list : List ) : TypedKey [ ] => {
596
623
const keys : TypedKey [ ] = [ ] ;
597
624
@@ -651,7 +678,11 @@ const convertNestedListToTypedKeys = (list: List): TypedKey[] => {
651
678
const tagMatch = tagMatcher . exec ( rawType ) ;
652
679
const cleanedType = rawType . replace ( / ? \( o p t i o n a l \) ? / i, '' ) . replace ( / _ .+ ?_ / g, '' ) ;
653
680
const subTypedKeys = item . nestedList ? convertNestedListToTypedKeys ( item . nestedList ) : null ;
654
- const type = rawTypeToTypeInformation ( cleanedType . trim ( ) , rawDescription , subTypedKeys ) ;
681
+ const type = rawTypeToTypeInformation (
682
+ cleanedType . trim ( ) ,
683
+ rawDescription ,
684
+ unconsumedTypedKeyList ( subTypedKeys ) ,
685
+ ) ;
655
686
656
687
keys . push ( {
657
688
type,
@@ -665,8 +696,8 @@ const convertNestedListToTypedKeys = (list: List): TypedKey[] => {
665
696
return keys ;
666
697
} ;
667
698
668
- export const convertListToTypedKeys = ( listTokens : Token [ ] ) : TypedKey [ ] => {
699
+ export const convertListToTypedKeys = ( listTokens : Token [ ] ) : TypedKeyList => {
669
700
const list = getNestedList ( listTokens ) ;
670
701
671
- return convertNestedListToTypedKeys ( list ) ;
702
+ return unconsumedTypedKeyList ( convertNestedListToTypedKeys ( list ) ) ;
672
703
} ;
0 commit comments