/
context.ts
303 lines (268 loc) · 8.74 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
import { ok as assert } from "assert";
import * as ts from "typescript";
import {
Reflection,
ProjectReflection,
ContainerReflection,
DeclarationReflection,
ReflectionKind,
ReflectionFlag,
} from "../models/index";
import type { Converter } from "./converter";
import { isNamedNode } from "./utils/nodes";
import { ConverterEvents } from "./converter-events";
import { resolveAliasedSymbol } from "./utils/symbols";
/**
* The context describes the current state the converter is in.
* @internal
*/
export class Context {
/**
* The converter instance that has created the context.
*/
readonly converter: Converter;
/**
* The TypeChecker instance returned by the TypeScript compiler.
*/
get checker(): ts.TypeChecker {
return this.program.getTypeChecker();
}
/**
* The program currently being converted.
* Accessing this property will throw if a source file is not currently being converted.
*/
get program(): ts.Program {
assert(
this._program,
"Tried to access Context.program when not converting a source file"
);
return this._program;
}
private _program?: ts.Program;
/**
* All programs being converted.
*/
readonly programs: readonly ts.Program[];
/**
* The project that is currently processed.
*/
readonly project: ProjectReflection;
/**
* The scope or parent reflection that is currently processed.
*/
readonly scope: Reflection;
/** @internal */
isConvertingTypeNode(): boolean {
return this.convertingTypeNode;
}
/** @internal */
setConvertingTypeNode() {
this.convertingTypeNode = true;
}
/**
* This is a horrible hack to avoid breaking backwards compatibility for plugins
* that use EVENT_CREATE_DECLARATION. The comment plugin needs to be able to check
* this to properly get the comment for module re-exports:
* ```ts
* /** We should use this comment */
* export * as Mod from "./mod"
* ```
* Will be removed in 0.22.
* @internal
*/
exportSymbol?: ts.Symbol;
/** @internal */
shouldBeStatic = false;
private convertingTypeNode = false;
/**
* Create a new Context instance.
*
* @param converter The converter instance that has created the context.
* @param entryPoints A list of all entry points for this project.
* @param checker The TypeChecker instance returned by the TypeScript compiler.
* @internal
*/
constructor(
converter: Converter,
programs: readonly ts.Program[],
project: ProjectReflection,
scope: Context["scope"] = project
) {
this.converter = converter;
this.programs = programs;
this.project = project;
this.scope = scope;
}
/** @internal */
get logger() {
return this.converter.application.logger;
}
/**
* 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 {
// ignore
}
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;
}
expectSymbolAtLocation(node: ts.Node): ts.Symbol {
const symbol = this.getSymbolAtLocation(node);
if (!symbol) {
const { line } = ts.getLineAndCharacterOfPosition(
node.getSourceFile(),
node.pos
);
throw new Error(
`Expected a symbol for node with kind ${
ts.SyntaxKind[node.kind]
} at ${node.getSourceFile().fileName}:${line + 1}`
);
}
return symbol;
}
resolveAliasedSymbol(symbol: ts.Symbol): ts.Symbol {
return resolveAliasedSymbol(symbol, this.checker);
}
createDeclarationReflection(
kind: ReflectionKind,
symbol: ts.Symbol | undefined,
exportSymbol: ts.Symbol | undefined,
// We need this because modules don't always have symbols.
nameOverride?: string
) {
const name = getHumanName(
nameOverride ?? exportSymbol?.name ?? symbol?.name ?? "unknown"
);
const reflection = new DeclarationReflection(name, kind, this.scope);
if (this.shouldBeStatic) {
reflection.setFlag(ReflectionFlag.Static);
}
reflection.escapedName = symbol?.escapedName;
this.addChild(reflection);
if (symbol && this.converter.isExternal(symbol)) {
reflection.setFlag(ReflectionFlag.External);
}
if (exportSymbol) {
this.registerReflection(reflection, exportSymbol);
}
this.registerReflection(reflection, symbol);
return reflection;
}
finalizeDeclarationReflection(
reflection: DeclarationReflection,
symbol: ts.Symbol | undefined,
exportSymbol?: ts.Symbol,
commentNode?: ts.Node
) {
this.exportSymbol = exportSymbol;
this.converter.trigger(
ConverterEvents.CREATE_DECLARATION,
this,
reflection,
(symbol &&
this.converter.getNodesForSymbol(symbol, reflection.kind)[0]) ??
commentNode
);
this.exportSymbol = undefined;
}
addChild(reflection: DeclarationReflection) {
if (this.scope instanceof ContainerReflection) {
this.scope.children ??= [];
this.scope.children.push(reflection);
}
}
shouldIgnore(symbol: ts.Symbol) {
return this.converter.shouldIgnore(symbol, this.checker);
}
/**
* Register a newly generated reflection. All created reflections should be
* passed to this method to ensure that the project helper functions work correctly.
*
* @param reflection The reflection that should be registered.
* @param symbol The symbol the given reflection was resolved from.
*/
registerReflection(reflection: Reflection, symbol: ts.Symbol | undefined) {
this.project.registerReflection(reflection, symbol);
}
/**
* 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);
}
/** @internal */
setActiveProgram(program: ts.Program | undefined) {
this._program = program;
}
/**
* @param callback The callback function that should be executed with the changed context.
*/
public withScope(scope: Reflection): Context {
const context = new Context(
this.converter,
this.programs,
this.project,
scope
);
context.convertingTypeNode = this.convertingTypeNode;
context.setActiveProgram(this._program);
return context;
}
}
const builtInSymbolRegExp = /^__@(\w+)$/;
const uniqueSymbolRegExp = /^__@(.*)@\d+$/;
function getHumanName(name: string) {
// TS 4.0, 4.1, 4.2 - well known symbols are treated specially.
let match = builtInSymbolRegExp.exec(name);
if (match) {
return `[Symbol.${match[1]}]`;
}
match = uniqueSymbolRegExp.exec(name);
if (match) {
return `[${match[1]}]`;
}
return name;
}