@@ -347,30 +347,10 @@ namespace ts {
347
347
}
348
348
349
349
getDocumentationComment ( checker : TypeChecker | undefined ) : SymbolDisplayPart [ ] {
350
- if ( this . documentationComment === undefined ) {
351
- if ( this . declarations ) {
352
- this . documentationComment = JsDoc . getJsDocCommentsFromDeclarations ( this . declarations ) ;
353
-
354
- if ( this . documentationComment . length === 0 || this . declarations . some ( hasJSDocInheritDocTag ) ) {
355
- if ( checker ) {
356
- for ( const declaration of this . declarations ) {
357
- const inheritedDocs = findInheritedJSDocComments ( declaration , this . getName ( ) , checker ) ;
358
- if ( inheritedDocs . length > 0 ) {
359
- if ( this . documentationComment . length > 0 ) {
360
- inheritedDocs . push ( lineBreakPart ( ) ) ;
361
- }
362
- this . documentationComment = concatenate ( inheritedDocs , this . documentationComment ) ;
363
- break ;
364
- }
365
- }
366
- }
367
- }
368
- }
369
- else {
370
- this . documentationComment = [ ] ;
371
- }
350
+ if ( ! this . documentationComment ) {
351
+ this . documentationComment = emptyArray ; // Set temporarily to avoid an infinite loop finding inherited docs
352
+ this . documentationComment = getDocumentationComment ( this . declarations , checker ) ;
372
353
}
373
-
374
354
return this . documentationComment ;
375
355
}
376
356
@@ -504,27 +484,7 @@ namespace ts {
504
484
}
505
485
506
486
getDocumentationComment ( ) : SymbolDisplayPart [ ] {
507
- if ( this . documentationComment === undefined ) {
508
- if ( this . declaration ) {
509
- this . documentationComment = JsDoc . getJsDocCommentsFromDeclarations ( [ this . declaration ] ) ;
510
-
511
- if ( this . documentationComment . length === 0 || hasJSDocInheritDocTag ( this . declaration ) ) {
512
- const inheritedDocs = findInheritedJSDocComments ( this . declaration , this . declaration . symbol . getName ( ) , this . checker ) ;
513
- if ( this . documentationComment . length > 0 ) {
514
- inheritedDocs . push ( lineBreakPart ( ) ) ;
515
- }
516
- this . documentationComment = concatenate (
517
- inheritedDocs ,
518
- this . documentationComment
519
- ) ;
520
- }
521
- }
522
- else {
523
- this . documentationComment = [ ] ;
524
- }
525
- }
526
-
527
- return this . documentationComment ;
487
+ return this . documentationComment || ( this . documentationComment = getDocumentationComment ( singleElementArray ( this . declaration ) , this . checker ) ) ;
528
488
}
529
489
530
490
getJsDocTags ( ) : JSDocTagInfo [ ] {
@@ -545,6 +505,20 @@ namespace ts {
545
505
return getJSDocTags ( node ) . some ( tag => tag . tagName . text === "inheritDoc" ) ;
546
506
}
547
507
508
+ function getDocumentationComment ( declarations : ReadonlyArray < Declaration > | undefined , checker : TypeChecker | undefined ) : SymbolDisplayPart [ ] {
509
+ if ( ! declarations ) return emptyArray ;
510
+
511
+ let doc = JsDoc . getJsDocCommentsFromDeclarations ( declarations ) ;
512
+ if ( doc . length === 0 || declarations . some ( hasJSDocInheritDocTag ) ) {
513
+ for ( const declaration of declarations ) {
514
+ const inheritedDocs = findInheritedJSDocComments ( declaration , declaration . symbol . name , checker ) ;
515
+ // TODO: GH#16312 Return a ReadonlyArray, avoid copying inheritedDocs
516
+ if ( inheritedDocs ) doc = doc . length === 0 ? inheritedDocs . slice ( ) : inheritedDocs . concat ( lineBreakPart ( ) , doc ) ;
517
+ }
518
+ }
519
+ return doc ;
520
+ }
521
+
548
522
/**
549
523
* Attempts to find JSDoc comments for possibly-inherited properties. Checks superclasses then traverses
550
524
* implemented interfaces until a symbol is found with the same name and with documentation.
@@ -553,23 +527,12 @@ namespace ts {
553
527
* @param typeChecker A TypeChecker, used to find inherited properties.
554
528
* @returns A filled array of documentation comments if any were found, otherwise an empty array.
555
529
*/
556
- function findInheritedJSDocComments ( declaration : Declaration , propertyName : string , typeChecker : TypeChecker ) : SymbolDisplayPart [ ] {
557
- let foundDocs = false ;
558
- return flatMap ( declaration . parent ? getAllSuperTypeNodes ( declaration . parent ) : emptyArray , superTypeNode => {
559
- if ( foundDocs ) {
560
- return emptyArray ;
561
- }
530
+ function findInheritedJSDocComments ( declaration : Declaration , propertyName : string , typeChecker : TypeChecker ) : ReadonlyArray < SymbolDisplayPart > | undefined {
531
+ return firstDefined ( declaration . parent ? getAllSuperTypeNodes ( declaration . parent ) : emptyArray , superTypeNode => {
562
532
const superType = typeChecker . getTypeAtLocation ( superTypeNode ) ;
563
- if ( ! superType ) {
564
- return emptyArray ;
565
- }
566
- const baseProperty = typeChecker . getPropertyOfType ( superType , propertyName ) ;
567
- if ( ! baseProperty ) {
568
- return emptyArray ;
569
- }
570
- const inheritedDocs = baseProperty . getDocumentationComment ( typeChecker ) ;
571
- foundDocs = inheritedDocs . length > 0 ;
572
- return inheritedDocs ;
533
+ const baseProperty = superType && typeChecker . getPropertyOfType ( superType , propertyName ) ;
534
+ const inheritedDocs = baseProperty && baseProperty . getDocumentationComment ( typeChecker ) ;
535
+ return inheritedDocs && inheritedDocs . length ? inheritedDocs : undefined ;
573
536
} ) ;
574
537
}
575
538
0 commit comments