Skip to content

Commit 67e1ebd

Browse files
committed
Reintroduce the validation that warns about forgotten exports
1 parent 4adc7b0 commit 67e1ebd

18 files changed

Lines changed: 169 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ApiModelGenerator } from '../generators/ApiModelGenerator';
3131
import { ApiPackage } from './model/ApiPackage';
3232
import { ReviewFileGenerator } from '../generators/ReviewFileGenerator';
3333
import { PackageMetadataManager } from '../analyzer/PackageMetadataManager';
34+
import { VisibilityChecker } from '../collector/VisibilityChecker';
3435

3536
/**
3637
* Options for {@link Extractor.processProject}.
@@ -391,6 +392,8 @@ export class Extractor {
391392

392393
collector.analyze();
393394

395+
VisibilityChecker.check(collector);
396+
394397
const modelBuilder: ApiModelGenerator = new ApiModelGenerator(collector);
395398
const apiPackage: ApiPackage = modelBuilder.buildApiPackage();
396399

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ export const enum ExtractorMessageId {
2828
MissingReleaseTag = 'ae-missing-release-tag',
2929

3030
/**
31-
* The @packageDocumentation comment must appear at the top of entry point *.d.ts file.
31+
* The `@packageDocumentation` comment must appear at the top of entry point *.d.ts file.
3232
*/
33-
MisplacedPackageTag = 'ae-misplaced-package-tag'
33+
MisplacedPackageTag = 'ae-misplaced-package-tag',
34+
35+
/**
36+
* The symbol ___ needs to be exported from the entry point ___.
37+
*/
38+
ForgottenExport = 'ae-forgotten-export'
3439
}
3540

