Skip to content

Commit 959f03f

Browse files
committed
- Implement ae-internal-missing-underscore
- Introduce ExtractorMessage.properties to track the affected name
1 parent e444346 commit 959f03f

6 files changed

Lines changed: 73 additions & 7 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ import * as path from 'path';
66
import { ExtractorMessageId } from './ExtractorMessageId';
77
import { Path, Text } from '@microsoft/node-core-library';
88

9+
/**
10+
* Used by {@link ExtractorMessage.properties}.
11+
*
12+
* @public
13+
*/
14+
export interface IExtractorMessageProperties {
15+
16+
/**
17+
* A declaration can have multiple names if it is exported more than once.
18+
* If an `ExtractorMessage` applies to a specific export name, this property can indicate that.
19+
*
20+
* @remarks
21+
*
22+
* Used by {@link ExtractorMessageId.InternalMissingUnderscore}.
23+
*/
24+
readonly exportName?: string;
25+
}
26+
927
/**
1028
* Specifies a category of messages for use with {@link ExtractorMessage}.
1129
* @public
@@ -49,6 +67,7 @@ export interface IExtractorMessageOptions {
4967
sourceFilePath?: string;
5068
sourceFileLine?: number;
5169
sourceFileColumn?: number;
70+
properties?: IExtractorMessageProperties;
5271
}
5372

5473
/**
@@ -90,6 +109,12 @@ export class ExtractorMessage {
90109
*/
91110
public readonly sourceFileColumn: number | undefined;
92111

112+
/**
113+
* Additional contextual information about the message that may be useful when reporting errors.
114+
* All properties are optional.
115+
*/
116+
public readonly properties: IExtractorMessageProperties;
117+
93118
/** @internal */
94119
public constructor(options: IExtractorMessageOptions) {
95120
this.category = options.category;
@@ -98,6 +123,7 @@ export class ExtractorMessage {
98123
this.sourceFilePath = options.sourceFilePath;
99124
this.sourceFileLine = options.sourceFileLine;
100125
this.sourceFileColumn = options.sourceFileColumn;
126+
this.properties = options.properties || { };
101127
}
102128

103129
/**

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ export const enum ExtractorMessageId {
4040
/**
4141
* The symbol ___ needs to be exported by the entry point ___.
4242
*/
43-
ForgottenExport = 'ae-forgotten-export'
43+
ForgottenExport = 'ae-forgotten-export',
44+
45+
/**
46+
* The name ___ should be prefixed with an underscore because the declaration is marked as `@internal`.
47+
*/
48+
InternalMissingUnderscore = 'ae-internal-missing-underscore'
4449
}
4550

