Skip to content

Commit b558d5a

Browse files
authored
Merge pull request microsoft#448 from Microsoft/pgonzal/dts-generator-try3
api-extractor initial support for generating *.d.ts files
2 parents 457288f + f165fd8 commit b558d5a

26 files changed

Lines changed: 895 additions & 69 deletions

‎.pullapprove.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ groups:
3636
conditions:
3737
files:
3838
- "apps/*"
39-
- "libraries/*"
39+
- "build-tests/*"
4040
- "core-build/*"
41+
- "libraries/*"
4142
- "webpack/*"
4243
required: 1
4344
users:

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

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

44
// NOTE: THIS SOURCE FILE IS FOR DEBUGGING PURPOSES ONLY.
5-
// IT IS INVOKED BY THE "Run.cmd" AND "Debug.cmd" BATCH FILES.
5+
// IT IS INVOKED BY THE 'Run.cmd' AND 'Debug.cmd' BATCH FILES.
66

77
import { Extractor } from './extractor/Extractor';
8+
import * as path from 'path';
89

910
const extractor: Extractor = new Extractor(
1011
{
1112
compiler: {
1213
configType: 'tsconfig',
13-
rootFolder: '.'
14+
overrideTsconfig: {
15+
'compilerOptions': {
16+
'target': 'es6',
17+
'forceConsistentCasingInFileNames': true,
18+
'module': 'commonjs',
19+
'declaration': true,
20+
'sourceMap': true,
21+
'experimentalDecorators': true,
22+
'types': [
23+
'node'
24+
],
25+
'lib': [
26+
'es5',
27+
'scripthost',
28+
'es2015.collection',
29+
'es2015.promise',
30+
'es2015.iterable',
31+
'dom'
32+
],
33+
'strictNullChecks': true
34+
},
35+
'include': [ 'lib/**/*.d.ts' ]
36+
},
37+
rootFolder: '../../libraries/node-core-library'
1438
},
1539
project: {
16-
entryPointSourceFile: 'src/index.ts',
17-
externalJsonFileFolders: ['./testInputs/external-api-json' ]
40+
entryPointSourceFile: 'lib/index.d.ts',
41+
externalJsonFileFolders: [ ]
1842
},
1943
apiReviewFile: {
20-
enabled: true,
21-
apiReviewFolder: __dirname
44+
enabled: false,
45+
apiReviewFolder: path.join(__dirname, 'debug')
2246
},
2347
apiJsonFile: {
24-
enabled: true
48+
enabled: true,
49+
outputFolder: path.join(__dirname, 'debug')
50+
},
51+
packageTypings: {
52+
enabled: true,
53+
outputFolder: path.join(__dirname, 'debug')
2554
}
2655
}
2756
);
2857

