Skip to content

Commit c28e041

Browse files
committed
Fix handling of transitive @inheritDoc chains
1 parent 4a8a44e commit c28e041

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

apps/api-extractor/src/enhancers/DocCommentEnhancer.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { AstDeclaration } from '../analyzer/AstDeclaration';
1010
import { DeclarationMetadata, VisitorState } from '../collector/DeclarationMetadata';
1111
import { AedocDefinitions } from '@microsoft/api-extractor-model';
1212
import { InternalError } from '@microsoft/node-core-library';
13-
import { ResultOrError } from '../analyzer/AstReferenceResolver';
1413
import { ExtractorMessageId } from '../api/ExtractorMessageId';
1514

1615
export class DocCommentEnhancer {
@@ -41,8 +40,10 @@ export class DocCommentEnhancer {
4140
const metadata: DeclarationMetadata = this._collector.fetchMetadata(astDeclaration);
4241
if (metadata.docCommentEnhancerVisitorStage === VisitorState.Visited) {
4342
return;
44-
} else if (metadata.docCommentEnhancerVisitorStage === VisitorState.Visiting) {
45-
throw new InternalError('Infinite loop');
43+
}
44+
45+
if (metadata.docCommentEnhancerVisitorStage === VisitorState.Visiting) {
46+
throw new InternalError('Infinite loop in DocCommentEnhancer._analyzeDeclaration()');
4647
}
4748
metadata.docCommentEnhancerVisitorStage = VisitorState.Visiting;
4849

@@ -51,6 +52,8 @@ export class DocCommentEnhancer {
5152
}
5253

5354
this._analyzeNeedsDocumentation(astDeclaration, metadata);
55+
56+
metadata.docCommentEnhancerVisitorStage = VisitorState.Visited;
5457
}
5558

5659
private _analyzeNeedsDocumentation(astDeclaration: AstDeclaration, metadata: DeclarationMetadata): void {
@@ -87,6 +90,9 @@ export class DocCommentEnhancer {
8790
}
8891
}
8992

93+
/**
94+
* Follow an `{@inheritDoc ___}` reference and copy the content that we find in the referenced comment.
95+
*/
9096
private _analyzeInheritDoc(astDeclaration: AstDeclaration, docComment: tsdoc.DocComment,
9197
inheritDocTag: tsdoc.DocInheritDocTag): void {
9298

@@ -97,22 +103,27 @@ export class DocCommentEnhancer {
97103
return;
98104
}
99105

100-
const sourceAstDeclaration: ResultOrError<AstDeclaration> = this._collector.astReferenceResolver
106+
const referencedAstDeclaration: AstDeclaration | Error = this._collector.astReferenceResolver
101107
.resolve(inheritDocTag.declarationReference);
102108

103-
if (sourceAstDeclaration instanceof Error) {
109+
if (referencedAstDeclaration instanceof Error) {
104110
this._collector.messageRouter.addAnalyzerIssue(ExtractorMessageId.UnresolvedInheritDocReference,
105-
'The `@inheritDoc` reference could not be resolved: ' + sourceAstDeclaration.message, astDeclaration);
111+
'The `@inheritDoc` reference could not be resolved: ' + referencedAstDeclaration.message, astDeclaration);
106112
return;
107113
}
108114

109-
const sourceMetadata: DeclarationMetadata = this._collector.fetchMetadata(sourceAstDeclaration);
115+
this._analyzeDeclaration(referencedAstDeclaration);
116+
117+
const referencedMetadata: DeclarationMetadata = this._collector.fetchMetadata(referencedAstDeclaration);
110118

111-
if (sourceMetadata.tsdocComment) {
112-
this._copyInheritedDocs(docComment, sourceMetadata.tsdocComment);
119+
if (referencedMetadata.tsdocComment) {
120+
this._copyInheritedDocs(docComment, referencedMetadata.tsdocComment);
113121
}
114122
}
115123

124+
/**
125+
* Copy the content from `sourceDocComment` to `targetDocComment`.
126+
*/
116127
private _copyInheritedDocs(targetDocComment: tsdoc.DocComment, sourceDocComment: tsdoc.DocComment): void {
117128
targetDocComment.summarySection = sourceDocComment.summarySection;
118129
targetDocComment.remarksBlock = sourceDocComment.remarksBlock;

build-tests/api-extractor-scenarios/etc/test-outputs/docReferences2/api-extractor-scenarios.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{
3939
"kind": "Method",
4040
"canonicalReference": "(methodA1:instance,0)",
41-
"docComment": "",
41+
"docComment": "/**\n * THE COMMENT\n */\n",
4242
"excerptTokens": [
4343
{
4444
"kind": "Reference",

build-tests/api-extractor-scenarios/etc/test-outputs/docReferences2/api-extractor-scenarios.api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
// @public (undocumented)
88
export class CyclicA {
9-
// (undocumented)
109
methodA1(): void;
1110
methodA3(): void;
1211
}

0 commit comments

Comments
 (0)