@ivlev-dev/silent-validation-vite 0.0.0-stage → 0.12.0
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/LICENSE +21 -0
- package/README.md +73 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/typegen.d.ts +77 -0
- package/dist/typegen.d.ts.map +1 -0
- package/dist/typegen.js +1233 -0
- package/dist/typegen.js.map +1 -0
- package/dist/vite.d.ts +11 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +1286 -0
- package/dist/vite.js.map +1 -0
- package/package.json +55 -3
package/dist/typegen.js
ADDED
|
@@ -0,0 +1,1233 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
const SCHEMA_KEY = "silent.validation.schema";
|
|
3
|
+
const MAX_DEPTH = 64;
|
|
4
|
+
const INDENT_UNIT = " ";
|
|
5
|
+
const BUILTIN_NAMES = new Map([
|
|
6
|
+
["Date", "date"],
|
|
7
|
+
["RegExp", "regexp"],
|
|
8
|
+
["Error", "error"],
|
|
9
|
+
["ArrayBuffer", "arraybuffer"],
|
|
10
|
+
["SharedArrayBuffer", "sharedarraybuffer"],
|
|
11
|
+
["DataView", "dataview"],
|
|
12
|
+
["URL", "url"],
|
|
13
|
+
["URLSearchParams", "urlsearchparams"],
|
|
14
|
+
["Iterable", "iterable"],
|
|
15
|
+
["AsyncIterable", "asynciterable"],
|
|
16
|
+
["Iterator", "iterator"],
|
|
17
|
+
["AsyncIterator", "asynciterator"],
|
|
18
|
+
["Int8Array", "int8array"],
|
|
19
|
+
["Uint8Array", "uint8array"],
|
|
20
|
+
["Uint8ClampedArray", "uint8clampedarray"],
|
|
21
|
+
["Int16Array", "int16array"],
|
|
22
|
+
["Uint16Array", "uint16array"],
|
|
23
|
+
["Int32Array", "int32array"],
|
|
24
|
+
["Uint32Array", "uint32array"],
|
|
25
|
+
["Float32Array", "float32array"],
|
|
26
|
+
["Float64Array", "float64array"],
|
|
27
|
+
["BigInt64Array", "bigint64array"],
|
|
28
|
+
["BigUint64Array", "biguint64array"],
|
|
29
|
+
["Function", "function"],
|
|
30
|
+
]);
|
|
31
|
+
const COLLECTION_NAMES = new Map([
|
|
32
|
+
["Map", "map"],
|
|
33
|
+
["Set", "set"],
|
|
34
|
+
["WeakMap", "weakmap"],
|
|
35
|
+
["WeakSet", "weakset"],
|
|
36
|
+
["Promise", "promise"],
|
|
37
|
+
]);
|
|
38
|
+
const TYPED_ARRAY_BASES = [
|
|
39
|
+
["Int8Array", "int8array"],
|
|
40
|
+
["Uint8Array", "uint8array"],
|
|
41
|
+
["Uint8ClampedArray", "uint8clampedarray"],
|
|
42
|
+
["Int16Array", "int16array"],
|
|
43
|
+
["Uint16Array", "uint16array"],
|
|
44
|
+
["Int32Array", "int32array"],
|
|
45
|
+
["Uint32Array", "uint32array"],
|
|
46
|
+
["Float32Array", "float32array"],
|
|
47
|
+
["Float64Array", "float64array"],
|
|
48
|
+
["BigInt64Array", "bigint64array"],
|
|
49
|
+
["BigUint64Array", "biguint64array"],
|
|
50
|
+
];
|
|
51
|
+
const WILDCARD_MEMBERS = [
|
|
52
|
+
"string",
|
|
53
|
+
"number",
|
|
54
|
+
"boolean",
|
|
55
|
+
"bigint",
|
|
56
|
+
"symbol",
|
|
57
|
+
"object",
|
|
58
|
+
"function",
|
|
59
|
+
"undefined",
|
|
60
|
+
"null",
|
|
61
|
+
];
|
|
62
|
+
function primitive(name) {
|
|
63
|
+
return { kind: "primitive", name };
|
|
64
|
+
}
|
|
65
|
+
function wildcardNode() {
|
|
66
|
+
return {
|
|
67
|
+
kind: "union",
|
|
68
|
+
members: WILDCARD_MEMBERS.map((name) => primitive(name)),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function resolveSymbol(symbol, checker) {
|
|
72
|
+
if (symbol === undefined) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
if ((symbol.flags & ts.SymbolFlags.Alias) !== 0) {
|
|
76
|
+
return checker.getAliasedSymbol(symbol);
|
|
77
|
+
}
|
|
78
|
+
return symbol;
|
|
79
|
+
}
|
|
80
|
+
function typeArguments(type, checker) {
|
|
81
|
+
if (type.aliasTypeArguments !== undefined && type.aliasTypeArguments.length > 0) {
|
|
82
|
+
return type.aliasTypeArguments;
|
|
83
|
+
}
|
|
84
|
+
if ((type.flags & ts.TypeFlags.Object) !== 0) {
|
|
85
|
+
const objectType = type;
|
|
86
|
+
if ((objectType.objectFlags & ts.ObjectFlags.Reference) !== 0) {
|
|
87
|
+
return checker.getTypeArguments(type);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
function baseFileName(fileName) {
|
|
93
|
+
const slash = Math.max(fileName.lastIndexOf("/"), fileName.lastIndexOf("\\"));
|
|
94
|
+
return fileName.slice(slash + 1);
|
|
95
|
+
}
|
|
96
|
+
function isDefaultLibFile(sourceFile) {
|
|
97
|
+
return /^lib(?:\.[^/\\]+)?\.d\.ts$/.test(baseFileName(sourceFile.fileName));
|
|
98
|
+
}
|
|
99
|
+
function isDomLibFile(sourceFile) {
|
|
100
|
+
return /^lib\.(?:dom|webworker|scripthost)/.test(baseFileName(sourceFile.fileName));
|
|
101
|
+
}
|
|
102
|
+
function isNodeDeclarationFile(sourceFile) {
|
|
103
|
+
return /[\\/]@types[\\/]node[\\/]/.test(sourceFile.fileName);
|
|
104
|
+
}
|
|
105
|
+
function isOpaqueDeclaration(sourceFile) {
|
|
106
|
+
return isDomLibFile(sourceFile) || isNodeDeclarationFile(sourceFile);
|
|
107
|
+
}
|
|
108
|
+
function isGlobalAugmentation(declaration) {
|
|
109
|
+
let node = declaration.parent;
|
|
110
|
+
while (node !== undefined) {
|
|
111
|
+
if (ts.isModuleDeclaration(node) && (node.flags & ts.NodeFlags.GlobalAugmentation) !== 0) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
node = node.parent;
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
function hasDeclareModifier(node) {
|
|
119
|
+
if (!ts.canHaveModifiers(node)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const modifiers = ts.getModifiers(node);
|
|
123
|
+
return modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DeclareKeyword) ?? false;
|
|
124
|
+
}
|
|
125
|
+
function isAmbientWithoutRuntime(declaration) {
|
|
126
|
+
if (isGlobalAugmentation(declaration)) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
if (declaration.getSourceFile().isDeclarationFile) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
let node = declaration;
|
|
133
|
+
while (node !== undefined && !ts.isSourceFile(node)) {
|
|
134
|
+
if (hasDeclareModifier(node)) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
node = node.parent;
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
function typeIsOpaque(type, checker) {
|
|
142
|
+
const symbols = [type.aliasSymbol, type.getSymbol()];
|
|
143
|
+
for (const symbol of symbols) {
|
|
144
|
+
const resolved = resolveSymbol(symbol, checker);
|
|
145
|
+
if (resolved === undefined) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
for (const declaration of resolved.declarations ?? []) {
|
|
149
|
+
if (isOpaqueDeclaration(declaration.getSourceFile())) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
function defaultLibSymbolName(symbol, checker) {
|
|
157
|
+
const resolved = resolveSymbol(symbol, checker);
|
|
158
|
+
if (resolved === undefined) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
const valueDeclaration = resolved.valueDeclaration;
|
|
162
|
+
if (valueDeclaration !== undefined
|
|
163
|
+
&& (ts.isClassDeclaration(valueDeclaration) || ts.isClassExpression(valueDeclaration))
|
|
164
|
+
&& !isDefaultLibFile(valueDeclaration.getSourceFile())) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const declarations = resolved.declarations ?? [];
|
|
168
|
+
for (const declaration of declarations) {
|
|
169
|
+
if (isDefaultLibFile(declaration.getSourceFile())) {
|
|
170
|
+
return resolved.getName();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
function builtinNameOfType(type, checker) {
|
|
176
|
+
const alias = defaultLibSymbolName(type.aliasSymbol, checker);
|
|
177
|
+
if (alias !== null) {
|
|
178
|
+
return alias;
|
|
179
|
+
}
|
|
180
|
+
return defaultLibSymbolName(type.getSymbol(), checker);
|
|
181
|
+
}
|
|
182
|
+
function hasBuiltinBase(type, name, checker, visited) {
|
|
183
|
+
if (visited.has(type)) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
visited.add(type);
|
|
187
|
+
const bases = type.getBaseTypes() ?? [];
|
|
188
|
+
for (const base of bases) {
|
|
189
|
+
if (defaultLibSymbolName(base.getSymbol(), checker) === name) {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
if (hasBuiltinBase(base, name, checker, visited)) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
function builtinCollectionNode(name) {
|
|
199
|
+
const named = BUILTIN_NAMES.get(name);
|
|
200
|
+
if (named !== undefined) {
|
|
201
|
+
return primitive(named);
|
|
202
|
+
}
|
|
203
|
+
const collection = COLLECTION_NAMES.get(name);
|
|
204
|
+
if (collection !== undefined) {
|
|
205
|
+
return primitive(collection);
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
function builtinBaseNode(type, state) {
|
|
210
|
+
const bases = type.getBaseTypes() ?? [];
|
|
211
|
+
for (const base of bases) {
|
|
212
|
+
const name = builtinNameOfType(base, state.checker);
|
|
213
|
+
if (name !== null) {
|
|
214
|
+
const node = builtinCollectionNode(name);
|
|
215
|
+
if (node !== null) {
|
|
216
|
+
return node;
|
|
217
|
+
}
|
|
218
|
+
if (name === "Array" || name === "ReadonlyArray") {
|
|
219
|
+
const element = typeArguments(base, state.checker)[0];
|
|
220
|
+
const schema = element === undefined ? null : serializeType(element, state);
|
|
221
|
+
return { kind: "array", element: schema ?? wildcardNode() };
|
|
222
|
+
}
|
|
223
|
+
if (typeIsOpaque(base, state.checker)) {
|
|
224
|
+
return primitive("object");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
else if (typeIsOpaque(base, state.checker)) {
|
|
228
|
+
return primitive("object");
|
|
229
|
+
}
|
|
230
|
+
const nested = builtinBaseNode(base, state);
|
|
231
|
+
if (nested !== null) {
|
|
232
|
+
return nested;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
function enumValueOf(type, checker) {
|
|
238
|
+
const symbol = type.getSymbol();
|
|
239
|
+
const declaration = symbol?.valueDeclaration ?? symbol?.declarations?.[0];
|
|
240
|
+
if (declaration !== undefined) {
|
|
241
|
+
const constant = checker.getConstantValue(declaration);
|
|
242
|
+
if (constant !== undefined) {
|
|
243
|
+
return constant;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const value = type.value;
|
|
247
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
248
|
+
return value;
|
|
249
|
+
}
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
function enumValuesOf(types, checker) {
|
|
253
|
+
if (types.length === 0) {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
const values = [];
|
|
257
|
+
for (const type of types) {
|
|
258
|
+
if ((type.flags & ts.TypeFlags.EnumLiteral) === 0) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
const value = enumValueOf(type, checker);
|
|
262
|
+
if (value === undefined) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
values.push(value);
|
|
266
|
+
}
|
|
267
|
+
return values;
|
|
268
|
+
}
|
|
269
|
+
function enumValuesOfSymbol(type, checker) {
|
|
270
|
+
const symbol = resolveSymbol(type.getSymbol(), checker);
|
|
271
|
+
if (symbol === undefined) {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
const declarations = symbol.declarations ?? [];
|
|
275
|
+
for (const declaration of declarations) {
|
|
276
|
+
if (!ts.isEnumDeclaration(declaration)) {
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const values = [];
|
|
280
|
+
for (const member of declaration.members) {
|
|
281
|
+
const value = checker.getConstantValue(member);
|
|
282
|
+
if (value === undefined) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
values.push(value);
|
|
286
|
+
}
|
|
287
|
+
return values;
|
|
288
|
+
}
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
function stripUndefined(node) {
|
|
292
|
+
return node.kind === "optional" ? node.inner : node;
|
|
293
|
+
}
|
|
294
|
+
function wrapOptional(node) {
|
|
295
|
+
return node.kind === "optional" ? node : { kind: "optional", inner: node };
|
|
296
|
+
}
|
|
297
|
+
function serializeLiteral(type) {
|
|
298
|
+
const flags = type.flags;
|
|
299
|
+
if ((flags & ts.TypeFlags.StringLiteral) !== 0) {
|
|
300
|
+
return { kind: "literal", value: type.value };
|
|
301
|
+
}
|
|
302
|
+
if ((flags & ts.TypeFlags.NumberLiteral) !== 0) {
|
|
303
|
+
return { kind: "literal", value: type.value };
|
|
304
|
+
}
|
|
305
|
+
if ((flags & ts.TypeFlags.BooleanLiteral) !== 0) {
|
|
306
|
+
const intrinsic = type.intrinsicName;
|
|
307
|
+
return { kind: "literal", value: intrinsic === "true" };
|
|
308
|
+
}
|
|
309
|
+
if ((flags & ts.TypeFlags.BigIntLiteral) !== 0) {
|
|
310
|
+
return primitive("bigint");
|
|
311
|
+
}
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
function booleanLiteralValue(type) {
|
|
315
|
+
if ((type.flags & ts.TypeFlags.BooleanLiteral) === 0) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
return type.intrinsicName === "true";
|
|
319
|
+
}
|
|
320
|
+
function serializeUnion(type, state) {
|
|
321
|
+
const rest = [];
|
|
322
|
+
let optional = false;
|
|
323
|
+
let nullable = false;
|
|
324
|
+
for (const member of type.types) {
|
|
325
|
+
if ((member.flags & ts.TypeFlags.Undefined) !== 0) {
|
|
326
|
+
optional = true;
|
|
327
|
+
}
|
|
328
|
+
else if ((member.flags & ts.TypeFlags.Null) !== 0) {
|
|
329
|
+
nullable = true;
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
rest.push(member);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
let booleans = 0;
|
|
336
|
+
const restWithoutBooleans = [];
|
|
337
|
+
for (const member of rest) {
|
|
338
|
+
if (booleanLiteralValue(member) !== null) {
|
|
339
|
+
booleans += 1;
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
restWithoutBooleans.push(member);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const hasBoolean = booleans >= 2;
|
|
346
|
+
if (hasBoolean) {
|
|
347
|
+
rest.length = 0;
|
|
348
|
+
rest.push(...restWithoutBooleans);
|
|
349
|
+
}
|
|
350
|
+
let base;
|
|
351
|
+
if (rest.length === 0) {
|
|
352
|
+
base = hasBoolean ? primitive("boolean") : primitive(optional ? "undefined" : "null");
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
const values = hasBoolean ? null : enumValuesOf(rest, state.checker);
|
|
356
|
+
if (values !== null) {
|
|
357
|
+
base = { kind: "enum", values };
|
|
358
|
+
}
|
|
359
|
+
else if (rest.length === 1 && !hasBoolean) {
|
|
360
|
+
base = serializeType(rest[0], state);
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
const members = [];
|
|
364
|
+
if (hasBoolean) {
|
|
365
|
+
members.push(primitive("boolean"));
|
|
366
|
+
}
|
|
367
|
+
for (const member of rest) {
|
|
368
|
+
const schema = serializeType(member, state);
|
|
369
|
+
if (schema === null) {
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
members.push(schema);
|
|
373
|
+
}
|
|
374
|
+
base = { kind: "union", members };
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (base === null) {
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
let result = base;
|
|
381
|
+
if (nullable) {
|
|
382
|
+
result = { kind: "nullable", inner: result };
|
|
383
|
+
}
|
|
384
|
+
if (optional) {
|
|
385
|
+
result = { kind: "optional", inner: result };
|
|
386
|
+
}
|
|
387
|
+
return result;
|
|
388
|
+
}
|
|
389
|
+
function nodeKey(node, seen = new Set()) {
|
|
390
|
+
switch (node.kind) {
|
|
391
|
+
case "primitive":
|
|
392
|
+
return `p:${node.name}`;
|
|
393
|
+
case "literal":
|
|
394
|
+
return `l:${JSON.stringify(node.value)}`;
|
|
395
|
+
case "enum":
|
|
396
|
+
return `e:${JSON.stringify(node.values)}`;
|
|
397
|
+
case "array":
|
|
398
|
+
return `a:${nodeKey(node.element, seen)}`;
|
|
399
|
+
case "tuple":
|
|
400
|
+
return `t:[${node.elements.map((element) => nodeKey(element, seen)).join(",")}]${node.rest === null ? "" : `:${nodeKey(node.rest, seen)}`}`;
|
|
401
|
+
case "union":
|
|
402
|
+
return `u:[${node.members.map((member) => nodeKey(member, seen)).join(",")}]`;
|
|
403
|
+
case "intersection":
|
|
404
|
+
return `i:[${node.members.map((member) => nodeKey(member, seen)).join(",")}]`;
|
|
405
|
+
case "object":
|
|
406
|
+
return `o:{${node.fields.map((field) => `${JSON.stringify(field.name)}:${nodeKey(field.schema, seen)}`).join(",")}}`;
|
|
407
|
+
case "record":
|
|
408
|
+
return `r:${nodeKey(node.value, seen)}`;
|
|
409
|
+
case "mapof":
|
|
410
|
+
return `m:${nodeKey(node.key, seen)},${nodeKey(node.value, seen)}`;
|
|
411
|
+
case "setof":
|
|
412
|
+
return `s:${nodeKey(node.element, seen)}`;
|
|
413
|
+
case "optional":
|
|
414
|
+
return `q:${nodeKey(node.inner, seen)}`;
|
|
415
|
+
case "nullable":
|
|
416
|
+
return `n:${nodeKey(node.inner, seen)}`;
|
|
417
|
+
case "class":
|
|
418
|
+
return `c:${node.reference}`;
|
|
419
|
+
case "lazy": {
|
|
420
|
+
if (seen.has(node.cell) || node.cell.node === null) {
|
|
421
|
+
return "z:";
|
|
422
|
+
}
|
|
423
|
+
seen.add(node.cell);
|
|
424
|
+
try {
|
|
425
|
+
return `z:${nodeKey(node.cell.node, seen)}`;
|
|
426
|
+
}
|
|
427
|
+
finally {
|
|
428
|
+
seen.delete(node.cell);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
function dedupeNodes(nodes) {
|
|
434
|
+
const unique = new Map();
|
|
435
|
+
for (const node of nodes) {
|
|
436
|
+
const key = nodeKey(node);
|
|
437
|
+
if (!unique.has(key)) {
|
|
438
|
+
unique.set(key, node);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return [...unique.values()];
|
|
442
|
+
}
|
|
443
|
+
function mergeObjects(members) {
|
|
444
|
+
const objects = members.filter((member) => member.kind === "object");
|
|
445
|
+
if (objects.length < 2) {
|
|
446
|
+
return members;
|
|
447
|
+
}
|
|
448
|
+
const merged = new Map();
|
|
449
|
+
for (const object of objects) {
|
|
450
|
+
for (const field of object.fields) {
|
|
451
|
+
merged.set(field.name, field.schema);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const fields = [...merged].map(([name, schema]) => ({ name, schema }));
|
|
455
|
+
const others = members.filter((member) => member.kind !== "object");
|
|
456
|
+
if (others.length === 0) {
|
|
457
|
+
return [{ kind: "object", fields }];
|
|
458
|
+
}
|
|
459
|
+
return [...others, { kind: "object", fields }];
|
|
460
|
+
}
|
|
461
|
+
function serializeIntersection(type, state) {
|
|
462
|
+
const members = [];
|
|
463
|
+
for (const member of type.types) {
|
|
464
|
+
const schema = serializeType(member, state);
|
|
465
|
+
if (schema === null) {
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
members.push(schema);
|
|
469
|
+
}
|
|
470
|
+
const merged = mergeObjects(members);
|
|
471
|
+
const unique = dedupeNodes(merged);
|
|
472
|
+
if (unique.length === 1) {
|
|
473
|
+
return unique[0];
|
|
474
|
+
}
|
|
475
|
+
return { kind: "intersection", members: unique };
|
|
476
|
+
}
|
|
477
|
+
function serializeTuple(type, state) {
|
|
478
|
+
const args = state.checker.getTypeArguments(type);
|
|
479
|
+
const target = type.target;
|
|
480
|
+
const elementFlags = target.elementFlags;
|
|
481
|
+
let restIndex = -1;
|
|
482
|
+
let variadic = false;
|
|
483
|
+
for (let index = 0; index < elementFlags.length; index += 1) {
|
|
484
|
+
const flag = elementFlags[index];
|
|
485
|
+
if (flag === undefined) {
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
if ((flag & ts.ElementFlags.Variadic) !== 0) {
|
|
489
|
+
variadic = true;
|
|
490
|
+
}
|
|
491
|
+
if ((flag & ts.ElementFlags.Rest) !== 0) {
|
|
492
|
+
restIndex = index;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (!variadic && restIndex >= 0 && restIndex === args.length - 1) {
|
|
496
|
+
const elements = [];
|
|
497
|
+
for (let index = 0; index < restIndex; index += 1) {
|
|
498
|
+
const flag = elementFlags[index];
|
|
499
|
+
if (flag !== undefined && (flag & ts.ElementFlags.Optional) !== 0) {
|
|
500
|
+
const optional = serializeOptionalElement(args[index], state);
|
|
501
|
+
if (optional === null) {
|
|
502
|
+
return null;
|
|
503
|
+
}
|
|
504
|
+
elements.push(optional);
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
const schema = serializeType(args[index], state);
|
|
508
|
+
if (schema === null) {
|
|
509
|
+
return null;
|
|
510
|
+
}
|
|
511
|
+
elements.push(schema);
|
|
512
|
+
}
|
|
513
|
+
const rest = serializeType(args[restIndex], state);
|
|
514
|
+
if (rest === null) {
|
|
515
|
+
return null;
|
|
516
|
+
}
|
|
517
|
+
return { kind: "tuple", elements, rest };
|
|
518
|
+
}
|
|
519
|
+
if (variadic || restIndex >= 0) {
|
|
520
|
+
const elements = [];
|
|
521
|
+
for (const arg of args) {
|
|
522
|
+
const schema = serializeType(arg, state);
|
|
523
|
+
if (schema === null) {
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
elements.push(schema);
|
|
527
|
+
}
|
|
528
|
+
const element = elements.length === 1 ? elements[0] : { kind: "union", members: elements };
|
|
529
|
+
return { kind: "array", element };
|
|
530
|
+
}
|
|
531
|
+
const elements = [];
|
|
532
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
533
|
+
const flag = elementFlags[index];
|
|
534
|
+
if (flag !== undefined && (flag & ts.ElementFlags.Optional) !== 0) {
|
|
535
|
+
const optional = serializeOptionalElement(args[index], state);
|
|
536
|
+
if (optional === null) {
|
|
537
|
+
return null;
|
|
538
|
+
}
|
|
539
|
+
elements.push(optional);
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
const schema = serializeType(args[index], state);
|
|
543
|
+
if (schema === null) {
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
elements.push(schema);
|
|
547
|
+
}
|
|
548
|
+
return { kind: "tuple", elements, rest: null };
|
|
549
|
+
}
|
|
550
|
+
function serializeOptionalElement(type, state) {
|
|
551
|
+
const schema = serializeType(type, state);
|
|
552
|
+
if (schema === null) {
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
return wrapOptional(stripUndefined(schema));
|
|
556
|
+
}
|
|
557
|
+
function lazyCell(type, state) {
|
|
558
|
+
const existing = state.lazy.get(type);
|
|
559
|
+
if (existing !== undefined) {
|
|
560
|
+
return existing;
|
|
561
|
+
}
|
|
562
|
+
const cell = { node: null, path: [...state.path] };
|
|
563
|
+
state.lazy.set(type, cell);
|
|
564
|
+
state.queue.push(type);
|
|
565
|
+
return cell;
|
|
566
|
+
}
|
|
567
|
+
function reportUnsupported(state, path, type) {
|
|
568
|
+
const message = `${state.sourceFile.fileName}: ${path.join(".")}: тип ${state.checker.typeToString(type)} не поддерживается`;
|
|
569
|
+
state.report(message);
|
|
570
|
+
if (state.strict) {
|
|
571
|
+
throw new Error(message);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
function reportSkipped(state, path, reason) {
|
|
575
|
+
const message = `${state.sourceFile.fileName}: ${path.join(".")}: ${reason}`;
|
|
576
|
+
state.report(message);
|
|
577
|
+
if (state.strict) {
|
|
578
|
+
throw new Error(message);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
function nameTextOf(name, sourceFile) {
|
|
582
|
+
if (ts.isIdentifier(name) || ts.isPrivateIdentifier(name)) {
|
|
583
|
+
return name.text;
|
|
584
|
+
}
|
|
585
|
+
if (ts.isStringLiteral(name) || ts.isNumericLiteral(name)) {
|
|
586
|
+
return name.text;
|
|
587
|
+
}
|
|
588
|
+
if (ts.isComputedPropertyName(name)) {
|
|
589
|
+
return `[${name.expression.getText(sourceFile)}]`;
|
|
590
|
+
}
|
|
591
|
+
return name.getText(sourceFile);
|
|
592
|
+
}
|
|
593
|
+
function isWellKnownSymbolProperty(property) {
|
|
594
|
+
for (const declaration of property.declarations ?? []) {
|
|
595
|
+
const name = ts.getNameOfDeclaration(declaration);
|
|
596
|
+
if (name === undefined || !ts.isComputedPropertyName(name)) {
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
const expression = name.expression;
|
|
600
|
+
if (ts.isPropertyAccessExpression(expression)
|
|
601
|
+
&& ts.isIdentifier(expression.expression)
|
|
602
|
+
&& expression.expression.text === "Symbol") {
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
function isSymbolNamedProperty(property, checker) {
|
|
609
|
+
if (property.getName().startsWith("__@")) {
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
for (const declaration of property.declarations ?? []) {
|
|
613
|
+
const name = ts.getNameOfDeclaration(declaration);
|
|
614
|
+
if (name === undefined || !ts.isComputedPropertyName(name)) {
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
const expressionType = checker.getTypeAtLocation(name.expression);
|
|
618
|
+
if ((expressionType.flags & (ts.TypeFlags.ESSymbol | ts.TypeFlags.UniqueESSymbol)) !== 0) {
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
function serializeObjectProperties(type, state) {
|
|
625
|
+
const fields = [];
|
|
626
|
+
for (const property of state.checker.getPropertiesOfType(type)) {
|
|
627
|
+
if (isWellKnownSymbolProperty(property)) {
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
const name = property.getName();
|
|
631
|
+
if (isSymbolNamedProperty(property, state.checker)) {
|
|
632
|
+
state.path.push(name);
|
|
633
|
+
try {
|
|
634
|
+
reportSkipped(state, state.path, "символьное свойство пропущено");
|
|
635
|
+
}
|
|
636
|
+
finally {
|
|
637
|
+
state.path.pop();
|
|
638
|
+
}
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
const propertyType = state.checker.getTypeOfSymbol(property);
|
|
642
|
+
state.path.push(name);
|
|
643
|
+
let schema;
|
|
644
|
+
try {
|
|
645
|
+
schema = serializeType(propertyType, state);
|
|
646
|
+
if (schema === null) {
|
|
647
|
+
reportUnsupported(state, state.path, propertyType);
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
if ((property.flags & ts.SymbolFlags.Optional) !== 0) {
|
|
651
|
+
schema = wrapOptional(stripUndefined(schema));
|
|
652
|
+
}
|
|
653
|
+
fields.push({ name, schema });
|
|
654
|
+
}
|
|
655
|
+
finally {
|
|
656
|
+
state.path.pop();
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
return { kind: "object", fields };
|
|
660
|
+
}
|
|
661
|
+
function serializeObject(type, state) {
|
|
662
|
+
if (state.checker.isTupleType(type)) {
|
|
663
|
+
return serializeTuple(type, state);
|
|
664
|
+
}
|
|
665
|
+
const args = typeArguments(type, state.checker);
|
|
666
|
+
const builtin = builtinNameOfType(type, state.checker);
|
|
667
|
+
if (builtin === "ArrayLike" || builtin === "ReadonlyArrayLike") {
|
|
668
|
+
const element = args[0];
|
|
669
|
+
const schema = element === undefined ? null : serializeType(element, state);
|
|
670
|
+
return {
|
|
671
|
+
kind: "union",
|
|
672
|
+
members: [
|
|
673
|
+
{ kind: "array", element: schema ?? wildcardNode() },
|
|
674
|
+
{ kind: "object", fields: [{ name: "length", schema: primitive("number") }] },
|
|
675
|
+
],
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
if (builtin === "Array" || builtin === "ReadonlyArray" || state.checker.isArrayType(type)) {
|
|
679
|
+
const element = args[0] ?? state.checker.getIndexTypeOfType(type, ts.IndexKind.Number);
|
|
680
|
+
if (element === undefined) {
|
|
681
|
+
return { kind: "array", element: wildcardNode() };
|
|
682
|
+
}
|
|
683
|
+
const schema = serializeType(element, state);
|
|
684
|
+
return { kind: "array", element: schema ?? wildcardNode() };
|
|
685
|
+
}
|
|
686
|
+
if (builtin === "WeakMap") {
|
|
687
|
+
return primitive("weakmap");
|
|
688
|
+
}
|
|
689
|
+
if (builtin === "WeakSet") {
|
|
690
|
+
return primitive("weakset");
|
|
691
|
+
}
|
|
692
|
+
if (builtin === "Map" || builtin === "ReadonlyMap") {
|
|
693
|
+
const key = args[0];
|
|
694
|
+
const value = args[1];
|
|
695
|
+
if (key === undefined || value === undefined) {
|
|
696
|
+
return primitive("map");
|
|
697
|
+
}
|
|
698
|
+
const keySchema = serializeType(key, state);
|
|
699
|
+
const valueSchema = serializeType(value, state);
|
|
700
|
+
if (keySchema === null || valueSchema === null) {
|
|
701
|
+
return primitive("map");
|
|
702
|
+
}
|
|
703
|
+
return { kind: "mapof", key: keySchema, value: valueSchema };
|
|
704
|
+
}
|
|
705
|
+
if (builtin === "Set" || builtin === "ReadonlySet") {
|
|
706
|
+
const element = args[0];
|
|
707
|
+
if (element === undefined) {
|
|
708
|
+
return primitive("set");
|
|
709
|
+
}
|
|
710
|
+
const schema = serializeType(element, state);
|
|
711
|
+
return schema === null ? primitive("set") : { kind: "setof", element: schema };
|
|
712
|
+
}
|
|
713
|
+
if (builtin === "Promise") {
|
|
714
|
+
return primitive("promise");
|
|
715
|
+
}
|
|
716
|
+
if (builtin !== null) {
|
|
717
|
+
const named = BUILTIN_NAMES.get(builtin);
|
|
718
|
+
if (named !== undefined) {
|
|
719
|
+
return primitive(named);
|
|
720
|
+
}
|
|
721
|
+
const collection = COLLECTION_NAMES.get(builtin);
|
|
722
|
+
if (collection !== undefined) {
|
|
723
|
+
return primitive(collection);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
for (const [baseName, typedArray] of TYPED_ARRAY_BASES) {
|
|
727
|
+
if (hasBuiltinBase(type, baseName, state.checker, new Set())) {
|
|
728
|
+
return primitive(typedArray);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
if (hasBuiltinBase(type, "Error", state.checker, new Set())) {
|
|
732
|
+
return primitive("error");
|
|
733
|
+
}
|
|
734
|
+
const symbolName = type.getSymbol()?.getName();
|
|
735
|
+
if (builtin === null && symbolName === "Buffer" && typeIsOpaque(type, state.checker)) {
|
|
736
|
+
return primitive("uint8array");
|
|
737
|
+
}
|
|
738
|
+
const symbol = resolveSymbol(type.getSymbol(), state.checker);
|
|
739
|
+
const declaration = symbol?.valueDeclaration ?? symbol?.declarations?.[0];
|
|
740
|
+
if (symbol !== undefined && declaration !== undefined && (ts.isClassDeclaration(declaration) || ts.isClassExpression(declaration))) {
|
|
741
|
+
if (isAmbientWithoutRuntime(declaration)) {
|
|
742
|
+
return { kind: "class", reference: "undefined" };
|
|
743
|
+
}
|
|
744
|
+
return { kind: "class", reference: state.runtimeRef(symbol) };
|
|
745
|
+
}
|
|
746
|
+
const baseNode = builtinBaseNode(type, state);
|
|
747
|
+
if (baseNode !== null) {
|
|
748
|
+
return baseNode;
|
|
749
|
+
}
|
|
750
|
+
if (type.getCallSignatures().length > 0) {
|
|
751
|
+
return primitive("function");
|
|
752
|
+
}
|
|
753
|
+
if (builtin === "Record" && args.length === 2) {
|
|
754
|
+
const valueSchema = serializeType(args[1], state);
|
|
755
|
+
return valueSchema === null ? primitive("object") : { kind: "record", value: valueSchema };
|
|
756
|
+
}
|
|
757
|
+
if (typeIsOpaque(type, state.checker)) {
|
|
758
|
+
return primitive("object");
|
|
759
|
+
}
|
|
760
|
+
const properties = state.checker.getPropertiesOfType(type);
|
|
761
|
+
const stringIndex = state.checker.getIndexTypeOfType(type, ts.IndexKind.String);
|
|
762
|
+
const numberIndex = state.checker.getIndexTypeOfType(type, ts.IndexKind.Number);
|
|
763
|
+
const indexNodes = [];
|
|
764
|
+
if (stringIndex !== undefined) {
|
|
765
|
+
const valueSchema = serializeType(stringIndex, state);
|
|
766
|
+
if (valueSchema !== null) {
|
|
767
|
+
indexNodes.push({ kind: "record", value: valueSchema });
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
if (numberIndex !== undefined && properties.length === 0) {
|
|
771
|
+
const valueSchema = serializeType(numberIndex, state);
|
|
772
|
+
if (valueSchema !== null) {
|
|
773
|
+
indexNodes.push({ kind: "record", value: valueSchema });
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
if (properties.length === 0) {
|
|
777
|
+
if (indexNodes.length === 0) {
|
|
778
|
+
return builtin === null ? { kind: "object", fields: [] } : primitive("object");
|
|
779
|
+
}
|
|
780
|
+
const unique = dedupeNodes(indexNodes);
|
|
781
|
+
return unique.length === 1 ? unique[0] : { kind: "intersection", members: unique };
|
|
782
|
+
}
|
|
783
|
+
const objectNode = serializeObjectProperties(type, state);
|
|
784
|
+
if (objectNode === null) {
|
|
785
|
+
return null;
|
|
786
|
+
}
|
|
787
|
+
if (indexNodes.length > 0) {
|
|
788
|
+
const unique = dedupeNodes(indexNodes);
|
|
789
|
+
return { kind: "intersection", members: dedupeNodes([objectNode, ...unique]) };
|
|
790
|
+
}
|
|
791
|
+
return objectNode;
|
|
792
|
+
}
|
|
793
|
+
function serializeTypeUncached(type, state) {
|
|
794
|
+
const flags = type.flags;
|
|
795
|
+
if ((flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter)) !== 0) {
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
if ((flags & ts.TypeFlags.Boolean) !== 0) {
|
|
799
|
+
return primitive("boolean");
|
|
800
|
+
}
|
|
801
|
+
if ((flags & ts.TypeFlags.String) !== 0) {
|
|
802
|
+
return primitive("string");
|
|
803
|
+
}
|
|
804
|
+
if ((flags & ts.TypeFlags.Number) !== 0) {
|
|
805
|
+
return primitive("number");
|
|
806
|
+
}
|
|
807
|
+
if ((flags & ts.TypeFlags.BigInt) !== 0) {
|
|
808
|
+
return primitive("bigint");
|
|
809
|
+
}
|
|
810
|
+
if ((flags & (ts.TypeFlags.ESSymbol | ts.TypeFlags.UniqueESSymbol)) !== 0) {
|
|
811
|
+
return primitive("symbol");
|
|
812
|
+
}
|
|
813
|
+
if ((flags & ts.TypeFlags.NonPrimitive) !== 0) {
|
|
814
|
+
return primitive("object");
|
|
815
|
+
}
|
|
816
|
+
if ((flags & ts.TypeFlags.EnumLiteral) !== 0 && (flags & ts.TypeFlags.Union) === 0) {
|
|
817
|
+
const values = enumValuesOf([type], state.checker);
|
|
818
|
+
return values === null ? null : { kind: "enum", values };
|
|
819
|
+
}
|
|
820
|
+
if ((flags & ts.TypeFlags.Union) !== 0) {
|
|
821
|
+
return serializeUnion(type, state);
|
|
822
|
+
}
|
|
823
|
+
if ((flags & ts.TypeFlags.Intersection) !== 0) {
|
|
824
|
+
return serializeIntersection(type, state);
|
|
825
|
+
}
|
|
826
|
+
if ((flags & ts.TypeFlags.Enum) !== 0) {
|
|
827
|
+
const values = enumValuesOfSymbol(type, state.checker);
|
|
828
|
+
return values === null ? null : { kind: "enum", values };
|
|
829
|
+
}
|
|
830
|
+
const literal = serializeLiteral(type);
|
|
831
|
+
if (literal !== null) {
|
|
832
|
+
return literal;
|
|
833
|
+
}
|
|
834
|
+
if ((flags & ts.TypeFlags.Never) !== 0) {
|
|
835
|
+
return primitive("never");
|
|
836
|
+
}
|
|
837
|
+
if ((flags & ts.TypeFlags.Void) !== 0) {
|
|
838
|
+
return primitive("void");
|
|
839
|
+
}
|
|
840
|
+
if ((flags & ts.TypeFlags.Undefined) !== 0) {
|
|
841
|
+
return primitive("undefined");
|
|
842
|
+
}
|
|
843
|
+
if ((flags & ts.TypeFlags.Null) !== 0) {
|
|
844
|
+
return primitive("null");
|
|
845
|
+
}
|
|
846
|
+
if ((flags & ts.TypeFlags.Object) !== 0) {
|
|
847
|
+
const object = type;
|
|
848
|
+
if ((object.objectFlags & ts.ObjectFlags.Mapped) !== 0
|
|
849
|
+
&& state.checker.getPropertiesOfType(type).length === 0
|
|
850
|
+
&& state.checker.getIndexTypeOfType(type, ts.IndexKind.String) === undefined
|
|
851
|
+
&& state.checker.getIndexTypeOfType(type, ts.IndexKind.Number) === undefined) {
|
|
852
|
+
return null;
|
|
853
|
+
}
|
|
854
|
+
return serializeObject(type, state);
|
|
855
|
+
}
|
|
856
|
+
return null;
|
|
857
|
+
}
|
|
858
|
+
function serializeType(type, state) {
|
|
859
|
+
if (state.active.has(type)) {
|
|
860
|
+
return { kind: "lazy", cell: lazyCell(type, state) };
|
|
861
|
+
}
|
|
862
|
+
const cached = state.cache.get(type);
|
|
863
|
+
if (cached !== undefined) {
|
|
864
|
+
return cached;
|
|
865
|
+
}
|
|
866
|
+
if (state.depth >= MAX_DEPTH) {
|
|
867
|
+
return { kind: "lazy", cell: lazyCell(type, state) };
|
|
868
|
+
}
|
|
869
|
+
state.active.add(type);
|
|
870
|
+
state.depth += 1;
|
|
871
|
+
try {
|
|
872
|
+
const node = serializeTypeUncached(type, state);
|
|
873
|
+
state.cache.set(type, node);
|
|
874
|
+
return node;
|
|
875
|
+
}
|
|
876
|
+
finally {
|
|
877
|
+
state.depth -= 1;
|
|
878
|
+
state.active.delete(type);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
function formatTuple(node, names) {
|
|
882
|
+
const elements = node.elements
|
|
883
|
+
.map((element) => renderNode(element, names))
|
|
884
|
+
.join(",");
|
|
885
|
+
if (node.rest === null) {
|
|
886
|
+
return `["tuple",[${elements}]]`;
|
|
887
|
+
}
|
|
888
|
+
return `["tuple",[${elements}],${renderNode(node.rest, names)}]`;
|
|
889
|
+
}
|
|
890
|
+
export function schemaHasClassRef(schema, isUnavailable = (reference) => reference === "undefined", seen = new Set()) {
|
|
891
|
+
switch (schema.kind) {
|
|
892
|
+
case "class":
|
|
893
|
+
return isUnavailable(schema.reference);
|
|
894
|
+
case "array":
|
|
895
|
+
return schemaHasClassRef(schema.element, isUnavailable, seen);
|
|
896
|
+
case "tuple":
|
|
897
|
+
return schema.elements.some((element) => schemaHasClassRef(element, isUnavailable, seen))
|
|
898
|
+
|| (schema.rest !== null && schemaHasClassRef(schema.rest, isUnavailable, seen));
|
|
899
|
+
case "union":
|
|
900
|
+
case "intersection":
|
|
901
|
+
return schema.members.some((member) => schemaHasClassRef(member, isUnavailable, seen));
|
|
902
|
+
case "object":
|
|
903
|
+
return schema.fields.some((field) => schemaHasClassRef(field.schema, isUnavailable, seen));
|
|
904
|
+
case "record":
|
|
905
|
+
return schemaHasClassRef(schema.value, isUnavailable, seen);
|
|
906
|
+
case "mapof":
|
|
907
|
+
return schemaHasClassRef(schema.key, isUnavailable, seen)
|
|
908
|
+
|| schemaHasClassRef(schema.value, isUnavailable, seen);
|
|
909
|
+
case "setof":
|
|
910
|
+
return schemaHasClassRef(schema.element, isUnavailable, seen);
|
|
911
|
+
case "optional":
|
|
912
|
+
case "nullable":
|
|
913
|
+
return schemaHasClassRef(schema.inner, isUnavailable, seen);
|
|
914
|
+
case "lazy": {
|
|
915
|
+
if (seen.has(schema.cell)) {
|
|
916
|
+
return false;
|
|
917
|
+
}
|
|
918
|
+
seen.add(schema.cell);
|
|
919
|
+
try {
|
|
920
|
+
return schema.cell.node !== null && schemaHasClassRef(schema.cell.node, isUnavailable, seen);
|
|
921
|
+
}
|
|
922
|
+
finally {
|
|
923
|
+
seen.delete(schema.cell);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
default:
|
|
927
|
+
return false;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
function renderFieldKey(name) {
|
|
931
|
+
return name === "__proto__" ? '["__proto__"]' : JSON.stringify(name);
|
|
932
|
+
}
|
|
933
|
+
function renderNode(node, names) {
|
|
934
|
+
switch (node.kind) {
|
|
935
|
+
case "primitive":
|
|
936
|
+
return JSON.stringify(node.name);
|
|
937
|
+
case "array":
|
|
938
|
+
return `["array",${renderNode(node.element, names)}]`;
|
|
939
|
+
case "tuple":
|
|
940
|
+
return formatTuple(node, names);
|
|
941
|
+
case "union":
|
|
942
|
+
return `["union",[${node.members.map((member) => renderNode(member, names)).join(",")}]]`;
|
|
943
|
+
case "intersection":
|
|
944
|
+
return `["intersection",[${node.members.map((member) => renderNode(member, names)).join(",")}]]`;
|
|
945
|
+
case "literal":
|
|
946
|
+
return `["literal",${JSON.stringify(node.value)}]`;
|
|
947
|
+
case "enum":
|
|
948
|
+
return `["enum",[${node.values.map((value) => JSON.stringify(value)).join(",")}]]`;
|
|
949
|
+
case "class":
|
|
950
|
+
return `["class",()=>${node.reference}]`;
|
|
951
|
+
case "lazy":
|
|
952
|
+
return `["lazy",()=>${names.get(node.cell) ?? "undefined"}]`;
|
|
953
|
+
case "object":
|
|
954
|
+
return `["object",{${node.fields.map((field) => `${renderFieldKey(field.name)}:${renderNode(field.schema, names)}`).join(",")}}]`;
|
|
955
|
+
case "record":
|
|
956
|
+
return `["record",${renderNode(node.value, names)}]`;
|
|
957
|
+
case "mapof":
|
|
958
|
+
return `["mapof",${renderNode(node.key, names)},${renderNode(node.value, names)}]`;
|
|
959
|
+
case "setof":
|
|
960
|
+
return `["setof",${renderNode(node.element, names)}]`;
|
|
961
|
+
case "optional":
|
|
962
|
+
return `["optional",${renderNode(node.inner, names)}]`;
|
|
963
|
+
case "nullable":
|
|
964
|
+
return `["nullable",${renderNode(node.inner, names)}]`;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
function renderFields(entries) {
|
|
968
|
+
const names = new Map();
|
|
969
|
+
const collect = (node) => {
|
|
970
|
+
switch (node.kind) {
|
|
971
|
+
case "array":
|
|
972
|
+
collect(node.element);
|
|
973
|
+
return;
|
|
974
|
+
case "tuple":
|
|
975
|
+
for (const element of node.elements) {
|
|
976
|
+
collect(element);
|
|
977
|
+
}
|
|
978
|
+
if (node.rest !== null) {
|
|
979
|
+
collect(node.rest);
|
|
980
|
+
}
|
|
981
|
+
return;
|
|
982
|
+
case "union":
|
|
983
|
+
case "intersection":
|
|
984
|
+
for (const member of node.members) {
|
|
985
|
+
collect(member);
|
|
986
|
+
}
|
|
987
|
+
return;
|
|
988
|
+
case "object":
|
|
989
|
+
for (const field of node.fields) {
|
|
990
|
+
collect(field.schema);
|
|
991
|
+
}
|
|
992
|
+
return;
|
|
993
|
+
case "record":
|
|
994
|
+
collect(node.value);
|
|
995
|
+
return;
|
|
996
|
+
case "mapof":
|
|
997
|
+
collect(node.key);
|
|
998
|
+
collect(node.value);
|
|
999
|
+
return;
|
|
1000
|
+
case "setof":
|
|
1001
|
+
collect(node.element);
|
|
1002
|
+
return;
|
|
1003
|
+
case "optional":
|
|
1004
|
+
case "nullable":
|
|
1005
|
+
collect(node.inner);
|
|
1006
|
+
return;
|
|
1007
|
+
case "lazy":
|
|
1008
|
+
if (names.has(node.cell)) {
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
names.set(node.cell, `lazy${names.size}`);
|
|
1012
|
+
if (node.cell.node !== null) {
|
|
1013
|
+
collect(node.cell.node);
|
|
1014
|
+
}
|
|
1015
|
+
return;
|
|
1016
|
+
default:
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
};
|
|
1020
|
+
for (const entry of entries) {
|
|
1021
|
+
collect(entry.schema);
|
|
1022
|
+
}
|
|
1023
|
+
const definitions = [...names].map(([cell, name]) => `const ${name} = ${renderNode(cell.node ?? primitive("object"), names)};`);
|
|
1024
|
+
const fields = entries
|
|
1025
|
+
.map((entry) => `${renderFieldKey(entry.name)}: ${renderNode(entry.schema, names)}`)
|
|
1026
|
+
.join(", ");
|
|
1027
|
+
return { fields, definitions };
|
|
1028
|
+
}
|
|
1029
|
+
function fieldSchema(field, state) {
|
|
1030
|
+
if (field.type === undefined) {
|
|
1031
|
+
return null;
|
|
1032
|
+
}
|
|
1033
|
+
const type = state.checker.getTypeFromTypeNode(field.type);
|
|
1034
|
+
return serializeType(type, state);
|
|
1035
|
+
}
|
|
1036
|
+
function hasStaticModifier(member) {
|
|
1037
|
+
const modifiers = ts.canHaveModifiers(member) ? ts.getModifiers(member) : undefined;
|
|
1038
|
+
if (modifiers === undefined) {
|
|
1039
|
+
return false;
|
|
1040
|
+
}
|
|
1041
|
+
return modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword);
|
|
1042
|
+
}
|
|
1043
|
+
function lineBreakOf(sourceFile) {
|
|
1044
|
+
return sourceFile.text.includes("\r\n") ? "\r\n" : "\n";
|
|
1045
|
+
}
|
|
1046
|
+
function closingPosition(sourceFile, declaration) {
|
|
1047
|
+
const end = declaration.getEnd();
|
|
1048
|
+
const position = end - 1;
|
|
1049
|
+
if (sourceFile.text[position] === "}") {
|
|
1050
|
+
return position;
|
|
1051
|
+
}
|
|
1052
|
+
const fallback = sourceFile.text.lastIndexOf("}", end);
|
|
1053
|
+
return fallback >= 0 ? fallback : position;
|
|
1054
|
+
}
|
|
1055
|
+
function blockIndentation(sourceFile, position, declaration) {
|
|
1056
|
+
const lineStart = sourceFile.text.lastIndexOf("\n", position - 1) + 1;
|
|
1057
|
+
const prefix = sourceFile.text.slice(lineStart, position);
|
|
1058
|
+
if (lineStart > 0 && /^[ \t]*$/.test(prefix)) {
|
|
1059
|
+
return { closing: prefix, lead: "" };
|
|
1060
|
+
}
|
|
1061
|
+
const declarationStart = declaration.getStart(sourceFile);
|
|
1062
|
+
const declarationLineStart = sourceFile.text.lastIndexOf("\n", declarationStart - 1) + 1;
|
|
1063
|
+
const declarationPrefix = sourceFile.text.slice(declarationLineStart, declarationStart);
|
|
1064
|
+
const closing = /^[ \t]*$/.test(declarationPrefix) ? declarationPrefix : "";
|
|
1065
|
+
return { closing, lead: `${lineBreakOf(sourceFile)}${closing}` };
|
|
1066
|
+
}
|
|
1067
|
+
function flushLazy(state) {
|
|
1068
|
+
while (state.queue.length > 0) {
|
|
1069
|
+
const type = state.queue.shift();
|
|
1070
|
+
if (type === undefined) {
|
|
1071
|
+
continue;
|
|
1072
|
+
}
|
|
1073
|
+
const cell = state.lazy.get(type);
|
|
1074
|
+
if (cell === undefined || cell.node !== null) {
|
|
1075
|
+
continue;
|
|
1076
|
+
}
|
|
1077
|
+
const traversal = {
|
|
1078
|
+
...state,
|
|
1079
|
+
active: new Set(),
|
|
1080
|
+
path: [...cell.path],
|
|
1081
|
+
depth: 0,
|
|
1082
|
+
};
|
|
1083
|
+
const node = serializeType(type, traversal);
|
|
1084
|
+
cell.node = node === null || (node.kind === "lazy" && node.cell === cell)
|
|
1085
|
+
? primitive("object")
|
|
1086
|
+
: node;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
function buildBlock(sourceFile, declaration, entries) {
|
|
1090
|
+
const position = closingPosition(sourceFile, declaration);
|
|
1091
|
+
const indentation = blockIndentation(sourceFile, position, declaration);
|
|
1092
|
+
const newline = lineBreakOf(sourceFile);
|
|
1093
|
+
const inner = `${indentation.closing}${INDENT_UNIT}${INDENT_UNIT}`;
|
|
1094
|
+
const memberIndent = `${indentation.closing}${INDENT_UNIT}`;
|
|
1095
|
+
const rendered = renderFields(entries);
|
|
1096
|
+
const definitions = rendered.definitions.map((definition) => `${inner}${definition}`);
|
|
1097
|
+
const assignment = `this[Symbol.for(${JSON.stringify(SCHEMA_KEY)})] = { version: 1, fields: { ${rendered.fields} } };`;
|
|
1098
|
+
const code = [
|
|
1099
|
+
`${indentation.lead}${INDENT_UNIT}static {`,
|
|
1100
|
+
...definitions,
|
|
1101
|
+
`${inner}${assignment}`,
|
|
1102
|
+
`${memberIndent}}`,
|
|
1103
|
+
`${indentation.closing}`,
|
|
1104
|
+
].join(newline);
|
|
1105
|
+
return { position, code };
|
|
1106
|
+
}
|
|
1107
|
+
function collectClasses(node, classes) {
|
|
1108
|
+
if (ts.isClassDeclaration(node) || ts.isClassExpression(node)) {
|
|
1109
|
+
classes.push(node);
|
|
1110
|
+
}
|
|
1111
|
+
ts.forEachChild(node, (child) => {
|
|
1112
|
+
collectClasses(child, classes);
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
function classLikeName(declaration, sourceFile) {
|
|
1116
|
+
if (declaration.name !== undefined) {
|
|
1117
|
+
return declaration.name.text;
|
|
1118
|
+
}
|
|
1119
|
+
const parent = declaration.parent;
|
|
1120
|
+
if (ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name)) {
|
|
1121
|
+
return parent.name.text;
|
|
1122
|
+
}
|
|
1123
|
+
if (ts.isPropertyDeclaration(parent) || ts.isPropertyAssignment(parent)) {
|
|
1124
|
+
return nameTextOf(parent.name, sourceFile);
|
|
1125
|
+
}
|
|
1126
|
+
if (ts.isExportAssignment(parent)) {
|
|
1127
|
+
return "default";
|
|
1128
|
+
}
|
|
1129
|
+
if (ts.isClassDeclaration(declaration)) {
|
|
1130
|
+
return "default";
|
|
1131
|
+
}
|
|
1132
|
+
return "class";
|
|
1133
|
+
}
|
|
1134
|
+
function shouldEmit(shouldGenerate, declaration) {
|
|
1135
|
+
if (shouldGenerate === undefined) {
|
|
1136
|
+
return true;
|
|
1137
|
+
}
|
|
1138
|
+
return shouldGenerate(declaration);
|
|
1139
|
+
}
|
|
1140
|
+
export function generateValidationBlocks(options) {
|
|
1141
|
+
const { sourceFile, checker, runtimeRef, shouldGenerate } = options;
|
|
1142
|
+
const blocks = [];
|
|
1143
|
+
const classes = [];
|
|
1144
|
+
const cache = new WeakMap();
|
|
1145
|
+
const lazy = new Map();
|
|
1146
|
+
const queue = [];
|
|
1147
|
+
const reported = new Set();
|
|
1148
|
+
const report = (message) => {
|
|
1149
|
+
if (reported.has(message)) {
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
reported.add(message);
|
|
1153
|
+
options.diagnostics?.(message);
|
|
1154
|
+
};
|
|
1155
|
+
collectClasses(sourceFile, classes);
|
|
1156
|
+
for (const declaration of classes) {
|
|
1157
|
+
if (isAmbientWithoutRuntime(declaration)) {
|
|
1158
|
+
continue;
|
|
1159
|
+
}
|
|
1160
|
+
const className = classLikeName(declaration, sourceFile);
|
|
1161
|
+
if (!shouldEmit(shouldGenerate, declaration)) {
|
|
1162
|
+
continue;
|
|
1163
|
+
}
|
|
1164
|
+
if (sourceFile.text.slice(declaration.getStart(sourceFile), declaration.getEnd()).includes(SCHEMA_KEY)) {
|
|
1165
|
+
continue;
|
|
1166
|
+
}
|
|
1167
|
+
const state = {
|
|
1168
|
+
checker,
|
|
1169
|
+
runtimeRef,
|
|
1170
|
+
sourceFile,
|
|
1171
|
+
report,
|
|
1172
|
+
strict: options.strict === true,
|
|
1173
|
+
active: new Set(),
|
|
1174
|
+
cache,
|
|
1175
|
+
lazy,
|
|
1176
|
+
queue,
|
|
1177
|
+
path: [className],
|
|
1178
|
+
depth: 0,
|
|
1179
|
+
};
|
|
1180
|
+
const entries = [];
|
|
1181
|
+
for (const member of declaration.members) {
|
|
1182
|
+
if (!ts.isAutoAccessorPropertyDeclaration(member)) {
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
if (hasStaticModifier(member)) {
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
if (!ts.isIdentifier(member.name)) {
|
|
1189
|
+
if (ts.isPrivateIdentifier(member.name)) {
|
|
1190
|
+
continue;
|
|
1191
|
+
}
|
|
1192
|
+
const label = nameTextOf(member.name, sourceFile);
|
|
1193
|
+
state.path.push(label);
|
|
1194
|
+
try {
|
|
1195
|
+
reportSkipped(state, state.path, "поле пропущено: поддерживаются только идентификаторы");
|
|
1196
|
+
}
|
|
1197
|
+
finally {
|
|
1198
|
+
state.path.pop();
|
|
1199
|
+
}
|
|
1200
|
+
continue;
|
|
1201
|
+
}
|
|
1202
|
+
const name = member.name.text;
|
|
1203
|
+
state.path.push(name);
|
|
1204
|
+
let schema;
|
|
1205
|
+
try {
|
|
1206
|
+
schema = fieldSchema(member, state);
|
|
1207
|
+
}
|
|
1208
|
+
finally {
|
|
1209
|
+
state.path.pop();
|
|
1210
|
+
}
|
|
1211
|
+
if (schema === null) {
|
|
1212
|
+
if (member.type !== undefined) {
|
|
1213
|
+
const type = checker.getTypeFromTypeNode(member.type);
|
|
1214
|
+
reportUnsupported(state, [className, name], type);
|
|
1215
|
+
}
|
|
1216
|
+
continue;
|
|
1217
|
+
}
|
|
1218
|
+
entries.push({ name, schema });
|
|
1219
|
+
}
|
|
1220
|
+
if (entries.length === 0) {
|
|
1221
|
+
continue;
|
|
1222
|
+
}
|
|
1223
|
+
flushLazy(state);
|
|
1224
|
+
const block = buildBlock(sourceFile, declaration, entries);
|
|
1225
|
+
const fields = new Map(entries.map((entry) => [entry.name, entry.schema]));
|
|
1226
|
+
const unavailableFields = entries
|
|
1227
|
+
.filter((entry) => schemaHasClassRef(entry.schema))
|
|
1228
|
+
.map((entry) => entry.name);
|
|
1229
|
+
blocks.push({ ...block, className, fields, unavailableFields });
|
|
1230
|
+
}
|
|
1231
|
+
return blocks;
|
|
1232
|
+
}
|
|
1233
|
+
//# sourceMappingURL=typegen.js.map
|