Skip to content

Commit f0bfe5c

Browse files
authored
Merge pull request microsoft#560 from Microsoft/pgonzal/ae-trimming-4
[API Extractor] Groundwork for member trimming
2 parents e89aefb + be1667b commit f0bfe5c

56 files changed

Lines changed: 2116 additions & 607 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/api-extractor/.vscode/launch.json‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
87
{
98
"type": "node",
109
"request": "launch",
1110
"name": "test-01",
1211
"program": "${workspaceFolder}/lib/start.js",
1312
"cwd": "${workspaceFolder}/../../build-tests/api-extractor-test-01",
1413
"args": [
14+
"-d",
1515
"run",
1616
"-l"
1717
]
@@ -23,6 +23,19 @@
2323
"program": "${workspaceFolder}/lib/start.js",
2424
"cwd": "${workspaceFolder}/../../build-tests/api-extractor-test-02",
2525
"args": [
26+
"-d",
27+
"run",
28+
"-l"
29+
]
30+
},
31+
{
32+
"type": "node",
33+
"request": "launch",
34+
"name": "test-03",
35+
"program": "${workspaceFolder}/lib/start.js",
36+
"cwd": "${workspaceFolder}/../../build-tests/api-extractor-test-03",
37+
"args": [
38+
"-d",
2639
"run",
2740
"-l"
2841
]
@@ -34,6 +47,19 @@
3447
"program": "${workspaceFolder}/lib/start.js",
3548
"cwd": "${workspaceFolder}/../../build-tests/api-extractor-test-04",
3649
"args": [
50+
"-d",
51+
"run",
52+
"-l"
53+
]
54+
},
55+
{
56+
"type": "node",
57+
"request": "launch",
58+
"name": "scratch",
59+
"program": "${workspaceFolder}/lib/start.js",
60+
"cwd": "(your project path)",
61+
"args": [
62+
"-d",
3763
"run",
3864
"-l"
3965
]

‎apps/api-extractor/CHANGELOG.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"comments": {
6363
"patch": [
6464
{
65-
"comment": "Add preliminary support for preview and public outputs for packageTypings gtenerator"
65+
"comment": "Add preliminary support for preview and public outputs for packageTypings generator"
6666
}
6767
]
6868
}

‎apps/api-extractor/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"homepage": "http://aka.ms/extractor",
2121
"main": "lib/index.js",
2222
"typings": "dist/index-internal.d.ts",
23+
"tsdoc": {
24+
"tsdocFlavor": "AEDoc"
25+
},
2326
"bin": {
2427
"api-extractor": "./bin/api-extractor"
2528
},

‎apps/api-extractor/src/cli/ApiExtractorCommandLine.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import { CommandLineParser } from '@microsoft/ts-command-line';
4+
import * as colors from 'colors';
5+
import * as os from 'os';
6+
7+
import { CommandLineParser, CommandLineFlagParameter } from '@microsoft/ts-command-line';
58
import { RunAction } from './RunAction';
69

710
export class ApiExtractorCommandLine extends CommandLineParser {
11+
private _debugParameter: CommandLineFlagParameter;
12+
813
constructor() {
914
super({
1015
toolFilename: 'api-extractor',
@@ -14,6 +19,24 @@ export class ApiExtractorCommandLine extends CommandLineParser {
1419
}
1520

1621
protected onDefineParameters(): void { // override
22+
this._debugParameter = this.defineFlagParameter({
23+
parameterLongName: '--debug',
24+
parameterShortName: '-d',
25+
description: 'Show the full call stack if an error occurs while executing the tool'
26+
});
27+
}
28+
29+
protected onExecute(): Promise<void> { // override
30+
return super.onExecute().catch((error) => {
31+
32+
if (this._debugParameter.value) {
33+
console.error(os.EOL + error.stack);
34+
} else {
35+
console.error(os.EOL + colors.red('ERROR: ' + error.message.trim()));
36+
}
37+
38+
process.exitCode = 1;
39+
});
1740
}
1841

1942
private _populateActions(): void {

‎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);
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
import { AstSymbol } from './AstSymbol';
6+
import { Span } from '../../utils/Span';
7+
8+
/**
9+
* Constructor parameters for AstDeclaration
10+
*/
11+
export interface IAstDeclarationParameters {
12+
readonly declaration: ts.Declaration;
13+
readonly astSymbol: AstSymbol;
14+
readonly parent: AstDeclaration | undefined;
15+
}
16+
17+
/**
18+
* The AstDeclaration and AstSymbol classes are API Extractor's equivalent of the compiler's
19+
* ts.Declaration and ts.Symbol objects. They are created by the SymbolTable class.
20+
*
21+
* @remarks
22+
* The AstDeclaration represents one or more syntax components of a symbol. Usually there is
23+
* only one AstDeclaration per AstSymbol, but certain TypeScript constructs can have multiple
24+
* declarations (e.g. overloaded functions, declaration merging, etc).
25+
*
26+
* Because of this the AstDeclaration manages the parent/child nesting hierarchy (e.g. with
27+
* declaration merging, each declaration has its own children) and becomes the main focus
28+
* of analyzing AEDoc and emitting *.d.ts files.
29+
*
30+
* The AstDeclarations correspond to items from the compiler's ts.Node hierarchy, but
31+
* omitting/skipping any nodes that don't match the SymbolAnalyzer.isAstDeclaration()
32+
* criteria. This simplification makes the other API Extractor stages easier to implement.
33+
*/
34+
export class AstDeclaration {
35+
public readonly declaration: ts.Declaration;
36+
37+
public readonly astSymbol: AstSymbol;
38+
39+
/**
40+
* The parent, if this object is nested inside another AstDeclaration.
41+
*/
42+
public readonly parent: AstDeclaration | undefined;
43+
44+
private readonly _analyzedChildren: AstDeclaration[] = [];
45+
46+
private readonly _analyzedReferencedAstSymbolsSet: Set<AstSymbol> = new Set<AstSymbol>();
47+
48+
public constructor(parameters: IAstDeclarationParameters) {
49+
this.declaration = parameters.declaration;
50+
this.astSymbol = parameters.astSymbol;
51+
this.parent = parameters.parent;
52+
53+
this.astSymbol._notifyDeclarationAttach(this);
54+
55+
if (this.parent) {
56+
this.parent._notifyChildAttach(this);
57+
}
58+
}
59+
60+
/**
61+
* Returns the children for this AstDeclaration.
62+
* @remarks
63+
* The collection will be empty until AstSymbol.analyzed is true.
64+
*/
65+
public get children(): ReadonlyArray<AstDeclaration> {
66+
return this.astSymbol.analyzed ? this._analyzedChildren : [];
67+
}
68+
69+
/**
70+
* Returns the AstSymbols referenced by this node.
71+
* @remarks
72+
* NOTE: The collection will be empty until AstSymbol.analyzed is true.
73+
*
74+
* Since we assume references are always collected by a traversal starting at the
75+
* root of the nesting declarations, this array omits the following items because they
76+
* would be redundant:
77+
* - symbols corresponding to parents of this declaration (e.g. a method that returns its own class)
78+
* - symbols already listed in the referencedAstSymbols property for parents of this declaration
79+
* (e.g. a method that returns its own class's base class)
80+
* - symbols that are referenced only by nested children of this declaration
81+
* (e.g. if a method returns an enum, this doesn't imply that the method's class references that enum)
82+
*/
83+
public get referencedAstSymbols(): ReadonlyArray<AstSymbol> {
84+
return this.astSymbol.analyzed ? [...this._analyzedReferencedAstSymbolsSet] : [];
85+
}
86+
87+
/**
88+
* This is an internal callback used when the SymbolTable attaches a new
89+
* child AstDeclaration to this object.
90+
* @internal
91+
*/
92+
public _notifyChildAttach(child: AstDeclaration): void {
93+
if (child.parent !== this) {
94+
throw new Error('Program Bug: Invalid call to notifyChildAttach()');
95+
}
96+
97+
this._analyzedChildren.push(child);
98+
}
99+
100+
/**
101+
* Returns a diagnostic dump of the tree, which reports the hierarchy of
102+
* AstDefinition objects.
103+
*/
104+
public getDump(indent: string = ''): string {
105+
const declarationKind: string = ts.SyntaxKind[this.declaration.kind];
106+
let result: string = indent + `+ ${this.astSymbol.localName} (${declarationKind})`;
107+
if (this.astSymbol.nominal) {
108+
result += ' (nominal)';
109+
}
110+
result += '\n';
111+
112+
for (const referencedAstSymbol of this._analyzedReferencedAstSymbolsSet.values()) {
113+
result += indent + ` ref: ${referencedAstSymbol.localName}\n`;
114+
}
115+
116+
for (const child of this.children) {
117+
result += child.getDump(indent + ' ');
118+
}
119+
120+
return result;
121+
}
122+
123+
/**
124+
* Returns a diagnostic dump using Span.getDump(), which reports the detailed
125+
* compiler structure.
126+
*/
127+
public getSpanDump(indent: string = ''): string {
128+
const span: Span = new Span(this.declaration);
129+
return span.getDump(indent);
130+
}
131+
132+
/**
133+
* This is an internal callback used when SymbolTable.analyze() discovers a new
134+
* type reference associated with this declaration.
135+
* @internal
136+
*/
137+
public _notifyReferencedAstSymbol(referencedAstSymbol: AstSymbol): void {
138+
if (this.astSymbol.analyzed) {
139+
throw new Error('Program Bug: notifyReferencedAstSymbol() called after analysis is already complete');
140+
}
141+
142+
for (let current: AstDeclaration | undefined = this; current; current = current.parent) {
143+
// Don't add references to symbols that are already referenced by a parent
144+
if (current._analyzedReferencedAstSymbolsSet.has(referencedAstSymbol)) {
145+
return;
146+
}
147+
// Don't add the symbols of parents either
148+
if (referencedAstSymbol === current.astSymbol) {
149+
return;
150+
}
151+
}
152+
153+
this._analyzedReferencedAstSymbolsSet.add(referencedAstSymbol);
154+
}
155+
156+
/**
157+
* Visits all the current declaration and all children recursively in a depth-first traversal,
158+
* and performs the specified action for each one.
159+
*/
160+
public forEachDeclarationRecursive(action: (astDeclaration: AstDeclaration) => void): void {
161+
action(this);
162+
for (const child of this.children) {
163+
child.forEachDeclarationRecursive(action);
164+
}
165+
}
166+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 { AstSymbol } from './AstSymbol';
5+
6+
/**
7+
* Constructor parameters for AstEntryPoint
8+
*/
9+
export interface IExportedMember {
10+
readonly name: string;
11+
readonly astSymbol: AstSymbol;
12+
}
13+
14+
export interface IAstEntryPointParameters {
15+
readonly exportedMembers: ReadonlyArray<IExportedMember>;
16+
}
17+
18+
/**
19+
* This class is used by AstSymbolTable to return an entry point.
20+
* (If AstDeclaration could be used to represent a ts.SyntaxKind.SourceFile node,
21+
* then this class would not be needed.)
22+
*/
23+
export class AstEntryPoint {
24+
public readonly exportedMembers: ReadonlyArray<IExportedMember>;
25+
26+
public constructor(parameters: IAstEntryPointParameters) {
27+
this.exportedMembers = parameters.exportedMembers;
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
/**
5+
* Constructor parameters for AstImport
6+
*/
7+
export interface IAstImportParameters {
8+
readonly modulePath: string;
9+
readonly exportName: string;
10+
}
11+
12+
/**
13+
* For a symbol that was imported from an external package, this tracks the import
14+
* statement that was used to reach it.
15+
*/
16+
export class AstImport {
17+
/**
18+
* The name of the external package (and possibly module path) that this definition
19+
* was imported from. If it was defined in the referencing source file, or if it was
20+
* imported from a local file, or if it is an ambient definition, then externalPackageName
21+
* will be undefined.
22+
*
23+
* Example: "@microsoft/gulp-core-build/lib/IBuildConfig"
24+
*/
25+
public readonly modulePath: string;
26+
27+
/**
28+
* If importPackagePath is defined, then this specifies the export name for the definition.
29+
*
30+
* Example: "IBuildConfig"
31+
*/
32+
public readonly exportName: string;
33+
34+
/**
35+
* If importPackagePath and importPackageExportName are defined, then this is a dictionary key
36+
* that combines them with a colon (":").
37+
*
38+
* Example: "@microsoft/gulp-core-build/lib/IBuildConfig:IBuildConfig"
39+
*/
40+
public readonly key: string;
41+
42+
public constructor(parameters: IAstImportParameters) {
43+
this.modulePath = parameters.modulePath;
44+
this.exportName = parameters.exportName;
45+
46+
this.key = `${this.modulePath}:${this.exportName}`;
47+
}
48+
}

0 commit comments

Comments
 (0)