3641
export const allExtractorMessageIds: Set<string> = new Set<string>([

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ export class Collector {
251251
return undefined;
252252
}
253253

254+
/**
255+
* Returns the associated `CollectorEntity` for the given `astEntity`, if one was created during analysis.
256+
*/
257+
public tryGetCollectorEntity(astEntity: AstEntity): CollectorEntity | undefined {
258+
return this._entitiesByAstEntity.get(astEntity);
259+
}
260+
254261
public fetchMetadata(astSymbol: AstSymbol): SymbolMetadata;
255262
public fetchMetadata(astDeclaration: AstDeclaration): DeclarationMetadata;
256263
public fetchMetadata(symbolOrDeclaration: AstSymbol | AstDeclaration): SymbolMetadata | DeclarationMetadata {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import * as path from 'path';
2+
import * as ts from 'typescript';
3+
4+
import { Collector } from './Collector';
5+
import { AstSymbol } from '../analyzer/AstSymbol';
6+
import { AstDeclaration } from '../analyzer/AstDeclaration';
7+
// import { SymbolMetadata } from './SymbolMetadata';
8+
import { CollectorEntity } from './CollectorEntity';
9+
import { ExtractorMessageId } from '../api/ExtractorMessageId';
10+
11+
export class VisibilityChecker {
12+
13+
public static check(collector: Collector): void {
14+
const alreadyWarnedSymbols: Set<AstSymbol> = new Set<AstSymbol>();
15+
16+
for (const entity of collector.entities) {
17+
if (entity.astEntity instanceof AstSymbol) {
18+
if (entity.exported) {
19+
entity.astEntity.forEachDeclarationRecursive((astDeclaration: AstDeclaration) => {
20+
VisibilityChecker._checkReferences(collector, astDeclaration, alreadyWarnedSymbols);
21+
});
22+
23+
}
24+
}
25+
}
26+
}
27+
28+
private static _checkReferences(collector: Collector, astDeclaration: AstDeclaration,
29+
alreadyWarnedSymbols: Set<AstSymbol>): void {
30+
31+
for (const referencedEntity of astDeclaration.referencedAstEntities) {
32+
33+
if (referencedEntity instanceof AstSymbol) {
34+
if (!referencedEntity.isExternal) {
35+
const collectorEntity: CollectorEntity | undefined = collector.tryGetCollectorEntity(referencedEntity);
36+
37+
if (collectorEntity && collectorEntity.exported) {
38+
// const metadata: SymbolMetadata = collector.fetchMetadata(referencedEntity);
39+
return;
40+
} else {
41+
const entryPointFilename: string = path.basename(collector.workingPackage.entryPointSourceFile.fileName);
42+
43+
if (!alreadyWarnedSymbols.has(referencedEntity)) {
44+
alreadyWarnedSymbols.add(referencedEntity);
45+
46+
// The main usage scenario for ECMAScript symbols is to attach private data to a JavaScript object,
47+
// so as a special case, we do NOT report them as forgotten exports.
48+
if (!VisibilityChecker._isEcmaScriptSymbol(referencedEntity)) {
49+
50+
collector.messageRouter.addAnalyzerIssue(ExtractorMessageId.ForgottenExport,
51+
`The symbol ${referencedEntity.localName} needs to be exported`
52+
+ ` from the entry point ${entryPointFilename}`,
53+
astDeclaration);
54+
}
55+
56+
}
57+
58+
}
59+
}
60+
}
61+
}
62+
}
63+
64+
// Detect an AstSymbol that refers to an ECMAScript symbol declaration such as:
65+
//
66+
// const mySymbol: unique symbol = Symbol('mySymbol');
67+
private static _isEcmaScriptSymbol(astSymbol: AstSymbol): boolean {
68+
if (astSymbol.astDeclarations.length !== 1) {
69+
return false;
70+
}
71+
72+
// We are matching a form like this:
73+
//
74+
// - VariableDeclaration:
75+
// - Identifier: pre=[mySymbol]
76+
// - ColonToken: pre=[:] sep=[ ]
77+
// - TypeOperator:
78+
// - UniqueKeyword: pre=[unique] sep=[ ]
79+
// - SymbolKeyword: pre=[symbol]
80+
const astDeclaration: AstDeclaration = astSymbol.astDeclarations[0];
81+
if (ts.isVariableDeclaration(astDeclaration.declaration)) {
82+
const variableTypeNode: ts.TypeNode | undefined = astDeclaration.declaration.type;
83+
if (variableTypeNode) {
84+
for (const token of variableTypeNode.getChildren()) {
85+
if (token.kind === ts.SyntaxKind.SymbolKeyword) {
86+
return true;
87+
}
88+
}
89+
}
90+
}
91+
92+
return false;
93+
}
94+
95+
}

‎apps/api-extractor/src/schemas/api-extractor-defaults.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
"extractorMessageReporting": {
5252
"default": {
5353
"logLevel": "warning"
54+
},
55+
"ae-forgotten-export": {
56+
"logLevel": "warning",
57+
"addToApiReviewFile": true
5458
}
5559
},
5660
"tsdocMessageReporting": {

‎build-tests/api-extractor-lib1-test/etc/api-extractor-lib1-test.api.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Warning: (ae-forgotten-export) The symbol Lib1ForgottenExport needs to be exported from the entry point index.d.ts
2+
//
13
// @public (undocumented)
24
declare class Lib1Class extends Lib1ForgottenExport {
35
}

‎build-tests/api-extractor-scenarios/etc/test-outputs/ambientNameConflict/api-extractor-scenarios.api.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Warning: (ae-forgotten-export) The symbol Promise needs to be exported from the entry point index.d.ts
2+
//
13
// @public (undocumented)
24
declare function ambientNameConflict(p1: Promise<void>, p2: Promise_2<void>): void;
35

‎build-tests/api-extractor-scenarios/etc/test-outputs/typeOf/api-extractor-scenarios.api.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @public
22
declare function f(): typeof Lib1Class | undefined;
33

4+
// Warning: (ae-forgotten-export) The symbol ForgottenExport needs to be exported from the entry point index.d.ts
5+
//
46
// @public
57
declare function g(): typeof ForgottenExport | undefined;
68

‎build-tests/api-extractor-test-01/etc/api-extractor-test-01.api.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,24 @@ declare class DefaultExportEdgeCase {
8181

8282
// @public (undocumented)
8383
declare class ForgottenExportConsumer1 {
84+
// Warning: (ae-forgotten-export) The symbol IForgottenExport needs to be exported from the entry point index.d.ts
85+
//
8486
// (undocumented)
8587
test1(): IForgottenExport | undefined;
8688
}
8789

8890
// @public (undocumented)
8991
declare class ForgottenExportConsumer2 {
92+
// Warning: (ae-forgotten-export) The symbol IForgottenExport needs to be exported from the entry point index.d.ts
93+
//
9094
// (undocumented)
9195
test2(): IForgottenExport_2 | undefined;
9296
}
9397

9498
// @beta
9599
declare class ForgottenExportConsumer3 {
100+
// Warning: (ae-forgotten-export) The symbol IForgottenDirectDependency needs to be exported from the entry point index.d.ts
101+
//
96102
// (undocumented)
97103
test2(): IForgottenDirectDependency | undefined;
98104
}

‎build-tests/api-extractor-test-04/etc/api-extractor-test-04.api.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ declare namespace EntangledNamespace {
4444
// @internal
4545
class _ClassY {
4646
b: EntangledNamespace.N2.ClassX;
47+
// Warning: (ae-forgotten-export) The symbol N2 needs to be exported from the entry point index.d.ts
4748
c(): typeof N2.ClassX.a;
4849
}
4950
}

0 commit comments

Comments
 (0)