Skip to content

Commit 91d49fb

Browse files
committed
Send TSDoc parser messages to the MessageRouter
1 parent cb4c820 commit 91d49fb

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

‎apps/api-extractor/src/api/items/ApiDocumentedItem.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ export class ApiDocumentedItem extends ApiItem {
4040

4141
if (documentedJson.docComment) {
4242
const tsdocParser: tsdoc.TSDocParser = new tsdoc.TSDocParser(AedocDefinitions.tsdocConfiguration);
43-
const parserContext: tsdoc.ParserContext = tsdocParser.parseString(documentedJson.docComment);
4443

45-
// TODO: Warn about parser errors
44+
// NOTE: For now, we ignore TSDoc errors found in a serialized .api.json file.
45+
// Normally these errors would have already been reported by API Extractor during analysis.
46+
// However, they could also arise if the JSON file was edited manually, or if the file was saved
47+
// using a different release of the software that used an incompatible syntax.
48+
const parserContext: tsdoc.ParserContext = tsdocParser.parseString(documentedJson.docComment);
4649

4750
options.docComment = parserContext.docComment;
4851
}

‎apps/api-extractor/src/collector/Collector.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,24 @@ export class Collector {
179179
}
180180

181181
// Build the entry point
182+
const entryPointSourceFile: ts.SourceFile = this.workingPackage.entryPointSourceFile;
183+
182184
const astEntryPoint: AstModule = this.astSymbolTable.fetchAstModuleFromWorkingPackage(
183-
this.workingPackage.entryPointSourceFile);
185+
entryPointSourceFile);
184186
this._astEntryPoint = astEntryPoint;
185187

186188
const packageDocCommentTextRange: ts.TextRange | undefined = PackageDocComment.tryFindInSourceFile(
187-
this.workingPackage.entryPointSourceFile, this);
189+
entryPointSourceFile, this);
188190

189191
if (packageDocCommentTextRange) {
190-
const range: tsdoc.TextRange = tsdoc.TextRange.fromStringRange(this.workingPackage.entryPointSourceFile.text,
192+
const range: tsdoc.TextRange = tsdoc.TextRange.fromStringRange(entryPointSourceFile.text,
191193
packageDocCommentTextRange.pos, packageDocCommentTextRange.end);
192194

193195
this.workingPackage.tsdocParserContext = this._tsdocParser.parseRange(range);
196+
197+
this.messageRouter.addTsdocMessages(this.workingPackage.tsdocParserContext,
198+
entryPointSourceFile);
199+
194200
this.workingPackage.tsdocComment = this.workingPackage.tsdocParserContext!.docComment;
195201
}
196202

@@ -629,7 +635,11 @@ export class Collector {
629635
const tsdocTextRange: tsdoc.TextRange = tsdoc.TextRange.fromStringRange(sourceFileText,
630636
range.pos, range.end);
631637

632-
return this._tsdocParser.parseRange(tsdocTextRange);
638+
const parserContext: tsdoc.ParserContext = this._tsdocParser.parseRange(tsdocTextRange);
639+
640+
this.messageRouter.addTsdocMessages(parserContext, declaration.getSourceFile());
641+
642+
return parserContext;
633643
}
634644

635645
private _collectReferenceDirectives(astSymbol: AstSymbol): void {

‎apps/api-extractor/src/collector/MessageRouter.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import * as ts from 'typescript';
5+
import * as tsdoc from '@microsoft/tsdoc';
56

67
import { TypeScriptMessageFormatter } from '../analyzer/TypeScriptMessageFormatter';
78
import { AstDeclaration } from '../analyzer/AstDeclaration';
@@ -24,6 +25,9 @@ export class MessageRouter {
2425
return this._messages;
2526
}
2627

28+
/**
29+
* Add a diagnostic message reported by the TypeScript compiler
30+
*/
2731
public addCompilerDiagnostic(diagnostic: ts.Diagnostic): void {
2832
switch (diagnostic.category) {
2933
case ts.DiagnosticCategory.Suggestion:
@@ -51,6 +55,9 @@ export class MessageRouter {
5155
this._messages.push(new ExtractorMessage(options));
5256
}
5357

58+
/**
59+
* Add a message from the API Extractor analysis
60+
*/
5461
public addAnalyzerIssue(messageId: ExtractorMessageId, messageText: string,
5562
astDeclarationOrSymbol: AstDeclaration | AstSymbol): void {
5663

@@ -64,6 +71,31 @@ export class MessageRouter {
6471
this.addAnalyzerIssueForPosition(messageId, messageText, declaration.getSourceFile(), declaration.pos);
6572
}
6673

74+
/**
75+
* Add all messages produced from an invocation of the TSDoc parser, assuming they refer to
76+
* code in the specified source file.
77+
*/
78+
public addTsdocMessages(parserContext: tsdoc.ParserContext, sourceFile: ts.SourceFile): void {
79+
for (const message of parserContext.log.messages) {
80+
const lineAndCharacter: ts.LineAndCharacter = sourceFile.getLineAndCharacterOfPosition(
81+
message.textRange.pos);
82+
83+
this._messages.push(
84+
new ExtractorMessage({
85+
category: ExtractorMessageCategory.TSDoc,
86+
messageId: message.messageId,
87+
text: message.text,
88+
sourceFilePath: sourceFile.fileName,
89+
sourceFileLine: lineAndCharacter.line,
90+
sourceFileColumn: lineAndCharacter.character
91+
})
92+
);
93+
}
94+
}
95+
96+
/**
97+
* Add a message for a location in an arbitrary source file.
98+
*/
6799
public addAnalyzerIssueForPosition(messageId: ExtractorMessageId, messageText: string,
68100
sourceFile: ts.SourceFile, pos: number): void {
69101

@@ -81,5 +113,4 @@ export class MessageRouter {
81113

82114
this._messages.push(new ExtractorMessage(options));
83115
}
84-
85116
}

0 commit comments

Comments
 (0)