@@ -466,11 +466,22 @@ module.exports = {
466
466
* @return {Object } The representation of the declaration, empty object means
467
467
* the property is declared without the need for further analysis.
468
468
*/
469
- function buildTypeAnnotationDeclarationTypes ( annotation ) {
469
+ function buildTypeAnnotationDeclarationTypes ( annotation , seen ) {
470
+ if ( typeof seen === 'undefined' ) {
471
+ // Keeps track of annotations we've already seen to
472
+ // prevent problems with recursive types.
473
+ seen = new Set ( ) ;
474
+ }
475
+ if ( seen . has ( annotation ) ) {
476
+ // This must be a recursive type annotation, so just accept anything.
477
+ return { } ;
478
+ }
479
+ seen . add ( annotation ) ;
480
+
470
481
switch ( annotation . type ) {
471
482
case 'GenericTypeAnnotation' :
472
483
if ( typeScope ( annotation . id . name ) ) {
473
- return buildTypeAnnotationDeclarationTypes ( typeScope ( annotation . id . name ) ) ;
484
+ return buildTypeAnnotationDeclarationTypes ( typeScope ( annotation . id . name ) , seen ) ;
474
485
}
475
486
return { } ;
476
487
case 'ObjectTypeAnnotation' :
@@ -483,7 +494,7 @@ module.exports = {
483
494
if ( ! childKey && ! childValue ) {
484
495
containsObjectTypeSpread = true ;
485
496
} else {
486
- shapeTypeDefinition . children [ childKey ] = buildTypeAnnotationDeclarationTypes ( childValue ) ;
497
+ shapeTypeDefinition . children [ childKey ] = buildTypeAnnotationDeclarationTypes ( childValue , seen ) ;
487
498
}
488
499
} ) ;
489
500
@@ -498,7 +509,7 @@ module.exports = {
498
509
children : [ ]
499
510
} ;
500
511
for ( let i = 0 , j = annotation . types . length ; i < j ; i ++ ) {
501
- const type = buildTypeAnnotationDeclarationTypes ( annotation . types [ i ] ) ;
512
+ const type = buildTypeAnnotationDeclarationTypes ( annotation . types [ i ] , seen ) ;
502
513
// keep only complex type
503
514
if ( Object . keys ( type ) . length > 0 ) {
504
515
if ( type . children === true ) {
@@ -519,7 +530,7 @@ module.exports = {
519
530
return {
520
531
type : 'object' ,
521
532
children : {
522
- __ANY_KEY__ : buildTypeAnnotationDeclarationTypes ( annotation . elementType )
533
+ __ANY_KEY__ : buildTypeAnnotationDeclarationTypes ( annotation . elementType , seen )
523
534
}
524
535
} ;
525
536
default :
0 commit comments