Skip to content

Commit cb4c820

Browse files
committed
Introduce a MessageRouter to collect error messages, and annotate all analyzer messages with an ExtractorMessageId
1 parent 23a846d commit cb4c820

6 files changed

Lines changed: 182 additions & 36 deletions

File tree

‎apps/api-extractor/src/aedoc/PackageDocComment.ts‎

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

44
import * as ts from 'typescript';
55
import { Collector } from '../collector/Collector';
6+
import { ExtractorMessageId } from '../collector/ExtractorMessageId';
67

78
export class PackageDocComment {
89
/**
@@ -53,7 +54,8 @@ export class PackageDocComment {
5354
const commentBody: string = sourceFile.text.substring(commentRange.pos, commentRange.end);
5455

5556
if (/\@packageDocumentation/i.test(commentBody)) {
56-
collector.reportError(
57+
collector.messageRouter.addAnalyzerIssueForPosition(
58+
ExtractorMessageId.MisplacedPackageTag,
5759
'The @packageDocumentation comment must appear at the top of entry point *.d.ts file',
5860
sourceFile, commentRange.pos
5961
);

‎apps/api-extractor/src/api/ILogger.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export interface ILogger {
2222
logWarning(message: string): void;
2323

2424
/**
25-
* Log an error message. Typically it is shown in red and will break a production build.
25+
* Log an error message. Typically it is shown in red and will break the build, even if it
26+
* is not a production build.
2627
*/
2728
logError(message: string): void;
2829
}

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

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