4651
export const allExtractorMessageIds: Set<string> = new Set<string>([
@@ -49,5 +54,6 @@ export const allExtractorMessageIds: Set<string> = new Set<string>([
4954
'ae-incompatible-release-tags',
5055
'ae-missing-release-tag',
5156
'ae-misplaced-package-tag',
52-
'ae-forgotten-export'
57+
'ae-forgotten-export',
58+
'ae-internal-missing-underscore'
5359
]);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { AstSymbol } from '../analyzer/AstSymbol';
1212
import {
1313
ExtractorMessage,
1414
ExtractorMessageCategory,
15-
IExtractorMessageOptions
15+
IExtractorMessageOptions,
16+
IExtractorMessageProperties
1617
} from '../api/ExtractorMessage';
1718
import { ExtractorMessageId, allExtractorMessageIds } from '../api/ExtractorMessageId';
1819
import {
@@ -164,7 +165,7 @@ export class MessageRouter {
164165
* Add a message from the API Extractor analysis
165166
*/
166167
public addAnalyzerIssue(messageId: ExtractorMessageId, messageText: string,
167-
astDeclarationOrSymbol: AstDeclaration | AstSymbol): void {
168+
astDeclarationOrSymbol: AstDeclaration | AstSymbol, properties?: IExtractorMessageProperties): void {
168169

169170
let astDeclaration: AstDeclaration;
170171
if (astDeclarationOrSymbol instanceof AstDeclaration) {
@@ -175,7 +176,7 @@ export class MessageRouter {
175176

176177
const extractorMessage: ExtractorMessage = this.addAnalyzerIssueForPosition(
177178
messageId, messageText, astDeclaration.declaration.getSourceFile(),
178-
astDeclaration.declaration.getStart());
179+
astDeclaration.declaration.getStart(), properties);
179180

180181
this._associateMessageWithAstDeclaration(extractorMessage, astDeclaration);
181182
}
@@ -231,7 +232,7 @@ export class MessageRouter {
231232
* Add a message for a location in an arbitrary source file.
232233
*/
233234
public addAnalyzerIssueForPosition(messageId: ExtractorMessageId, messageText: string,
234-
sourceFile: ts.SourceFile, pos: number): ExtractorMessage {
235+
sourceFile: ts.SourceFile, pos: number, properties?: IExtractorMessageProperties): ExtractorMessage {
235236

236237
const lineAndCharacter: ts.LineAndCharacter = sourceFile.getLineAndCharacterOfPosition(
237238
pos);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,33 @@ export class VisibilityChecker {
2121
VisibilityChecker._checkReferences(collector, astDeclaration, alreadyWarnedSymbols);
2222
});
2323

24+
VisibilityChecker._checkForInternalUnderscore(collector, entity, entity.astEntity);
2425
}
2526
}
2627
}
2728
}
2829

30+
private static _checkForInternalUnderscore(collector: Collector, collectorEntity: CollectorEntity,
31+
astSymbol: AstSymbol): void {
32+
33+
const astSymbolMetadata: SymbolMetadata = collector.fetchMetadata(astSymbol);
34+
35+
if (astSymbolMetadata.releaseTag === ReleaseTag.Internal && !astSymbolMetadata.releaseTagSameAsParent) {
36+
for (const exportName of collectorEntity.exportNames) {
37+
if (exportName[0] !== '_') {
38+
collector.messageRouter.addAnalyzerIssue(
39+
ExtractorMessageId.InternalMissingUnderscore,
40+
`The name ${exportName} should be prefixed with an underscore`
41+
+ ` because the declaration is marked as "@internal"`,
42+
astSymbol,
43+
{ exportName }
44+
);
45+
}
46+
}
47+
}
48+
49+
}
50+
2951
private static _checkReferences(collector: Collector, astDeclaration: AstDeclaration,
3052
alreadyWarnedSymbols: Set<AstSymbol>): void {
3153

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
*/
1111

1212
export { Extractor, IAnalyzeProjectOptions, IExtractorOptions } from './api/Extractor';
13-
export { ExtractorMessage, ExtractorMessageCategory } from './api/ExtractorMessage';
13+
export {
14+
ExtractorMessage,
15+
IExtractorMessageProperties,
16+
ExtractorMessageCategory
17+
} from './api/ExtractorMessage';
1418
export { ExtractorMessageId } from './api/ExtractorMessageId';
1519
export {
1620
IExtractorTsconfigCompilerConfig,

‎common/reviews/api/api-extractor.api.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare class ExtractorMessage {
3131
// (undocumented)
3232
formatMessageWithoutLocation(): string;
3333
readonly messageId: tsdoc.TSDocMessageId | ExtractorMessageId | string;
34+
readonly properties: IExtractorMessageProperties;
3435
readonly sourceFileColumn: number | undefined;
3536
readonly sourceFileLine: number | undefined;
3637
readonly sourceFilePath: string | undefined;
@@ -50,6 +51,7 @@ declare const enum ExtractorMessageId {
5051
ExtraReleaseTag = "ae-extra-release-tag",
5152
ForgottenExport = "ae-forgotten-export",
5253
IncompatibleReleaseTags = "ae-incompatible-release-tags",
54+
InternalMissingUnderscore = "ae-internal-missing-underscore",
5355
MisplacedPackageTag = "ae-misplaced-package-tag",
5456
MissingReleaseTag = "ae-missing-release-tag"
5557
}
@@ -120,6 +122,11 @@ interface IExtractorDtsRollupConfig {
120122
trimming?: boolean;
121123
}
122124

125+
// @public
126+
interface IExtractorMessageProperties {
127+
readonly exportName?: string;
128+
}
129+
123130
// @public
124131
interface IExtractorMessageReportingRuleConfig {
125132
addToApiReviewFile?: boolean;

0 commit comments

Comments
 (0)