@powerlines/deepkit 0.5.2 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capnp.cjs +49 -48
- package/dist/capnp.js +4 -3
- package/dist/{chunk-C7BRLIIA.js → chunk-35PNVYKI.js} +4 -4
- package/dist/chunk-3GQAWCBQ.js +10 -0
- package/dist/{chunk-QABG54Y3.cjs → chunk-3GRTJOID.cjs} +9 -9
- package/dist/{chunk-6QPIKQMO.cjs → chunk-3PKIT7K2.cjs} +4 -4
- package/dist/{chunk-MEMIQ5AA.js → chunk-47WBQTA6.js} +1 -1
- package/dist/chunk-4UQ4UDET.cjs +19685 -0
- package/dist/chunk-5NPGWGPO.cjs +109 -0
- package/dist/{chunk-VMRRVNO2.cjs → chunk-B47NLUDW.cjs} +2 -2
- package/dist/chunk-BGYQAVKQ.cjs +13 -0
- package/dist/chunk-F5QUFY7D.cjs +135 -0
- package/dist/{chunk-2ODODKQQ.js → chunk-GVS5O43Z.js} +2 -2
- package/dist/{chunk-LPLPOVLC.js → chunk-JSH5MVQG.js} +2 -2
- package/dist/{chunk-EKHXI2JI.cjs → chunk-KDKCOXXC.cjs} +5 -5
- package/dist/chunk-KWUXT6EF.js +127 -0
- package/dist/{chunk-J2FVIV7W.cjs → chunk-MSO7PT57.cjs} +126 -126
- package/dist/chunk-O2LKJ62Q.cjs +2863 -0
- package/dist/chunk-U7WEV6HK.js +19678 -0
- package/dist/chunk-W423FBJ2.js +2854 -0
- package/dist/{chunk-I5CVIT7W.js → chunk-XI47ZADG.js} +42 -42
- package/dist/chunk-XU55W26B.js +109 -0
- package/dist/esbuild-plugin.cjs +6 -5
- package/dist/esbuild-plugin.js +5 -4
- package/dist/index.cjs +68 -67
- package/dist/index.js +10 -9
- package/dist/reflect-type.cjs +8 -7
- package/dist/reflect-type.js +7 -6
- package/dist/resolve-reflections.cjs +3 -3
- package/dist/resolve-reflections.js +2 -2
- package/dist/transformer.cjs +5 -4
- package/dist/transformer.js +3 -2
- package/dist/transpile.cjs +5 -4
- package/dist/transpile.js +4 -3
- package/dist/utilities.cjs +11 -10
- package/dist/utilities.js +4 -3
- package/package.json +4 -4
- package/dist/chunk-7J3ZDT6O.cjs +0 -30
- package/dist/chunk-GRNJVY7I.cjs +0 -19
- package/dist/chunk-IRPJW6HH.js +0 -16
- package/dist/chunk-J4SVRFV4.js +0 -127
- package/dist/chunk-MNUBEEIW.js +0 -41
- package/dist/chunk-QLKLDV3V.cjs +0 -135
- package/dist/chunk-VXZTOOL6.cjs +0 -69
- package/dist/chunk-XGQQM64U.js +0 -8
|
@@ -0,0 +1,2854 @@
|
|
|
1
|
+
import { ReflectionOp, TypeNumberBrand } from './chunk-XU55W26B.js';
|
|
2
|
+
import { __name, __require } from './chunk-3GQAWCBQ.js';
|
|
3
|
+
import ts4, { isBigIntLiteral } from 'typescript';
|
|
4
|
+
import { knownLibFilesForCompilerOptions } from '@typescript/vfs';
|
|
5
|
+
import { dirname, isAbsolute, join } from 'path';
|
|
6
|
+
import { readFileSync } from 'fs';
|
|
7
|
+
import micromatch from 'micromatch';
|
|
8
|
+
import { cloneNode } from '@marcj/ts-clone-node';
|
|
9
|
+
import defu from 'defu';
|
|
10
|
+
|
|
11
|
+
// ../../node_modules/.pnpm/@deepkit+type-compiler@1.0.5_patch_hash=88e8267ac73f46799c551ae3696181dc1f272895f490f8c_ef8b874d406bec4900e1fddc1ea375be/node_modules/@deepkit/type-compiler/dist/esm/src/debug.js
|
|
12
|
+
function isDebug(level = 1) {
|
|
13
|
+
const expected = "deepkit" + (level > 1 ? "+".repeat(level - 1) : "");
|
|
14
|
+
return "undefined" !== typeof process && "string" === typeof process.env.DEBUG && process.env.DEBUG.includes(expected);
|
|
15
|
+
}
|
|
16
|
+
__name(isDebug, "isDebug");
|
|
17
|
+
function debug(...message) {
|
|
18
|
+
if (isDebug(1)) {
|
|
19
|
+
console.debug(...message);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
__name(debug, "debug");
|
|
23
|
+
function debug2(...message) {
|
|
24
|
+
if (isDebug(2)) {
|
|
25
|
+
console.debug(...message);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
__name(debug2, "debug2");
|
|
29
|
+
var { createSourceFile, resolveModuleName, isStringLiteral, JSDocParsingMode, ScriptTarget } = ts4;
|
|
30
|
+
function patternMatch(path, patterns, base) {
|
|
31
|
+
const include = patterns.filter((pattern) => pattern[0] !== "!");
|
|
32
|
+
const exclude = patterns.filter((pattern) => pattern[0] === "!").map((pattern) => pattern.substring(1));
|
|
33
|
+
return micromatch.isMatch(path, include, {
|
|
34
|
+
ignore: exclude
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
__name(patternMatch, "patternMatch");
|
|
38
|
+
var Resolver = class {
|
|
39
|
+
static {
|
|
40
|
+
__name(this, "Resolver");
|
|
41
|
+
}
|
|
42
|
+
constructor(compilerOptions, host, sourceFiles) {
|
|
43
|
+
this.compilerOptions = compilerOptions;
|
|
44
|
+
this.host = host;
|
|
45
|
+
this.sourceFiles = sourceFiles;
|
|
46
|
+
}
|
|
47
|
+
resolve(from, importOrExportNode) {
|
|
48
|
+
const moduleSpecifier = importOrExportNode.moduleSpecifier;
|
|
49
|
+
if (!moduleSpecifier) return;
|
|
50
|
+
if (!isStringLiteral(moduleSpecifier)) return;
|
|
51
|
+
return this.resolveSourceFile(from, moduleSpecifier);
|
|
52
|
+
}
|
|
53
|
+
resolveImpl(modulePath, sourceFile) {
|
|
54
|
+
if (this.host.resolveModuleNameLiterals !== void 0) {
|
|
55
|
+
const results = this.host.resolveModuleNameLiterals(
|
|
56
|
+
[
|
|
57
|
+
modulePath
|
|
58
|
+
],
|
|
59
|
+
sourceFile.fileName,
|
|
60
|
+
/*reusedNames*/
|
|
61
|
+
void 0,
|
|
62
|
+
this.compilerOptions,
|
|
63
|
+
sourceFile,
|
|
64
|
+
void 0
|
|
65
|
+
);
|
|
66
|
+
if (results[0]) return results[0].resolvedModule;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (this.host.resolveModuleNames !== void 0) {
|
|
70
|
+
return this.host.resolveModuleNames(
|
|
71
|
+
[
|
|
72
|
+
modulePath.text
|
|
73
|
+
],
|
|
74
|
+
sourceFile.fileName,
|
|
75
|
+
/*reusedNames*/
|
|
76
|
+
void 0,
|
|
77
|
+
/*redirectedReference*/
|
|
78
|
+
void 0,
|
|
79
|
+
this.compilerOptions
|
|
80
|
+
)[0];
|
|
81
|
+
}
|
|
82
|
+
const result = resolveModuleName(modulePath.text, sourceFile.fileName, this.compilerOptions, this.host);
|
|
83
|
+
return result.resolvedModule;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Tries to resolve the .ts/d.ts file path for a given module path.
|
|
87
|
+
* Scans relative paths. Looks into package.json "types" and "exports" (with new 4.7 support)
|
|
88
|
+
*
|
|
89
|
+
* @param sourceFile the SourceFile of the file that contains the import. modulePath is relative to that.
|
|
90
|
+
* @param modulePath the x in 'from x'.
|
|
91
|
+
*/
|
|
92
|
+
resolveSourceFile(sourceFile, modulePath) {
|
|
93
|
+
const result = this.resolveImpl(modulePath, sourceFile);
|
|
94
|
+
if (!result) return;
|
|
95
|
+
if (!result.resolvedFileName.endsWith(".ts") && !result.resolvedFileName.endsWith(".tsx") && !result.resolvedFileName.endsWith(".d.ts")) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const fileName = result.resolvedFileName;
|
|
99
|
+
if (this.sourceFiles[fileName]) return this.sourceFiles[fileName];
|
|
100
|
+
const source = this.host.readFile(result.resolvedFileName);
|
|
101
|
+
if (!source) return;
|
|
102
|
+
const moduleSourceFile = this.sourceFiles[fileName] = createSourceFile(fileName, source, {
|
|
103
|
+
languageVersion: this.compilerOptions.target || ScriptTarget.ES2018,
|
|
104
|
+
// JSDocParsingMode is not available in TS < 5.3
|
|
105
|
+
jsDocParsingMode: JSDocParsingMode ? JSDocParsingMode.ParseNone : void 0
|
|
106
|
+
}, true);
|
|
107
|
+
this.sourceFiles[fileName] = moduleSourceFile;
|
|
108
|
+
ts4.bindSourceFile(moduleSourceFile, this.compilerOptions);
|
|
109
|
+
return moduleSourceFile;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// ../../node_modules/.pnpm/@deepkit+type-compiler@1.0.5_patch_hash=88e8267ac73f46799c551ae3696181dc1f272895f490f8c_ef8b874d406bec4900e1fddc1ea375be/node_modules/@deepkit/type-compiler/dist/esm/src/config.js
|
|
114
|
+
function isObject(obj) {
|
|
115
|
+
if (!obj) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
return typeof obj === "object" && !Array.isArray(obj);
|
|
119
|
+
}
|
|
120
|
+
__name(isObject, "isObject");
|
|
121
|
+
var defaultMergeStrategy = "merge";
|
|
122
|
+
function readTsConfig(parseConfigHost, path) {
|
|
123
|
+
const configFile = ts4.readConfigFile(path, (path2) => parseConfigHost.readFile(path2));
|
|
124
|
+
if (configFile.error) {
|
|
125
|
+
debug(`Failed to read tsconfig ${path}: ${configFile.error.messageText}`);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const parsed = ts4.parseJsonConfigFileContent(configFile.config, parseConfigHost, dirname(path));
|
|
129
|
+
const ignoredErrors = [
|
|
130
|
+
18003
|
|
131
|
+
// No inputs were found in config file.
|
|
132
|
+
];
|
|
133
|
+
const softErrors = [
|
|
134
|
+
18e3
|
|
135
|
+
// Circularity detected while resolving configuration
|
|
136
|
+
];
|
|
137
|
+
const errors = parsed.errors.filter((v) => !ignoredErrors.includes(v.code));
|
|
138
|
+
if (errors.length) {
|
|
139
|
+
debug(`Failed to parse tsconfig ${path}: ${parsed.errors.map((v) => v.messageText).join(", ")}`);
|
|
140
|
+
}
|
|
141
|
+
const hardErrors = errors.filter((v) => !softErrors.includes(v.code));
|
|
142
|
+
if (hardErrors.length) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const json = readFileSync(path, "utf8");
|
|
146
|
+
const tsconfig = JSON.parse(json);
|
|
147
|
+
return Object.assign(configFile.config, {
|
|
148
|
+
compilerOptions: parsed.options,
|
|
149
|
+
reflectionLevel: tsconfig.reflectionLevel ? String(tsconfig.reflectionLevel) : void 0
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
__name(readTsConfig, "readTsConfig");
|
|
153
|
+
function reflectionModeMatcher(config, filePath) {
|
|
154
|
+
if (Array.isArray(config.exclude)) {
|
|
155
|
+
if (patternMatch(filePath, config.exclude)) return "never";
|
|
156
|
+
}
|
|
157
|
+
if (Array.isArray(config.reflection)) {
|
|
158
|
+
return patternMatch(filePath, config.reflection) ? "default" : "never";
|
|
159
|
+
}
|
|
160
|
+
if (config.reflection === "default" || config.reflection === "explicit") return config.reflection;
|
|
161
|
+
return "never";
|
|
162
|
+
}
|
|
163
|
+
__name(reflectionModeMatcher, "reflectionModeMatcher");
|
|
164
|
+
function ensureStringArray(value) {
|
|
165
|
+
if (Array.isArray(value)) return value.map((v) => "" + v);
|
|
166
|
+
if ("string" === typeof value) return [
|
|
167
|
+
value
|
|
168
|
+
];
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
__name(ensureStringArray, "ensureStringArray");
|
|
172
|
+
function parseRawMode(mode) {
|
|
173
|
+
if ("boolean" === typeof mode) return mode ? "default" : "never";
|
|
174
|
+
if (mode === "default" || mode === "explicit") return mode;
|
|
175
|
+
return ensureStringArray(mode);
|
|
176
|
+
}
|
|
177
|
+
__name(parseRawMode, "parseRawMode");
|
|
178
|
+
function resolvePaths(baseDir, paths) {
|
|
179
|
+
if (!paths || !Array.isArray(paths)) return;
|
|
180
|
+
for (let i = 0; i < paths.length; i++) {
|
|
181
|
+
if ("string" !== typeof paths[i]) continue;
|
|
182
|
+
if (isAbsolute(paths[i])) continue;
|
|
183
|
+
let path = paths[i];
|
|
184
|
+
let exclude = false;
|
|
185
|
+
if (path.startsWith("!")) {
|
|
186
|
+
exclude = true;
|
|
187
|
+
path = path.substring(1);
|
|
188
|
+
}
|
|
189
|
+
if (path.startsWith("./") || path.includes("/")) {
|
|
190
|
+
path = join(baseDir, path);
|
|
191
|
+
}
|
|
192
|
+
path = path.replace(/\\/g, "/");
|
|
193
|
+
if (exclude) path = "!" + path;
|
|
194
|
+
paths[i] = path;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
__name(resolvePaths, "resolvePaths");
|
|
198
|
+
function appendPaths(strategy = defaultMergeStrategy, parent, existing) {
|
|
199
|
+
if (strategy === "replace") {
|
|
200
|
+
return [
|
|
201
|
+
...existing || parent
|
|
202
|
+
];
|
|
203
|
+
}
|
|
204
|
+
if (!existing) return [
|
|
205
|
+
...parent
|
|
206
|
+
];
|
|
207
|
+
return [
|
|
208
|
+
...parent,
|
|
209
|
+
...existing
|
|
210
|
+
];
|
|
211
|
+
}
|
|
212
|
+
__name(appendPaths, "appendPaths");
|
|
213
|
+
function applyConfigValues(existing, parent, baseDir) {
|
|
214
|
+
const parentReflection = isObject(parent.deepkitCompilerOptions) ? parent.deepkitCompilerOptions?.reflection : parent.reflection;
|
|
215
|
+
const parentReflectionLevel = parent.deepkitCompilerOptions?.reflectionLevel || parent.reflectionLevel || parent.compilerOptions?.reflectionLevel;
|
|
216
|
+
if (isObject(parent.deepkitCompilerOptions) && "undefined" === typeof existing.mergeStrategy) {
|
|
217
|
+
existing.mergeStrategy = parent.deepkitCompilerOptions.mergeStrategy;
|
|
218
|
+
}
|
|
219
|
+
if ("undefined" !== typeof parentReflection) {
|
|
220
|
+
const next = parseRawMode(parentReflection);
|
|
221
|
+
if ("undefined" === typeof existing.reflection) {
|
|
222
|
+
existing.reflection = next;
|
|
223
|
+
} else if ("string" === typeof existing.reflection) ; else if (Array.isArray(next) && Array.isArray(existing.reflection)) {
|
|
224
|
+
existing.reflection = appendPaths(existing.mergeStrategy, next, existing.reflection);
|
|
225
|
+
} else if ("string" === typeof next && Array.isArray(existing.reflection)) ;
|
|
226
|
+
}
|
|
227
|
+
if ("undefined" !== typeof parentReflectionLevel) {
|
|
228
|
+
if ("undefined" === typeof existing.reflectionLevel) {
|
|
229
|
+
existing.reflectionLevel = parentReflectionLevel;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (isObject(parent.deepkitCompilerOptions)) {
|
|
233
|
+
if (`undefined` !== typeof parent.deepkitCompilerOptions.exclude) {
|
|
234
|
+
const next = ensureStringArray(parent.deepkitCompilerOptions.exclude);
|
|
235
|
+
existing.exclude = appendPaths(existing.mergeStrategy, next, existing.exclude);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
resolvePaths(baseDir, existing.reflection);
|
|
239
|
+
resolvePaths(baseDir, existing.exclude);
|
|
240
|
+
if (parent.compilerOptions) {
|
|
241
|
+
if (Object.keys(existing.compilerOptions).length === 0) {
|
|
242
|
+
Object.assign(existing.compilerOptions, parent.compilerOptions);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
existing.extends = parent.extends;
|
|
246
|
+
}
|
|
247
|
+
__name(applyConfigValues, "applyConfigValues");
|
|
248
|
+
var defaultExcluded = [
|
|
249
|
+
"lib.dom*.d.ts",
|
|
250
|
+
"*typedarrays.d.ts",
|
|
251
|
+
"lib.webworker*.d.ts",
|
|
252
|
+
"lib.decorator*.d.ts",
|
|
253
|
+
"lib.es2015.proxy.d.ts",
|
|
254
|
+
"lib.es2020.sharedmemory.d.ts",
|
|
255
|
+
"lib.es2015.core.d.ts"
|
|
256
|
+
];
|
|
257
|
+
function getConfigResolver(cache3, host, compilerOptions, sourceFile, tsConfigPath = "") {
|
|
258
|
+
let config = {
|
|
259
|
+
// We use the parameter `compilerOptions` only for compilerOptions.configFilePath.
|
|
260
|
+
// We load the compilerOptions manually since transformers don't get the full picture
|
|
261
|
+
// (path aliases are missing for example).
|
|
262
|
+
// It's important to load compilerOptions manually if there is compilerOptions.configFilePath
|
|
263
|
+
// since not all build tools provide the full compilerOptions.
|
|
264
|
+
compilerOptions: {}
|
|
265
|
+
};
|
|
266
|
+
tsConfigPath = tsConfigPath || ("string" === typeof compilerOptions.configFilePath ? compilerOptions.configFilePath : "");
|
|
267
|
+
if (tsConfigPath) {
|
|
268
|
+
if (cache3[tsConfigPath]) {
|
|
269
|
+
return cache3[tsConfigPath];
|
|
270
|
+
}
|
|
271
|
+
const configFile = readTsConfig(host, tsConfigPath);
|
|
272
|
+
if (configFile) applyConfigValues(config, configFile, dirname(tsConfigPath));
|
|
273
|
+
} else {
|
|
274
|
+
if (!tsConfigPath && sourceFile) {
|
|
275
|
+
const baseDir = dirname(sourceFile.fileName);
|
|
276
|
+
const configPath = ts4.findConfigFile(baseDir, (path) => {
|
|
277
|
+
path = isAbsolute(path) ? path : join(baseDir, path);
|
|
278
|
+
return host.fileExists(path);
|
|
279
|
+
});
|
|
280
|
+
debug2(`No tsConfigPath|compilerOptions.configFilePath provided. Manually searching for tsconfig.json in ${baseDir} returned ${configPath}`);
|
|
281
|
+
if (configPath) {
|
|
282
|
+
tsConfigPath = isAbsolute(configPath) ? configPath : join(baseDir, configPath);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (tsConfigPath) {
|
|
286
|
+
if (cache3[tsConfigPath]) return cache3[tsConfigPath];
|
|
287
|
+
const configFile = readTsConfig(host, tsConfigPath);
|
|
288
|
+
if (configFile) applyConfigValues(config, configFile, dirname(tsConfigPath));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (tsConfigPath) {
|
|
292
|
+
let basePath = dirname(tsConfigPath);
|
|
293
|
+
let currentConfig = config;
|
|
294
|
+
const seen = /* @__PURE__ */ new Set();
|
|
295
|
+
seen.add(tsConfigPath);
|
|
296
|
+
while (currentConfig.extends) {
|
|
297
|
+
const path = join(basePath, currentConfig.extends);
|
|
298
|
+
if (seen.has(path)) break;
|
|
299
|
+
seen.add(path);
|
|
300
|
+
const nextConfig = ts4.readConfigFile(path, (path2) => host.readFile(path2));
|
|
301
|
+
if (!nextConfig) break;
|
|
302
|
+
basePath = dirname(path);
|
|
303
|
+
applyConfigValues(currentConfig, nextConfig.config, basePath);
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
throw new Error(`No tsconfig found for ${sourceFile?.fileName}, that is weird. Either provide a tsconfig or compilerOptions.configFilePath`);
|
|
307
|
+
}
|
|
308
|
+
config.exclude = config.exclude ? [
|
|
309
|
+
...defaultExcluded,
|
|
310
|
+
...config.exclude
|
|
311
|
+
] : [
|
|
312
|
+
...defaultExcluded
|
|
313
|
+
];
|
|
314
|
+
config.compilerOptions.configFilePath = tsConfigPath;
|
|
315
|
+
const resolvedConfig = {
|
|
316
|
+
path: tsConfigPath,
|
|
317
|
+
// we want to maintain options passed from tsc API (transpile, Program)
|
|
318
|
+
compilerOptions: Object.assign(config.compilerOptions, compilerOptions),
|
|
319
|
+
exclude: config.exclude,
|
|
320
|
+
reflection: config.reflection,
|
|
321
|
+
reflectionLevel: config.reflectionLevel || (config.compilerOptions?.reflectionLevel ? String(config.compilerOptions?.reflectionLevel) : void 0) || "normal",
|
|
322
|
+
mergeStrategy: config.mergeStrategy || defaultMergeStrategy
|
|
323
|
+
};
|
|
324
|
+
if (isDebug()) {
|
|
325
|
+
debug(`Found config ${resolvedConfig.path}:
|
|
326
|
+
reflection:`, resolvedConfig.reflection, `
|
|
327
|
+
exclude:`, resolvedConfig.exclude, `
|
|
328
|
+
paths:`, resolvedConfig.compilerOptions.paths);
|
|
329
|
+
}
|
|
330
|
+
const match = /* @__PURE__ */ __name((path) => {
|
|
331
|
+
const mode = reflectionModeMatcher(config, path);
|
|
332
|
+
return {
|
|
333
|
+
mode,
|
|
334
|
+
tsConfigPath,
|
|
335
|
+
level: resolvedConfig.reflectionLevel
|
|
336
|
+
};
|
|
337
|
+
}, "match");
|
|
338
|
+
return cache3[tsConfigPath] = {
|
|
339
|
+
config: resolvedConfig,
|
|
340
|
+
match
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
__name(getConfigResolver, "getConfigResolver");
|
|
344
|
+
var { isArrowFunction, isComputedPropertyName, isIdentifier, isNamedImports, isNumericLiteral, isPrivateIdentifier, isStringLiteral: isStringLiteral2, isStringLiteralLike, setOriginalNode, getLeadingCommentRanges, isNoSubstitutionTemplateLiteral, NodeFlags, SyntaxKind } = ts4;
|
|
345
|
+
function is__String(value) {
|
|
346
|
+
return typeof value === "string";
|
|
347
|
+
}
|
|
348
|
+
__name(is__String, "is__String");
|
|
349
|
+
function getIdentifierName(node) {
|
|
350
|
+
if (is__String(node)) return node;
|
|
351
|
+
if (isIdentifier(node) || isPrivateIdentifier(node)) {
|
|
352
|
+
return ts4.unescapeLeadingUnderscores(node.escapedText);
|
|
353
|
+
}
|
|
354
|
+
if (isStringLiteral2(node)) return node.text;
|
|
355
|
+
return "";
|
|
356
|
+
}
|
|
357
|
+
__name(getIdentifierName, "getIdentifierName");
|
|
358
|
+
function getEscapedText(node) {
|
|
359
|
+
if (is__String(node)) return node;
|
|
360
|
+
if (isIdentifier(node) || isPrivateIdentifier(node)) return node.escapedText;
|
|
361
|
+
return getIdentifierName(node);
|
|
362
|
+
}
|
|
363
|
+
__name(getEscapedText, "getEscapedText");
|
|
364
|
+
function findSourceFile(node) {
|
|
365
|
+
if (node.kind === SyntaxKind.SourceFile) return node;
|
|
366
|
+
let current = node.parent;
|
|
367
|
+
while (current && current.kind !== SyntaxKind.SourceFile) {
|
|
368
|
+
current = current.parent;
|
|
369
|
+
}
|
|
370
|
+
return current;
|
|
371
|
+
}
|
|
372
|
+
__name(findSourceFile, "findSourceFile");
|
|
373
|
+
function joinQualifiedName(name) {
|
|
374
|
+
if (isIdentifier(name)) return getIdentifierName(name);
|
|
375
|
+
return joinQualifiedName(name.left) + "_" + getIdentifierName(name.right);
|
|
376
|
+
}
|
|
377
|
+
__name(joinQualifiedName, "joinQualifiedName");
|
|
378
|
+
var BANNER_REGEX = /^\/\*\s---.*\n[\s\S]*\s*---\s*\*\//;
|
|
379
|
+
function getCommentOfNode(sourceFile, node) {
|
|
380
|
+
const commentLines = getLeadingCommentRanges(sourceFile.text, node.pos);
|
|
381
|
+
if (!commentLines || commentLines.length === 0) return;
|
|
382
|
+
const commentLine = commentLines.slice(-1)[0];
|
|
383
|
+
if (!commentLine) return;
|
|
384
|
+
const comment = sourceFile.text.slice(commentLine.pos, commentLine.end);
|
|
385
|
+
if (comment.match(BANNER_REGEX)) {
|
|
386
|
+
return void 0;
|
|
387
|
+
}
|
|
388
|
+
return comment;
|
|
389
|
+
}
|
|
390
|
+
__name(getCommentOfNode, "getCommentOfNode");
|
|
391
|
+
function parseJSDocAttributeFromText(comment, attribute) {
|
|
392
|
+
const attributeStr = `@${attribute} `;
|
|
393
|
+
const index = comment.indexOf(attributeStr);
|
|
394
|
+
if (index === -1) {
|
|
395
|
+
let start2 = 0;
|
|
396
|
+
while (true) {
|
|
397
|
+
const withoutContent = comment.indexOf(`@${attribute}`, start2);
|
|
398
|
+
if (withoutContent === -1) {
|
|
399
|
+
return [
|
|
400
|
+
void 0,
|
|
401
|
+
-1
|
|
402
|
+
];
|
|
403
|
+
}
|
|
404
|
+
const nextCharacter = comment[withoutContent + attribute.length + 1];
|
|
405
|
+
if (!nextCharacter || nextCharacter === " " || nextCharacter === "\n" || nextCharacter === "\r" || nextCharacter === " ") {
|
|
406
|
+
return [
|
|
407
|
+
void 0,
|
|
408
|
+
withoutContent + attribute.length + 1
|
|
409
|
+
];
|
|
410
|
+
}
|
|
411
|
+
start2 = withoutContent + attribute.length + 1;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const start = index + attributeStr.length;
|
|
415
|
+
const nextAttribute = comment.indexOf("@", start);
|
|
416
|
+
const endOfComment = comment.indexOf("*/", start);
|
|
417
|
+
const end = nextAttribute === -1 ? endOfComment : Math.min(nextAttribute, endOfComment);
|
|
418
|
+
const content = comment.substring(start, end).trim();
|
|
419
|
+
return [
|
|
420
|
+
content.split("\n").map((v) => {
|
|
421
|
+
const indexOfStar = v.indexOf("*");
|
|
422
|
+
if (indexOfStar === -1) {
|
|
423
|
+
return v.trim();
|
|
424
|
+
}
|
|
425
|
+
return v.substring(indexOfStar + 1).trim();
|
|
426
|
+
}).join("\n").replace(/\n*$/g, ""),
|
|
427
|
+
end + 1
|
|
428
|
+
];
|
|
429
|
+
}
|
|
430
|
+
__name(parseJSDocAttributeFromText, "parseJSDocAttributeFromText");
|
|
431
|
+
function parseJSDocBooleanFromText(comment, attribute) {
|
|
432
|
+
return new RegExp(`^s*@(${attribute?.trim()})s*(false|False|FALSE|0|n|N|no|No|NO)s*$`, "gm").test(comment?.trim()) ? false : new RegExp(`^s*@(${attribute?.trim()})s*(true|True|TRUE|1|y|Y|yes|Yes|YES)?s*$`, "gm").test(comment?.trim()) ? true : void 0;
|
|
433
|
+
}
|
|
434
|
+
__name(parseJSDocBooleanFromText, "parseJSDocBooleanFromText");
|
|
435
|
+
function parseJSDocDescription(comment) {
|
|
436
|
+
let lines = comment.split("\n").filter((line) => line && !line.includes("/**") && !line.includes("*/")).map((line) => line.replace(/^\s*\*\s*/g, "").replace(/^\*\s*/g, "").trim());
|
|
437
|
+
const attribute = lines.findIndex((line) => line.startsWith("@"));
|
|
438
|
+
if (attribute !== -1) {
|
|
439
|
+
lines = lines.slice(0, attribute);
|
|
440
|
+
}
|
|
441
|
+
return lines.join("\n").trim() || void 0;
|
|
442
|
+
}
|
|
443
|
+
__name(parseJSDocDescription, "parseJSDocDescription");
|
|
444
|
+
function extractJSDocAttribute(sourceFile, node, attribute) {
|
|
445
|
+
if (!node) return void 0;
|
|
446
|
+
const comment = getCommentOfNode(sourceFile, node);
|
|
447
|
+
if (!comment) return void 0;
|
|
448
|
+
let result = parseJSDocAttributeFromText(comment, attribute);
|
|
449
|
+
if (!result[0] && attribute === "description") {
|
|
450
|
+
result[0] = parseJSDocDescription(comment);
|
|
451
|
+
}
|
|
452
|
+
return result[0];
|
|
453
|
+
}
|
|
454
|
+
__name(extractJSDocAttribute, "extractJSDocAttribute");
|
|
455
|
+
function extractJSDocArray(sourceFile, node, attribute) {
|
|
456
|
+
if (!node) return void 0;
|
|
457
|
+
const comment = getCommentOfNode(sourceFile, node);
|
|
458
|
+
if (!comment) return void 0;
|
|
459
|
+
const results = [];
|
|
460
|
+
try {
|
|
461
|
+
let result = parseJSDocAttributeFromText(comment, attribute);
|
|
462
|
+
if (result.length < 2) {
|
|
463
|
+
return void 0;
|
|
464
|
+
}
|
|
465
|
+
if (result[0] && result[1] > -1) {
|
|
466
|
+
results.push(result[0]);
|
|
467
|
+
}
|
|
468
|
+
let currentComment = comment;
|
|
469
|
+
while (result[1] > -1 && currentComment.length > result[1]) {
|
|
470
|
+
currentComment = currentComment.substring(result[1]);
|
|
471
|
+
result = parseJSDocAttributeFromText(currentComment, attribute);
|
|
472
|
+
if (result[0]) {
|
|
473
|
+
results.push(result[0]);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
} catch (error) {
|
|
477
|
+
console.error("Error parsing JSDoc attribute:", attribute, "in comment:", comment);
|
|
478
|
+
console.error(error);
|
|
479
|
+
}
|
|
480
|
+
return results;
|
|
481
|
+
}
|
|
482
|
+
__name(extractJSDocArray, "extractJSDocArray");
|
|
483
|
+
function extractJSDocBoolean(sourceFile, node, attribute) {
|
|
484
|
+
if (!node) {
|
|
485
|
+
return void 0;
|
|
486
|
+
}
|
|
487
|
+
const comment = getCommentOfNode(sourceFile, node);
|
|
488
|
+
if (!comment) {
|
|
489
|
+
return void 0;
|
|
490
|
+
}
|
|
491
|
+
return parseJSDocBooleanFromText(comment, attribute);
|
|
492
|
+
}
|
|
493
|
+
__name(extractJSDocBoolean, "extractJSDocBoolean");
|
|
494
|
+
function getPropertyName(f, node) {
|
|
495
|
+
if (!node) return "";
|
|
496
|
+
if (isIdentifier(node)) return getIdentifierName(node);
|
|
497
|
+
if (isStringLiteral2(node)) return node.text;
|
|
498
|
+
if (isNumericLiteral(node)) return +node.text;
|
|
499
|
+
if (isNoSubstitutionTemplateLiteral(node)) return node.text;
|
|
500
|
+
if (isComputedPropertyName(node)) {
|
|
501
|
+
return f.createArrowFunction(void 0, void 0, [], void 0, void 0, node.expression);
|
|
502
|
+
}
|
|
503
|
+
if (isPrivateIdentifier(node)) return getIdentifierName(node);
|
|
504
|
+
return "";
|
|
505
|
+
}
|
|
506
|
+
__name(getPropertyName, "getPropertyName");
|
|
507
|
+
function getNameAsString(node) {
|
|
508
|
+
if (!node) return "";
|
|
509
|
+
if (isIdentifier(node)) return getIdentifierName(node);
|
|
510
|
+
if (isStringLiteral2(node)) return node.text;
|
|
511
|
+
if (isNumericLiteral(node)) return node.text;
|
|
512
|
+
if (isBigIntLiteral(node)) return node.text;
|
|
513
|
+
if (isNoSubstitutionTemplateLiteral(node)) return node.text;
|
|
514
|
+
if (isComputedPropertyName(node)) {
|
|
515
|
+
if (isStringLiteralLike(node) || isNumericLiteral(node)) return node.text;
|
|
516
|
+
return "";
|
|
517
|
+
}
|
|
518
|
+
if (isPrivateIdentifier(node)) return getIdentifierName(node);
|
|
519
|
+
return joinQualifiedName(node);
|
|
520
|
+
}
|
|
521
|
+
__name(getNameAsString, "getNameAsString");
|
|
522
|
+
function hasModifier(node, modifier) {
|
|
523
|
+
if (!node.modifiers) return false;
|
|
524
|
+
return node.modifiers.some((v) => v.kind === modifier);
|
|
525
|
+
}
|
|
526
|
+
__name(hasModifier, "hasModifier");
|
|
527
|
+
var cloneHook = /* @__PURE__ */ __name((node, payload) => {
|
|
528
|
+
if (isIdentifier(node)) {
|
|
529
|
+
return {
|
|
530
|
+
text: /* @__PURE__ */ __name(() => {
|
|
531
|
+
return getIdentifierName(node);
|
|
532
|
+
}, "text")
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
return;
|
|
536
|
+
}, "cloneHook");
|
|
537
|
+
var NodeConverter = class {
|
|
538
|
+
static {
|
|
539
|
+
__name(this, "NodeConverter");
|
|
540
|
+
}
|
|
541
|
+
constructor(f) {
|
|
542
|
+
this.f = f;
|
|
543
|
+
}
|
|
544
|
+
toExpression(node) {
|
|
545
|
+
if (node === void 0) return this.f.createIdentifier("undefined");
|
|
546
|
+
if (Array.isArray(node)) {
|
|
547
|
+
return this.f.createArrayLiteralExpression(this.f.createNodeArray(node.map((v) => this.toExpression(v))));
|
|
548
|
+
}
|
|
549
|
+
if ("string" === typeof node) return this.f.createStringLiteral(node, true);
|
|
550
|
+
if ("number" === typeof node) return this.f.createNumericLiteral(node);
|
|
551
|
+
if ("bigint" === typeof node) return this.f.createBigIntLiteral(String(node));
|
|
552
|
+
if ("boolean" === typeof node) return node ? this.f.createTrue() : this.f.createFalse();
|
|
553
|
+
if (node.pos === -1 && node.end === -1 && node.parent === void 0) {
|
|
554
|
+
if (isArrowFunction(node)) {
|
|
555
|
+
if (node.body.pos === -1 && node.body.end === -1 && node.body.parent === void 0) return node;
|
|
556
|
+
return this.f.createArrowFunction(node.modifiers, node.typeParameters, node.parameters, node.type, node.equalsGreaterThanToken, this.toExpression(node.body));
|
|
557
|
+
}
|
|
558
|
+
return node;
|
|
559
|
+
}
|
|
560
|
+
switch (node.kind) {
|
|
561
|
+
case SyntaxKind.Identifier:
|
|
562
|
+
return finish(node, this.f.createIdentifier(getIdentifierName(node)));
|
|
563
|
+
case SyntaxKind.StringLiteral:
|
|
564
|
+
return finish(node, this.f.createStringLiteral(node.text));
|
|
565
|
+
case SyntaxKind.NumericLiteral:
|
|
566
|
+
return finish(node, this.f.createNumericLiteral(node.text));
|
|
567
|
+
case SyntaxKind.BigIntLiteral:
|
|
568
|
+
return finish(node, this.f.createBigIntLiteral(node.text));
|
|
569
|
+
case SyntaxKind.TrueKeyword:
|
|
570
|
+
return finish(node, this.f.createTrue());
|
|
571
|
+
case SyntaxKind.FalseKeyword:
|
|
572
|
+
return finish(node, this.f.createFalse());
|
|
573
|
+
}
|
|
574
|
+
try {
|
|
575
|
+
return cloneNode(node, {
|
|
576
|
+
preserveComments: false,
|
|
577
|
+
factory: this.f,
|
|
578
|
+
setOriginalNodes: true,
|
|
579
|
+
preserveSymbols: true,
|
|
580
|
+
setParents: true,
|
|
581
|
+
hook: cloneHook
|
|
582
|
+
});
|
|
583
|
+
} catch (error) {
|
|
584
|
+
console.error("could not clone node", node);
|
|
585
|
+
throw error;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
function isExternalOrCommonJsModule(file) {
|
|
590
|
+
return (file.externalModuleIndicator || file.commonJsModuleIndicator) !== void 0;
|
|
591
|
+
}
|
|
592
|
+
__name(isExternalOrCommonJsModule, "isExternalOrCommonJsModule");
|
|
593
|
+
function isNodeWithLocals(node) {
|
|
594
|
+
return "locals" in node;
|
|
595
|
+
}
|
|
596
|
+
__name(isNodeWithLocals, "isNodeWithLocals");
|
|
597
|
+
function getGlobalsOfSourceFile(file) {
|
|
598
|
+
if (file.redirectInfo) return;
|
|
599
|
+
if (!isNodeWithLocals(file)) return;
|
|
600
|
+
if (!isExternalOrCommonJsModule(file)) return file.locals;
|
|
601
|
+
if (file.jsGlobalAugmentations) return file.jsGlobalAugmentations;
|
|
602
|
+
if (file.symbol && file.symbol.globalExports) return file.symbol.globalExports;
|
|
603
|
+
}
|
|
604
|
+
__name(getGlobalsOfSourceFile, "getGlobalsOfSourceFile");
|
|
605
|
+
function ensureImportIsEmitted(importDeclaration, specifierName) {
|
|
606
|
+
if (specifierName && importDeclaration.importClause && importDeclaration.importClause.namedBindings) {
|
|
607
|
+
if (isNamedImports(importDeclaration.importClause.namedBindings)) {
|
|
608
|
+
for (const element of importDeclaration.importClause.namedBindings.elements) {
|
|
609
|
+
if (element.name.escapedText === specifierName.escapedText) {
|
|
610
|
+
element.flags |= NodeFlags.Synthesized;
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
importDeclaration.flags |= NodeFlags.Synthesized;
|
|
617
|
+
}
|
|
618
|
+
__name(ensureImportIsEmitted, "ensureImportIsEmitted");
|
|
619
|
+
function serializeEntityNameAsExpression(f, node) {
|
|
620
|
+
switch (node.kind) {
|
|
621
|
+
case SyntaxKind.Identifier:
|
|
622
|
+
return finish(node, f.createIdentifier(getIdentifierName(node)));
|
|
623
|
+
case SyntaxKind.QualifiedName:
|
|
624
|
+
return finish(node, serializeQualifiedNameAsExpression(f, node));
|
|
625
|
+
}
|
|
626
|
+
return node;
|
|
627
|
+
}
|
|
628
|
+
__name(serializeEntityNameAsExpression, "serializeEntityNameAsExpression");
|
|
629
|
+
function serializeQualifiedNameAsExpression(f, node) {
|
|
630
|
+
return f.createPropertyAccessExpression(serializeEntityNameAsExpression(f, node.left), node.right);
|
|
631
|
+
}
|
|
632
|
+
__name(serializeQualifiedNameAsExpression, "serializeQualifiedNameAsExpression");
|
|
633
|
+
function finish(oldNode, newNode) {
|
|
634
|
+
setOriginalNode(newNode, oldNode);
|
|
635
|
+
newNode._original = newNode.original;
|
|
636
|
+
newNode._symbol = oldNode._symbol ?? oldNode.symbol;
|
|
637
|
+
newNode.symbol = newNode._symbol;
|
|
638
|
+
return newNode;
|
|
639
|
+
}
|
|
640
|
+
__name(finish, "finish");
|
|
641
|
+
|
|
642
|
+
// ../../node_modules/.pnpm/@deepkit+type-compiler@1.0.5_patch_hash=88e8267ac73f46799c551ae3696181dc1f272895f490f8c_ef8b874d406bec4900e1fddc1ea375be/node_modules/@deepkit/type-compiler/dist/esm/src/compiler.js
|
|
643
|
+
var { visitEachChild, visitNode, isPropertyAssignment, isArrayTypeNode, isArrowFunction: isArrowFunction2, isBlock, isCallExpression, isCallSignatureDeclaration, isClassDeclaration, isClassExpression, isConstructorDeclaration, isConstructorTypeNode, isConstructSignatureDeclaration, isEnumDeclaration, isExportDeclaration, isExpression, isExpressionWithTypeArguments, isFunctionDeclaration, isFunctionExpression, isFunctionLike, isIdentifier: isIdentifier2, isImportClause, isImportDeclaration, isImportSpecifier, isInferTypeNode, isInterfaceDeclaration, isMethodDeclaration, isMethodSignature, isModuleDeclaration, isNamedExports, isNamedTupleMember, isNewExpression, isObjectLiteralExpression, isOptionalTypeNode, isParameter, isParenthesizedExpression, isParenthesizedTypeNode, isPropertyAccessExpression, isQualifiedName, isSourceFile, isStringLiteral: isStringLiteral3, isTypeAliasDeclaration, isTypeLiteralNode, isTypeParameterDeclaration, isTypeQueryNode, isTypeReferenceNode, isUnionTypeNode, isExpressionStatement, isVariableDeclaration, getEffectiveConstraintOfTypeParameter, addSyntheticLeadingComment, createCompilerHost, createPrinter, escapeLeadingUnderscores, EmitHint, NodeFlags: NodeFlags2, SyntaxKind: SyntaxKind2, ScriptTarget: ScriptTarget2, ModifierFlags, ScriptKind } = ts4;
|
|
644
|
+
function encodeOps(ops) {
|
|
645
|
+
return ops.map((v) => String.fromCharCode(v + 33)).join("");
|
|
646
|
+
}
|
|
647
|
+
__name(encodeOps, "encodeOps");
|
|
648
|
+
function filterUndefined(object) {
|
|
649
|
+
return Object.fromEntries(Object.entries(object).filter(([, v]) => v !== void 0));
|
|
650
|
+
}
|
|
651
|
+
__name(filterUndefined, "filterUndefined");
|
|
652
|
+
({
|
|
653
|
+
[ReflectionOp.literal]: {
|
|
654
|
+
},
|
|
655
|
+
// [ReflectionOp.pointer]: { params: 1 },
|
|
656
|
+
// [ReflectionOp.arg]: { params: 1 },
|
|
657
|
+
[ReflectionOp.classReference]: {
|
|
658
|
+
},
|
|
659
|
+
[ReflectionOp.propertySignature]: {
|
|
660
|
+
},
|
|
661
|
+
[ReflectionOp.property]: {
|
|
662
|
+
},
|
|
663
|
+
[ReflectionOp.jump]: {
|
|
664
|
+
},
|
|
665
|
+
[ReflectionOp.enum]: {
|
|
666
|
+
},
|
|
667
|
+
[ReflectionOp.enumMember]: {
|
|
668
|
+
},
|
|
669
|
+
[ReflectionOp.typeParameter]: {
|
|
670
|
+
},
|
|
671
|
+
[ReflectionOp.typeParameterDefault]: {
|
|
672
|
+
},
|
|
673
|
+
[ReflectionOp.mappedType]: {
|
|
674
|
+
},
|
|
675
|
+
[ReflectionOp.call]: {
|
|
676
|
+
},
|
|
677
|
+
[ReflectionOp.inline]: {
|
|
678
|
+
},
|
|
679
|
+
[ReflectionOp.inlineCall]: {
|
|
680
|
+
},
|
|
681
|
+
[ReflectionOp.loads]: {
|
|
682
|
+
},
|
|
683
|
+
[ReflectionOp.extends]: {
|
|
684
|
+
},
|
|
685
|
+
[ReflectionOp.infer]: {
|
|
686
|
+
},
|
|
687
|
+
[ReflectionOp.defaultValue]: {
|
|
688
|
+
},
|
|
689
|
+
[ReflectionOp.parameter]: {
|
|
690
|
+
},
|
|
691
|
+
[ReflectionOp.method]: {
|
|
692
|
+
},
|
|
693
|
+
[ReflectionOp.function]: {
|
|
694
|
+
},
|
|
695
|
+
[ReflectionOp.description]: {
|
|
696
|
+
},
|
|
697
|
+
[ReflectionOp.numberBrand]: {
|
|
698
|
+
},
|
|
699
|
+
[ReflectionOp.typeof]: {
|
|
700
|
+
},
|
|
701
|
+
[ReflectionOp.classExtends]: {
|
|
702
|
+
},
|
|
703
|
+
[ReflectionOp.distribute]: {
|
|
704
|
+
},
|
|
705
|
+
[ReflectionOp.jumpCondition]: {
|
|
706
|
+
},
|
|
707
|
+
[ReflectionOp.typeName]: {
|
|
708
|
+
},
|
|
709
|
+
[ReflectionOp.implements]: {
|
|
710
|
+
},
|
|
711
|
+
[ReflectionOp.tags]: {
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
function findVariable(frame, name, frameOffset = 0) {
|
|
715
|
+
const variable = frame.variables.find((v) => v.name === name);
|
|
716
|
+
if (variable) {
|
|
717
|
+
return {
|
|
718
|
+
frameOffset,
|
|
719
|
+
stackIndex: variable.index
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
if (frame.previous) return findVariable(frame.previous, name, frameOffset + 1);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
__name(findVariable, "findVariable");
|
|
726
|
+
function findConditionalFrame(frame) {
|
|
727
|
+
if (frame.conditional) return frame;
|
|
728
|
+
if (frame.previous) return findConditionalFrame(frame.previous);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
__name(findConditionalFrame, "findConditionalFrame");
|
|
732
|
+
var CompilerProgram = class CompilerProgram2 {
|
|
733
|
+
static {
|
|
734
|
+
__name(this, "CompilerProgram");
|
|
735
|
+
}
|
|
736
|
+
constructor(forNode, sourceFile) {
|
|
737
|
+
this.forNode = forNode;
|
|
738
|
+
this.sourceFile = sourceFile;
|
|
739
|
+
this.ops = [];
|
|
740
|
+
this.stack = [];
|
|
741
|
+
this.mainOffset = 0;
|
|
742
|
+
this.stackPosition = 0;
|
|
743
|
+
this.frame = {
|
|
744
|
+
variables: [],
|
|
745
|
+
opIndex: 0
|
|
746
|
+
};
|
|
747
|
+
this.activeCoRoutines = [];
|
|
748
|
+
this.coRoutines = [];
|
|
749
|
+
this.resolveFunctionParameters = /* @__PURE__ */ new Map();
|
|
750
|
+
}
|
|
751
|
+
buildPackStruct() {
|
|
752
|
+
const ops = [
|
|
753
|
+
...this.ops
|
|
754
|
+
];
|
|
755
|
+
if (this.coRoutines.length) {
|
|
756
|
+
for (let i = this.coRoutines.length - 1; i >= 0; i--) {
|
|
757
|
+
ops.unshift(...this.coRoutines[i].ops);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
if (this.mainOffset) {
|
|
761
|
+
ops.unshift(ReflectionOp.jump, this.mainOffset);
|
|
762
|
+
}
|
|
763
|
+
return {
|
|
764
|
+
ops,
|
|
765
|
+
stack: this.stack
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
isEmpty() {
|
|
769
|
+
return this.ops.length === 0;
|
|
770
|
+
}
|
|
771
|
+
pushConditionalFrame() {
|
|
772
|
+
const frame = this.pushFrame();
|
|
773
|
+
frame.conditional = true;
|
|
774
|
+
}
|
|
775
|
+
pushStack(item) {
|
|
776
|
+
this.stack.push(item);
|
|
777
|
+
return this.stackPosition++;
|
|
778
|
+
}
|
|
779
|
+
pushCoRoutine() {
|
|
780
|
+
this.pushFrame(true);
|
|
781
|
+
this.activeCoRoutines.push({
|
|
782
|
+
ops: []
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
popCoRoutine() {
|
|
786
|
+
const coRoutine = this.activeCoRoutines.pop();
|
|
787
|
+
if (!coRoutine) throw new Error("No active co routine found");
|
|
788
|
+
this.popFrameImplicit();
|
|
789
|
+
if (this.mainOffset === 0) {
|
|
790
|
+
this.mainOffset = 2;
|
|
791
|
+
}
|
|
792
|
+
const startIndex = this.mainOffset;
|
|
793
|
+
coRoutine.ops.push(ReflectionOp.return);
|
|
794
|
+
this.coRoutines.push(coRoutine);
|
|
795
|
+
this.mainOffset += coRoutine.ops.length;
|
|
796
|
+
return startIndex;
|
|
797
|
+
}
|
|
798
|
+
pushOp(...ops) {
|
|
799
|
+
for (const op of ops) {
|
|
800
|
+
if ("number" !== typeof op) {
|
|
801
|
+
throw new Error("No valid OP added");
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
if (this.activeCoRoutines.length) {
|
|
805
|
+
this.activeCoRoutines[this.activeCoRoutines.length - 1].ops.push(...ops);
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
this.ops.push(...ops);
|
|
809
|
+
}
|
|
810
|
+
pushOpAtFrame(frame, ...ops) {
|
|
811
|
+
if (this.activeCoRoutines.length) {
|
|
812
|
+
this.activeCoRoutines[this.activeCoRoutines.length - 1].ops.splice(frame.opIndex, 0, ...ops);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
this.ops.splice(frame.opIndex, 0, ...ops);
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Returns the index of the `entry` in the stack, if already exists. If not, add it, and return that new index.
|
|
819
|
+
*/
|
|
820
|
+
findOrAddStackEntry(entry) {
|
|
821
|
+
const index = this.stack.indexOf(entry);
|
|
822
|
+
if (index !== -1) return index;
|
|
823
|
+
return this.pushStack(entry);
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* To make room for a stack entry expected on the stack as input for example.
|
|
827
|
+
*/
|
|
828
|
+
increaseStackPosition() {
|
|
829
|
+
return this.stackPosition++;
|
|
830
|
+
}
|
|
831
|
+
resolveFunctionParametersIncrease(fn) {
|
|
832
|
+
this.resolveFunctionParameters.set(fn, (this.resolveFunctionParameters.get(fn) || 0) + 1);
|
|
833
|
+
}
|
|
834
|
+
resolveFunctionParametersDecrease(fn) {
|
|
835
|
+
this.resolveFunctionParameters.set(fn, (this.resolveFunctionParameters.get(fn) || 1) - 1);
|
|
836
|
+
}
|
|
837
|
+
isResolveFunctionParameters(fn) {
|
|
838
|
+
return (this.resolveFunctionParameters.get(fn) || 0) > 0;
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
*
|
|
842
|
+
* Each pushFrame() call needs a popFrame() call.
|
|
843
|
+
*/
|
|
844
|
+
pushFrame(implicit = false) {
|
|
845
|
+
if (!implicit) this.pushOp(ReflectionOp.frame);
|
|
846
|
+
const opIndex = this.activeCoRoutines.length ? this.activeCoRoutines[this.activeCoRoutines.length - 1].ops.length : this.ops.length;
|
|
847
|
+
this.frame = {
|
|
848
|
+
previous: this.frame,
|
|
849
|
+
variables: [],
|
|
850
|
+
opIndex
|
|
851
|
+
};
|
|
852
|
+
return this.frame;
|
|
853
|
+
}
|
|
854
|
+
findConditionalFrame() {
|
|
855
|
+
return findConditionalFrame(this.frame);
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Remove stack without doing it as OP in the processor. Some other command calls popFrame() already, which makes popFrameImplicit() an implicit popFrame.
|
|
859
|
+
* e.g. union, class, etc. all call popFrame(). the current CompilerProgram needs to be aware of that, which this function is for.
|
|
860
|
+
*/
|
|
861
|
+
popFrameImplicit() {
|
|
862
|
+
if (this.frame.previous) this.frame = this.frame.previous;
|
|
863
|
+
}
|
|
864
|
+
moveFrame() {
|
|
865
|
+
this.pushOp(ReflectionOp.moveFrame);
|
|
866
|
+
if (this.frame.previous) this.frame = this.frame.previous;
|
|
867
|
+
}
|
|
868
|
+
pushVariable(name, frame = this.frame) {
|
|
869
|
+
this.pushOpAtFrame(frame, ReflectionOp.var);
|
|
870
|
+
frame.variables.push({
|
|
871
|
+
index: frame.variables.length,
|
|
872
|
+
name
|
|
873
|
+
});
|
|
874
|
+
return frame.variables.length - 1;
|
|
875
|
+
}
|
|
876
|
+
pushTemplateParameter(name, withDefault = false) {
|
|
877
|
+
this.pushOp(withDefault ? ReflectionOp.typeParameterDefault : ReflectionOp.typeParameter, this.findOrAddStackEntry(name));
|
|
878
|
+
this.frame.variables.push({
|
|
879
|
+
index: this.frame.variables.length,
|
|
880
|
+
name
|
|
881
|
+
});
|
|
882
|
+
return this.frame.variables.length - 1;
|
|
883
|
+
}
|
|
884
|
+
findVariable(name, frame = this.frame) {
|
|
885
|
+
return findVariable(frame, name);
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
function getAssignTypeExpression(call) {
|
|
889
|
+
if (isParenthesizedExpression(call) && isCallExpression(call.expression)) {
|
|
890
|
+
call = call.expression;
|
|
891
|
+
}
|
|
892
|
+
if (isCallExpression(call) && isIdentifier2(call.expression) && getIdentifierName(call.expression) === "__assignType" && call.arguments.length > 0) {
|
|
893
|
+
return call.arguments[0];
|
|
894
|
+
}
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
__name(getAssignTypeExpression, "getAssignTypeExpression");
|
|
898
|
+
function getReceiveTypeParameter(type) {
|
|
899
|
+
if (isUnionTypeNode(type)) {
|
|
900
|
+
for (const t of type.types) {
|
|
901
|
+
const rfn = getReceiveTypeParameter(t);
|
|
902
|
+
if (rfn) return rfn;
|
|
903
|
+
}
|
|
904
|
+
} else if (isTypeReferenceNode(type) && isIdentifier2(type.typeName) && getIdentifierName(type.typeName) === "ReceiveType" && !!type.typeArguments && type.typeArguments.length === 1) return type;
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
__name(getReceiveTypeParameter, "getReceiveTypeParameter");
|
|
908
|
+
var Cache = class {
|
|
909
|
+
static {
|
|
910
|
+
__name(this, "Cache");
|
|
911
|
+
}
|
|
912
|
+
constructor() {
|
|
913
|
+
this.resolver = {};
|
|
914
|
+
this.sourceFiles = {};
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Signals the cache to check if it needs to be cleared.
|
|
918
|
+
*/
|
|
919
|
+
tick() {
|
|
920
|
+
if (Object.keys(this.sourceFiles).length > 300) {
|
|
921
|
+
this.sourceFiles = {};
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
var ReflectionTransformer = class {
|
|
926
|
+
static {
|
|
927
|
+
__name(this, "ReflectionTransformer");
|
|
928
|
+
}
|
|
929
|
+
constructor(context, cache3 = new Cache()) {
|
|
930
|
+
this.context = context;
|
|
931
|
+
this.cache = cache3;
|
|
932
|
+
this.embedAssignType = false;
|
|
933
|
+
this.compileDeclarations = /* @__PURE__ */ new Map();
|
|
934
|
+
this.embedDeclarations = /* @__PURE__ */ new Map();
|
|
935
|
+
this.compiledDeclarations = /* @__PURE__ */ new Set();
|
|
936
|
+
this.addImports = [];
|
|
937
|
+
this.overriddenHost = false;
|
|
938
|
+
this.knownClasses = {
|
|
939
|
+
"Int8Array": ReflectionOp.int8Array,
|
|
940
|
+
"Uint8Array": ReflectionOp.uint8Array,
|
|
941
|
+
"Uint8ClampedArray": ReflectionOp.uint8ClampedArray,
|
|
942
|
+
"Int16Array": ReflectionOp.int16Array,
|
|
943
|
+
"Uint16Array": ReflectionOp.uint16Array,
|
|
944
|
+
"Int32Array": ReflectionOp.int32Array,
|
|
945
|
+
"Uint32Array": ReflectionOp.uint32Array,
|
|
946
|
+
"Float32Array": ReflectionOp.float32Array,
|
|
947
|
+
"Float64Array": ReflectionOp.float64Array,
|
|
948
|
+
"ArrayBuffer": ReflectionOp.arrayBuffer,
|
|
949
|
+
"BigInt64Array": ReflectionOp.bigInt64Array,
|
|
950
|
+
"Date": ReflectionOp.date,
|
|
951
|
+
"RegExp": ReflectionOp.regexp,
|
|
952
|
+
"String": ReflectionOp.string,
|
|
953
|
+
"Number": ReflectionOp.number,
|
|
954
|
+
"BigInt": ReflectionOp.bigint,
|
|
955
|
+
"Boolean": ReflectionOp.boolean
|
|
956
|
+
};
|
|
957
|
+
this.f = context.factory;
|
|
958
|
+
this.nodeConverter = new NodeConverter(this.f);
|
|
959
|
+
this.compilerOptions = {
|
|
960
|
+
...filterUndefined(context.getCompilerOptions())
|
|
961
|
+
};
|
|
962
|
+
this.host = createCompilerHost(this.compilerOptions);
|
|
963
|
+
this.resolver = new Resolver(this.compilerOptions, this.host, this.cache.sourceFiles);
|
|
964
|
+
this.parseConfigHost = {
|
|
965
|
+
useCaseSensitiveFileNames: true,
|
|
966
|
+
fileExists: /* @__PURE__ */ __name((path) => this.host.fileExists(path), "fileExists"),
|
|
967
|
+
readFile: /* @__PURE__ */ __name((path) => this.host.readFile(path), "readFile"),
|
|
968
|
+
readDirectory: /* @__PURE__ */ __name((path, extensions, exclude, include, depth) => {
|
|
969
|
+
if (!this.host.readDirectory) return [];
|
|
970
|
+
return this.host.readDirectory(path, extensions || [], exclude, include || [], depth);
|
|
971
|
+
}, "readDirectory")
|
|
972
|
+
};
|
|
973
|
+
{
|
|
974
|
+
const T = this.f.createIdentifier("T");
|
|
975
|
+
const Options = this.f.createIdentifier("Options");
|
|
976
|
+
this.intrinsicMetaDeclaration = this.f.createTypeAliasDeclaration([], "TypeAnnotation", [
|
|
977
|
+
this.f.createTypeParameterDeclaration([], T),
|
|
978
|
+
this.f.createTypeParameterDeclaration([], Options, void 0, this.f.createTypeReferenceNode("never"))
|
|
979
|
+
], this.f.createTypeLiteralNode([
|
|
980
|
+
this.f.createPropertySignature(void 0, "__meta", this.f.createToken(SyntaxKind2.QuestionToken), this.f.createIntersectionTypeNode([
|
|
981
|
+
this.f.createTypeReferenceNode("never"),
|
|
982
|
+
this.f.createTupleTypeNode([
|
|
983
|
+
this.f.createTypeReferenceNode(T),
|
|
984
|
+
this.f.createTypeReferenceNode(Options)
|
|
985
|
+
])
|
|
986
|
+
]))
|
|
987
|
+
]));
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
forHost(host) {
|
|
991
|
+
this.host = host;
|
|
992
|
+
this.resolver.host = host;
|
|
993
|
+
this.overriddenHost = true;
|
|
994
|
+
return this;
|
|
995
|
+
}
|
|
996
|
+
withReflection(config) {
|
|
997
|
+
const configResolver = {
|
|
998
|
+
reflectionLevel: "normal",
|
|
999
|
+
...config,
|
|
1000
|
+
path: "",
|
|
1001
|
+
mergeStrategy: "replace",
|
|
1002
|
+
compilerOptions: this.compilerOptions
|
|
1003
|
+
};
|
|
1004
|
+
const match = /* @__PURE__ */ __name((path) => {
|
|
1005
|
+
const mode = reflectionModeMatcher(config, path);
|
|
1006
|
+
return {
|
|
1007
|
+
mode,
|
|
1008
|
+
tsConfigPath: "",
|
|
1009
|
+
level: configResolver.reflectionLevel
|
|
1010
|
+
};
|
|
1011
|
+
}, "match");
|
|
1012
|
+
this.overriddenConfigResolver = {
|
|
1013
|
+
config: configResolver,
|
|
1014
|
+
match
|
|
1015
|
+
};
|
|
1016
|
+
return this;
|
|
1017
|
+
}
|
|
1018
|
+
transformBundle(node) {
|
|
1019
|
+
return node;
|
|
1020
|
+
}
|
|
1021
|
+
getTempResultIdentifier() {
|
|
1022
|
+
if (this.tempResultIdentifier) return this.tempResultIdentifier;
|
|
1023
|
+
const locals = isNodeWithLocals(this.sourceFile) ? this.sourceFile.locals : void 0;
|
|
1024
|
+
if (locals) {
|
|
1025
|
+
let found = "\u03A9r";
|
|
1026
|
+
for (let i = 0; ; i++) {
|
|
1027
|
+
found = "\u03A9r" + (i ? i : "");
|
|
1028
|
+
if (!locals.has(escapeLeadingUnderscores(found))) break;
|
|
1029
|
+
}
|
|
1030
|
+
this.tempResultIdentifier = this.f.createIdentifier(found);
|
|
1031
|
+
} else {
|
|
1032
|
+
this.tempResultIdentifier = this.f.createIdentifier("\u03A9r");
|
|
1033
|
+
}
|
|
1034
|
+
return this.tempResultIdentifier;
|
|
1035
|
+
}
|
|
1036
|
+
getConfigResolver(sourceFile) {
|
|
1037
|
+
if (this.overriddenConfigResolver) {
|
|
1038
|
+
return this.overriddenConfigResolver;
|
|
1039
|
+
}
|
|
1040
|
+
return getConfigResolver(this.cache.resolver, this.parseConfigHost, this.compilerOptions, sourceFile);
|
|
1041
|
+
}
|
|
1042
|
+
getReflectionConfig(sourceFile) {
|
|
1043
|
+
const configResolver = this.getConfigResolver(sourceFile);
|
|
1044
|
+
return configResolver.match(sourceFile.fileName);
|
|
1045
|
+
}
|
|
1046
|
+
isWithReflection(sourceFile, node) {
|
|
1047
|
+
const mode = this.getExplicitReflectionMode(sourceFile, node);
|
|
1048
|
+
if (mode === false) return false;
|
|
1049
|
+
if (!sourceFile) return true;
|
|
1050
|
+
const reflection = this.getReflectionConfig(sourceFile);
|
|
1051
|
+
if (reflection.mode === "explicit") return mode === true;
|
|
1052
|
+
return reflection.mode === "default";
|
|
1053
|
+
}
|
|
1054
|
+
transformSourceFile(sourceFile) {
|
|
1055
|
+
this.sourceFile = sourceFile;
|
|
1056
|
+
if (sourceFile.scriptKind !== ScriptKind.TS && sourceFile.scriptKind !== ScriptKind.TSX) return sourceFile;
|
|
1057
|
+
if (sourceFile.deepkitTransformed) return sourceFile;
|
|
1058
|
+
this.embedAssignType = false;
|
|
1059
|
+
this.addImports = [];
|
|
1060
|
+
const start = Date.now();
|
|
1061
|
+
const configResolver = this.getConfigResolver(sourceFile);
|
|
1062
|
+
const reflection = configResolver.match(sourceFile.fileName);
|
|
1063
|
+
Object.assign(this.compilerOptions, configResolver.config.compilerOptions);
|
|
1064
|
+
if (reflection.mode === "never") {
|
|
1065
|
+
debug(`Transform file with reflection=${reflection.mode} took ${Date.now() - start}ms (${this.getModuleType()}) ${sourceFile.fileName} via config ${reflection.tsConfigPath || "none"}.`);
|
|
1066
|
+
return sourceFile;
|
|
1067
|
+
}
|
|
1068
|
+
if (!sourceFile.locals) {
|
|
1069
|
+
ts4.bindSourceFile(sourceFile, this.compilerOptions);
|
|
1070
|
+
}
|
|
1071
|
+
if (sourceFile.kind !== SyntaxKind2.SourceFile) {
|
|
1072
|
+
if ("undefined" === typeof __require) {
|
|
1073
|
+
throw new Error(`Invalid TypeScript library imported. SyntaxKind different ${sourceFile.kind} !== ${SyntaxKind2.SourceFile}.`);
|
|
1074
|
+
}
|
|
1075
|
+
const path = __require.resolve("typescript");
|
|
1076
|
+
throw new Error(`Invalid TypeScript library imported. SyntaxKind different ${sourceFile.kind} !== ${SyntaxKind2.SourceFile}. typescript package path: ${path}`);
|
|
1077
|
+
}
|
|
1078
|
+
const visitor = /* @__PURE__ */ __name((node) => {
|
|
1079
|
+
node = visitEachChild(node, visitor, this.context);
|
|
1080
|
+
if (isInterfaceDeclaration(node) || isTypeAliasDeclaration(node) || isEnumDeclaration(node)) {
|
|
1081
|
+
if (this.isWithReflection(sourceFile, node)) {
|
|
1082
|
+
this.compileDeclarations.set(node, {
|
|
1083
|
+
name: node.name,
|
|
1084
|
+
sourceFile: this.sourceFile
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
if (isMethodDeclaration(node) && node.parent && node.body && isObjectLiteralExpression(node.parent)) {
|
|
1089
|
+
let valid = true;
|
|
1090
|
+
if (node.name.kind === SyntaxKind2.Identifier && getIdentifierName(node.name) === "default") valid = false;
|
|
1091
|
+
if (valid) {
|
|
1092
|
+
const method = this.decorateFunctionExpression(this.f.createFunctionExpression(node.modifiers, node.asteriskToken, isIdentifier2(node.name) ? node.name : void 0, node.typeParameters, node.parameters, node.type, node.body));
|
|
1093
|
+
node = this.f.createPropertyAssignment(node.name, method);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
if (isClassDeclaration(node)) {
|
|
1097
|
+
return this.decorateClass(sourceFile, node);
|
|
1098
|
+
} else if (isParameter(node) && node.parent && node.type) {
|
|
1099
|
+
const typeParameters = isConstructorDeclaration(node.parent) ? node.parent.parent.typeParameters : node.parent.typeParameters;
|
|
1100
|
+
if (!typeParameters) return node;
|
|
1101
|
+
const receiveType = getReceiveTypeParameter(node.type);
|
|
1102
|
+
if (receiveType && receiveType.typeArguments) {
|
|
1103
|
+
const first = receiveType.typeArguments[0];
|
|
1104
|
+
if (first && isTypeReferenceNode(first) && isIdentifier2(first.typeName)) {
|
|
1105
|
+
const name = getIdentifierName(first.typeName);
|
|
1106
|
+
const index = typeParameters.findIndex((v) => getIdentifierName(v.name) === name);
|
|
1107
|
+
let container = this.f.createIdentifier("globalThis");
|
|
1108
|
+
if (isArrowFunction2(node.parent)) {
|
|
1109
|
+
const next = this.getArrowFunction\u03A9PropertyAccessIdentifier(node.parent);
|
|
1110
|
+
if (!next) return node;
|
|
1111
|
+
container = next;
|
|
1112
|
+
} else if ((isFunctionDeclaration(node.parent) || isFunctionExpression(node.parent)) && node.parent.name) {
|
|
1113
|
+
container = node.parent.name;
|
|
1114
|
+
} else if (isMethodDeclaration(node.parent) && isIdentifier2(node.parent.name)) {
|
|
1115
|
+
container = this.f.createPropertyAccessExpression(this.f.createIdentifier("this"), node.parent.name);
|
|
1116
|
+
} else if (isConstructorDeclaration(node.parent)) {
|
|
1117
|
+
container = this.f.createPropertyAccessExpression(this.f.createIdentifier("this"), "constructor");
|
|
1118
|
+
}
|
|
1119
|
+
return this.f.updateParameterDeclaration(node, node.modifiers, node.dotDotDotToken, node.name, node.questionToken, receiveType, this.f.createElementAccessChain(this.f.createPropertyAccessExpression(container, this.f.createIdentifier("\u03A9")), this.f.createToken(SyntaxKind2.QuestionDotToken), this.f.createNumericLiteral(index)));
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
} else if (isClassExpression(node)) {
|
|
1123
|
+
return this.decorateClass(sourceFile, node);
|
|
1124
|
+
} else if (isFunctionExpression(node)) {
|
|
1125
|
+
return this.decorateFunctionExpression(this.injectReset\u03A9(node));
|
|
1126
|
+
} else if (isFunctionDeclaration(node)) {
|
|
1127
|
+
return this.decorateFunctionDeclaration(this.injectReset\u03A9(node));
|
|
1128
|
+
} else if (isMethodDeclaration(node) || isConstructorDeclaration(node)) {
|
|
1129
|
+
return this.injectReset\u03A9(node);
|
|
1130
|
+
} else if (isArrowFunction2(node)) {
|
|
1131
|
+
return this.decorateArrowFunction(this.injectReset\u03A9(node));
|
|
1132
|
+
} else if ((isNewExpression(node) || isCallExpression(node)) && node.typeArguments && node.typeArguments.length > 0) {
|
|
1133
|
+
if (isCallExpression(node)) {
|
|
1134
|
+
const autoTypeFunctions = [
|
|
1135
|
+
"valuesOf",
|
|
1136
|
+
"propertiesOf",
|
|
1137
|
+
"typeOf"
|
|
1138
|
+
];
|
|
1139
|
+
if (isIdentifier2(node.expression) && autoTypeFunctions.includes(getIdentifierName(node.expression))) {
|
|
1140
|
+
const args = [
|
|
1141
|
+
...node.arguments
|
|
1142
|
+
];
|
|
1143
|
+
if (!args.length) {
|
|
1144
|
+
args.push(this.f.createArrayLiteralExpression());
|
|
1145
|
+
}
|
|
1146
|
+
const type = this.getTypeOfType(node.typeArguments[0]);
|
|
1147
|
+
if (!type) return node;
|
|
1148
|
+
args.push(type);
|
|
1149
|
+
return this.f.updateCallExpression(node, node.expression, node.typeArguments, this.f.createNodeArray(args));
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
const expressionToCheck = getAssignTypeExpression(node.expression) || node.expression;
|
|
1153
|
+
if (isArrowFunction2(expressionToCheck)) {
|
|
1154
|
+
return node;
|
|
1155
|
+
}
|
|
1156
|
+
const typeExpressions = [];
|
|
1157
|
+
for (const a of node.typeArguments) {
|
|
1158
|
+
const type = this.getTypeOfType(a);
|
|
1159
|
+
typeExpressions.push(type || this.f.createIdentifier("undefined"));
|
|
1160
|
+
}
|
|
1161
|
+
let container = this.f.createIdentifier("globalThis");
|
|
1162
|
+
if (isIdentifier2(node.expression)) {
|
|
1163
|
+
container = node.expression;
|
|
1164
|
+
} else if (isPropertyAccessExpression(node.expression)) {
|
|
1165
|
+
container = node.expression;
|
|
1166
|
+
}
|
|
1167
|
+
const assignQ = this.f.createBinaryExpression(this.f.createPropertyAccessExpression(container, "\u03A9"), this.f.createToken(SyntaxKind2.EqualsToken), this.f.createArrayLiteralExpression(typeExpressions));
|
|
1168
|
+
const update = isNewExpression(node) ? this.f.updateNewExpression : this.f.updateCallExpression;
|
|
1169
|
+
if (isPropertyAccessExpression(node.expression)) {
|
|
1170
|
+
if (isCallExpression(node.expression.expression)) {
|
|
1171
|
+
const r = this.getTempResultIdentifier();
|
|
1172
|
+
const assignQ2 = this.f.createBinaryExpression(this.f.createPropertyAccessExpression(this.f.createPropertyAccessExpression(r, node.expression.name), "\u03A9"), this.f.createToken(SyntaxKind2.EqualsToken), this.f.createArrayLiteralExpression(typeExpressions));
|
|
1173
|
+
return update(node, this.f.createPropertyAccessExpression(this.f.createParenthesizedExpression(this.f.createBinaryExpression(this.f.createBinaryExpression(this.f.createBinaryExpression(r, this.f.createToken(ts4.SyntaxKind.EqualsToken), node.expression.expression), this.f.createToken(ts4.SyntaxKind.CommaToken), assignQ2), this.f.createToken(ts4.SyntaxKind.CommaToken), r)), node.expression.name), node.typeArguments, node.arguments);
|
|
1174
|
+
} else if (isParenthesizedExpression(node.expression.expression)) {
|
|
1175
|
+
const r = this.getTempResultIdentifier();
|
|
1176
|
+
const assignQ2 = this.f.createBinaryExpression(this.f.createPropertyAccessExpression(this.f.createPropertyAccessExpression(r, node.expression.name), "\u03A9"), this.f.createToken(SyntaxKind2.EqualsToken), this.f.createArrayLiteralExpression(typeExpressions));
|
|
1177
|
+
const updatedNode = update(node, this.f.updatePropertyAccessExpression(node.expression, this.f.updateParenthesizedExpression(node.expression.expression, this.f.createBinaryExpression(this.f.createBinaryExpression(this.f.createBinaryExpression(r, this.f.createToken(SyntaxKind2.EqualsToken), node.expression.expression.expression), this.f.createToken(SyntaxKind2.CommaToken), assignQ2), this.f.createToken(SyntaxKind2.CommaToken), r)), node.expression.name), node.typeArguments, node.arguments);
|
|
1178
|
+
return this.f.createParenthesizedExpression(updatedNode);
|
|
1179
|
+
} else ;
|
|
1180
|
+
}
|
|
1181
|
+
return this.f.createParenthesizedExpression(this.f.createBinaryExpression(assignQ, this.f.createToken(SyntaxKind2.CommaToken), node));
|
|
1182
|
+
}
|
|
1183
|
+
return node;
|
|
1184
|
+
}, "visitor");
|
|
1185
|
+
this.sourceFile = visitNode(this.sourceFile, visitor);
|
|
1186
|
+
const newTopStatements = [];
|
|
1187
|
+
while (true) {
|
|
1188
|
+
let allCompiled = true;
|
|
1189
|
+
for (const d of this.compileDeclarations.values()) {
|
|
1190
|
+
if (d.compiled) continue;
|
|
1191
|
+
allCompiled = false;
|
|
1192
|
+
break;
|
|
1193
|
+
}
|
|
1194
|
+
if (this.embedDeclarations.size === 0 && allCompiled) break;
|
|
1195
|
+
for (const [node, d] of [
|
|
1196
|
+
...this.compileDeclarations.entries()
|
|
1197
|
+
]) {
|
|
1198
|
+
if (d.compiled) continue;
|
|
1199
|
+
d.compiled = this.createProgramVarFromNode(node, d.name, this.sourceFile);
|
|
1200
|
+
}
|
|
1201
|
+
if (this.embedDeclarations.size) {
|
|
1202
|
+
for (const node of this.embedDeclarations.keys()) {
|
|
1203
|
+
this.compiledDeclarations.add(node);
|
|
1204
|
+
}
|
|
1205
|
+
const entries = Array.from(this.embedDeclarations.entries());
|
|
1206
|
+
this.embedDeclarations.clear();
|
|
1207
|
+
for (const [node, d] of entries) {
|
|
1208
|
+
newTopStatements.push(...this.createProgramVarFromNode(node, d.name, d.sourceFile));
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
const compileDeclarations = /* @__PURE__ */ __name((node) => {
|
|
1213
|
+
node = visitEachChild(node, compileDeclarations, this.context);
|
|
1214
|
+
if (isTypeAliasDeclaration(node) || isInterfaceDeclaration(node) || isEnumDeclaration(node)) {
|
|
1215
|
+
const d = this.compileDeclarations.get(node);
|
|
1216
|
+
if (!d) {
|
|
1217
|
+
return node;
|
|
1218
|
+
}
|
|
1219
|
+
this.compileDeclarations.delete(node);
|
|
1220
|
+
this.compiledDeclarations.add(node);
|
|
1221
|
+
if (d.compiled) {
|
|
1222
|
+
return [
|
|
1223
|
+
...d.compiled,
|
|
1224
|
+
node
|
|
1225
|
+
];
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
return node;
|
|
1229
|
+
}, "compileDeclarations");
|
|
1230
|
+
this.sourceFile = visitNode(this.sourceFile, compileDeclarations);
|
|
1231
|
+
if (this.addImports.length) {
|
|
1232
|
+
const handledIdentifier = [];
|
|
1233
|
+
for (const imp of this.addImports) {
|
|
1234
|
+
if (handledIdentifier.includes(getIdentifierName(imp.identifier))) continue;
|
|
1235
|
+
handledIdentifier.push(getIdentifierName(imp.identifier));
|
|
1236
|
+
if (this.getModuleType() === "cjs") {
|
|
1237
|
+
const test = this.f.createIdentifier(getIdentifierName(imp.identifier));
|
|
1238
|
+
const variable = this.f.createVariableStatement(void 0, this.f.createVariableDeclarationList([
|
|
1239
|
+
this.f.createVariableDeclaration(this.f.createObjectBindingPattern([
|
|
1240
|
+
this.f.createBindingElement(void 0, void 0, test)
|
|
1241
|
+
]), void 0, void 0, this.f.createCallExpression(this.f.createIdentifier("require"), void 0, [
|
|
1242
|
+
imp.from
|
|
1243
|
+
]))
|
|
1244
|
+
], NodeFlags2.Const));
|
|
1245
|
+
const typeDeclWithComment = addSyntheticLeadingComment(variable, SyntaxKind2.MultiLineCommentTrivia, "@ts-ignore", true);
|
|
1246
|
+
newTopStatements.push(typeDeclWithComment);
|
|
1247
|
+
} else {
|
|
1248
|
+
const specifier = this.f.createImportSpecifier(false, void 0, imp.identifier);
|
|
1249
|
+
const namedImports = this.f.createNamedImports([
|
|
1250
|
+
specifier
|
|
1251
|
+
]);
|
|
1252
|
+
const importStatement = this.f.createImportDeclaration(void 0, this.f.createImportClause(false, void 0, namedImports), imp.from);
|
|
1253
|
+
const typeDeclWithComment = addSyntheticLeadingComment(importStatement, SyntaxKind2.MultiLineCommentTrivia, "@ts-ignore", true);
|
|
1254
|
+
newTopStatements.push(typeDeclWithComment);
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
if (this.embedAssignType) {
|
|
1259
|
+
const assignType = this.f.createFunctionDeclaration(void 0, void 0, this.f.createIdentifier("__assignType"), void 0, [
|
|
1260
|
+
this.f.createParameterDeclaration(void 0, void 0, this.f.createIdentifier("fn"), void 0, void 0, void 0),
|
|
1261
|
+
this.f.createParameterDeclaration(void 0, void 0, this.f.createIdentifier("args"), void 0, void 0, void 0)
|
|
1262
|
+
], void 0, this.f.createBlock([
|
|
1263
|
+
this.f.createExpressionStatement(this.f.createBinaryExpression(this.f.createPropertyAccessExpression(this.f.createIdentifier("fn"), this.f.createIdentifier("__type")), this.f.createToken(SyntaxKind2.EqualsToken), this.f.createIdentifier("args"))),
|
|
1264
|
+
this.f.createReturnStatement(this.f.createIdentifier("fn"))
|
|
1265
|
+
], true));
|
|
1266
|
+
newTopStatements.push(assignType);
|
|
1267
|
+
}
|
|
1268
|
+
if (this.tempResultIdentifier) {
|
|
1269
|
+
newTopStatements.push(this.f.createVariableStatement(void 0, this.f.createVariableDeclarationList([
|
|
1270
|
+
this.f.createVariableDeclaration(this.tempResultIdentifier, void 0, void 0, void 0)
|
|
1271
|
+
], ts4.NodeFlags.None)));
|
|
1272
|
+
}
|
|
1273
|
+
if (newTopStatements.length) {
|
|
1274
|
+
const indexOfFirstLiteralExpression = this.sourceFile.statements.findIndex((v) => isExpressionStatement(v) && isStringLiteral3(v.expression));
|
|
1275
|
+
const newStatements = indexOfFirstLiteralExpression === -1 ? [
|
|
1276
|
+
...newTopStatements,
|
|
1277
|
+
...this.sourceFile.statements
|
|
1278
|
+
] : [
|
|
1279
|
+
...this.sourceFile.statements.slice(0, indexOfFirstLiteralExpression + 1),
|
|
1280
|
+
...newTopStatements,
|
|
1281
|
+
...this.sourceFile.statements.slice(indexOfFirstLiteralExpression + 1)
|
|
1282
|
+
];
|
|
1283
|
+
this.sourceFile = this.f.updateSourceFile(this.sourceFile, newStatements);
|
|
1284
|
+
}
|
|
1285
|
+
const took = Date.now() - start;
|
|
1286
|
+
debug(`Transform file with reflection=${reflection.mode} took ${took}ms (${this.getModuleType()}) ${sourceFile.fileName} via config ${reflection.tsConfigPath || "none"}.`);
|
|
1287
|
+
this.sourceFile.deepkitTransformed = true;
|
|
1288
|
+
return this.sourceFile;
|
|
1289
|
+
}
|
|
1290
|
+
getModuleType() {
|
|
1291
|
+
if (this.compilerOptions.module === ts4.ModuleKind.Node16 || this.compilerOptions.module === ts4.ModuleKind.NodeNext) {
|
|
1292
|
+
if (this.sourceFile.impliedNodeFormat === ts4.ModuleKind.ESNext) {
|
|
1293
|
+
return "esm";
|
|
1294
|
+
}
|
|
1295
|
+
return "cjs";
|
|
1296
|
+
}
|
|
1297
|
+
return this.compilerOptions.module === ts4.ModuleKind.CommonJS ? "cjs" : "esm";
|
|
1298
|
+
}
|
|
1299
|
+
getArrowFunction\u03A9PropertyAccessIdentifier(node) {
|
|
1300
|
+
let { parent } = node.original || node;
|
|
1301
|
+
if (isVariableDeclaration(parent) && isIdentifier2(parent.name)) {
|
|
1302
|
+
return parent.name;
|
|
1303
|
+
} else if (isPropertyAssignment(parent) && isIdentifier2(parent.name)) {
|
|
1304
|
+
const names = [];
|
|
1305
|
+
while (parent) {
|
|
1306
|
+
if (isObjectLiteralExpression(parent)) {
|
|
1307
|
+
parent = parent.parent;
|
|
1308
|
+
} else if (isVariableDeclaration(parent)) {
|
|
1309
|
+
names.unshift(getIdentifierName(parent.name));
|
|
1310
|
+
break;
|
|
1311
|
+
} else if (isIdentifier2(parent.name)) {
|
|
1312
|
+
names.unshift(getIdentifierName(parent.name));
|
|
1313
|
+
parent = parent.parent;
|
|
1314
|
+
} else {
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
return this.f.createIdentifier(names.join("."));
|
|
1319
|
+
}
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
injectReset\u03A9(node) {
|
|
1323
|
+
let hasReceiveType = false;
|
|
1324
|
+
for (const param of node.parameters) {
|
|
1325
|
+
if (param.type && getReceiveTypeParameter(param.type)) hasReceiveType = true;
|
|
1326
|
+
}
|
|
1327
|
+
if (!hasReceiveType) return node;
|
|
1328
|
+
let container = this.f.createIdentifier("globalThis");
|
|
1329
|
+
if (isArrowFunction2(node)) {
|
|
1330
|
+
const next = this.getArrowFunction\u03A9PropertyAccessIdentifier(node);
|
|
1331
|
+
if (!next) return node;
|
|
1332
|
+
container = next;
|
|
1333
|
+
} else if ((isFunctionDeclaration(node) || isFunctionExpression(node)) && node.name) {
|
|
1334
|
+
container = node.name;
|
|
1335
|
+
} else if (isMethodDeclaration(node) && isIdentifier2(node.name)) {
|
|
1336
|
+
container = this.f.createPropertyAccessExpression(this.f.createIdentifier("this"), node.name);
|
|
1337
|
+
} else if (isConstructorDeclaration(node)) {
|
|
1338
|
+
container = this.f.createPropertyAccessExpression(this.f.createIdentifier("this"), "constructor");
|
|
1339
|
+
}
|
|
1340
|
+
const reset = this.f.createExpressionStatement(this.f.createBinaryExpression(this.f.createPropertyAccessExpression(container, this.f.createIdentifier("\u03A9")), this.f.createToken(ts4.SyntaxKind.EqualsToken), this.f.createIdentifier("undefined")));
|
|
1341
|
+
let body = node.body && isBlock(node.body) ? node.body : void 0;
|
|
1342
|
+
let bodyStatements = node.body && isBlock(node.body) ? [
|
|
1343
|
+
...node.body.statements
|
|
1344
|
+
] : [];
|
|
1345
|
+
if (node.body) {
|
|
1346
|
+
if (isExpression(node.body)) {
|
|
1347
|
+
bodyStatements = [
|
|
1348
|
+
this.f.createReturnStatement(node.body)
|
|
1349
|
+
];
|
|
1350
|
+
}
|
|
1351
|
+
body = this.f.updateBlock(node.body, [
|
|
1352
|
+
reset,
|
|
1353
|
+
...bodyStatements
|
|
1354
|
+
]);
|
|
1355
|
+
}
|
|
1356
|
+
if (isArrowFunction2(node)) {
|
|
1357
|
+
return this.f.updateArrowFunction(node, node.modifiers, node.typeParameters, node.parameters, node.type, node.equalsGreaterThanToken, body);
|
|
1358
|
+
} else if (isFunctionDeclaration(node)) {
|
|
1359
|
+
return this.f.updateFunctionDeclaration(node, node.modifiers, node.asteriskToken, node.name, node.typeParameters, node.parameters, node.type, body);
|
|
1360
|
+
} else if (isFunctionExpression(node)) {
|
|
1361
|
+
return this.f.updateFunctionExpression(node, node.modifiers, node.asteriskToken, node.name, node.typeParameters, node.parameters, node.type, body || node.body);
|
|
1362
|
+
} else if (isMethodDeclaration(node)) {
|
|
1363
|
+
return this.f.updateMethodDeclaration(node, node.modifiers, node.asteriskToken, node.name, node.questionToken, node.typeParameters, node.parameters, node.type, body);
|
|
1364
|
+
} else if (isConstructorDeclaration(node)) {
|
|
1365
|
+
return this.f.updateConstructorDeclaration(node, node.modifiers, node.parameters, body);
|
|
1366
|
+
}
|
|
1367
|
+
return node;
|
|
1368
|
+
}
|
|
1369
|
+
createProgramVarFromNode(node, name, sourceFile) {
|
|
1370
|
+
const typeProgram = new CompilerProgram(node, sourceFile);
|
|
1371
|
+
if ((isTypeAliasDeclaration(node) || isInterfaceDeclaration(node)) && node.typeParameters) {
|
|
1372
|
+
for (const param of node.typeParameters) {
|
|
1373
|
+
if (param.default) {
|
|
1374
|
+
this.extractPackStructOfType(param.default, typeProgram);
|
|
1375
|
+
}
|
|
1376
|
+
typeProgram.pushTemplateParameter(getIdentifierName(param.name), !!param.default);
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
this.extractPackStructOfType(node, typeProgram);
|
|
1380
|
+
if (isTypeAliasDeclaration(node) || isInterfaceDeclaration(node) || isClassDeclaration(node) || isClassExpression(node)) {
|
|
1381
|
+
typeProgram.pushOp(ReflectionOp.nominal);
|
|
1382
|
+
}
|
|
1383
|
+
const typeProgramExpression = this.packOpsAndStack(typeProgram);
|
|
1384
|
+
const variable = this.f.createVariableStatement([], this.f.createVariableDeclarationList([
|
|
1385
|
+
this.f.createVariableDeclaration(this.getDeclarationVariableName(name), void 0, void 0, typeProgramExpression)
|
|
1386
|
+
], NodeFlags2.Const));
|
|
1387
|
+
if (hasModifier(node, SyntaxKind2.ExportKeyword)) {
|
|
1388
|
+
const exportNode = this.f.createExportDeclaration(void 0, false, this.f.createNamedExports([
|
|
1389
|
+
this.f.createExportSpecifier(false, this.getDeclarationVariableName(name), this.getDeclarationVariableName(name))
|
|
1390
|
+
]));
|
|
1391
|
+
return [
|
|
1392
|
+
variable,
|
|
1393
|
+
exportNode
|
|
1394
|
+
];
|
|
1395
|
+
}
|
|
1396
|
+
return [
|
|
1397
|
+
variable
|
|
1398
|
+
];
|
|
1399
|
+
}
|
|
1400
|
+
extractPackStructOfExpression(node, program) {
|
|
1401
|
+
switch (node.kind) {
|
|
1402
|
+
case SyntaxKind2.StringLiteral: {
|
|
1403
|
+
program.pushOp(ReflectionOp.string);
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
case SyntaxKind2.NumericLiteral: {
|
|
1407
|
+
program.pushOp(ReflectionOp.number);
|
|
1408
|
+
return;
|
|
1409
|
+
}
|
|
1410
|
+
case SyntaxKind2.FalseKeyword:
|
|
1411
|
+
case SyntaxKind2.TrueKeyword: {
|
|
1412
|
+
program.pushOp(ReflectionOp.boolean);
|
|
1413
|
+
return;
|
|
1414
|
+
}
|
|
1415
|
+
case SyntaxKind2.BigIntLiteral: {
|
|
1416
|
+
program.pushOp(ReflectionOp.bigint);
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
//Symbol() is a function call, so we need to check for that
|
|
1420
|
+
case SyntaxKind2.CallExpression: {
|
|
1421
|
+
const call = node;
|
|
1422
|
+
if (isIdentifier2(call.expression) && getIdentifierName(call.expression) === "Symbol") {
|
|
1423
|
+
program.pushOp(ReflectionOp.symbol);
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1426
|
+
break;
|
|
1427
|
+
}
|
|
1428
|
+
//new Date()
|
|
1429
|
+
case SyntaxKind2.NewExpression: {
|
|
1430
|
+
const call = node;
|
|
1431
|
+
if (isIdentifier2(call.expression)) {
|
|
1432
|
+
const map = {
|
|
1433
|
+
"Date": ReflectionOp.date,
|
|
1434
|
+
"RegExp": ReflectionOp.regexp,
|
|
1435
|
+
"Uint8Array": ReflectionOp.uint8Array,
|
|
1436
|
+
"Uint8ClampedArray": ReflectionOp.uint8ClampedArray,
|
|
1437
|
+
"Uint16Array": ReflectionOp.uint16Array,
|
|
1438
|
+
"Uint32Array": ReflectionOp.uint32Array,
|
|
1439
|
+
"Int8Array": ReflectionOp.int8Array,
|
|
1440
|
+
"Int16Array": ReflectionOp.int16Array,
|
|
1441
|
+
"Int32Array": ReflectionOp.int32Array,
|
|
1442
|
+
"Float32Array": ReflectionOp.float32Array,
|
|
1443
|
+
"Float64Array": ReflectionOp.float64Array,
|
|
1444
|
+
"ArrayBuffer": ReflectionOp.arrayBuffer
|
|
1445
|
+
};
|
|
1446
|
+
const op = map[getIdentifierName(call.expression)];
|
|
1447
|
+
if (op) {
|
|
1448
|
+
program.pushOp(op);
|
|
1449
|
+
return;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
break;
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
program.pushOp(ReflectionOp.never);
|
|
1456
|
+
}
|
|
1457
|
+
parseDefaultValue(kind, defaultValue) {
|
|
1458
|
+
switch (kind) {
|
|
1459
|
+
case SyntaxKind2.NumberKeyword: {
|
|
1460
|
+
return Number.isInteger(Number.parseFloat(defaultValue)) ? Number.parseInt(defaultValue) : Number.parseFloat(defaultValue);
|
|
1461
|
+
}
|
|
1462
|
+
case SyntaxKind2.BooleanKeyword: {
|
|
1463
|
+
return Boolean(defaultValue?.trim());
|
|
1464
|
+
}
|
|
1465
|
+
case SyntaxKind2.BigIntKeyword: {
|
|
1466
|
+
return Number.parseInt(defaultValue);
|
|
1467
|
+
}
|
|
1468
|
+
case SyntaxKind2.ObjectKeyword: {
|
|
1469
|
+
return JSON.parse(defaultValue);
|
|
1470
|
+
}
|
|
1471
|
+
case SyntaxKind2.NullKeyword: {
|
|
1472
|
+
return null;
|
|
1473
|
+
}
|
|
1474
|
+
case SyntaxKind2.TrueKeyword: {
|
|
1475
|
+
return true;
|
|
1476
|
+
}
|
|
1477
|
+
case SyntaxKind2.FalseKeyword: {
|
|
1478
|
+
return false;
|
|
1479
|
+
}
|
|
1480
|
+
case SyntaxKind2.UndefinedKeyword: {
|
|
1481
|
+
return void 0;
|
|
1482
|
+
}
|
|
1483
|
+
case SyntaxKind2.StringLiteral:
|
|
1484
|
+
case SyntaxKind2.StringKeyword:
|
|
1485
|
+
default: {
|
|
1486
|
+
if (defaultValue?.trim().toLowerCase() === "false") {
|
|
1487
|
+
return false;
|
|
1488
|
+
} else if (defaultValue?.trim().toLowerCase() === "true") {
|
|
1489
|
+
return true;
|
|
1490
|
+
}
|
|
1491
|
+
return defaultValue;
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
parseTagReflection(node) {
|
|
1496
|
+
const tags = {};
|
|
1497
|
+
tags.hidden = extractJSDocBoolean(this.sourceFile, node, "hidden");
|
|
1498
|
+
tags.ignore = extractJSDocBoolean(this.sourceFile, node, "ignore");
|
|
1499
|
+
tags.internal = extractJSDocBoolean(this.sourceFile, node, "internal");
|
|
1500
|
+
tags.alias = extractJSDocArray(this.sourceFile, node, "alias");
|
|
1501
|
+
tags.permission = extractJSDocArray(this.sourceFile, node, "permission");
|
|
1502
|
+
tags.domain = extractJSDocAttribute(this.sourceFile, node, "domain");
|
|
1503
|
+
if (this.getReflectionConfig(this.sourceFile).level === "verbose") {
|
|
1504
|
+
tags.title = extractJSDocAttribute(this.sourceFile, node, "title");
|
|
1505
|
+
}
|
|
1506
|
+
return tags;
|
|
1507
|
+
}
|
|
1508
|
+
extractTagReflections(program, node) {
|
|
1509
|
+
if (!node) return;
|
|
1510
|
+
const tags = this.parseTagReflection(node);
|
|
1511
|
+
if (Object.entries(tags).filter(([, value]) => value !== void 0 && (!Array.isArray(value) || value.length > 0) && (typeof value !== "string" || value?.trim().replace(/^"/gm, "").replace(/"$/gm, ""))).length > 0) {
|
|
1512
|
+
program.pushOp(ReflectionOp.tags, program.findOrAddStackEntry(this.f.createObjectLiteralExpression(this.f.createNodeArray(Object.entries(tags).filter(([, value]) => value !== void 0 && (!Array.isArray(value) || value.length > 0) && (typeof value !== "string" || value.trim().replace(/^"/gm, "").replace(/"$/gm, ""))).map(([key, value]) => {
|
|
1513
|
+
return this.f.createPropertyAssignment(key, Array.isArray(value) ? this.f.createArrayLiteralExpression(value.map((item) => this.valueToExpression(item))) : typeof value === "boolean" ? value ? this.f.createTrue() : this.f.createFalse() : this.valueToExpression(value));
|
|
1514
|
+
})))));
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
extractDefaultValueReflection(program, node) {
|
|
1518
|
+
if (!node) return;
|
|
1519
|
+
let defaultValue = extractJSDocAttribute(this.sourceFile, node, "defaultValue");
|
|
1520
|
+
if (!defaultValue) {
|
|
1521
|
+
defaultValue = extractJSDocAttribute(this.sourceFile, node, "default");
|
|
1522
|
+
}
|
|
1523
|
+
if (defaultValue) {
|
|
1524
|
+
program.pushOp(ReflectionOp.defaultValue, program.findOrAddStackEntry(this.parseDefaultValue((node.kind === SyntaxKind2.ClassDeclaration || node.kind === SyntaxKind2.ClassExpression ? node?.name?.parent?.kind : node.kind === SyntaxKind2.PropertySignature || node.kind === SyntaxKind2.PropertyDeclaration ? node.type?.kind : node.kind) || SyntaxKind2.StringKeyword, defaultValue.trim().replace(/^"/gm, "").replace(/"$/gm, ""))));
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
extractDescriptionReflection(program, node) {
|
|
1528
|
+
if (!node || this.getReflectionConfig(this.sourceFile).level !== "verbose") return;
|
|
1529
|
+
const description = extractJSDocAttribute(this.sourceFile, node, "description");
|
|
1530
|
+
if (description) {
|
|
1531
|
+
program.pushOp(ReflectionOp.description, program.findOrAddStackEntry(description.trim().replace(/^"/gm, "").replace(/"$/gm, "")));
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
extractPackStructOfType(node, program) {
|
|
1535
|
+
if (isParenthesizedTypeNode(node)) return this.extractPackStructOfType(node.type, program);
|
|
1536
|
+
switch (node.kind) {
|
|
1537
|
+
case SyntaxKind2.StringKeyword: {
|
|
1538
|
+
program.pushOp(ReflectionOp.string);
|
|
1539
|
+
break;
|
|
1540
|
+
}
|
|
1541
|
+
case SyntaxKind2.NumberKeyword: {
|
|
1542
|
+
program.pushOp(ReflectionOp.number);
|
|
1543
|
+
break;
|
|
1544
|
+
}
|
|
1545
|
+
case SyntaxKind2.BooleanKeyword: {
|
|
1546
|
+
program.pushOp(ReflectionOp.boolean);
|
|
1547
|
+
break;
|
|
1548
|
+
}
|
|
1549
|
+
case SyntaxKind2.BigIntKeyword: {
|
|
1550
|
+
program.pushOp(ReflectionOp.bigint);
|
|
1551
|
+
break;
|
|
1552
|
+
}
|
|
1553
|
+
case SyntaxKind2.VoidKeyword: {
|
|
1554
|
+
program.pushOp(ReflectionOp.void);
|
|
1555
|
+
break;
|
|
1556
|
+
}
|
|
1557
|
+
case SyntaxKind2.UnknownKeyword: {
|
|
1558
|
+
program.pushOp(ReflectionOp.unknown);
|
|
1559
|
+
break;
|
|
1560
|
+
}
|
|
1561
|
+
case SyntaxKind2.ObjectKeyword: {
|
|
1562
|
+
program.pushOp(ReflectionOp.object);
|
|
1563
|
+
break;
|
|
1564
|
+
}
|
|
1565
|
+
case SyntaxKind2.SymbolKeyword: {
|
|
1566
|
+
program.pushOp(ReflectionOp.symbol);
|
|
1567
|
+
break;
|
|
1568
|
+
}
|
|
1569
|
+
case SyntaxKind2.NullKeyword: {
|
|
1570
|
+
program.pushOp(ReflectionOp.null);
|
|
1571
|
+
break;
|
|
1572
|
+
}
|
|
1573
|
+
case SyntaxKind2.NeverKeyword: {
|
|
1574
|
+
program.pushOp(ReflectionOp.never);
|
|
1575
|
+
break;
|
|
1576
|
+
}
|
|
1577
|
+
case SyntaxKind2.AnyKeyword: {
|
|
1578
|
+
program.pushOp(ReflectionOp.any);
|
|
1579
|
+
break;
|
|
1580
|
+
}
|
|
1581
|
+
case SyntaxKind2.UndefinedKeyword: {
|
|
1582
|
+
program.pushOp(ReflectionOp.undefined);
|
|
1583
|
+
break;
|
|
1584
|
+
}
|
|
1585
|
+
case SyntaxKind2.TrueKeyword: {
|
|
1586
|
+
program.pushOp(ReflectionOp.literal, program.pushStack(this.f.createTrue()));
|
|
1587
|
+
break;
|
|
1588
|
+
}
|
|
1589
|
+
case SyntaxKind2.FalseKeyword: {
|
|
1590
|
+
program.pushOp(ReflectionOp.literal, program.pushStack(this.f.createFalse()));
|
|
1591
|
+
break;
|
|
1592
|
+
}
|
|
1593
|
+
case SyntaxKind2.ClassDeclaration:
|
|
1594
|
+
case SyntaxKind2.ClassExpression: {
|
|
1595
|
+
const narrowed = node;
|
|
1596
|
+
if (node) {
|
|
1597
|
+
const members = [];
|
|
1598
|
+
if (narrowed.typeParameters) {
|
|
1599
|
+
for (const typeParameter of narrowed.typeParameters) {
|
|
1600
|
+
const name = getNameAsString(typeParameter.name);
|
|
1601
|
+
if (typeParameter.default) {
|
|
1602
|
+
this.extractPackStructOfType(typeParameter.default, program);
|
|
1603
|
+
}
|
|
1604
|
+
program.pushTemplateParameter(name, !!typeParameter.default);
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
if (narrowed.heritageClauses) {
|
|
1608
|
+
for (const heritage of narrowed.heritageClauses) {
|
|
1609
|
+
if (heritage.token === SyntaxKind2.ExtendsKeyword) {
|
|
1610
|
+
for (const extendType of heritage.types) {
|
|
1611
|
+
program.pushFrame();
|
|
1612
|
+
if (extendType.typeArguments) {
|
|
1613
|
+
for (const typeArgument of extendType.typeArguments) {
|
|
1614
|
+
this.extractPackStructOfType(typeArgument, program);
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
const index = program.pushStack(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, this.nodeConverter.toExpression(extendType.expression)));
|
|
1618
|
+
program.pushOp(ReflectionOp.classReference, index);
|
|
1619
|
+
program.popFrameImplicit();
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
for (const member of narrowed.members) {
|
|
1625
|
+
const name = getNameAsString(member.name);
|
|
1626
|
+
if (name) {
|
|
1627
|
+
const has = members.some((v) => getNameAsString(v.name) === name);
|
|
1628
|
+
if (has) continue;
|
|
1629
|
+
}
|
|
1630
|
+
members.push(member);
|
|
1631
|
+
this.extractPackStructOfType(member, program);
|
|
1632
|
+
}
|
|
1633
|
+
program.pushOp(ReflectionOp.class);
|
|
1634
|
+
if (narrowed.heritageClauses) {
|
|
1635
|
+
for (const heritageClause of narrowed.heritageClauses) {
|
|
1636
|
+
if (heritageClause.token === SyntaxKind2.ExtendsKeyword) {
|
|
1637
|
+
const first = heritageClause.types[0];
|
|
1638
|
+
if (isExpressionWithTypeArguments(first) && first.typeArguments) {
|
|
1639
|
+
for (const typeArgument of first.typeArguments) {
|
|
1640
|
+
this.extractPackStructOfType(typeArgument, program);
|
|
1641
|
+
}
|
|
1642
|
+
program.pushOp(ReflectionOp.classExtends, first.typeArguments.length);
|
|
1643
|
+
}
|
|
1644
|
+
} else if (heritageClause.token === SyntaxKind2.ImplementsKeyword) {
|
|
1645
|
+
for (const type of heritageClause.types) {
|
|
1646
|
+
this.extractPackStructOfTypeReference(type, program);
|
|
1647
|
+
}
|
|
1648
|
+
program.pushOp(ReflectionOp.implements, heritageClause.types.length);
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
if (narrowed.name) this.resolveTypeName(getIdentifierName(narrowed.name), program);
|
|
1653
|
+
this.extractDefaultValueReflection(program, narrowed.name?.parent);
|
|
1654
|
+
this.extractDescriptionReflection(program, narrowed.name?.parent);
|
|
1655
|
+
this.extractTagReflections(program, narrowed.name?.parent);
|
|
1656
|
+
}
|
|
1657
|
+
break;
|
|
1658
|
+
}
|
|
1659
|
+
case SyntaxKind2.IntersectionType: {
|
|
1660
|
+
const narrowed = node;
|
|
1661
|
+
program.pushFrame();
|
|
1662
|
+
for (const type of narrowed.types) {
|
|
1663
|
+
this.extractPackStructOfType(type, program);
|
|
1664
|
+
}
|
|
1665
|
+
program.pushOp(ReflectionOp.intersection);
|
|
1666
|
+
program.popFrameImplicit();
|
|
1667
|
+
break;
|
|
1668
|
+
}
|
|
1669
|
+
case SyntaxKind2.MappedType: {
|
|
1670
|
+
const narrowed = node;
|
|
1671
|
+
program.pushFrame();
|
|
1672
|
+
program.pushVariable(getIdentifierName(narrowed.typeParameter.name));
|
|
1673
|
+
const constraint = getEffectiveConstraintOfTypeParameter(narrowed.typeParameter);
|
|
1674
|
+
if (constraint) {
|
|
1675
|
+
this.extractPackStructOfType(constraint, program);
|
|
1676
|
+
} else {
|
|
1677
|
+
program.pushOp(ReflectionOp.never);
|
|
1678
|
+
}
|
|
1679
|
+
let modifier = 0;
|
|
1680
|
+
if (narrowed.questionToken) {
|
|
1681
|
+
if (narrowed.questionToken.kind === SyntaxKind2.QuestionToken) {
|
|
1682
|
+
modifier |= 1;
|
|
1683
|
+
}
|
|
1684
|
+
if (narrowed.questionToken.kind === SyntaxKind2.MinusToken) {
|
|
1685
|
+
modifier |= 2;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
if (narrowed.readonlyToken) {
|
|
1689
|
+
if (narrowed.readonlyToken.kind === SyntaxKind2.ReadonlyKeyword) {
|
|
1690
|
+
modifier |= 4;
|
|
1691
|
+
}
|
|
1692
|
+
if (narrowed.readonlyToken.kind === SyntaxKind2.MinusToken) {
|
|
1693
|
+
modifier |= 8;
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
program.pushCoRoutine();
|
|
1697
|
+
if (narrowed.nameType) program.pushFrame();
|
|
1698
|
+
if (narrowed.type) {
|
|
1699
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
1700
|
+
} else {
|
|
1701
|
+
program.pushOp(ReflectionOp.never);
|
|
1702
|
+
}
|
|
1703
|
+
if (narrowed.nameType) {
|
|
1704
|
+
this.extractPackStructOfType(narrowed.nameType, program);
|
|
1705
|
+
program.pushOp(ReflectionOp.tuple);
|
|
1706
|
+
program.popFrameImplicit();
|
|
1707
|
+
}
|
|
1708
|
+
const coRoutineIndex = program.popCoRoutine();
|
|
1709
|
+
if (narrowed.nameType) {
|
|
1710
|
+
program.pushOp(ReflectionOp.mappedType2, coRoutineIndex, modifier);
|
|
1711
|
+
} else {
|
|
1712
|
+
program.pushOp(ReflectionOp.mappedType, coRoutineIndex, modifier);
|
|
1713
|
+
}
|
|
1714
|
+
program.popFrameImplicit();
|
|
1715
|
+
break;
|
|
1716
|
+
}
|
|
1717
|
+
case SyntaxKind2.TypeAliasDeclaration: {
|
|
1718
|
+
let narrowed = node;
|
|
1719
|
+
if (program.sourceFile && getNameAsString(narrowed.name) === "TypeAnnotation") {
|
|
1720
|
+
const attribute = extractJSDocAttribute(program.sourceFile, narrowed, "intrinsic");
|
|
1721
|
+
if (attribute !== void 0) {
|
|
1722
|
+
narrowed = this.intrinsicMetaDeclaration;
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
1726
|
+
if (narrowed.name) this.resolveTypeName(getIdentifierName(narrowed.name), program);
|
|
1727
|
+
break;
|
|
1728
|
+
}
|
|
1729
|
+
case SyntaxKind2.TypeLiteral:
|
|
1730
|
+
case SyntaxKind2.InterfaceDeclaration: {
|
|
1731
|
+
const narrowed = node;
|
|
1732
|
+
let descriptionNode = narrowed;
|
|
1733
|
+
program.pushFrame();
|
|
1734
|
+
if (isInterfaceDeclaration(narrowed) && narrowed.heritageClauses) {
|
|
1735
|
+
for (const heritage of narrowed.heritageClauses) {
|
|
1736
|
+
if (heritage.token === SyntaxKind2.ExtendsKeyword) {
|
|
1737
|
+
for (const extendType of heritage.types) {
|
|
1738
|
+
this.extractPackStructOfTypeReference(extendType, program);
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
for (const member of narrowed.members) {
|
|
1744
|
+
this.extractPackStructOfType(member, program);
|
|
1745
|
+
}
|
|
1746
|
+
program.pushOp(ReflectionOp.objectLiteral);
|
|
1747
|
+
if (isTypeLiteralNode(narrowed)) {
|
|
1748
|
+
descriptionNode = narrowed.parent;
|
|
1749
|
+
}
|
|
1750
|
+
this.extractDefaultValueReflection(program, descriptionNode);
|
|
1751
|
+
this.extractDescriptionReflection(program, descriptionNode);
|
|
1752
|
+
this.extractTagReflections(program, descriptionNode);
|
|
1753
|
+
if (isInterfaceDeclaration(narrowed)) {
|
|
1754
|
+
if (narrowed.name) this.resolveTypeName(getIdentifierName(narrowed.name), program);
|
|
1755
|
+
}
|
|
1756
|
+
program.popFrameImplicit();
|
|
1757
|
+
break;
|
|
1758
|
+
}
|
|
1759
|
+
case SyntaxKind2.TypeReference: {
|
|
1760
|
+
this.extractPackStructOfTypeReference(node, program);
|
|
1761
|
+
break;
|
|
1762
|
+
}
|
|
1763
|
+
case SyntaxKind2.ArrayType: {
|
|
1764
|
+
this.extractPackStructOfType(node.elementType, program);
|
|
1765
|
+
program.pushOp(ReflectionOp.array);
|
|
1766
|
+
break;
|
|
1767
|
+
}
|
|
1768
|
+
case SyntaxKind2.RestType: {
|
|
1769
|
+
let type = node.type;
|
|
1770
|
+
if (isArrayTypeNode(type)) {
|
|
1771
|
+
type = type.elementType;
|
|
1772
|
+
}
|
|
1773
|
+
this.extractPackStructOfType(type, program);
|
|
1774
|
+
program.pushOp(ReflectionOp.rest);
|
|
1775
|
+
break;
|
|
1776
|
+
}
|
|
1777
|
+
case SyntaxKind2.TupleType: {
|
|
1778
|
+
program.pushFrame();
|
|
1779
|
+
for (const element of node.elements) {
|
|
1780
|
+
if (isOptionalTypeNode(element)) {
|
|
1781
|
+
this.extractPackStructOfType(element.type, program);
|
|
1782
|
+
program.pushOp(ReflectionOp.tupleMember);
|
|
1783
|
+
program.pushOp(ReflectionOp.optional);
|
|
1784
|
+
} else if (isNamedTupleMember(element)) {
|
|
1785
|
+
if (element.dotDotDotToken) {
|
|
1786
|
+
let type = element.type;
|
|
1787
|
+
if (isArrayTypeNode(type)) {
|
|
1788
|
+
type = type.elementType;
|
|
1789
|
+
}
|
|
1790
|
+
this.extractPackStructOfType(type, program);
|
|
1791
|
+
program.pushOp(ReflectionOp.rest);
|
|
1792
|
+
} else {
|
|
1793
|
+
this.extractPackStructOfType(element.type, program);
|
|
1794
|
+
}
|
|
1795
|
+
const index = program.findOrAddStackEntry(getIdentifierName(element.name));
|
|
1796
|
+
program.pushOp(ReflectionOp.namedTupleMember, index);
|
|
1797
|
+
if (element.questionToken) {
|
|
1798
|
+
program.pushOp(ReflectionOp.optional);
|
|
1799
|
+
}
|
|
1800
|
+
} else {
|
|
1801
|
+
this.extractPackStructOfType(element, program);
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
program.pushOp(ReflectionOp.tuple);
|
|
1805
|
+
program.popFrameImplicit();
|
|
1806
|
+
break;
|
|
1807
|
+
}
|
|
1808
|
+
case SyntaxKind2.PropertySignature: {
|
|
1809
|
+
const narrowed = node;
|
|
1810
|
+
if (narrowed.type) {
|
|
1811
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
1812
|
+
const name = getPropertyName(this.f, narrowed.name);
|
|
1813
|
+
program.pushOp(ReflectionOp.propertySignature, program.findOrAddStackEntry(name));
|
|
1814
|
+
if (narrowed.questionToken) {
|
|
1815
|
+
program.pushOp(ReflectionOp.optional);
|
|
1816
|
+
}
|
|
1817
|
+
if (hasModifier(narrowed, SyntaxKind2.ReadonlyKeyword)) program.pushOp(ReflectionOp.readonly);
|
|
1818
|
+
else {
|
|
1819
|
+
const readonly = extractJSDocBoolean(this.sourceFile, narrowed, "readonly");
|
|
1820
|
+
if (readonly) {
|
|
1821
|
+
program.pushOp(ReflectionOp.readonly);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
this.extractDefaultValueReflection(program, narrowed);
|
|
1825
|
+
this.extractDescriptionReflection(program, narrowed);
|
|
1826
|
+
this.extractTagReflections(program, narrowed);
|
|
1827
|
+
} else {
|
|
1828
|
+
program.pushOp(ReflectionOp.unknown);
|
|
1829
|
+
}
|
|
1830
|
+
break;
|
|
1831
|
+
}
|
|
1832
|
+
case SyntaxKind2.PropertyDeclaration: {
|
|
1833
|
+
const narrowed = node;
|
|
1834
|
+
if (false === this.getExplicitReflectionMode(program.sourceFile, narrowed)) return;
|
|
1835
|
+
if (narrowed.type) {
|
|
1836
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
1837
|
+
} else if (narrowed.initializer) {
|
|
1838
|
+
this.extractPackStructOfExpression(narrowed.initializer, program);
|
|
1839
|
+
} else {
|
|
1840
|
+
program.pushOp(ReflectionOp.unknown);
|
|
1841
|
+
}
|
|
1842
|
+
program.pushOp(ReflectionOp.property, program.findOrAddStackEntry(getPropertyName(this.f, narrowed.name)));
|
|
1843
|
+
if (narrowed.questionToken) program.pushOp(ReflectionOp.optional);
|
|
1844
|
+
if (hasModifier(narrowed, SyntaxKind2.ReadonlyKeyword)) {
|
|
1845
|
+
program.pushOp(ReflectionOp.readonly);
|
|
1846
|
+
} else {
|
|
1847
|
+
const readonly = extractJSDocBoolean(this.sourceFile, narrowed, "readonly");
|
|
1848
|
+
if (readonly) {
|
|
1849
|
+
program.pushOp(ReflectionOp.readonly);
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
if (hasModifier(narrowed, SyntaxKind2.PrivateKeyword)) program.pushOp(ReflectionOp.private);
|
|
1853
|
+
if (hasModifier(narrowed, SyntaxKind2.ProtectedKeyword)) program.pushOp(ReflectionOp.protected);
|
|
1854
|
+
if (hasModifier(narrowed, SyntaxKind2.AbstractKeyword)) program.pushOp(ReflectionOp.abstract);
|
|
1855
|
+
if (hasModifier(narrowed, SyntaxKind2.StaticKeyword)) program.pushOp(ReflectionOp.static);
|
|
1856
|
+
if (narrowed.initializer) {
|
|
1857
|
+
program.pushOp(ReflectionOp.defaultValue, program.findOrAddStackEntry(this.f.createFunctionExpression(void 0, void 0, void 0, void 0, void 0, void 0, this.f.createBlock([
|
|
1858
|
+
this.f.createReturnStatement(narrowed.initializer)
|
|
1859
|
+
]))));
|
|
1860
|
+
} else {
|
|
1861
|
+
this.extractDefaultValueReflection(program, narrowed);
|
|
1862
|
+
}
|
|
1863
|
+
this.extractDescriptionReflection(program, narrowed);
|
|
1864
|
+
this.extractTagReflections(program, narrowed);
|
|
1865
|
+
break;
|
|
1866
|
+
}
|
|
1867
|
+
case SyntaxKind2.ConditionalType: {
|
|
1868
|
+
const narrowed = node;
|
|
1869
|
+
const distributiveOverIdentifier = isTypeReferenceNode(narrowed.checkType) && isIdentifier2(narrowed.checkType.typeName) ? narrowed.checkType.typeName : void 0;
|
|
1870
|
+
if (distributiveOverIdentifier) {
|
|
1871
|
+
program.pushFrame();
|
|
1872
|
+
this.extractPackStructOfType(narrowed.checkType, program);
|
|
1873
|
+
program.pushVariable(getIdentifierName(distributiveOverIdentifier));
|
|
1874
|
+
program.pushCoRoutine();
|
|
1875
|
+
}
|
|
1876
|
+
program.pushConditionalFrame();
|
|
1877
|
+
this.extractPackStructOfType(narrowed.checkType, program);
|
|
1878
|
+
this.extractPackStructOfType(narrowed.extendsType, program);
|
|
1879
|
+
program.pushOp(ReflectionOp.extends);
|
|
1880
|
+
program.pushCoRoutine();
|
|
1881
|
+
this.extractPackStructOfType(narrowed.trueType, program);
|
|
1882
|
+
const trueProgram = program.popCoRoutine();
|
|
1883
|
+
program.pushCoRoutine();
|
|
1884
|
+
this.extractPackStructOfType(narrowed.falseType, program);
|
|
1885
|
+
const falseProgram = program.popCoRoutine();
|
|
1886
|
+
program.pushOp(ReflectionOp.jumpCondition, trueProgram, falseProgram);
|
|
1887
|
+
program.moveFrame();
|
|
1888
|
+
if (distributiveOverIdentifier) {
|
|
1889
|
+
const coRoutineIndex = program.popCoRoutine();
|
|
1890
|
+
program.pushOp(ReflectionOp.distribute, coRoutineIndex);
|
|
1891
|
+
program.popFrameImplicit();
|
|
1892
|
+
}
|
|
1893
|
+
break;
|
|
1894
|
+
}
|
|
1895
|
+
case SyntaxKind2.InferType: {
|
|
1896
|
+
const narrowed = node;
|
|
1897
|
+
const frame = program.findConditionalFrame();
|
|
1898
|
+
if (frame) {
|
|
1899
|
+
const typeParameterName = getIdentifierName(narrowed.typeParameter.name);
|
|
1900
|
+
let variable = program.findVariable(typeParameterName);
|
|
1901
|
+
if (!variable) {
|
|
1902
|
+
program.pushVariable(typeParameterName, frame);
|
|
1903
|
+
variable = program.findVariable(typeParameterName);
|
|
1904
|
+
if (!variable) throw new Error("Could not find inserted infer variable");
|
|
1905
|
+
}
|
|
1906
|
+
program.pushOp(ReflectionOp.infer, variable.frameOffset, variable.stackIndex);
|
|
1907
|
+
} else {
|
|
1908
|
+
program.pushOp(ReflectionOp.never);
|
|
1909
|
+
}
|
|
1910
|
+
break;
|
|
1911
|
+
}
|
|
1912
|
+
case SyntaxKind2.MethodSignature:
|
|
1913
|
+
case SyntaxKind2.MethodDeclaration:
|
|
1914
|
+
case SyntaxKind2.Constructor:
|
|
1915
|
+
case SyntaxKind2.ArrowFunction:
|
|
1916
|
+
case SyntaxKind2.FunctionExpression:
|
|
1917
|
+
case SyntaxKind2.ConstructSignature:
|
|
1918
|
+
case SyntaxKind2.ConstructorType:
|
|
1919
|
+
case SyntaxKind2.FunctionType:
|
|
1920
|
+
case SyntaxKind2.CallSignature:
|
|
1921
|
+
case SyntaxKind2.FunctionDeclaration: {
|
|
1922
|
+
const narrowed = node;
|
|
1923
|
+
if (false === this.getExplicitReflectionMode(program.sourceFile, narrowed)) {
|
|
1924
|
+
program.pushOp(ReflectionOp.any);
|
|
1925
|
+
return;
|
|
1926
|
+
}
|
|
1927
|
+
const name = isCallSignatureDeclaration(node) ? "" : isConstructorTypeNode(narrowed) || isConstructSignatureDeclaration(node) ? "new" : isConstructorDeclaration(narrowed) ? "constructor" : getPropertyName(this.f, narrowed.name);
|
|
1928
|
+
if (!narrowed.type && narrowed.parameters.length === 0 && !name) return;
|
|
1929
|
+
program.pushFrame();
|
|
1930
|
+
for (let i = 0; i < narrowed.parameters.length; i++) {
|
|
1931
|
+
const parameter = narrowed.parameters[i];
|
|
1932
|
+
const parameterName = isIdentifier2(parameter.name) ? getNameAsString(parameter.name) : "param" + i;
|
|
1933
|
+
const type = parameter.type ? parameter.dotDotDotToken && isArrayTypeNode(parameter.type) ? parameter.type.elementType : parameter.type : void 0;
|
|
1934
|
+
if (type) {
|
|
1935
|
+
this.extractPackStructOfType(type, program);
|
|
1936
|
+
} else {
|
|
1937
|
+
program.pushOp(ReflectionOp.any);
|
|
1938
|
+
}
|
|
1939
|
+
if (parameter.dotDotDotToken) {
|
|
1940
|
+
program.pushOp(ReflectionOp.rest);
|
|
1941
|
+
}
|
|
1942
|
+
program.pushOp(ReflectionOp.parameter, program.findOrAddStackEntry(parameterName));
|
|
1943
|
+
if (parameter.questionToken) program.pushOp(ReflectionOp.optional);
|
|
1944
|
+
if (hasModifier(parameter, SyntaxKind2.PublicKeyword)) program.pushOp(ReflectionOp.public);
|
|
1945
|
+
if (hasModifier(parameter, SyntaxKind2.PrivateKeyword)) program.pushOp(ReflectionOp.private);
|
|
1946
|
+
if (hasModifier(parameter, SyntaxKind2.ProtectedKeyword)) program.pushOp(ReflectionOp.protected);
|
|
1947
|
+
if (hasModifier(parameter, SyntaxKind2.ReadonlyKeyword)) program.pushOp(ReflectionOp.readonly);
|
|
1948
|
+
else {
|
|
1949
|
+
let readonly = extractJSDocBoolean(this.sourceFile, parameter, "readonly");
|
|
1950
|
+
if (readonly) {
|
|
1951
|
+
program.pushOp(ReflectionOp.readonly);
|
|
1952
|
+
} else {
|
|
1953
|
+
readonly = extractJSDocBoolean(this.sourceFile, narrowed, "readonly");
|
|
1954
|
+
if (readonly) {
|
|
1955
|
+
program.pushOp(ReflectionOp.readonly);
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
if (parameter.initializer && parameter.type && !getReceiveTypeParameter(parameter.type)) {
|
|
1960
|
+
program.pushOp(ReflectionOp.defaultValue, program.findOrAddStackEntry(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, parameter.initializer)));
|
|
1961
|
+
} else {
|
|
1962
|
+
this.extractDefaultValueReflection(program, parameter);
|
|
1963
|
+
}
|
|
1964
|
+
this.extractDescriptionReflection(program, parameter);
|
|
1965
|
+
}
|
|
1966
|
+
if (narrowed.type) {
|
|
1967
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
1968
|
+
} else {
|
|
1969
|
+
program.pushOp(ReflectionOp.any);
|
|
1970
|
+
}
|
|
1971
|
+
program.pushOp(isCallSignatureDeclaration(node) ? ReflectionOp.callSignature : isMethodSignature(narrowed) || isConstructSignatureDeclaration(narrowed) ? ReflectionOp.methodSignature : isMethodDeclaration(narrowed) || isConstructorDeclaration(narrowed) ? ReflectionOp.method : ReflectionOp.function, program.findOrAddStackEntry(name));
|
|
1972
|
+
if (isMethodDeclaration(narrowed)) {
|
|
1973
|
+
if (hasModifier(narrowed, SyntaxKind2.PrivateKeyword)) program.pushOp(ReflectionOp.private);
|
|
1974
|
+
if (hasModifier(narrowed, SyntaxKind2.ProtectedKeyword)) program.pushOp(ReflectionOp.protected);
|
|
1975
|
+
if (hasModifier(narrowed, SyntaxKind2.AbstractKeyword)) program.pushOp(ReflectionOp.abstract);
|
|
1976
|
+
if (hasModifier(narrowed, SyntaxKind2.StaticKeyword)) program.pushOp(ReflectionOp.static);
|
|
1977
|
+
}
|
|
1978
|
+
this.extractDefaultValueReflection(program, narrowed);
|
|
1979
|
+
this.extractDescriptionReflection(program, narrowed);
|
|
1980
|
+
this.extractTagReflections(program, narrowed);
|
|
1981
|
+
program.popFrameImplicit();
|
|
1982
|
+
break;
|
|
1983
|
+
}
|
|
1984
|
+
case SyntaxKind2.LiteralType: {
|
|
1985
|
+
const narrowed = node;
|
|
1986
|
+
if (narrowed.literal.kind === SyntaxKind2.NullKeyword) {
|
|
1987
|
+
program.pushOp(ReflectionOp.null);
|
|
1988
|
+
} else {
|
|
1989
|
+
program.pushOp(ReflectionOp.literal, program.findOrAddStackEntry(narrowed.literal));
|
|
1990
|
+
}
|
|
1991
|
+
break;
|
|
1992
|
+
}
|
|
1993
|
+
case SyntaxKind2.TemplateLiteralType: {
|
|
1994
|
+
const narrowed = node;
|
|
1995
|
+
program.pushFrame();
|
|
1996
|
+
if (narrowed.head.rawText) {
|
|
1997
|
+
program.pushOp(ReflectionOp.literal, program.findOrAddStackEntry(narrowed.head.rawText));
|
|
1998
|
+
}
|
|
1999
|
+
for (const span of narrowed.templateSpans) {
|
|
2000
|
+
this.extractPackStructOfType(span.type, program);
|
|
2001
|
+
if (span.literal.rawText) {
|
|
2002
|
+
program.pushOp(ReflectionOp.literal, program.findOrAddStackEntry(span.literal.rawText));
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
program.pushOp(ReflectionOp.templateLiteral);
|
|
2006
|
+
program.popFrameImplicit();
|
|
2007
|
+
break;
|
|
2008
|
+
}
|
|
2009
|
+
case SyntaxKind2.UnionType: {
|
|
2010
|
+
const narrowed = node;
|
|
2011
|
+
if (narrowed.types.length === 0) ; else if (narrowed.types.length === 1) {
|
|
2012
|
+
this.extractPackStructOfType(narrowed.types[0], program);
|
|
2013
|
+
} else {
|
|
2014
|
+
program.pushFrame();
|
|
2015
|
+
for (const subType of narrowed.types) {
|
|
2016
|
+
this.extractPackStructOfType(subType, program);
|
|
2017
|
+
}
|
|
2018
|
+
program.pushOp(ReflectionOp.union);
|
|
2019
|
+
program.popFrameImplicit();
|
|
2020
|
+
}
|
|
2021
|
+
break;
|
|
2022
|
+
}
|
|
2023
|
+
case SyntaxKind2.EnumDeclaration: {
|
|
2024
|
+
const narrowed = node;
|
|
2025
|
+
program.pushFrame();
|
|
2026
|
+
for (const type of narrowed.members) {
|
|
2027
|
+
const name = getPropertyName(this.f, type.name);
|
|
2028
|
+
program.pushOp(ReflectionOp.enumMember, program.findOrAddStackEntry(name));
|
|
2029
|
+
this.extractDescriptionReflection(program, type);
|
|
2030
|
+
if (type.initializer) {
|
|
2031
|
+
program.pushOp(ReflectionOp.defaultValue, program.findOrAddStackEntry(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, type.initializer)));
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
program.pushOp(ReflectionOp.enum);
|
|
2035
|
+
this.extractDefaultValueReflection(program, narrowed);
|
|
2036
|
+
this.extractDescriptionReflection(program, narrowed);
|
|
2037
|
+
this.extractTagReflections(program, narrowed);
|
|
2038
|
+
if (narrowed.name) this.resolveTypeName(getIdentifierName(narrowed.name), program);
|
|
2039
|
+
program.popFrameImplicit();
|
|
2040
|
+
break;
|
|
2041
|
+
}
|
|
2042
|
+
case SyntaxKind2.IndexSignature: {
|
|
2043
|
+
const narrowed = node;
|
|
2044
|
+
if (narrowed.parameters.length && narrowed.parameters[0].type) {
|
|
2045
|
+
this.extractPackStructOfType(narrowed.parameters[0].type, program);
|
|
2046
|
+
} else {
|
|
2047
|
+
program.pushOp(ReflectionOp.any);
|
|
2048
|
+
}
|
|
2049
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
2050
|
+
program.pushOp(ReflectionOp.indexSignature);
|
|
2051
|
+
break;
|
|
2052
|
+
}
|
|
2053
|
+
case SyntaxKind2.TypeQuery: {
|
|
2054
|
+
const narrowed = node;
|
|
2055
|
+
if (isIdentifier2(narrowed.exprName)) {
|
|
2056
|
+
const resolved = this.resolveDeclaration(narrowed.exprName);
|
|
2057
|
+
if (resolved && findSourceFile(resolved.declaration) !== this.sourceFile && resolved.importDeclaration) {
|
|
2058
|
+
ensureImportIsEmitted(resolved.importDeclaration, narrowed.exprName);
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
const expression = serializeEntityNameAsExpression(this.f, narrowed.exprName);
|
|
2062
|
+
program.pushOp(ReflectionOp.typeof, program.pushStack(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, expression)));
|
|
2063
|
+
break;
|
|
2064
|
+
}
|
|
2065
|
+
case SyntaxKind2.TypeOperator: {
|
|
2066
|
+
const narrowed = node;
|
|
2067
|
+
if (narrowed.type.kind === SyntaxKind2.ThisType) {
|
|
2068
|
+
program.pushOp(ReflectionOp.any);
|
|
2069
|
+
break;
|
|
2070
|
+
}
|
|
2071
|
+
switch (narrowed.operator) {
|
|
2072
|
+
case SyntaxKind2.KeyOfKeyword: {
|
|
2073
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
2074
|
+
program.pushOp(ReflectionOp.keyof);
|
|
2075
|
+
break;
|
|
2076
|
+
}
|
|
2077
|
+
case SyntaxKind2.ReadonlyKeyword: {
|
|
2078
|
+
this.extractPackStructOfType(narrowed.type, program);
|
|
2079
|
+
program.pushOp(ReflectionOp.readonly);
|
|
2080
|
+
break;
|
|
2081
|
+
}
|
|
2082
|
+
default: {
|
|
2083
|
+
program.pushOp(ReflectionOp.never);
|
|
2084
|
+
}
|
|
2085
|
+
}
|
|
2086
|
+
break;
|
|
2087
|
+
}
|
|
2088
|
+
case SyntaxKind2.IndexedAccessType: {
|
|
2089
|
+
const narrowed = node;
|
|
2090
|
+
this.extractPackStructOfType(narrowed.objectType, program);
|
|
2091
|
+
this.extractPackStructOfType(narrowed.indexType, program);
|
|
2092
|
+
program.pushOp(ReflectionOp.indexAccess);
|
|
2093
|
+
break;
|
|
2094
|
+
}
|
|
2095
|
+
case SyntaxKind2.Identifier: {
|
|
2096
|
+
const narrowed = node;
|
|
2097
|
+
const variable = program.findVariable(getIdentifierName(narrowed));
|
|
2098
|
+
if (variable) {
|
|
2099
|
+
program.pushOp(ReflectionOp.loads, variable.frameOffset, variable.stackIndex);
|
|
2100
|
+
} else {
|
|
2101
|
+
program.pushOp(ReflectionOp.never);
|
|
2102
|
+
}
|
|
2103
|
+
break;
|
|
2104
|
+
}
|
|
2105
|
+
default: {
|
|
2106
|
+
program.pushOp(ReflectionOp.never);
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
getGlobalLibs() {
|
|
2111
|
+
if (this.cache.globalSourceFiles) return this.cache.globalSourceFiles;
|
|
2112
|
+
this.cache.globalSourceFiles = [];
|
|
2113
|
+
const options = {
|
|
2114
|
+
...this.compilerOptions
|
|
2115
|
+
};
|
|
2116
|
+
if (options.target && options.target === ScriptTarget2.ESNext) {
|
|
2117
|
+
options.target = ScriptTarget2.ES2022;
|
|
2118
|
+
}
|
|
2119
|
+
const libs = knownLibFilesForCompilerOptions(options, ts4);
|
|
2120
|
+
for (const lib of libs) {
|
|
2121
|
+
if (this.isExcluded(lib)) continue;
|
|
2122
|
+
const sourceFile = this.resolver.resolveSourceFile(this.sourceFile, this.f.createStringLiteral("typescript/lib/" + lib.replace(".d.ts", "")));
|
|
2123
|
+
if (!sourceFile) continue;
|
|
2124
|
+
this.cache.globalSourceFiles.push(sourceFile);
|
|
2125
|
+
}
|
|
2126
|
+
return this.cache.globalSourceFiles;
|
|
2127
|
+
}
|
|
2128
|
+
/**
|
|
2129
|
+
* This is a custom resolver based on populated `locals` from the binder. It uses a custom resolution algorithm since
|
|
2130
|
+
* we have no access to the binder/TypeChecker directly and instantiating a TypeChecker per file/transformer is incredible slow.
|
|
2131
|
+
*/
|
|
2132
|
+
resolveDeclaration(typeName) {
|
|
2133
|
+
let current = typeName.parent;
|
|
2134
|
+
if (typeName.kind === SyntaxKind2.QualifiedName) return;
|
|
2135
|
+
let declaration = void 0;
|
|
2136
|
+
while (current) {
|
|
2137
|
+
if (isNodeWithLocals(current) && current.locals) {
|
|
2138
|
+
const found = current.locals.get(typeName.escapedText);
|
|
2139
|
+
if (found && found.declarations && found.declarations[0]) {
|
|
2140
|
+
if (!isParameter(found.declarations[0])) {
|
|
2141
|
+
declaration = found.declarations[0];
|
|
2142
|
+
break;
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
if (current.kind === SyntaxKind2.SourceFile) break;
|
|
2147
|
+
current = current.parent;
|
|
2148
|
+
}
|
|
2149
|
+
if (!declaration) {
|
|
2150
|
+
for (const file of this.getGlobalLibs()) {
|
|
2151
|
+
const globals = getGlobalsOfSourceFile(file);
|
|
2152
|
+
if (!globals) continue;
|
|
2153
|
+
const symbol = globals.get(typeName.escapedText);
|
|
2154
|
+
if (symbol && symbol.declarations && symbol.declarations[0]) {
|
|
2155
|
+
declaration = symbol.declarations[0];
|
|
2156
|
+
break;
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
let importDeclaration = void 0;
|
|
2161
|
+
let typeOnly = false;
|
|
2162
|
+
if (declaration && isImportSpecifier(declaration)) {
|
|
2163
|
+
if (declaration.isTypeOnly) typeOnly = true;
|
|
2164
|
+
importDeclaration = declaration.parent.parent.parent;
|
|
2165
|
+
} else if (declaration && isImportDeclaration(declaration)) {
|
|
2166
|
+
importDeclaration = declaration;
|
|
2167
|
+
} else if (declaration && isImportClause(declaration)) {
|
|
2168
|
+
importDeclaration = declaration.parent;
|
|
2169
|
+
}
|
|
2170
|
+
if (importDeclaration) {
|
|
2171
|
+
if (importDeclaration.importClause && importDeclaration.importClause.isTypeOnly) typeOnly = true;
|
|
2172
|
+
declaration = this.resolveImportSpecifier(getEscapedText(typeName), importDeclaration, this.sourceFile);
|
|
2173
|
+
}
|
|
2174
|
+
if (declaration && declaration.kind === SyntaxKind2.TypeParameter && declaration.parent.kind === SyntaxKind2.TypeAliasDeclaration) {
|
|
2175
|
+
declaration = declaration.parent;
|
|
2176
|
+
}
|
|
2177
|
+
if (!declaration) return;
|
|
2178
|
+
return {
|
|
2179
|
+
declaration,
|
|
2180
|
+
importDeclaration,
|
|
2181
|
+
typeOnly
|
|
2182
|
+
};
|
|
2183
|
+
}
|
|
2184
|
+
getDeclarationVariableName(typeName) {
|
|
2185
|
+
if (isIdentifier2(typeName)) {
|
|
2186
|
+
return this.f.createIdentifier("__\u03A9" + getIdentifierName(typeName));
|
|
2187
|
+
}
|
|
2188
|
+
function joinQualifiedName2(name) {
|
|
2189
|
+
if (isIdentifier2(name)) return getIdentifierName(name);
|
|
2190
|
+
return joinQualifiedName2(name.left) + "_" + getIdentifierName(name.right);
|
|
2191
|
+
}
|
|
2192
|
+
__name(joinQualifiedName2, "joinQualifiedName");
|
|
2193
|
+
return this.f.createIdentifier("__\u03A9" + joinQualifiedName2(typeName));
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* The semantic of isExcluded is different from checking if the fileName is part
|
|
2197
|
+
* of reflection config option. isExcluded checks if the file should be excluded
|
|
2198
|
+
* via the exclude option. mainly used to exclude globals and external libraries.
|
|
2199
|
+
*/
|
|
2200
|
+
isExcluded(fileName) {
|
|
2201
|
+
const resolver = this.overriddenConfigResolver || getConfigResolver(this.cache.resolver, this.parseConfigHost, this.compilerOptions, this.sourceFile);
|
|
2202
|
+
const res = reflectionModeMatcher({
|
|
2203
|
+
reflection: "default",
|
|
2204
|
+
exclude: resolver.config.exclude
|
|
2205
|
+
}, fileName);
|
|
2206
|
+
return res === "never";
|
|
2207
|
+
}
|
|
2208
|
+
extractPackStructOfTypeReference(type, program) {
|
|
2209
|
+
const typeName = isTypeReferenceNode(type) ? type.typeName : isIdentifier2(type.expression) ? type.expression : void 0;
|
|
2210
|
+
if (!typeName) {
|
|
2211
|
+
program.pushOp(ReflectionOp.any);
|
|
2212
|
+
return;
|
|
2213
|
+
}
|
|
2214
|
+
if (isIdentifier2(typeName) && getIdentifierName(typeName) === "InlineRuntimeType" && type.typeArguments && type.typeArguments[0] && isTypeQueryNode(type.typeArguments[0])) {
|
|
2215
|
+
const expression = serializeEntityNameAsExpression(this.f, type.typeArguments[0].exprName);
|
|
2216
|
+
program.pushOp(ReflectionOp.arg, program.pushStack(expression));
|
|
2217
|
+
return;
|
|
2218
|
+
}
|
|
2219
|
+
if (isIdentifier2(typeName) && getIdentifierName(typeName) !== "constructor" && this.knownClasses[getIdentifierName(typeName)]) {
|
|
2220
|
+
const name = getIdentifierName(typeName);
|
|
2221
|
+
const op = this.knownClasses[name];
|
|
2222
|
+
program.pushOp(op);
|
|
2223
|
+
} else if (isIdentifier2(typeName) && getIdentifierName(typeName) === "Promise") {
|
|
2224
|
+
if (type.typeArguments && type.typeArguments[0]) {
|
|
2225
|
+
this.extractPackStructOfType(type.typeArguments[0], program);
|
|
2226
|
+
} else {
|
|
2227
|
+
program.pushOp(ReflectionOp.any);
|
|
2228
|
+
}
|
|
2229
|
+
program.pushOp(ReflectionOp.promise);
|
|
2230
|
+
} else if (isIdentifier2(typeName) && getIdentifierName(typeName) === "integer") {
|
|
2231
|
+
program.pushOp(ReflectionOp.numberBrand, TypeNumberBrand.integer);
|
|
2232
|
+
} else if (isIdentifier2(typeName) && getIdentifierName(typeName) !== "constructor" && TypeNumberBrand[getIdentifierName(typeName)] !== void 0) {
|
|
2233
|
+
program.pushOp(ReflectionOp.numberBrand, TypeNumberBrand[getIdentifierName(typeName)]);
|
|
2234
|
+
} else {
|
|
2235
|
+
if (isIdentifier2(typeName)) {
|
|
2236
|
+
const variable = program.findVariable(getIdentifierName(typeName));
|
|
2237
|
+
if (variable) {
|
|
2238
|
+
program.pushOp(ReflectionOp.loads, variable.frameOffset, variable.stackIndex);
|
|
2239
|
+
return;
|
|
2240
|
+
}
|
|
2241
|
+
} else if (isInferTypeNode(typeName)) {
|
|
2242
|
+
this.extractPackStructOfType(typeName, program);
|
|
2243
|
+
return;
|
|
2244
|
+
}
|
|
2245
|
+
const resolved = this.resolveDeclaration(typeName);
|
|
2246
|
+
if (!resolved) {
|
|
2247
|
+
if (isQualifiedName(typeName)) {
|
|
2248
|
+
if (isIdentifier2(typeName.left)) {
|
|
2249
|
+
const resolved2 = this.resolveDeclaration(typeName.left);
|
|
2250
|
+
if (resolved2 && isEnumDeclaration(resolved2.declaration)) {
|
|
2251
|
+
let lastExpression;
|
|
2252
|
+
let indexValue = 0;
|
|
2253
|
+
for (const member of resolved2.declaration.members) {
|
|
2254
|
+
if (getNameAsString(member.name) === getNameAsString(typeName.right)) {
|
|
2255
|
+
if (member.initializer) {
|
|
2256
|
+
program.pushOp(ReflectionOp.arg, program.pushStack(this.nodeConverter.toExpression(member.initializer)));
|
|
2257
|
+
} else if (lastExpression) {
|
|
2258
|
+
const exp = this.nodeConverter.toExpression(lastExpression);
|
|
2259
|
+
program.pushOp(ReflectionOp.arg, program.pushStack(this.f.createBinaryExpression(exp, SyntaxKind2.PlusToken, this.nodeConverter.toExpression(indexValue))));
|
|
2260
|
+
} else {
|
|
2261
|
+
program.pushOp(ReflectionOp.arg, program.pushStack(this.nodeConverter.toExpression(indexValue)));
|
|
2262
|
+
}
|
|
2263
|
+
return;
|
|
2264
|
+
} else {
|
|
2265
|
+
indexValue++;
|
|
2266
|
+
if (member.initializer) {
|
|
2267
|
+
lastExpression = member.initializer;
|
|
2268
|
+
indexValue = 0;
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
program.pushOp(ReflectionOp.never);
|
|
2276
|
+
debug2(`Could not resolve ${getNameAsString(typeName)} in ${program.sourceFile?.fileName || "intrinsic"}`);
|
|
2277
|
+
return;
|
|
2278
|
+
}
|
|
2279
|
+
let declaration = resolved.declaration;
|
|
2280
|
+
const declarationSourceFile = findSourceFile(declaration);
|
|
2281
|
+
const isGlobal = !declarationSourceFile || resolved.importDeclaration === void 0 && declarationSourceFile.fileName !== this.sourceFile.fileName;
|
|
2282
|
+
const isFromImport = resolved.importDeclaration !== void 0;
|
|
2283
|
+
if (isVariableDeclaration(declaration)) {
|
|
2284
|
+
if (declaration.type) {
|
|
2285
|
+
declaration = declaration.type;
|
|
2286
|
+
} else if (declaration.initializer) {
|
|
2287
|
+
declaration = declaration.initializer;
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
if (isModuleDeclaration(declaration) && resolved.importDeclaration) {
|
|
2291
|
+
if (isIdentifier2(typeName)) ensureImportIsEmitted(resolved.importDeclaration, typeName);
|
|
2292
|
+
program.pushOp(ReflectionOp.typeof, program.pushStack(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, serializeEntityNameAsExpression(this.f, typeName))));
|
|
2293
|
+
} else if (isTypeAliasDeclaration(declaration) || isInterfaceDeclaration(declaration) || isEnumDeclaration(declaration)) {
|
|
2294
|
+
const name = getNameAsString(typeName);
|
|
2295
|
+
if (name === "Array") {
|
|
2296
|
+
if (type.typeArguments && type.typeArguments[0]) {
|
|
2297
|
+
this.extractPackStructOfType(type.typeArguments[0], program);
|
|
2298
|
+
} else {
|
|
2299
|
+
program.pushOp(ReflectionOp.any);
|
|
2300
|
+
}
|
|
2301
|
+
program.pushOp(ReflectionOp.array);
|
|
2302
|
+
return;
|
|
2303
|
+
} else if (name === "Function") {
|
|
2304
|
+
program.pushFrame();
|
|
2305
|
+
const index2 = program.pushStack(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, this.f.createIdentifier("Function")));
|
|
2306
|
+
program.pushOp(ReflectionOp.functionReference, index2);
|
|
2307
|
+
program.popFrameImplicit();
|
|
2308
|
+
return;
|
|
2309
|
+
} else if (name === "Set") {
|
|
2310
|
+
if (type.typeArguments && type.typeArguments[0]) {
|
|
2311
|
+
this.extractPackStructOfType(type.typeArguments[0], program);
|
|
2312
|
+
} else {
|
|
2313
|
+
program.pushOp(ReflectionOp.any);
|
|
2314
|
+
}
|
|
2315
|
+
program.pushOp(ReflectionOp.set);
|
|
2316
|
+
return;
|
|
2317
|
+
} else if (name === "Map") {
|
|
2318
|
+
if (type.typeArguments && type.typeArguments[0]) {
|
|
2319
|
+
this.extractPackStructOfType(type.typeArguments[0], program);
|
|
2320
|
+
} else {
|
|
2321
|
+
program.pushOp(ReflectionOp.any);
|
|
2322
|
+
}
|
|
2323
|
+
if (type.typeArguments && type.typeArguments[1]) {
|
|
2324
|
+
this.extractPackStructOfType(type.typeArguments[1], program);
|
|
2325
|
+
} else {
|
|
2326
|
+
program.pushOp(ReflectionOp.any);
|
|
2327
|
+
}
|
|
2328
|
+
program.pushOp(ReflectionOp.map);
|
|
2329
|
+
return;
|
|
2330
|
+
}
|
|
2331
|
+
const runtimeTypeName = this.getDeclarationVariableName(typeName);
|
|
2332
|
+
if (!this.compiledDeclarations.has(declaration) && !this.compileDeclarations.has(declaration)) {
|
|
2333
|
+
if (declarationSourceFile && this.isExcluded(declarationSourceFile.fileName)) {
|
|
2334
|
+
program.pushOp(ReflectionOp.any);
|
|
2335
|
+
return;
|
|
2336
|
+
}
|
|
2337
|
+
if (isGlobal) {
|
|
2338
|
+
this.embedDeclarations.set(declaration, {
|
|
2339
|
+
name: typeName,
|
|
2340
|
+
sourceFile: declarationSourceFile
|
|
2341
|
+
});
|
|
2342
|
+
} else if (isFromImport) {
|
|
2343
|
+
if (resolved.importDeclaration) {
|
|
2344
|
+
if (resolved.typeOnly) {
|
|
2345
|
+
this.resolveTypeOnlyImport(typeName, program);
|
|
2346
|
+
return;
|
|
2347
|
+
}
|
|
2348
|
+
if (declarationSourceFile.fileName.endsWith(".d.ts")) {
|
|
2349
|
+
const resolverDecVariable = this.resolveImportSpecifier(getEscapedText(runtimeTypeName), resolved.importDeclaration, this.sourceFile);
|
|
2350
|
+
if (!resolverDecVariable) {
|
|
2351
|
+
debug2(`Symbol ${runtimeTypeName.escapedText} not found in ${declarationSourceFile.fileName}`);
|
|
2352
|
+
this.resolveTypeOnlyImport(typeName, program);
|
|
2353
|
+
return;
|
|
2354
|
+
}
|
|
2355
|
+
this.addImports.push({
|
|
2356
|
+
identifier: runtimeTypeName,
|
|
2357
|
+
from: resolved.importDeclaration.moduleSpecifier
|
|
2358
|
+
});
|
|
2359
|
+
} else {
|
|
2360
|
+
const reflection = this.getReflectionConfig(declarationSourceFile);
|
|
2361
|
+
if (reflection.mode === "never") {
|
|
2362
|
+
this.resolveTypeOnlyImport(typeName, program);
|
|
2363
|
+
return;
|
|
2364
|
+
}
|
|
2365
|
+
const declarationReflection = this.isWithReflection(declarationSourceFile, declaration);
|
|
2366
|
+
if (!declarationReflection) {
|
|
2367
|
+
this.resolveTypeOnlyImport(typeName, program);
|
|
2368
|
+
return;
|
|
2369
|
+
}
|
|
2370
|
+
this.addImports.push({
|
|
2371
|
+
identifier: runtimeTypeName,
|
|
2372
|
+
from: resolved.importDeclaration.moduleSpecifier
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
} else {
|
|
2377
|
+
const reflection = this.isWithReflection(program.sourceFile, declaration);
|
|
2378
|
+
if (!reflection) {
|
|
2379
|
+
this.resolveTypeOnlyImport(typeName, program);
|
|
2380
|
+
return;
|
|
2381
|
+
}
|
|
2382
|
+
this.compileDeclarations.set(declaration, {
|
|
2383
|
+
name: typeName,
|
|
2384
|
+
sourceFile: declarationSourceFile
|
|
2385
|
+
});
|
|
2386
|
+
}
|
|
2387
|
+
}
|
|
2388
|
+
const index = program.pushStack(program.forNode === declaration ? 0 : this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, runtimeTypeName));
|
|
2389
|
+
if (type.typeArguments) {
|
|
2390
|
+
for (const argument of type.typeArguments) {
|
|
2391
|
+
this.extractPackStructOfType(argument, program);
|
|
2392
|
+
}
|
|
2393
|
+
program.pushOp(ReflectionOp.inlineCall, index, type.typeArguments.length);
|
|
2394
|
+
} else {
|
|
2395
|
+
program.pushOp(ReflectionOp.inline, index);
|
|
2396
|
+
}
|
|
2397
|
+
} else if (isClassDeclaration(declaration) || isFunctionDeclaration(declaration) || isFunctionExpression(declaration) || isArrowFunction2(declaration)) {
|
|
2398
|
+
if (resolved.typeOnly) {
|
|
2399
|
+
this.resolveTypeOnlyImport(typeName, program);
|
|
2400
|
+
return;
|
|
2401
|
+
}
|
|
2402
|
+
const reflection = declarationSourceFile?.fileName.endsWith(".d.ts") || this.isWithReflection(program.sourceFile, declaration);
|
|
2403
|
+
if (!reflection) {
|
|
2404
|
+
this.resolveTypeOnlyImport(typeName, program);
|
|
2405
|
+
return;
|
|
2406
|
+
}
|
|
2407
|
+
if (resolved.importDeclaration && isIdentifier2(typeName)) ensureImportIsEmitted(resolved.importDeclaration, typeName);
|
|
2408
|
+
program.pushFrame();
|
|
2409
|
+
if (type.typeArguments) {
|
|
2410
|
+
for (const typeArgument of type.typeArguments) {
|
|
2411
|
+
this.extractPackStructOfType(typeArgument, program);
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
const body = isIdentifier2(typeName) ? typeName : this.createAccessorForEntityName(typeName);
|
|
2415
|
+
const index = program.pushStack(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, body));
|
|
2416
|
+
program.pushOp(isClassDeclaration(declaration) ? ReflectionOp.classReference : ReflectionOp.functionReference, index);
|
|
2417
|
+
program.popFrameImplicit();
|
|
2418
|
+
} else if (isTypeParameterDeclaration(declaration)) {
|
|
2419
|
+
this.resolveTypeParameter(declaration, type, program);
|
|
2420
|
+
} else {
|
|
2421
|
+
this.extractPackStructOfType(declaration, program);
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
}
|
|
2425
|
+
/**
|
|
2426
|
+
* Returns the class declaration, function/arrow declaration, or block where type was used.
|
|
2427
|
+
*/
|
|
2428
|
+
getTypeUser(type) {
|
|
2429
|
+
let current = type;
|
|
2430
|
+
while (current) {
|
|
2431
|
+
if (current.kind === SyntaxKind2.Block) return current;
|
|
2432
|
+
if (current.kind === SyntaxKind2.ClassDeclaration) return current;
|
|
2433
|
+
if (current.kind === SyntaxKind2.ClassExpression) return current;
|
|
2434
|
+
if (current.kind === SyntaxKind2.Constructor) return current.parent;
|
|
2435
|
+
if (current.kind === SyntaxKind2.MethodDeclaration) return current.parent;
|
|
2436
|
+
if (current.kind === SyntaxKind2.ArrowFunction || current.kind === SyntaxKind2.FunctionDeclaration || current.kind === SyntaxKind2.FunctionExpression) return current;
|
|
2437
|
+
current = current.parent;
|
|
2438
|
+
}
|
|
2439
|
+
return current;
|
|
2440
|
+
}
|
|
2441
|
+
/**
|
|
2442
|
+
* With this function we want to check if `type` is used in the signature itself from the parent of `declaration`.
|
|
2443
|
+
* If so, we do not try to infer the type from runtime values.
|
|
2444
|
+
*
|
|
2445
|
+
* Examples where we do not infer from runtime, `type` being `T` and `declaration` being `<T>` (return false):
|
|
2446
|
+
*
|
|
2447
|
+
* ```typescript
|
|
2448
|
+
* class User<T> {
|
|
2449
|
+
* config: T;
|
|
2450
|
+
* }
|
|
2451
|
+
*
|
|
2452
|
+
* class User<T> {
|
|
2453
|
+
* constructor(public config: T) {}
|
|
2454
|
+
* }
|
|
2455
|
+
*
|
|
2456
|
+
* function do<T>(item: T): void {}
|
|
2457
|
+
* function do<T>(item: T): T {}
|
|
2458
|
+
* ```
|
|
2459
|
+
*
|
|
2460
|
+
* Examples where we infer from runtime (return true):
|
|
2461
|
+
*
|
|
2462
|
+
* ```typescript
|
|
2463
|
+
* function do<T>(item: T) {
|
|
2464
|
+
* return typeOf<T>; //<-- because of that
|
|
2465
|
+
* }
|
|
2466
|
+
*
|
|
2467
|
+
* function do<T>(item: T) {
|
|
2468
|
+
* class A {
|
|
2469
|
+
* config: T; //<-- because of that
|
|
2470
|
+
* }
|
|
2471
|
+
* return A;
|
|
2472
|
+
* }
|
|
2473
|
+
*
|
|
2474
|
+
* function do<T>(item: T) {
|
|
2475
|
+
* class A {
|
|
2476
|
+
* doIt() {
|
|
2477
|
+
* class B {
|
|
2478
|
+
* config: T; //<-- because of that
|
|
2479
|
+
* }
|
|
2480
|
+
* return B;
|
|
2481
|
+
* }
|
|
2482
|
+
* }
|
|
2483
|
+
* return A;
|
|
2484
|
+
* }
|
|
2485
|
+
*
|
|
2486
|
+
* function do<T>(item: T) {
|
|
2487
|
+
* class A {
|
|
2488
|
+
* doIt(): T { //<-- because of that
|
|
2489
|
+
* }
|
|
2490
|
+
* }
|
|
2491
|
+
* return A;
|
|
2492
|
+
* }
|
|
2493
|
+
* ```
|
|
2494
|
+
*/
|
|
2495
|
+
needsToBeInferred(declaration, type) {
|
|
2496
|
+
const declarationUser = this.getTypeUser(declaration);
|
|
2497
|
+
const typeUser = this.getTypeUser(type);
|
|
2498
|
+
return declarationUser !== typeUser;
|
|
2499
|
+
}
|
|
2500
|
+
resolveTypeOnlyImport(entityName, program) {
|
|
2501
|
+
program.pushOp(ReflectionOp.any);
|
|
2502
|
+
const typeName = ts4.isIdentifier(entityName) ? getIdentifierName(entityName) : getIdentifierName(entityName.right);
|
|
2503
|
+
this.resolveTypeName(typeName, program);
|
|
2504
|
+
}
|
|
2505
|
+
resolveTypeName(typeName, program) {
|
|
2506
|
+
if (!typeName) return;
|
|
2507
|
+
program.pushOp(ReflectionOp.typeName, program.findOrAddStackEntry(typeName));
|
|
2508
|
+
}
|
|
2509
|
+
resolveTypeParameter(declaration, type, program) {
|
|
2510
|
+
const isUsedInFunction = isFunctionLike(declaration.parent);
|
|
2511
|
+
const resolveRuntimeTypeParameter = isUsedInFunction && program.isResolveFunctionParameters(declaration.parent) || this.needsToBeInferred(declaration, type);
|
|
2512
|
+
if (resolveRuntimeTypeParameter) {
|
|
2513
|
+
const argumentName = declaration.name.escapedText;
|
|
2514
|
+
const foundUsers = [];
|
|
2515
|
+
if (isUsedInFunction) {
|
|
2516
|
+
for (const parameter of declaration.parent.parameters) {
|
|
2517
|
+
if (!parameter.type) continue;
|
|
2518
|
+
let found = false;
|
|
2519
|
+
const searchArgument = /* @__PURE__ */ __name((node) => {
|
|
2520
|
+
node = visitEachChild(node, searchArgument, this.context);
|
|
2521
|
+
if (isTypeReferenceNode(node) && isIdentifier2(node.typeName) && node.typeName.escapedText === argumentName) {
|
|
2522
|
+
found = true;
|
|
2523
|
+
node = this.f.createInferTypeNode(declaration);
|
|
2524
|
+
}
|
|
2525
|
+
return node;
|
|
2526
|
+
}, "searchArgument");
|
|
2527
|
+
if (isIdentifier2(parameter.name)) {
|
|
2528
|
+
const updatedParameterType = visitEachChild(parameter.type, searchArgument, this.context);
|
|
2529
|
+
if (found) {
|
|
2530
|
+
foundUsers.push({
|
|
2531
|
+
type: updatedParameterType,
|
|
2532
|
+
parameterName: parameter.name
|
|
2533
|
+
});
|
|
2534
|
+
}
|
|
2535
|
+
}
|
|
2536
|
+
}
|
|
2537
|
+
}
|
|
2538
|
+
if (foundUsers.length) {
|
|
2539
|
+
if (foundUsers.length > 1) ;
|
|
2540
|
+
const isReceiveType = foundUsers.find((v) => isTypeReferenceNode(v.type) && isIdentifier2(v.type.typeName) && getIdentifierName(v.type.typeName) === "ReceiveType");
|
|
2541
|
+
if (isReceiveType) {
|
|
2542
|
+
program.pushOp(ReflectionOp.inline, program.pushStack(isReceiveType.parameterName));
|
|
2543
|
+
} else {
|
|
2544
|
+
for (const foundUser of foundUsers) {
|
|
2545
|
+
program.pushConditionalFrame();
|
|
2546
|
+
program.pushOp(ReflectionOp.typeof, program.pushStack(this.f.createArrowFunction(void 0, void 0, [], void 0, void 0, foundUser.parameterName)));
|
|
2547
|
+
this.extractPackStructOfType(foundUser.type, program);
|
|
2548
|
+
program.pushOp(ReflectionOp.extends);
|
|
2549
|
+
const found = program.findVariable(getIdentifierName(declaration.name));
|
|
2550
|
+
if (found) {
|
|
2551
|
+
this.extractPackStructOfType(declaration.name, program);
|
|
2552
|
+
} else {
|
|
2553
|
+
program.pushOp(ReflectionOp.any);
|
|
2554
|
+
}
|
|
2555
|
+
this.extractPackStructOfType({
|
|
2556
|
+
kind: SyntaxKind2.NeverKeyword
|
|
2557
|
+
}, program);
|
|
2558
|
+
program.pushOp(ReflectionOp.condition);
|
|
2559
|
+
program.popFrameImplicit();
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
if (foundUsers.length > 1) ;
|
|
2563
|
+
} else if (declaration.constraint) {
|
|
2564
|
+
if (isUsedInFunction) program.resolveFunctionParametersIncrease(declaration.parent);
|
|
2565
|
+
const constraint = getEffectiveConstraintOfTypeParameter(declaration);
|
|
2566
|
+
if (constraint) {
|
|
2567
|
+
this.extractPackStructOfType(constraint, program);
|
|
2568
|
+
} else {
|
|
2569
|
+
program.pushOp(ReflectionOp.never);
|
|
2570
|
+
}
|
|
2571
|
+
if (isUsedInFunction) program.resolveFunctionParametersDecrease(declaration.parent);
|
|
2572
|
+
} else {
|
|
2573
|
+
program.pushOp(ReflectionOp.never);
|
|
2574
|
+
}
|
|
2575
|
+
} else {
|
|
2576
|
+
program.pushOp(ReflectionOp.any);
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
createAccessorForEntityName(e) {
|
|
2580
|
+
return this.f.createPropertyAccessExpression(isIdentifier2(e.left) ? e.left : this.createAccessorForEntityName(e.left), e.right);
|
|
2581
|
+
}
|
|
2582
|
+
findDeclarationInFile(sourceFile, declarationName) {
|
|
2583
|
+
if (isNodeWithLocals(sourceFile) && sourceFile.locals) {
|
|
2584
|
+
const declarationSymbol = sourceFile.locals.get(declarationName);
|
|
2585
|
+
if (declarationSymbol && declarationSymbol.declarations && declarationSymbol.declarations[0]) {
|
|
2586
|
+
return declarationSymbol.declarations[0];
|
|
2587
|
+
}
|
|
2588
|
+
}
|
|
2589
|
+
return;
|
|
2590
|
+
}
|
|
2591
|
+
resolveImportSpecifier(_declarationName, importOrExport, sourceFile) {
|
|
2592
|
+
const declarationName = "string" === typeof _declarationName ? _declarationName : getIdentifierName(_declarationName);
|
|
2593
|
+
if (!importOrExport.moduleSpecifier || !isStringLiteral3(importOrExport.moduleSpecifier)) {
|
|
2594
|
+
return;
|
|
2595
|
+
}
|
|
2596
|
+
const source = this.resolver.resolve(sourceFile, importOrExport);
|
|
2597
|
+
if (!source) {
|
|
2598
|
+
debug("module not found", importOrExport.moduleSpecifier.text, "Is transpileOnly enabled? It needs to be disabled.");
|
|
2599
|
+
return;
|
|
2600
|
+
}
|
|
2601
|
+
const declaration = this.findDeclarationInFile(source, declarationName);
|
|
2602
|
+
sourceFile = source;
|
|
2603
|
+
if (declaration && !isImportSpecifier(declaration)) {
|
|
2604
|
+
if (isExportDeclaration(declaration)) {
|
|
2605
|
+
return this.followExport(declarationName, declaration, sourceFile);
|
|
2606
|
+
}
|
|
2607
|
+
return declaration;
|
|
2608
|
+
}
|
|
2609
|
+
if (isSourceFile(sourceFile)) {
|
|
2610
|
+
for (const statement of sourceFile.statements) {
|
|
2611
|
+
if (!isExportDeclaration(statement)) continue;
|
|
2612
|
+
const found = this.followExport(declarationName, statement, sourceFile);
|
|
2613
|
+
if (found) return found;
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
return;
|
|
2617
|
+
}
|
|
2618
|
+
followExport(declarationName, statement, sourceFile) {
|
|
2619
|
+
if (statement.exportClause) {
|
|
2620
|
+
if (isNamedExports(statement.exportClause)) {
|
|
2621
|
+
for (const element of statement.exportClause.elements) {
|
|
2622
|
+
if (getEscapedText(element.name) === declarationName) {
|
|
2623
|
+
if (!statement.moduleSpecifier || !isStringLiteral3(statement.moduleSpecifier)) {
|
|
2624
|
+
if (!statement.moduleSpecifier || !isStringLiteral3(statement.moduleSpecifier)) {
|
|
2625
|
+
if (isNodeWithLocals(sourceFile) && sourceFile.locals) {
|
|
2626
|
+
const found = sourceFile.locals.get(declarationName);
|
|
2627
|
+
if (found && found.declarations && found.declarations[0]) {
|
|
2628
|
+
const declaration = found.declarations[0];
|
|
2629
|
+
if (declaration && isImportSpecifier(declaration)) {
|
|
2630
|
+
const importOrExport = declaration.parent.parent.parent;
|
|
2631
|
+
const found2 = this.resolveImportSpecifier(element.propertyName ? getEscapedText(element.propertyName) : declarationName, importOrExport, sourceFile);
|
|
2632
|
+
if (found2) return found2;
|
|
2633
|
+
}
|
|
2634
|
+
return declaration;
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
} else {
|
|
2639
|
+
const found = this.resolveImportSpecifier(element.propertyName ? getEscapedText(element.propertyName) : declarationName, statement, sourceFile);
|
|
2640
|
+
if (found) return found;
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
}
|
|
2645
|
+
} else {
|
|
2646
|
+
const found = this.resolveImportSpecifier(declarationName, statement, sourceFile);
|
|
2647
|
+
if (found) {
|
|
2648
|
+
return found;
|
|
2649
|
+
}
|
|
2650
|
+
}
|
|
2651
|
+
return;
|
|
2652
|
+
}
|
|
2653
|
+
getTypeOfType(type) {
|
|
2654
|
+
const reflection = this.isWithReflection(this.sourceFile, type);
|
|
2655
|
+
if (!reflection) return;
|
|
2656
|
+
const program = new CompilerProgram(type, this.sourceFile);
|
|
2657
|
+
this.extractPackStructOfType(type, program);
|
|
2658
|
+
return this.packOpsAndStack(program);
|
|
2659
|
+
}
|
|
2660
|
+
packOpsAndStack(program) {
|
|
2661
|
+
const packStruct = program.buildPackStruct();
|
|
2662
|
+
if (packStruct.ops.length === 0) return;
|
|
2663
|
+
const packed = [
|
|
2664
|
+
...packStruct.stack,
|
|
2665
|
+
encodeOps(packStruct.ops)
|
|
2666
|
+
];
|
|
2667
|
+
return this.valueToExpression(packed);
|
|
2668
|
+
}
|
|
2669
|
+
/**
|
|
2670
|
+
* Note: We have to duplicate the expressions as it can be that incoming expression are from another file and contain wrong pos/end properties,
|
|
2671
|
+
* so the code generation is then broken when we simply reuse them. Wrong code like ``User.__type = [.toEqual({`` is then generated.
|
|
2672
|
+
* This function is probably not complete, but we add new copies when required.
|
|
2673
|
+
*/
|
|
2674
|
+
valueToExpression(value) {
|
|
2675
|
+
return this.nodeConverter.toExpression(value);
|
|
2676
|
+
}
|
|
2677
|
+
/**
|
|
2678
|
+
* A class is decorated with type information by adding a static variable.
|
|
2679
|
+
*
|
|
2680
|
+
* class Model {
|
|
2681
|
+
* static __types = pack(ReflectionOp.string); //<-- encoded type information
|
|
2682
|
+
* title: string;
|
|
2683
|
+
* }
|
|
2684
|
+
*/
|
|
2685
|
+
decorateClass(sourceFile, node) {
|
|
2686
|
+
const reflection = this.isWithReflection(sourceFile, node);
|
|
2687
|
+
if (!reflection) {
|
|
2688
|
+
return node;
|
|
2689
|
+
}
|
|
2690
|
+
const type = this.getTypeOfType(node);
|
|
2691
|
+
const __type = this.f.createPropertyDeclaration(this.f.createModifiersFromModifierFlags(ModifierFlags.Static), "__type", void 0, void 0, type);
|
|
2692
|
+
if (isClassDeclaration(node)) {
|
|
2693
|
+
return this.f.updateClassDeclaration(node, node.modifiers, node.name, node.typeParameters, node.heritageClauses, this.f.createNodeArray([
|
|
2694
|
+
...node.members,
|
|
2695
|
+
__type
|
|
2696
|
+
]));
|
|
2697
|
+
}
|
|
2698
|
+
return this.f.updateClassExpression(node, node.modifiers, node.name, node.typeParameters, node.heritageClauses, this.f.createNodeArray([
|
|
2699
|
+
...node.members,
|
|
2700
|
+
__type
|
|
2701
|
+
]));
|
|
2702
|
+
}
|
|
2703
|
+
/**
|
|
2704
|
+
* const fn = function() {}
|
|
2705
|
+
*
|
|
2706
|
+
* => const fn = __assignType(function() {}, [34])
|
|
2707
|
+
*/
|
|
2708
|
+
decorateFunctionExpression(expression) {
|
|
2709
|
+
const encodedType = this.getTypeOfType(expression);
|
|
2710
|
+
if (!encodedType) return expression;
|
|
2711
|
+
return this.wrapWithAssignType(expression, encodedType);
|
|
2712
|
+
}
|
|
2713
|
+
/**
|
|
2714
|
+
* function name() {}
|
|
2715
|
+
*
|
|
2716
|
+
* => function name() {}; name.__type = 34;
|
|
2717
|
+
*/
|
|
2718
|
+
decorateFunctionDeclaration(declaration) {
|
|
2719
|
+
const encodedType = this.getTypeOfType(declaration);
|
|
2720
|
+
if (!encodedType) return declaration;
|
|
2721
|
+
if (!declaration.name) {
|
|
2722
|
+
if (!declaration.body) return;
|
|
2723
|
+
const modifier = declaration.modifiers ? declaration.modifiers.filter((v) => v.kind !== SyntaxKind2.ExportKeyword && v.kind !== SyntaxKind2.DefaultKeyword && v.kind !== SyntaxKind2.Decorator) : [];
|
|
2724
|
+
return this.f.createExportAssignment(void 0, void 0, this.wrapWithAssignType(this.f.createFunctionExpression(modifier, declaration.asteriskToken, declaration.name, declaration.typeParameters, declaration.parameters, declaration.type, declaration.body), encodedType));
|
|
2725
|
+
}
|
|
2726
|
+
const statements = [
|
|
2727
|
+
declaration
|
|
2728
|
+
];
|
|
2729
|
+
statements.push(this.f.createExpressionStatement(this.f.createAssignment(this.f.createPropertyAccessExpression(serializeEntityNameAsExpression(this.f, declaration.name), "__type"), encodedType)));
|
|
2730
|
+
return statements;
|
|
2731
|
+
}
|
|
2732
|
+
/**
|
|
2733
|
+
* const fn = () => {}
|
|
2734
|
+
* => const fn = __assignType(() => {}, [34])
|
|
2735
|
+
*/
|
|
2736
|
+
decorateArrowFunction(expression) {
|
|
2737
|
+
const encodedType = this.getTypeOfType(expression);
|
|
2738
|
+
if (!encodedType) return expression;
|
|
2739
|
+
return this.wrapWithAssignType(expression, encodedType);
|
|
2740
|
+
}
|
|
2741
|
+
/**
|
|
2742
|
+
* Object.assign(fn, {__type: []}) is much slower than a custom implementation like
|
|
2743
|
+
*
|
|
2744
|
+
* assignType(fn, [])
|
|
2745
|
+
*
|
|
2746
|
+
* where we embed assignType() at the beginning of the type.
|
|
2747
|
+
*/
|
|
2748
|
+
wrapWithAssignType(fn, type) {
|
|
2749
|
+
this.embedAssignType = true;
|
|
2750
|
+
return this.f.createCallExpression(this.f.createIdentifier("__assignType"), void 0, [
|
|
2751
|
+
fn,
|
|
2752
|
+
type
|
|
2753
|
+
]);
|
|
2754
|
+
}
|
|
2755
|
+
/**
|
|
2756
|
+
* Checks if reflection was disabled/enabled in file via JSDoc attribute for a particular
|
|
2757
|
+
* Node, e.g `@reflection no`. If nothing is found, "reflection" config option needs to be used.
|
|
2758
|
+
*/
|
|
2759
|
+
getExplicitReflectionMode(sourceFile, node) {
|
|
2760
|
+
let current = node;
|
|
2761
|
+
let reflectionComment = void 0;
|
|
2762
|
+
while ("undefined" === typeof reflectionComment && current) {
|
|
2763
|
+
const next = sourceFile && extractJSDocAttribute(sourceFile, current, "reflection");
|
|
2764
|
+
if ("undefined" !== typeof next) reflectionComment = next;
|
|
2765
|
+
current = current.parent;
|
|
2766
|
+
}
|
|
2767
|
+
if (reflectionComment === "" || reflectionComment === "true" || reflectionComment === "default" || reflectionComment === "enabled" || reflectionComment === "1") {
|
|
2768
|
+
return true;
|
|
2769
|
+
}
|
|
2770
|
+
if (reflectionComment === "false" || reflectionComment === "disabled" || reflectionComment === "never" || reflectionComment === "no" || reflectionComment === "0") {
|
|
2771
|
+
return false;
|
|
2772
|
+
}
|
|
2773
|
+
return;
|
|
2774
|
+
}
|
|
2775
|
+
};
|
|
2776
|
+
var DeclarationTransformer = class extends ReflectionTransformer {
|
|
2777
|
+
static {
|
|
2778
|
+
__name(this, "DeclarationTransformer");
|
|
2779
|
+
}
|
|
2780
|
+
constructor() {
|
|
2781
|
+
super(...arguments);
|
|
2782
|
+
this.addExports = [];
|
|
2783
|
+
}
|
|
2784
|
+
transformSourceFile(sourceFile) {
|
|
2785
|
+
if (sourceFile.deepkitDeclarationTransformed) return sourceFile;
|
|
2786
|
+
this.sourceFile = sourceFile;
|
|
2787
|
+
this.addExports = [];
|
|
2788
|
+
const configResolver = this.getConfigResolver(sourceFile);
|
|
2789
|
+
const reflection = configResolver.match(sourceFile.fileName);
|
|
2790
|
+
Object.assign(this.compilerOptions, configResolver.config.compilerOptions);
|
|
2791
|
+
if (reflection.mode === "never") return sourceFile;
|
|
2792
|
+
const visitor = /* @__PURE__ */ __name((node) => {
|
|
2793
|
+
node = visitEachChild(node, visitor, this.context);
|
|
2794
|
+
if ((isTypeAliasDeclaration(node) || isInterfaceDeclaration(node) || isEnumDeclaration(node)) && hasModifier(node, SyntaxKind2.ExportKeyword)) {
|
|
2795
|
+
const reflection2 = this.isWithReflection(sourceFile, node);
|
|
2796
|
+
if (reflection2) {
|
|
2797
|
+
this.addExports.push({
|
|
2798
|
+
identifier: getIdentifierName(this.getDeclarationVariableName(node.name))
|
|
2799
|
+
});
|
|
2800
|
+
}
|
|
2801
|
+
}
|
|
2802
|
+
return node;
|
|
2803
|
+
}, "visitor");
|
|
2804
|
+
this.sourceFile = visitNode(this.sourceFile, visitor);
|
|
2805
|
+
if (this.addExports.length) {
|
|
2806
|
+
const exports$1 = [];
|
|
2807
|
+
const handledIdentifier = [];
|
|
2808
|
+
for (const imp of this.addExports) {
|
|
2809
|
+
if (handledIdentifier.includes(imp.identifier)) continue;
|
|
2810
|
+
handledIdentifier.push(imp.identifier);
|
|
2811
|
+
exports$1.push(this.f.createTypeAliasDeclaration([
|
|
2812
|
+
this.f.createModifier(SyntaxKind2.ExportKeyword),
|
|
2813
|
+
this.f.createModifier(SyntaxKind2.DeclareKeyword)
|
|
2814
|
+
], this.f.createIdentifier(imp.identifier), void 0, this.f.createArrayTypeNode(this.f.createKeywordTypeNode(SyntaxKind2.AnyKeyword))));
|
|
2815
|
+
}
|
|
2816
|
+
this.sourceFile = this.f.updateSourceFile(this.sourceFile, [
|
|
2817
|
+
...this.sourceFile.statements,
|
|
2818
|
+
...exports$1
|
|
2819
|
+
]);
|
|
2820
|
+
}
|
|
2821
|
+
this.sourceFile.deepkitDeclarationTransformed = true;
|
|
2822
|
+
return this.sourceFile;
|
|
2823
|
+
}
|
|
2824
|
+
};
|
|
2825
|
+
var cache2 = new Cache();
|
|
2826
|
+
function createTransformer(context, options = {}) {
|
|
2827
|
+
return /* @__PURE__ */ __name(function transformer2(ctx) {
|
|
2828
|
+
cache2.tick();
|
|
2829
|
+
return new ReflectionTransformer(ctx, cache2).withReflection(defu(options, {
|
|
2830
|
+
reflection: context.tsconfig.tsconfigJson.compilerOptions?.reflection || context.tsconfig.tsconfigJson.reflection,
|
|
2831
|
+
reflectionLevel: context.tsconfig.tsconfigJson.compilerOptions?.reflectionLevel || context.tsconfig.tsconfigJson.reflectionLevel
|
|
2832
|
+
}, {
|
|
2833
|
+
exclude: [],
|
|
2834
|
+
reflection: "default",
|
|
2835
|
+
reflectionLevel: "minimal"
|
|
2836
|
+
}));
|
|
2837
|
+
}, "transformer");
|
|
2838
|
+
}
|
|
2839
|
+
__name(createTransformer, "createTransformer");
|
|
2840
|
+
function createDeclarationTransformer(context, options = {}) {
|
|
2841
|
+
return /* @__PURE__ */ __name(function declarationTransformer2(ctx) {
|
|
2842
|
+
return new DeclarationTransformer(ctx, cache2).withReflection(defu(options, {
|
|
2843
|
+
reflection: context.tsconfig.tsconfigJson.compilerOptions?.reflection || context.tsconfig.tsconfigJson.reflection,
|
|
2844
|
+
reflectionLevel: context.tsconfig.tsconfigJson.compilerOptions?.reflectionLevel || context.tsconfig.tsconfigJson.reflectionLevel
|
|
2845
|
+
}, {
|
|
2846
|
+
exclude: [],
|
|
2847
|
+
reflection: "default",
|
|
2848
|
+
reflectionLevel: "minimal"
|
|
2849
|
+
}));
|
|
2850
|
+
}, "declarationTransformer");
|
|
2851
|
+
}
|
|
2852
|
+
__name(createDeclarationTransformer, "createDeclarationTransformer");
|
|
2853
|
+
|
|
2854
|
+
export { createDeclarationTransformer, createTransformer };
|