58+
console.log('CONFIG:' + JSON.stringify(extractor.actualConfig, undefined, ' '));
59+
2960
if (!extractor.processProject()) {
3061
console.log('processProject() failed the build');
3162
} else {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@ export default class TypeScriptHelpers {
2828
*/
2929
public static jsdocIntermediateRegEx: RegExp = /^\s*[*]\s?/g;
3030

31+
/**
32+
* This traverses any type aliases to find the original place where an item was defined.
33+
* For example, suppose a class is defined as "export default class MyClass { }"
34+
* but exported from the package's index.ts like this:
35+
*
36+
* export { default as _MyClass } from './MyClass';
37+
*
38+
* In this example, calling followAliases() on the _MyClass symbol will return the
39+
* original definition of MyClass, traversing any intermediary places where the
40+
* symbol was imported and re-exported.
41+
*/
42+
public static followAliases(symbol: ts.Symbol, typeChecker: ts.TypeChecker): ts.Symbol {
43+
let current: ts.Symbol = symbol;
44+
while (true) { // tslint:disable-line:no-constant-condition
45+
if (!(current.flags & ts.SymbolFlags.Alias)) {
46+
break;
47+
}
48+
const currentAlias: ts.Symbol = typeChecker.getAliasedSymbol(current);
49+
if (!currentAlias || currentAlias === current) {
50+
break;
51+
}
52+
current = currentAlias;
53+
}
54+
55+
return current;
56+
}
57+
58+
public static getImmediateAliasedSymbol(symbol: ts.Symbol, typeChecker: ts.TypeChecker): ts.Symbol {
59+
return (typeChecker as any).getImmediateAliasedSymbol(symbol); // tslint:disable-line:no-any
60+
}
61+
3162
/**
3263
* Returns the Symbol for the provided Declaration. This is a workaround for a missing
3364
* feature of the TypeScript Compiler API. It is the only apparent way to reach

‎apps/api-extractor/src/ast/AstItem.ts‎

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -417,33 +417,6 @@ abstract class AstItem {
417417
return false;
418418
}
419419

420-
/**
421-
* This traverses any type aliases to find the original place where an item was defined.
422-
* For example, suppose a class is defined as "export default class MyClass { }"
423-
* but exported from the package's index.ts like this:
424-
*
425-
* export { default as _MyClass } from './MyClass';
426-
*
427-
* In this example, calling followAliases() on the _MyClass symbol will return the
428-
* original definition of MyClass, traversing any intermediary places where the
429-
* symbol was imported and re-exported.
430-
*/
431-
protected followAliases(symbol: ts.Symbol): ts.Symbol {
432-
let current: ts.Symbol = symbol;
433-
while (true) {
434-
if (!(current.flags & ts.SymbolFlags.Alias)) {
435-
break;
436-
}
437-
const currentAlias: ts.Symbol = this.typeChecker.getAliasedSymbol(current);
438-
if (!currentAlias || currentAlias === current) {
439-
break;
440-
}
441-
current = currentAlias;
442-
}
443-
444-
return current;
445-
}
446-
447420
/**
448421
* Reports an error through the ApiErrorHandler interface that was registered with the Extractor,
449422
* adding the filename and line number information for the declaration of this AstItem.
@@ -580,7 +553,7 @@ abstract class AstItem {
580553
}
581554

582555
// Follow the aliases all the way to the ending SourceFile
583-
const currentSymbol: ts.Symbol = this.followAliases(symbol);
556+
const currentSymbol: ts.Symbol = TypeScriptHelpers.followAliases(symbol, this.typeChecker);
584557

585558
if (!currentSymbol.declarations || !currentSymbol.declarations.length) {
586559
// This is a degenerate case that happens sometimes

‎apps/api-extractor/src/ast/AstModule.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as ts from 'typescript';
77
import { IAstItemOptions } from './AstItem';
88
import AstItemContainer from './AstItemContainer';
9+
import TypeScriptHelpers from '../TypeScriptHelpers';
910
import AstStructuredType from './AstStructuredType';
1011
import AstEnum from './AstEnum';
1112
import AstFunction from './AstFunction';
@@ -16,7 +17,7 @@ import AstFunction from './AstFunction';
1617
abstract class AstModule extends AstItemContainer {
1718

1819
protected processModuleExport(exportSymbol: ts.Symbol): void {
19-
const followedSymbol: ts.Symbol = this.followAliases(exportSymbol);
20+
const followedSymbol: ts.Symbol = TypeScriptHelpers.followAliases(exportSymbol, this.typeChecker);
2021

2122
if (!followedSymbol.declarations) {
2223
// This is an API Extractor bug, but it could happen e.g. if we upgrade to a new

‎apps/api-extractor/src/ast/AstNamespace.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import AstModuleVariable from './AstModuleVariable';
88
import { AstItemKind, IAstItemOptions } from './AstItem';
99
import { IExportedSymbol } from './IExportedSymbol';
1010
import AstModule from './AstModule';
11+
import TypeScriptHelpers from '../TypeScriptHelpers';
1112

1213
const allowedTypes: string[] = ['string', 'number', 'boolean'];
1314

@@ -45,7 +46,7 @@ export default class AstNamespace extends AstModule {
4546
// Used when policies.namespaceSupport=conservative
4647
private _processConservativeMembers(exportSymbols: ts.Symbol[]): void {
4748
for (const exportSymbol of exportSymbols) {
48-
const followedSymbol: ts.Symbol = this.followAliases(exportSymbol);
49+
const followedSymbol: ts.Symbol = TypeScriptHelpers.followAliases(exportSymbol, this.typeChecker);
4950

5051
if (!followedSymbol.declarations) {
5152
// This is an API Extractor bug, but it could happen e.g. if we upgrade to a new

‎apps/api-extractor/src/ast/AstPackage.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ export default class AstPackage extends AstModule {
5151
// The scoped package name. (E.g. "@microsoft/api-extractor")
5252
this.name = context.packageName;
5353

54-
const exportSymbols: ts.Symbol[] = this.typeChecker.getExportsOfModule(this.declarationSymbol);
55-
if (exportSymbols) {
56-
for (const exportSymbol of exportSymbols) {
54+
const exportSymbols: ts.Symbol[] = this.typeChecker.getExportsOfModule(this.declarationSymbol) || [];
55+
56+
for (const exportSymbol of exportSymbols) {
5757
this.processModuleExport(exportSymbol);
5858

59-
const followedSymbol: ts.Symbol = this.followAliases(exportSymbol);
60-
this._exportedNormalizedSymbols.push({
61-
exportedName: exportSymbol.name,
62-
followedSymbol: followedSymbol
63-
});
64-
}
59+
const followedSymbol: ts.Symbol = TypeScriptHelpers.followAliases(exportSymbol, this.typeChecker);
60+
this._exportedNormalizedSymbols.push({
61+
exportedName: exportSymbol.name,
62+
followedSymbol: followedSymbol
63+
});
6564
}
6665
}
6766

@@ -77,7 +76,7 @@ export default class AstPackage extends AstModule {
7776
* the string "MyClass".
7877
*/
7978
public tryGetExportedSymbolName(symbol: ts.Symbol): string | undefined {
80-
const followedSymbol: ts.Symbol = this.followAliases(symbol);
79+
const followedSymbol: ts.Symbol = TypeScriptHelpers.followAliases(symbol, this.typeChecker);
8180
for (const exportedSymbol of this._exportedNormalizedSymbols) {
8281
if (exportedSymbol.followedSymbol === followedSymbol) {
8382
return exportedSymbol.exportedName;

‎apps/api-extractor/src/ast/AstStructuredType.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default class AstStructuredType extends AstItemContainer {
5151
this.kind = AstItemKind.Class;
5252
}
5353

54-
for (const memberDeclaration of this._classLikeDeclaration.members) {
54+
for (const memberDeclaration of this._classLikeDeclaration.members || []) {
5555
const memberSymbol: ts.Symbol = TypeScriptHelpers.tryGetSymbolForDeclaration(memberDeclaration);
5656
if (memberSymbol) {
5757
this._processMember(memberSymbol, memberDeclaration);

0 commit comments

Comments
 (0)