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