Skip to content

Commit 0126ea7

Browse files
committed
Based on PR feedback, moved isAstDeclaration() into SymbolAnalyzer alongside isExportableAstDeclaration(). Added some missing edge cases.
1 parent ded1b97 commit 0126ea7

4 files changed

Lines changed: 59 additions & 46 deletions

File tree

apps/api-extractor/src/cli/RunAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class RunAction extends CommandLineAction {
6363
// Otherwise, figure out which project we're in and look for the config file
6464
// at the project root
6565
const lookup: PackageJsonLookup = new PackageJsonLookup();
66-
const packageFolder: string|undefined = lookup.tryGetPackageFolderFor('.');
66+
const packageFolder: string | undefined = lookup.tryGetPackageFolderFor('.');
6767

6868
if (packageFolder) {
6969
configFilename = path.join(packageFolder, AE_CONFIG_FILENAME);

apps/api-extractor/src/generators/packageTypings/AstSymbolTable.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,34 +98,6 @@ export class AstSymbolTable {
9898
astSymbol.notifyAnalyzed();
9999
}
100100

101-
/**
102-
* This function determines which node types will generate an AstDeclaration.
103-
*/
104-
public isAstDeclaration(node: ts.Node): boolean {
105-
// (alphabetical order)
106-
switch (node.kind) {
107-
case ts.SyntaxKind.ClassDeclaration:
108-
case ts.SyntaxKind.EnumDeclaration:
109-
case ts.SyntaxKind.EnumMember:
110-
case ts.SyntaxKind.FunctionDeclaration:
111-
case ts.SyntaxKind.InterfaceDeclaration:
112-
case ts.SyntaxKind.MethodDeclaration:
113-
case ts.SyntaxKind.MethodSignature:
114-
115-
// ModuleDeclaration is used for both "module" and "namespace" declarations
116-
case ts.SyntaxKind.ModuleDeclaration:
117-
case ts.SyntaxKind.PropertyDeclaration:
118-
case ts.SyntaxKind.PropertySignature:
119-
120-
// SourceFile is used for "import * as file from 'file';"
121-
case ts.SyntaxKind.SourceFile:
122-
case ts.SyntaxKind.TypeAliasDeclaration:
123-
case ts.SyntaxKind.VariableDeclaration:
124-
return true;
125-
}
126-
return false;
127-
}
128-
129101
public tryGetAstSymbol(symbol: ts.Symbol): AstSymbol | undefined {
130102
return this._fetchAstSymbol(symbol, false);
131103
}
@@ -213,7 +185,7 @@ export class AstSymbolTable {
213185
}
214186

215187
private _fetchAstSymbolForNode(node: ts.Node): AstSymbol | undefined {
216-
if (!this.isAstDeclaration(node)) {
188+
if (!SymbolAnalyzer.isAstDeclaration(node.kind)) {
217189
return undefined;
218190
}
219191

@@ -245,7 +217,7 @@ export class AstSymbolTable {
245217
}
246218

247219
for (const declaration of followedSymbol.declarations || []) {
248-
if (!this.isAstDeclaration(declaration)) {
220+
if (!SymbolAnalyzer.isAstDeclaration(declaration.kind)) {
249221
throw new Error(`Program Bug: The "${followedSymbol.name}" symbol uses the construct`
250222
+ ` "${ts.SyntaxKind[declaration.kind]}" which may be an unimplemented language feature`);
251223
}
@@ -328,7 +300,7 @@ export class AstSymbolTable {
328300
private _tryFindFirstAstDeclarationParent(node: ts.Node): ts.Node | undefined {
329301
let currentNode: ts.Node | undefined = node.parent;
330302
while (currentNode) {
331-
if (this.isAstDeclaration(currentNode)) {
303+
if (SymbolAnalyzer.isAstDeclaration(currentNode.kind)) {
332304
return currentNode;
333305
}
334306
currentNode = currentNode.parent;

apps/api-extractor/src/generators/packageTypings/DtsEntry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class DtsEntry {
1919

2020
private _nameForEmit: string | undefined = undefined;
2121

22-
private _sortKey: string|undefined = undefined;
22+
private _sortKey: string | undefined = undefined;
2323

2424
public constructor(parameters: IDtsEntryParameters) {
2525
this.astSymbol = parameters.astSymbol;

apps/api-extractor/src/generators/packageTypings/SymbolAnalyzer.ts

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,55 @@ export interface IFollowAliasesResult {
3434
}
3535

3636
export class SymbolAnalyzer {
37+
38+
/**
39+
* This function determines which ts.Node kinds will generate an AstDeclaration.
40+
* These correspond to the definitions that we can add AEDoc to.
41+
*/
42+
public static isAstDeclaration(kind: ts.SyntaxKind): boolean {
43+
// (alphabetical order)
44+
switch (kind) {
45+
case ts.SyntaxKind.ClassDeclaration:
46+
case ts.SyntaxKind.EnumDeclaration:
47+
case ts.SyntaxKind.EnumMember:
48+
case ts.SyntaxKind.FunctionDeclaration:
49+
case ts.SyntaxKind.InterfaceDeclaration:
50+
case ts.SyntaxKind.MethodDeclaration:
51+
case ts.SyntaxKind.MethodSignature:
52+
53+
// ModuleDeclaration is used for both "module" and "namespace" declarations
54+
case ts.SyntaxKind.ModuleDeclaration:
55+
case ts.SyntaxKind.PropertyDeclaration:
56+
case ts.SyntaxKind.PropertySignature:
57+
58+
// SourceFile is used for "import * as file from 'file';"
59+
case ts.SyntaxKind.SourceFile:
60+
case ts.SyntaxKind.TypeAliasDeclaration:
61+
case ts.SyntaxKind.VariableDeclaration:
62+
return true;
63+
}
64+
return false;
65+
}
66+
67+
/**
68+
* This function detects the subset of isAstDeclaration() items that can use
69+
* the "export" keyword. This is part of the heuristic for recognizing ambient types.
70+
*/
71+
public static isExportableAstDeclaration(kind: ts.SyntaxKind): boolean {
72+
// (alphabetical order)
73+
switch (kind) {
74+
case ts.SyntaxKind.ClassDeclaration:
75+
case ts.SyntaxKind.EnumDeclaration:
76+
case ts.SyntaxKind.FunctionDeclaration:
77+
case ts.SyntaxKind.InterfaceDeclaration:
78+
case ts.SyntaxKind.ModuleDeclaration:
79+
case ts.SyntaxKind.TypeAliasDeclaration:
80+
case ts.SyntaxKind.VariableDeclaration:
81+
return true;
82+
}
83+
return false;
84+
}
85+
3786
/**
3887
* For the given symbol, follow imports and type alias to find the symbol that represents
3988
* the original definition.
@@ -96,19 +145,11 @@ export class SymbolAnalyzer {
96145
// Is the followedSymbol actually the kind of thing that can be ambient?
97146
if (isAmbient) {
98147
for (const declaration of current.declarations || []) {
99-
switch (declaration.kind) {
100-
case ts.SyntaxKind.ClassDeclaration:
101-
case ts.SyntaxKind.InterfaceDeclaration:
102-
case ts.SyntaxKind.FunctionDeclaration:
103-
case ts.SyntaxKind.ModuleDeclaration:
104-
case ts.SyntaxKind.VariableDeclaration:
105-
// These actually need "export" keywords
106-
break;
107-
default:
108-
// Everything else we assume is some kind of nested declaration that
109-
// doesn't need it.
110-
isAmbient = false;
111-
break;
148+
// These actually need "export" keywords
149+
if (!SymbolAnalyzer.isExportableAstDeclaration(declaration.kind)) {
150+
// Everything else we assume is some kind of nested declaration that
151+
// doesn't need it.
152+
isAmbient = false;
112153
}
113154
}
114155
}

0 commit comments

Comments
 (0)