Skip to content

Commit bcc4a85

Browse files
committed
Started work on type collector
1 parent 39a81f2 commit bcc4a85

1 file changed

Lines changed: 61 additions & 60 deletions

File tree

‎apps/api-extractor/src/generators/DtsGenerator.ts‎

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import * as fs from 'fs';
77
import * as ts from 'typescript';
8-
import * as dts from '../dts-dom';
98

109
import Extractor from '../Extractor';
1110
import IndentedWriter from '../IndentedWriter';
@@ -16,7 +15,8 @@ import { Span } from './Span';
1615
class Entry {
1716
public localName: string;
1817
public followedSymbol: ts.Symbol;
19-
public dtsDeclarations: dts.TopLevelDeclaration[] = [];
18+
public exported: boolean = false;
19+
2020
private _sortKey: string|undefined = undefined;
2121

2222
public getSortKey(): string {
@@ -34,6 +34,11 @@ class Entry {
3434
}
3535
}
3636

37+
interface IFollowAliasesResult {
38+
symbol: ts.Symbol;
39+
external: boolean;
40+
}
41+
3742
export default class DtsGenerator {
3843
private _extractor: Extractor;
3944
private _typeChecker: ts.TypeChecker;
@@ -62,7 +67,7 @@ export default class DtsGenerator {
6267
return current as T;
6368
}
6469

65-
private static _followAliases(symbol: ts.Symbol, typeChecker: ts.TypeChecker): ts.Symbol {
70+
private static _followAliases(symbol: ts.Symbol, typeChecker: ts.TypeChecker): IFollowAliasesResult {
6671
let current: ts.Symbol = symbol;
6772

6873
while (true) { // tslint:disable-line:no-constant-condition
@@ -88,15 +93,15 @@ export default class DtsGenerator {
8893
// Does it start with something like "'./"?
8994
// If not, then assume it's an import from an external package
9095
if (!/^['"\s]+\.[\/\\]/.test(moduleSpecifier)) {
91-
break;
96+
return { symbol: current, external: true };
9297
}
9398
}
9499
}
95100

96101
current = currentAlias;
97102
}
98103

99-
return current;
104+
return { symbol: current, external: false };
100105
}
101106

102107
public constructor(extractor: Extractor) {
@@ -119,20 +124,23 @@ export default class DtsGenerator {
119124
const packageSymbol: ts.Symbol = this._extractor.package.getDeclarationSymbol();
120125

121126
const exportSymbols: ts.Symbol[] = this._typeChecker.getExportsOfModule(packageSymbol) || [];
127+
122128
for (const exportSymbol of exportSymbols) {
129+
const entry: Entry | undefined = this._fetchEntryForSymbol(exportSymbol);
123130

124-
const entry: Entry = this._fetchEntryForSymbol(exportSymbol);
125-
for (const dtsDeclaration of entry.dtsDeclarations) {
126-
dtsDeclaration.flags |= dts.DeclarationFlags.Export;
131+
if (!entry) {
132+
throw new Error('Unsupported export type');
127133
}
134+
135+
entry.exported = true;
128136
}
129137

130138
this._entries.sort((a, b) => a.getSortKey().localeCompare(b.getSortKey()));
131139

132140
let content: string = '';
133141
for (const entry of this._entries) {
134-
for (const dtsDeclaration of entry.dtsDeclarations) {
135-
content += dts.emit(dtsDeclaration);
142+
for (const declaration of entry.followedSymbol.declarations) {
143+
content += declaration.getText() + '\n';
136144
}
137145
}
138146

@@ -141,79 +149,72 @@ export default class DtsGenerator {
141149
return fileContent;
142150
}
143151

144-
private _fetchEntryForSymbol(symbol: ts.Symbol): Entry {
145-
const followedSymbol: ts.Symbol = DtsGenerator._followAliases(symbol, this._typeChecker);
152+
private _fetchEntryForSymbol(symbol: ts.Symbol): Entry | undefined {
153+
const result: IFollowAliasesResult = DtsGenerator._followAliases(symbol, this._typeChecker);
154+
if (result.external) {
155+
return; // external definition
156+
}
157+
158+
const followedSymbol: ts.Symbol = result.symbol;
159+
if (followedSymbol.flags & (
160+
ts.SymbolFlags.TypeParameter | ts.SymbolFlags.TypeLiteral
161+
)) {
162+
return undefined;
163+
}
146164

147-
let entry: Entry = this._entriesBySymbol.get(followedSymbol);
165+
let entry: Entry = this._entriesBySymbol.get(result.symbol);
148166
if (entry) {
149167
return entry;
150168
}
151169

152-
entry = this._createEntry(symbol.name, followedSymbol);
170+
entry = this._createEntry(symbol.name, result.symbol);
153171

154-
for (const declaration of followedSymbol.declarations) {
155-
console.log(PrettyPrinter.dumpTree(declaration));
172+
for (const declaration of result.symbol.declarations) {
173+
// console.log(PrettyPrinter.dumpTree(declaration));
156174
console.log('-------------------------------------');
157175
const span: Span = new Span(declaration);
158176
console.log(span.getText());
159177
console.log('=====================================');
160178

161-
switch (declaration.kind) {
162-
case ts.SyntaxKind.ClassDeclaration:
163-
this._processClassDeclaration(entry, declaration as ts.ClassDeclaration);
164-
break;
165-
case ts.SyntaxKind.InterfaceDeclaration:
166-
this._processInterfaceDeclaration(entry, declaration as ts.InterfaceDeclaration);
167-
break;
168-
}
169-
179+
this._collectTypes(declaration);
170180
}
171181

172182
return entry;
173183
}
174184

175-
private _processClassDeclaration(entry: Entry, declaration: ts.ClassDeclaration): void {
176-
const classDts: dts.ClassDeclaration = dts.create.class(entry.localName);
177-
entry.dtsDeclarations.push(classDts);
178-
}
179-
180-
private _processInterfaceDeclaration(entry: Entry, declaration: ts.InterfaceDeclaration): void {
181-
const interfaceDts: dts.InterfaceDeclaration = dts.create.interface(entry.localName);
182-
entry.dtsDeclarations.push(interfaceDts);
183-
184-
for (const heritageClause of declaration.heritageClauses || []) {
185-
for (const baseTypeDeclaration of heritageClause.types || []) {
186-
const baseType: ts.TypeReference = this._typeChecker.getTypeAtLocation(
187-
baseTypeDeclaration) as ts.TypeReference;
188-
const baseDts: dts.NamedTypeReference = this._getTypeReferenceDts(baseType);
189-
interfaceDts.baseTypes.push(baseDts);
190-
}
185+
private _collectTypes(node: ts.Node): void {
186+
switch (node.kind) {
187+
case ts.SyntaxKind.Block:
188+
// Don't traverse into code
189+
return;
190+
191+
case ts.SyntaxKind.Identifier:
192+
if (node.parent) {
193+
switch (node.parent.kind) {
194+
case ts.SyntaxKind.ExpressionWithTypeArguments:
195+
case ts.SyntaxKind.TypeReference:
196+
this._processTypeReference(node);
197+
break;
198+
}
199+
}
200+
return;
201+
}
202+
for (const child of node.getChildren() || []) {
203+
this._collectTypes(child);
191204
}
192205
}
193206

194-
private _getTypeReferenceDts(typeReference: ts.TypeReference): dts.NamedTypeReference {
195-
if (!typeReference.symbol) {
196-
const intrinsicName: string = typeReference['intrinsicName']; // tslint:disable-line:no-string-literal
197-
if (intrinsicName) {
198-
// It is a simple primitive type
199-
return dts.create.namedTypeReference(intrinsicName);
200-
} else {
201-
throw new Error('Unimplemented type reference:\r\n' + this._typeChecker.typeToString(typeReference));
202-
}
203-
} else {
204-
const entry: Entry = this._fetchEntryForSymbol(typeReference.symbol);
205-
const referenceDts: dts.NamedTypeReference = dts.create.namedTypeReference(entry.localName);
206-
207-
for (const argument of typeReference.typeArguments || []) {
208-
const renderedArgument: dts.NamedTypeReference = this._getTypeReferenceDts(argument as ts.TypeReference);
209-
referenceDts.typeArguments.push(renderedArgument);
210-
}
211-
212-
return referenceDts;
207+
private _processTypeReference(identifier: ts.Node): void {
208+
const symbol: ts.Symbol = this._typeChecker.getSymbolAtLocation(identifier);
209+
if (!symbol) {
210+
throw new Error('Symbol not found');
213211
}
212+
213+
this._fetchEntryForSymbol(symbol);
214214
}
215215

216216
private _createEntry(localName: string, followedSymbol: ts.Symbol): Entry {
217+
console.log('======> ' + localName);
217218
const entry: Entry = new Entry();
218219
entry.localName = localName;
219220
entry.followedSymbol = followedSymbol;

0 commit comments

Comments
 (0)