Skip to content

Commit 040152d

Browse files
committed
Added documentation, removed some diagnostic logging
1 parent 57e6c0c commit 040152d

1 file changed

Lines changed: 62 additions & 4 deletions

File tree

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

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,31 @@ import IndentedWriter from '../IndentedWriter';
1111
import TypeScriptHelpers from '../TypeScriptHelpers';
1212
import { Span } from './Span';
1313

14+
/**
15+
* An "Entry" is a type definition that we encounter while traversing the
16+
* references from the package entry point. This data structure helps filter,
17+
* sort, and rename the entries that end up in the output package typings file.
18+
*/
1419
class Entry {
20+
/**
21+
* The original name of the symbol, as exported from the module (i.e. source file)
22+
* containing the original TypeScript definition.
23+
*/
1524
public localName: string;
25+
26+
/**
27+
* The localName, possibly renamed to ensure that all the top-level exports have unique names.
28+
*/
1629
public uniqueName: string | undefined = undefined;
30+
31+
/**
32+
* The compiler symbol where this type was defined, after following any aliases.
33+
*/
1734
public followedSymbol: ts.Symbol;
35+
36+
/**
37+
* If true, this entry should be emitted using the "export" keyword instead of the "declare" keyword.
38+
*/
1839
public exported: boolean = false;
1940

2041
private _sortKey: string|undefined = undefined;
@@ -34,8 +55,19 @@ class Entry {
3455
}
3556
}
3657

58+
/**
59+
* Return value for PackageTypingsGenerator._followAliases()
60+
*/
3761
interface IFollowAliasesResult {
62+
/**
63+
* The original symbol that defined this entry, after following any aliases.
64+
*/
3865
symbol: ts.Symbol;
66+
67+
/**
68+
* If true, the symbol was declared by an external package, e.g. versus being imported
69+
* from another source file in the current project.
70+
*/
3971
external: boolean;
4072
}
4173

@@ -44,7 +76,16 @@ export default class PackageTypingsGenerator {
4476
private _typeChecker: ts.TypeChecker;
4577
private _indentedWriter: IndentedWriter = new IndentedWriter();
4678

79+
/**
80+
* A cache that tells us the Entry that is tracking a given symbol. Because of aliases,
81+
* two different symbols can map to the same Entry object.
82+
*/
4783
private readonly _entriesBySymbol: Map<ts.Symbol, Entry> = new Map<ts.Symbol, Entry>();
84+
85+
/**
86+
* This data structure stores the same entries as _entriesBySymbol.values().
87+
* They are sorted according to Entry.getSortKey().
88+
*/
4889
private readonly _entries: Entry[] = [];
4990

5091
/**
@@ -71,6 +112,10 @@ export default class PackageTypingsGenerator {
71112
return current as T;
72113
}
73114

115+
/**
116+
* For the given symbol, follow imports and type alias to find the symbol that represents
117+
* the original definition.
118+
*/
74119
private static _followAliases(symbol: ts.Symbol, typeChecker: ts.TypeChecker): IFollowAliasesResult {
75120
let current: ts.Symbol = symbol;
76121

@@ -135,7 +180,8 @@ export default class PackageTypingsGenerator {
135180
const entry: Entry | undefined = this._fetchEntryForSymbol(exportSymbol);
136181

137182
if (!entry) {
138-
// We are reexporting an external definition
183+
// We are reexporting an external definition.
184+
// To handle this, we would need to emit an import statement.
139185
this._indentedWriter.writeLine('// Unsupported re-export: ' + exportSymbol.name);
140186
} else {
141187
entry.exported = true;
@@ -155,8 +201,8 @@ export default class PackageTypingsGenerator {
155201
// console.log('=====================================');
156202

157203
const span: Span = new Span(declaration);
158-
console.log(span.getDump());
159-
console.log('-------------------------------------');
204+
// console.log(span.getDump());
205+
// console.log('-------------------------------------');
160206

161207
this._modifySpan(span, entry);
162208

@@ -171,6 +217,9 @@ export default class PackageTypingsGenerator {
171217
return fileContent;
172218
}
173219

220+
/**
221+
* Before writing out a declaration, _modifySpan() applies various fixups to make it nice.
222+
*/
174223
private _modifySpan(rootSpan: Span, entry: Entry): void {
175224
rootSpan.modify((span: Span, previousSpan: Span | undefined, parentSpan: Span | undefined) => {
176225
switch (span.kind) {
@@ -243,7 +292,13 @@ export default class PackageTypingsGenerator {
243292

244293
const referencedEntry: Entry | undefined = this._fetchEntryForSymbol(symbol);
245294
if (referencedEntry) {
246-
span.modification.prefix = '/**/' + referencedEntry.uniqueName;
295+
if (!referencedEntry.uniqueName) {
296+
// This should never happen
297+
throw new Error('referencedEntry.uniqueName is undefined');
298+
}
299+
300+
span.modification.prefix = referencedEntry.uniqueName;
301+
// span.modification.prefix += '/**/';
247302
}
248303
}
249304
break;
@@ -255,6 +310,9 @@ export default class PackageTypingsGenerator {
255310
);
256311
}
257312

313+
/**
314+
* Ensures a unique name for each item in the package typings file.
315+
*/
258316
private _makeUniqueNames(): void {
259317
const usedNames: Set<string> = new Set<string>();
260318
for (const entry of this._entries) {

0 commit comments

Comments
 (0)