/
context.ts
407 lines (348 loc) · 13 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import * as ts from 'typescript';
import { IMinimatch } from 'minimatch';
import { Logger } from '../utils/loggers';
import { createMinimatch } from '../utils/paths';
import { Reflection, ProjectReflection, ContainerReflection, Type } from '../models/index';
import { createTypeParameter } from './factories/type-parameter';
import { Converter } from './converter';
/**
* The context describes the current state the converter is in.
*/
export class Context {
/**
* The converter instance that has created the context.
*/
converter: Converter;
/**
* A list of all files that have been passed to the TypeScript compiler.
*/
fileNames: string[];
/**
* The TypeChecker instance returned by the TypeScript compiler.
*/
checker: ts.TypeChecker;
/**
* The program that is currently processed.
*/
program: ts.Program;
/**
* The project that is currently processed.
*/
project: ProjectReflection;
/**
* The scope or parent reflection that is currently processed.
*/
scope: Reflection;
/**
* Is the current source file marked as being external?
*/
isExternal?: boolean;
/**
* Is the current source file a declaration file?
*/
isDeclaration?: boolean;
/**
* The currently set type parameters.
*/
typeParameters?: ts.MapLike<Type>;
/**
* The currently set type arguments.
*/
typeArguments?: Type[];
/**
* Is the converter in inheritance mode?
*/
isInherit?: boolean;
/**
* The node that has started the inheritance mode.
*/
inheritParent?: ts.Node;
/**
* List symbol ids of inherited children already visited while inheriting.
*/
inheritedChildren?: number[];
/**
* The names of the children of the scope before inheritance has been started.
*/
inherited?: string[];
/**
* A list of parent nodes that have been passed to the visit function.
*/
visitStack: ts.Node[];
/**
* Next free symbol id used by [[getSymbolID]].
*/
private symbolID = -1024;
/**
* The pattern that should be used to flag external source files.
*/
private externalPattern?: Array<IMinimatch>;
/**
* Create a new Context instance.
*
* @param converter The converter instance that has created the context.
* @param fileNames A list of all files that have been passed to the TypeScript compiler.
* @param checker The TypeChecker instance returned by the TypeScript compiler.
*/
constructor(converter: Converter, fileNames: string[], checker: ts.TypeChecker, program: ts.Program) {
this.converter = converter;
this.fileNames = fileNames;
this.checker = checker;
this.program = program;
this.visitStack = [];
const project = new ProjectReflection(converter.name);
this.project = project;
this.scope = project;
if (converter.externalPattern) {
this.externalPattern = createMinimatch(converter.externalPattern);
}
}
/**
* Return the compiler options.
*/
getCompilerOptions(): ts.CompilerOptions {
return this.converter.application.options.getCompilerOptions();
}
/**
* Return the type declaration of the given node.
*
* @param node The TypeScript node whose type should be resolved.
* @returns The type declaration of the given node.
*/
getTypeAtLocation(node: ts.Node): ts.Type | undefined {
let nodeType: ts.Type | undefined;
try {
nodeType = this.checker.getTypeAtLocation(node);
} catch (error) {
}
if (!nodeType) {
if (node.symbol) {
nodeType = this.checker.getDeclaredTypeOfSymbol(node.symbol);
} else if (node.parent && node.parent.symbol) {
nodeType = this.checker.getDeclaredTypeOfSymbol(node.parent.symbol);
} else if (node.parent && node.parent.parent && node.parent.parent.symbol) {
nodeType = this.checker.getDeclaredTypeOfSymbol(node.parent.parent.symbol);
}
}
return nodeType;
}
getSymbolAtLocation(node: ts.Node): ts.Symbol | undefined {
let symbol = this.checker.getSymbolAtLocation(node);
if (!symbol && isNamedNode(node)) {
symbol = this.checker.getSymbolAtLocation(node.name);
}
return symbol;
}
/**
* Return the current logger instance.
*
* @returns The current logger instance.
*/
getLogger(): Logger {
return this.converter.application.logger;
}
/**
* Return the symbol id of the given symbol.
*
* The compiler sometimes does not assign an id to symbols, this method makes sure that we have one.
* It will assign negative ids if they are not set.
*
* @param symbol The symbol whose id should be returned.
* @returns The id of the given symbol or undefined if no symbol is provided.
*/
getSymbolID(symbol: ts.Symbol | undefined): number | undefined {
if (!symbol) {
return;
}
if (!symbol.id) {
symbol.id = this.symbolID--;
}
return symbol.id;
}
/**
* Register a newly generated reflection.
*
* Ensures that the reflection is both listed in [[Project.reflections]] and
* [[Project.symbolMapping]] if applicable.
*
* @param reflection The reflection that should be registered.
* @param node The node the given reflection was resolved from.
* @param symbol The symbol the given reflection was resolved from.
*/
registerReflection(reflection: Reflection, node?: ts.Node, symbol?: ts.Symbol) {
this.project.reflections[reflection.id] = reflection;
const id = this.getSymbolID(symbol ? symbol : (node ? node.symbol : undefined));
if (!this.isInherit && id && !this.project.symbolMapping[id]) {
this.project.symbolMapping[id] = reflection.id;
}
}
/**
* Trigger a node reflection event.
*
* All events are dispatched on the current converter instance.
*
* @param name The name of the event that should be triggered.
* @param reflection The triggering reflection.
* @param node The triggering TypeScript node if available.
*/
trigger(name: string, reflection: Reflection, node?: ts.Node) {
this.converter.trigger(name, this, reflection, node);
}
/**
* Run the given callback with the context configured for the given source file.
*
* @param node The TypeScript node containing the source file declaration.
* @param callback The callback that should be executed.
*/
withSourceFile(node: ts.SourceFile, callback: Function) {
let isExternal = !this.fileNames.includes(node.fileName);
if (!isExternal && this.externalPattern) {
isExternal = this.externalPattern.some(mm => mm.match(node.fileName));
}
if (isExternal && this.converter.excludeExternals) {
return;
}
const isDeclaration = node.isDeclarationFile;
if (isDeclaration) {
const lib = this.converter.getDefaultLib();
const isLib = node.fileName.substr(-lib.length) === lib;
if (!this.converter.includeDeclarations || isLib) {
return;
}
}
this.isExternal = isExternal;
this.isDeclaration = isDeclaration;
this.trigger(Converter.EVENT_FILE_BEGIN, this.project, node);
callback();
this.isExternal = false;
this.isDeclaration = false;
}
/**
* @param callback The callback function that should be executed with the changed context.
*/
public withScope(scope: Reflection | undefined, callback: () => void): void;
/**
* @param parameters An array of type parameters that should be set on the context while the callback is invoked.
* @param callback The callback function that should be executed with the changed context.
*/
public withScope(
scope: Reflection | undefined,
parameters: ts.NodeArray<ts.TypeParameterDeclaration> | undefined,
callback: () => void): void;
/**
* @param parameters An array of type parameters that should be set on the context while the callback is invoked.
* @param preserve Should the currently set type parameters of the context be preserved?
* @param callback The callback function that should be executed with the changed context.
*/
public withScope(
scope: Reflection | undefined,
parameters: ts.NodeArray<ts.TypeParameterDeclaration> | undefined,
preserve: boolean,
callback: () => void): void;
/**
* Run the given callback with the scope of the context set to the given reflection.
*
* @param scope The reflection that should be set as the scope of the context while the callback is invoked.
*/
public withScope(scope: Reflection, ...args: any[]): void {
if (!scope || !args.length) {
return;
}
const callback = args.pop();
const parameters = args.shift();
const oldScope = this.scope;
const oldTypeArguments = this.typeArguments;
const oldTypeParameters = this.typeParameters;
this.scope = scope;
this.typeParameters = parameters ? this.extractTypeParameters(parameters, args.length > 0) : this.typeParameters;
this.typeArguments = undefined;
callback();
this.scope = oldScope;
this.typeParameters = oldTypeParameters;
this.typeArguments = oldTypeArguments;
}
/**
* Inherit the children of the given TypeScript node to the current scope.
*
* @param baseNode The node whose children should be inherited.
* @param typeArguments The type arguments that apply while inheriting the given node.
* @return The resulting reflection / the current scope.
*/
inherit(baseNode: ts.Node, typeArguments?: ts.NodeArray<ts.TypeNode>): Reflection {
const wasInherit = this.isInherit;
const oldInherited = this.inherited;
const oldInheritParent = this.inheritParent;
const oldTypeArguments = this.typeArguments;
this.isInherit = true;
this.inheritParent = baseNode;
this.inherited = [];
const target = <ContainerReflection> this.scope;
if (!(target instanceof ContainerReflection)) {
throw new Error('Expected container reflection');
}
if (baseNode.symbol) {
const id = this.getSymbolID(baseNode.symbol)!;
if (this.inheritedChildren && this.inheritedChildren.includes(id)) {
return target;
} else {
this.inheritedChildren = this.inheritedChildren || [];
this.inheritedChildren.push(id);
}
}
if (target.children) {
this.inherited = target.children.map((c) => c.name);
} else {
this.inherited = [];
}
if (typeArguments) {
this.typeArguments = this.converter.convertTypes(this, typeArguments);
} else {
this.typeArguments = undefined;
}
this.converter.convertNode(this, baseNode);
this.isInherit = wasInherit;
this.inherited = oldInherited;
this.inheritParent = oldInheritParent;
this.typeArguments = oldTypeArguments;
if (!this.isInherit) {
delete this.inheritedChildren;
}
return target;
}
/**
* Convert the given list of type parameter declarations into a type mapping.
*
* @param parameters The list of type parameter declarations that should be converted.
* @param preserve Should the currently set type parameters of the context be preserved?
* @returns The resulting type mapping.
*/
private extractTypeParameters(parameters: ts.NodeArray<ts.TypeParameterDeclaration>, preserve?: boolean): ts.MapLike<Type> {
const typeParameters: ts.MapLike<Type> = {};
if (preserve) {
Object.keys(this.typeParameters || {}).forEach(key => {
typeParameters[key] = this.typeParameters![key];
});
}
parameters.forEach((declaration: ts.TypeParameterDeclaration, index: number) => {
if (!declaration.symbol) {
return;
}
const name = declaration.symbol.name;
if (this.typeArguments && this.typeArguments[index]) {
typeParameters[name] = this.typeArguments[index];
} else {
const param = createTypeParameter(this, declaration);
if (param) {
typeParameters[name] = param;
}
}
});
return typeParameters;
}
}
function isNamedNode(node: ts.Node): node is ts.Node & { name: ts.Identifier | ts.ComputedPropertyName } {
return node['name'] && (
ts.isIdentifier(node['name']) ||
ts.isComputedPropertyName(node['name'])
);
}