44
import * as ts from 'typescript';
5-
import * as path from 'path';
65
import * as tsdoc from '@microsoft/tsdoc';
76
import {
87
PackageJsonLookup,
@@ -17,7 +16,7 @@ import {
1716
IExtractorValidationRulesConfig,
1817
ExtractorValidationRulePolicy
1918
} from '../api/IExtractorConfig';
20-
import { TypeScriptMessageFormatter } from '../analyzer/TypeScriptMessageFormatter';
19+
2120
import { CollectorEntity } from './CollectorEntity';
2221
import { AstSymbolTable, AstEntity } from '../analyzer/AstSymbolTable';
2322
import { AstModule, AstModuleExportInfo } from '../analyzer/AstModule';
@@ -30,6 +29,8 @@ import { PackageDocComment } from '../aedoc/PackageDocComment';
3029
import { DeclarationMetadata } from './DeclarationMetadata';
3130
import { SymbolMetadata } from './SymbolMetadata';
3231
import { TypeScriptInternals } from '../analyzer/TypeScriptInternals';
32+
import { MessageRouter } from './MessageRouter';
33+
import { ExtractorMessageId } from './ExtractorMessageId';
3334

3435
/**
3536
* Options for Collector constructor.
@@ -71,6 +72,7 @@ export class Collector {
7172
public readonly astSymbolTable: AstSymbolTable;
7273

7374
public readonly packageJsonLookup: PackageJsonLookup;
75+
public readonly messageRouter: MessageRouter;
7476

7577
public readonly policies: IExtractorPoliciesConfig;
7678
public readonly validationRules: IExtractorValidationRulesConfig;
@@ -95,11 +97,11 @@ export class Collector {
9597

9698
constructor(options: ICollectorOptions) {
9799
this.packageJsonLookup = new PackageJsonLookup();
100+
this.messageRouter = new MessageRouter();
98101

99102
this.policies = options.policies;
100103
this.validationRules = options.validationRules;
101104

102-
this.logger = options.logger;
103105
this._program = options.program;
104106

105107
const packageFolder: string | undefined = this.packageJsonLookup.tryGetPackageFolderFor(options.entryPointFile);
@@ -173,8 +175,7 @@ export class Collector {
173175
// with semantic information (i.e. symbols). The "diagnostics" are a subset of the everyday
174176
// compile errors that would result from a full compilation.
175177
for (const diagnostic of this._program.getSemanticDiagnostics()) {
176-
const errorText: string = TypeScriptMessageFormatter.format(diagnostic.messageText);
177-
this.reportError(`TypeScript: ${errorText}`, diagnostic.file, diagnostic.start);
178+
this.messageRouter.addCompilerDiagnostic(diagnostic);
178179
}
179180

180181
// Build the entry point
@@ -443,26 +444,6 @@ export class Collector {
443444
}
444445
}
445446

446-
/**
447-
* Reports an error message to the registered ApiErrorHandler.
448-
*/
449-
public reportError(message: string, sourceFile: ts.SourceFile | undefined, start: number | undefined): void {
450-
if (sourceFile && start) {
451-
const lineAndCharacter: ts.LineAndCharacter = sourceFile.getLineAndCharacterOfPosition(start);
452-
453-
// If the file is under the packageFolder, then show a relative path
454-
const relativePath: string = path.relative(this.workingPackage.packageFolder, sourceFile.fileName);
455-
const shownPath: string = relativePath.substr(0, 2) === '..' ? sourceFile.fileName : relativePath;
456-
457-
// Format the error so that VS Code can follow it. For example:
458-
// "src\MyClass.ts(15,1): The JSDoc tag "@blah" is not supported by AEDoc"
459-
this.logger.logError(`${shownPath}(${lineAndCharacter.line + 1},${lineAndCharacter.character + 1}): `
460-
+ message);
461-
} else {
462-
this.logger.logError(message);
463-
}
464-
}
465-
466447
private _fetchSymbolMetadata(astSymbol: AstSymbol): void {
467448
if (astSymbol.metadata) {
468449
return;
@@ -495,8 +476,11 @@ export class Collector {
495476
if (declaredReleaseTag !== ReleaseTag.None) {
496477
if (effectiveReleaseTag !== ReleaseTag.None && effectiveReleaseTag !== declaredReleaseTag) {
497478
if (!astSymbol.isExternal) { // for now, don't report errors for external code
498-
// TODO: Report error message
499-
this.reportError('Inconsistent release tags between declarations', undefined, undefined);
479+
this.messageRouter.addAnalyzerIssue(
480+
ExtractorMessageId.InconsistentReleaseTags,
481+
'This symbol has another declaration with a different release tag',
482+
astDeclaration
483+
);
500484
}
501485
} else {
502486
effectiveReleaseTag = declaredReleaseTag;
@@ -514,16 +498,18 @@ export class Collector {
514498
if (effectiveReleaseTag === ReleaseTag.None) {
515499
if (this.validationRules.missingReleaseTags !== ExtractorValidationRulePolicy.allow) {
516500
if (!astSymbol.isExternal) { // for now, don't report errors for external code
517-
// For now, don't report errors for forgotten exports
501+
// Don't report missing release tags for forgotten exports
518502
const entity: CollectorEntity | undefined = this._entitiesByAstEntity.get(astSymbol.rootAstSymbol);
519503
if (entity && entity.exported) {
520504
// We also don't report errors for the default export of an entry point, since its doc comment
521505
// isn't easy to obtain from the .d.ts file
522506
if (astSymbol.rootAstSymbol.localName !== '_default') {
523-
// TODO: Report error message
524-
const loc: string = astSymbol.rootAstSymbol.localName + ' in '
525-
+ astSymbol.rootAstSymbol.astDeclarations[0].declaration.getSourceFile().fileName;
526-
this.reportError('Missing release tag for ' + loc, undefined, undefined);
507+
508+
this.messageRouter.addAnalyzerIssue(
509+
ExtractorMessageId.MissingReleaseTag,
510+
'Missing release tag',
511+
astSymbol
512+
);
527513
}
528514
}
529515
}
@@ -580,8 +566,10 @@ export class Collector {
580566

581567
if (inconsistentReleaseTags) {
582568
if (!astDeclaration.astSymbol.isExternal) { // for now, don't report errors for external code
583-
// TODO: Report error message
584-
this.reportError('Inconsistent release tags in doc comment', undefined, undefined);
569+
this.messageRouter.addAnalyzerIssue(
570+
ExtractorMessageId.ExtraReleaseTag,
571+
'The doc comment should not contain more than one release tag',
572+
astDeclaration);
585573
}
586574
}
587575

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import * as tsdoc from '@microsoft/tsdoc';
5+
import { ExtractorMessageId } from './ExtractorMessageId';
6+
7+
export const enum ExtractorMessageCategory {
8+
Compiler,
9+
TSDoc,
10+
Extractor
11+
}
12+
13+
export interface IExtractorMessageOptions {
14+
category: ExtractorMessageCategory;
15+
messageId: tsdoc.TSDocMessageId | ExtractorMessageId | string;
16+
text: string;
17+
sourceFilePath?: string;
18+
sourceFileLine?: number;
19+
sourceFileColumn?: number;
20+
}
21+
22+
/**
23+
* Represents an error or warning that occurred during parsing.
24+
*/
25+
export class ExtractorMessage {
26+
public readonly category: ExtractorMessageCategory;
27+
28+
public readonly messageId: tsdoc.TSDocMessageId | ExtractorMessageId | string;
29+
30+
public readonly text: string;
31+
32+
public readonly sourceFilePath: string | undefined;
33+
34+
public readonly sourceFileLine: number | undefined;
35+
36+
public readonly sourceFileColumn: number | undefined;
37+
38+
public constructor(options: IExtractorMessageOptions) {
39+
this.category = options.category;
40+
this.messageId = options.messageId;
41+
this.text = options.text;
42+
this.sourceFilePath = options.sourceFilePath;
43+
this.sourceFileLine = options.sourceFileLine;
44+
this.sourceFileColumn = options.sourceFileColumn;
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
export const enum ExtractorMessageId {
5+
/**
6+
* The doc comment should not contain more than one release tag.
7+
*/
8+
ExtraReleaseTag = 'ae-extra-release-tag',
9+
10+
/**
11+
* This symbol has another declaration with a different release tag.
12+
*/
13+
InconsistentReleaseTags = 'ae-inconsistent-release-tags',
14+
15+
/**
16+
* The doc comment should not contain more than one release tag.
17+
*/
18+
MissingReleaseTag = 'ae-missing-release-tag',
19+
20+
/**
21+
* The @packageDocumentation comment must appear at the top of entry point *.d.ts file.
22+
*/
23+
MisplacedPackageTag = 'ae-misplaced-package-tag'
24+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import * as ts from 'typescript';
5+
6+
import { TypeScriptMessageFormatter } from '../analyzer/TypeScriptMessageFormatter';
7+
import { AstDeclaration } from '../analyzer/AstDeclaration';
8+
import {
9+
ExtractorMessage,
10+
ExtractorMessageCategory,
11+
IExtractorMessageOptions
12+
} from './ExtractorMessage';
13+
import { AstSymbol } from '../analyzer/AstSymbol';
14+
import { ExtractorMessageId } from './ExtractorMessageId';
15+
16+
export class MessageRouter {
17+
private _messages: ExtractorMessage[];
18+
19+
public constructor() {
20+
this._messages = [];
21+
}
22+
23+
public get messages(): ReadonlyArray<ExtractorMessage> {
24+
return this._messages;
25+
}
26+
27+
public addCompilerDiagnostic(diagnostic: ts.Diagnostic): void {
28+
switch (diagnostic.category) {
29+
case ts.DiagnosticCategory.Suggestion:
30+
case ts.DiagnosticCategory.Message:
31+
return; // ignore noise
32+
}
33+
34+
const messageText: string = TypeScriptMessageFormatter.format(diagnostic.messageText);
35+
const options: IExtractorMessageOptions = {
36+
category: ExtractorMessageCategory.Compiler,
37+
messageId: `TS${diagnostic.code}`,
38+
text: messageText
39+
};
40+
41+
if (diagnostic.file) {
42+
const sourceFile: ts.SourceFile = diagnostic.file;
43+
const lineAndCharacter: ts.LineAndCharacter = sourceFile.getLineAndCharacterOfPosition(
44+
diagnostic.start || 0);
45+
46+
options.sourceFilePath = sourceFile.fileName;
47+
options.sourceFileLine = lineAndCharacter.line;
48+
options.sourceFileColumn = lineAndCharacter.character;
49+
}
50+
51+
this._messages.push(new ExtractorMessage(options));
52+
}
53+
54+
public addAnalyzerIssue(messageId: ExtractorMessageId, messageText: string,
55+
astDeclarationOrSymbol: AstDeclaration | AstSymbol): void {
56+
57+
let declaration: ts.Declaration;
58+
if (astDeclarationOrSymbol instanceof AstDeclaration) {
59+
declaration = astDeclarationOrSymbol.declaration;
60+
} else {
61+
declaration = astDeclarationOrSymbol.astDeclarations[0].declaration;
62+
}
63+
64+
this.addAnalyzerIssueForPosition(messageId, messageText, declaration.getSourceFile(), declaration.pos);
65+
}
66+
67+
public addAnalyzerIssueForPosition(messageId: ExtractorMessageId, messageText: string,
68+
sourceFile: ts.SourceFile, pos: number): void {
69+
70+
const lineAndCharacter: ts.LineAndCharacter = sourceFile.getLineAndCharacterOfPosition(
71+
pos);
72+
73+
const options: IExtractorMessageOptions = {
74+
category: ExtractorMessageCategory.Extractor,
75+
messageId,
76+
text: messageText,
77+
sourceFilePath: sourceFile.fileName,
78+
sourceFileLine: lineAndCharacter.line,
79+
sourceFileColumn: lineAndCharacter.character
80+
};
81+
82+
this._messages.push(new ExtractorMessage(options));
83+
}
84+
85+
}

0 commit comments

Comments
 (0)