Skip to content

Commit b2899ef

Browse files
committed
Extracting ApiItem.followAliases() into TypeScriptHelpers, so it can be used by DtsGenerator
1 parent 457288f commit b2899ef

5 files changed

Lines changed: 41 additions & 40 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ 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+
3158
/**
3259
* Returns the Symbol for the provided Declaration. This is a workaround for a missing
3360
* 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;

0 commit comments

Comments
 (0)