typia 4.1.0 → 4.1.1-dev.20230621

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.
Files changed (34) hide show
  1. package/lib/factories/internal/metadata/iterate_metadata_array.js +1 -1
  2. package/lib/factories/internal/metadata/iterate_metadata_array.js.map +1 -1
  3. package/lib/module.d.ts +2 -2
  4. package/package.json +3 -2
  5. package/src/factories/CommentFactory.ts +64 -64
  6. package/src/factories/IdentifierFactory.ts +59 -59
  7. package/src/factories/MetadataFactory.ts +30 -30
  8. package/src/factories/internal/metadata/iterate_metadata_array.ts +1 -2
  9. package/src/metadata/MetadataObject.ts +118 -118
  10. package/src/module.ts +2038 -2038
  11. package/src/programmers/ApplicationProgrammer.ts +47 -47
  12. package/src/programmers/AssertCloneProgrammer.ts +71 -71
  13. package/src/programmers/AssertParseProgrammer.ts +66 -66
  14. package/src/programmers/AssertProgrammer.ts +279 -279
  15. package/src/programmers/AssertPruneProgrammer.ts +68 -68
  16. package/src/programmers/AssertStringifyProgrammer.ts +66 -66
  17. package/src/programmers/CloneProgrammer.ts +587 -587
  18. package/src/programmers/IsCloneProgrammer.ts +78 -78
  19. package/src/programmers/IsParseProgrammer.ts +72 -72
  20. package/src/programmers/IsProgrammer.ts +239 -239
  21. package/src/programmers/IsPruneProgrammer.ts +73 -73
  22. package/src/programmers/IsStringifyProgrammer.ts +76 -76
  23. package/src/programmers/LiteralsProgrammer.ts +60 -60
  24. package/src/programmers/PruneProgrammer.ts +542 -542
  25. package/src/programmers/RandomProgrammer.ts +581 -581
  26. package/src/programmers/StringifyProgrammer.ts +978 -978
  27. package/src/programmers/ValidateCloneProgrammer.ts +85 -85
  28. package/src/programmers/ValidateParseProgrammer.ts +70 -70
  29. package/src/programmers/ValidateProgrammer.ts +305 -305
  30. package/src/programmers/ValidatePruneProgrammer.ts +78 -78
  31. package/src/programmers/ValidateStringifyProgrammer.ts +84 -84
  32. package/src/programmers/internal/application_tuple.ts +57 -57
  33. package/src/programmers/internal/feature_object_entries.ts +63 -63
  34. package/src/schemas/IJsonSchema.ts +133 -133
@@ -1,581 +1,581 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../factories/MetadataCollection";
6
- import { MetadataFactory } from "../factories/MetadataFactory";
7
- import { StatementFactory } from "../factories/StatementFactory";
8
- import { TemplateFactory } from "../factories/TemplateFactory";
9
- import { TypeFactory } from "../factories/TypeFactory";
10
-
11
- import { ICommentTag } from "../metadata/ICommentTag";
12
- import { IMetadataTag } from "../metadata/IMetadataTag";
13
- import { Metadata } from "../metadata/Metadata";
14
- import { MetadataArray } from "../metadata/MetadataArray";
15
- import { MetadataObject } from "../metadata/MetadataObject";
16
- import { MetadataTuple } from "../metadata/MetadataTuple";
17
-
18
- import { IProject } from "../transformers/IProject";
19
-
20
- import { FunctionImporter } from "./helpers/FunctionImporeter";
21
- import { RandomJoiner } from "./helpers/RandomJoiner";
22
- import { RandomRanger } from "./helpers/RandomRanger";
23
- import { random_custom } from "./internal/random_custom";
24
-
25
- export namespace RandomProgrammer {
26
- export const write =
27
- (project: IProject) =>
28
- (modulo: ts.LeftHandSideExpression) =>
29
- (init?: ts.Expression) => {
30
- const importer: FunctionImporter = new FunctionImporter();
31
- return (type: ts.Type, name?: string) => {
32
- // INITIALIZE METADATA
33
- const collection: MetadataCollection = new MetadataCollection();
34
- const meta: Metadata = MetadataFactory.analyze(project.checker)(
35
- {
36
- resolve: true,
37
- constant: true,
38
- absorb: true,
39
- },
40
- )(collection)(type);
41
-
42
- // GENERATE FUNCTION
43
- const functions = {
44
- objects: write_object_functions(importer)(collection),
45
- arrays: write_array_functions(importer)(collection),
46
- tuples: write_tuple_functions(importer)(collection),
47
- };
48
-
49
- const output: ts.Expression = decode(importer)({
50
- function: false,
51
- recursive: false,
52
- })(meta, [], []);
53
-
54
- return ts.factory.createArrowFunction(
55
- undefined,
56
- undefined,
57
- [
58
- IdentifierFactory.parameter(
59
- "generator",
60
- ts.factory.createTypeReferenceNode(
61
- "Partial<typia.IRandomGenerator>",
62
- ),
63
- init ??
64
- ts.factory.createToken(
65
- ts.SyntaxKind.QuestionToken,
66
- ),
67
- ),
68
- ],
69
- ts.factory.createTypeReferenceNode(
70
- `typia.Primitive<${
71
- name ??
72
- TypeFactory.getFullName(project.checker)(type)
73
- }>`,
74
- ),
75
- undefined,
76
- ts.factory.createBlock(
77
- [
78
- ...importer.declare(modulo),
79
- ...functions.objects,
80
- ...functions.arrays,
81
- ...functions.tuples,
82
- ts.factory.createReturnStatement(output),
83
- ],
84
- true,
85
- ),
86
- );
87
- };
88
- };
89
-
90
- const write_object_functions =
91
- (importer: FunctionImporter) =>
92
- (collection: MetadataCollection): ts.VariableStatement[] =>
93
- collection.objects().map((obj, i) =>
94
- StatementFactory.constant(
95
- PREFIX.object(i),
96
- ts.factory.createArrowFunction(
97
- undefined,
98
- undefined,
99
- [
100
- IdentifierFactory.parameter(
101
- "_recursive",
102
- TypeFactory.keyword("boolean"),
103
- ts.factory.createIdentifier(
104
- String(obj.recursive),
105
- ),
106
- ),
107
- IdentifierFactory.parameter(
108
- "_depth",
109
- TypeFactory.keyword("number"),
110
- ts.factory.createNumericLiteral(0),
111
- ),
112
- ],
113
- TypeFactory.keyword("any"),
114
- undefined,
115
- RandomJoiner.object(COALESCE(importer))(
116
- decode(importer)({
117
- recursive: obj.recursive,
118
- function: true,
119
- }),
120
- )(obj),
121
- ),
122
- ),
123
- );
124
-
125
- const write_array_functions =
126
- (importer: FunctionImporter) =>
127
- (collection: MetadataCollection): ts.VariableStatement[] =>
128
- collection
129
- .arrays()
130
- .filter((a) => a.recursive)
131
- .map((array, i) =>
132
- StatementFactory.constant(
133
- PREFIX.array(i),
134
- ts.factory.createArrowFunction(
135
- undefined,
136
- undefined,
137
- [
138
- IdentifierFactory.parameter(
139
- "length",
140
- TypeFactory.keyword("number"),
141
- ),
142
- IdentifierFactory.parameter(
143
- "_recursive",
144
- TypeFactory.keyword("boolean"),
145
- ts.factory.createTrue(),
146
- ),
147
- IdentifierFactory.parameter(
148
- "_depth",
149
- TypeFactory.keyword("number"),
150
- ts.factory.createNumericLiteral(0),
151
- ),
152
- ],
153
- TypeFactory.keyword("any"),
154
- undefined,
155
- RandomJoiner.array(COALESCE(importer))(
156
- decode(importer)({
157
- recursive: true,
158
- function: true,
159
- }),
160
- )({
161
- recursive: true,
162
- function: true,
163
- })(ts.factory.createIdentifier("length"))(
164
- array.value,
165
- [],
166
- [],
167
- ),
168
- ),
169
- ),
170
- );
171
-
172
- const write_tuple_functions =
173
- (importer: FunctionImporter) =>
174
- (collection: MetadataCollection): ts.VariableStatement[] =>
175
- collection
176
- .tuples()
177
- .filter((a) => a.recursive)
178
- .map((tuple, i) =>
179
- StatementFactory.constant(
180
- PREFIX.tuple(i),
181
- ts.factory.createArrowFunction(
182
- undefined,
183
- undefined,
184
- [
185
- IdentifierFactory.parameter(
186
- "_recursive",
187
- TypeFactory.keyword("boolean"),
188
- ts.factory.createTrue(),
189
- ),
190
- IdentifierFactory.parameter(
191
- "_depth",
192
- TypeFactory.keyword("number"),
193
- ts.factory.createNumericLiteral(0),
194
- ),
195
- ],
196
- TypeFactory.keyword("any"),
197
- undefined,
198
- RandomJoiner.tuple(
199
- decode(importer)({
200
- function: true,
201
- recursive: true,
202
- }),
203
- )(tuple.elements, [], []),
204
- ),
205
- ),
206
- );
207
-
208
- /* -----------------------------------------------------------
209
- DECODERS
210
- ----------------------------------------------------------- */
211
- const decode =
212
- (importer: FunctionImporter) =>
213
- (explore: IExplore) =>
214
- (
215
- meta: Metadata,
216
- tags: IMetadataTag[],
217
- comments: ICommentTag[],
218
- ): ts.Expression => {
219
- const expressions: ts.Expression[] = [];
220
- if (meta.any)
221
- expressions.push(
222
- ts.factory.createStringLiteral(
223
- "fucking any type exists...",
224
- ),
225
- );
226
-
227
- // NULL COALESCING
228
- if (meta.required === false)
229
- expressions.push(ts.factory.createIdentifier("undefined"));
230
- if (meta.nullable === true)
231
- expressions.push(ts.factory.createNull());
232
-
233
- // CONSTANT TYPES
234
- for (const constant of meta.constants)
235
- for (const value of constant.values)
236
- expressions.push(decode_atomic(value));
237
-
238
- // ATOMIC VARIABLES
239
- for (const template of meta.templates)
240
- expressions.push(decode_template(importer)(explore)(template));
241
- for (const atomic of meta.atomics)
242
- if (atomic === "boolean")
243
- expressions.push(decode_boolean(importer));
244
- else if (atomic === "number")
245
- expressions.push(decode_number(importer)(tags)(comments));
246
- else if (atomic === "string")
247
- expressions.push(decode_string(importer)(tags)(comments));
248
- else if (atomic === "bigint")
249
- expressions.push(decode_bigint(importer)(tags)(comments));
250
-
251
- // INSTANCE TYPES
252
- if (meta.resolved)
253
- expressions.push(
254
- decode(importer)(explore)(
255
- meta.resolved.returns,
256
- tags,
257
- comments,
258
- ),
259
- );
260
- for (const array of meta.arrays)
261
- expressions.push(
262
- decode_array(importer)(explore)(array, tags, comments),
263
- );
264
- for (const tuple of meta.tuples)
265
- expressions.push(
266
- decode_tuple(importer)(explore)(tuple, tags, comments),
267
- );
268
- for (const o of meta.objects)
269
- expressions.push(decode_object(importer)(explore)(o));
270
- for (const native of meta.natives)
271
- if (native === "Boolean")
272
- expressions.push(decode_boolean(importer));
273
- else if (native === "Number")
274
- expressions.push(decode_number(importer)(tags)(comments));
275
- else if (native === "String")
276
- expressions.push(decode_string(importer)(tags)(comments));
277
- else expressions.push(ts.factory.createIdentifier("{}"));
278
- if (meta.sets.length || meta.maps.length)
279
- expressions.push(ts.factory.createIdentifier("{}"));
280
-
281
- // PRIMITIVE TYPES
282
- if (expressions.length === 1) return expressions[0]!;
283
- return ts.factory.createCallExpression(
284
- ts.factory.createCallExpression(
285
- importer.use("pick"),
286
- undefined,
287
- [
288
- ts.factory.createArrayLiteralExpression(
289
- expressions.map((expr) =>
290
- ts.factory.createArrowFunction(
291
- undefined,
292
- undefined,
293
- [],
294
- undefined,
295
- undefined,
296
- expr,
297
- ),
298
- ),
299
- true,
300
- ),
301
- ],
302
- ),
303
- undefined,
304
- undefined,
305
- );
306
- };
307
-
308
- const decode_boolean = (importer: FunctionImporter) =>
309
- ts.factory.createCallExpression(
310
- COALESCE(importer)("boolean"),
311
- undefined,
312
- undefined,
313
- );
314
-
315
- const decode_atomic = (value: Atomic) =>
316
- typeof value === "boolean"
317
- ? ts.factory.createIdentifier(value.toString())
318
- : typeof value === "number"
319
- ? ts.factory.createNumericLiteral(value)
320
- : typeof value === "string"
321
- ? ts.factory.createStringLiteral(value)
322
- : ts.factory.createBigIntLiteral(value.toString());
323
-
324
- const decode_template =
325
- (importer: FunctionImporter) =>
326
- (explore: IExplore) =>
327
- (template: Metadata[]) =>
328
- TemplateFactory.generate(
329
- template.map((meta) => decode(importer)(explore)(meta, [], [])),
330
- );
331
-
332
- const decode_number =
333
- (importer: FunctionImporter) =>
334
- (tags: IMetadataTag[]) =>
335
- (comments: ICommentTag[]): ts.Expression => {
336
- const type = tags.find(
337
- (t) => t.kind === "type" && t.value === "uint",
338
- )
339
- ? "int"
340
- : tags.find((t) => t.kind === "type" && t.value === "int")
341
- ? "uint"
342
- : "double";
343
- return random_custom(COALESCE(importer))("number")(comments)(
344
- RandomRanger.number({
345
- type,
346
- transform: (value) =>
347
- ts.factory.createNumericLiteral(value),
348
- setter: (args) =>
349
- ts.factory.createCallExpression(
350
- type === "double" &&
351
- tags.every(
352
- (t) =>
353
- t.kind !== "multipleOf" &&
354
- t.kind !== "step",
355
- )
356
- ? COALESCE(importer)("number")
357
- : COALESCE(importer)("integer"),
358
- undefined,
359
- args.map((val) =>
360
- ts.factory.createNumericLiteral(val),
361
- ),
362
- ),
363
- })({
364
- minimum: 0,
365
- maximum: 100,
366
- gap: 10,
367
- })(tags),
368
- );
369
- };
370
-
371
- const decode_bigint =
372
- (importer: FunctionImporter) =>
373
- (tags: IMetadataTag[]) =>
374
- (comments: ICommentTag[]): ts.Expression =>
375
- random_custom(COALESCE(importer))("bigint")(comments)(
376
- RandomRanger.number({
377
- type: tags.find(
378
- (t) => t.kind === "type" && t.value === "uint",
379
- )
380
- ? "uint"
381
- : "int",
382
- transform: (value) =>
383
- ts.factory.createCallExpression(
384
- ts.factory.createIdentifier("BigInt"),
385
- undefined,
386
- [ts.factory.createStringLiteral(value.toString())],
387
- ),
388
- setter: (args) =>
389
- ts.factory.createCallExpression(
390
- COALESCE(importer)("bigint"),
391
- undefined,
392
- args.map((value) =>
393
- ts.factory.createCallExpression(
394
- ts.factory.createIdentifier("BigInt"),
395
- undefined,
396
- [
397
- ts.factory.createStringLiteral(
398
- value.toString(),
399
- ),
400
- ],
401
- ),
402
- ),
403
- ),
404
- })({
405
- minimum: 0,
406
- maximum: 100,
407
- gap: 10,
408
- })(tags),
409
- );
410
-
411
- const decode_string =
412
- (importer: FunctionImporter) =>
413
- (tags: IMetadataTag[]) =>
414
- (comments: ICommentTag[]): ts.Expression =>
415
- random_custom(COALESCE(importer))("string")(comments)(
416
- (() => {
417
- for (const t of tags)
418
- if (t.kind === "format")
419
- return ts.factory.createCallExpression(
420
- COALESCE(importer)(t.value),
421
- undefined,
422
- undefined,
423
- );
424
- else if (t.kind === "pattern")
425
- return ts.factory.createCallExpression(
426
- COALESCE(importer)("pattern"),
427
- undefined,
428
- [ts.factory.createIdentifier(`/${t.value}/`)],
429
- );
430
-
431
- const tail = RandomRanger.length(COALESCE(importer))({
432
- minimum: 5,
433
- maximum: 25,
434
- gap: 5,
435
- })({
436
- fixed: "length",
437
- minimum: "minLength",
438
- maximum: "maxLength",
439
- })(tags);
440
- return ts.factory.createCallExpression(
441
- COALESCE(importer)("string"),
442
- undefined,
443
- tail ? [tail] : undefined,
444
- );
445
- })(),
446
- );
447
-
448
- const decode_array =
449
- (importer: FunctionImporter) =>
450
- (explore: IExplore) =>
451
- (
452
- array: MetadataArray,
453
- tags: IMetadataTag[],
454
- comments: ICommentTag[],
455
- ) => {
456
- const length: ts.Expression | undefined = RandomRanger.length(
457
- COALESCE(importer),
458
- )({
459
- minimum: 0,
460
- maximum: 3,
461
- gap: 3,
462
- })({
463
- fixed: "items",
464
- minimum: "minItems",
465
- maximum: "maxItems",
466
- })(tags);
467
- if (array.recursive)
468
- return ts.factory.createCallExpression(
469
- ts.factory.createIdentifier(
470
- importer.useLocal(PREFIX.array(array.index!)),
471
- ),
472
- undefined,
473
- [
474
- length ?? COALESCE(importer)("length"),
475
- ts.factory.createTrue(),
476
- explore.recursive
477
- ? ts.factory.createAdd(
478
- ts.factory.createNumericLiteral(1),
479
- ts.factory.createIdentifier("_depth"),
480
- )
481
- : ts.factory.createNumericLiteral(0),
482
- ],
483
- );
484
- const expr: ts.Expression = RandomJoiner.array(COALESCE(importer))(
485
- decode(importer)(explore),
486
- )(explore)(length)(array.value, tags, comments);
487
- return explore.recursive
488
- ? ts.factory.createConditionalExpression(
489
- ts.factory.createLogicalAnd(
490
- ts.factory.createIdentifier("_recursive"),
491
- ts.factory.createLessThan(
492
- ts.factory.createNumericLiteral(5),
493
- ts.factory.createIdentifier("_depth"),
494
- ),
495
- ),
496
- undefined,
497
- ts.factory.createIdentifier("[]"),
498
- undefined,
499
- expr,
500
- )
501
- : expr;
502
- };
503
-
504
- const decode_tuple =
505
- (importer: FunctionImporter) =>
506
- (explore: IExplore) =>
507
- (
508
- tuple: MetadataTuple,
509
- tags: IMetadataTag[],
510
- comments: ICommentTag[],
511
- ): ts.Expression =>
512
- tuple.recursive
513
- ? ts.factory.createCallExpression(
514
- ts.factory.createIdentifier(
515
- importer.useLocal(PREFIX.tuple(tuple.index!)),
516
- ),
517
- undefined,
518
- [
519
- ts.factory.createTrue(),
520
- explore.recursive
521
- ? ts.factory.createAdd(
522
- ts.factory.createNumericLiteral(1),
523
- ts.factory.createIdentifier("_depth"),
524
- )
525
- : ts.factory.createNumericLiteral(0),
526
- ],
527
- )
528
- : RandomJoiner.tuple(decode(importer)(explore))(
529
- tuple.elements,
530
- tags,
531
- comments,
532
- );
533
-
534
- const decode_object =
535
- (importer: FunctionImporter) =>
536
- (explore: IExplore) =>
537
- (object: MetadataObject) =>
538
- ts.factory.createCallExpression(
539
- ts.factory.createIdentifier(
540
- importer.useLocal(PREFIX.object(object.index)),
541
- ),
542
- undefined,
543
- explore.function
544
- ? [
545
- explore.recursive
546
- ? ts.factory.createTrue()
547
- : ts.factory.createIdentifier("_recursive"),
548
- ts.factory.createConditionalExpression(
549
- ts.factory.createIdentifier("_recursive"),
550
- undefined,
551
- ts.factory.createAdd(
552
- ts.factory.createNumericLiteral(1),
553
- ts.factory.createIdentifier("_depth"),
554
- ),
555
- undefined,
556
- ts.factory.createIdentifier("_depth"),
557
- ),
558
- ]
559
- : undefined,
560
- );
561
- }
562
-
563
- type Atomic = boolean | number | string | bigint;
564
- interface IExplore {
565
- function: boolean;
566
- recursive: boolean;
567
- }
568
-
569
- const PREFIX = {
570
- object: (i: number) => `$ro${i}`,
571
- array: (i: number) => `$ra${i}`,
572
- tuple: (i: number) => `$rt${i}`,
573
- };
574
- const COALESCE = (importer: FunctionImporter) => (name: string) =>
575
- ExpressionFactory.coalesce(
576
- ts.factory.createPropertyAccessChain(
577
- ts.factory.createIdentifier("generator"),
578
- ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
579
- ts.factory.createIdentifier(name),
580
- ),
581
- )(IdentifierFactory.access(importer.use("generator"))(name));
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../factories/MetadataCollection";
6
+ import { MetadataFactory } from "../factories/MetadataFactory";
7
+ import { StatementFactory } from "../factories/StatementFactory";
8
+ import { TemplateFactory } from "../factories/TemplateFactory";
9
+ import { TypeFactory } from "../factories/TypeFactory";
10
+
11
+ import { ICommentTag } from "../metadata/ICommentTag";
12
+ import { IMetadataTag } from "../metadata/IMetadataTag";
13
+ import { Metadata } from "../metadata/Metadata";
14
+ import { MetadataArray } from "../metadata/MetadataArray";
15
+ import { MetadataObject } from "../metadata/MetadataObject";
16
+ import { MetadataTuple } from "../metadata/MetadataTuple";
17
+
18
+ import { IProject } from "../transformers/IProject";
19
+
20
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
21
+ import { RandomJoiner } from "./helpers/RandomJoiner";
22
+ import { RandomRanger } from "./helpers/RandomRanger";
23
+ import { random_custom } from "./internal/random_custom";
24
+
25
+ export namespace RandomProgrammer {
26
+ export const write =
27
+ (project: IProject) =>
28
+ (modulo: ts.LeftHandSideExpression) =>
29
+ (init?: ts.Expression) => {
30
+ const importer: FunctionImporter = new FunctionImporter();
31
+ return (type: ts.Type, name?: string) => {
32
+ // INITIALIZE METADATA
33
+ const collection: MetadataCollection = new MetadataCollection();
34
+ const meta: Metadata = MetadataFactory.analyze(project.checker)(
35
+ {
36
+ resolve: true,
37
+ constant: true,
38
+ absorb: true,
39
+ },
40
+ )(collection)(type);
41
+
42
+ // GENERATE FUNCTION
43
+ const functions = {
44
+ objects: write_object_functions(importer)(collection),
45
+ arrays: write_array_functions(importer)(collection),
46
+ tuples: write_tuple_functions(importer)(collection),
47
+ };
48
+
49
+ const output: ts.Expression = decode(importer)({
50
+ function: false,
51
+ recursive: false,
52
+ })(meta, [], []);
53
+
54
+ return ts.factory.createArrowFunction(
55
+ undefined,
56
+ undefined,
57
+ [
58
+ IdentifierFactory.parameter(
59
+ "generator",
60
+ ts.factory.createTypeReferenceNode(
61
+ "Partial<typia.IRandomGenerator>",
62
+ ),
63
+ init ??
64
+ ts.factory.createToken(
65
+ ts.SyntaxKind.QuestionToken,
66
+ ),
67
+ ),
68
+ ],
69
+ ts.factory.createTypeReferenceNode(
70
+ `typia.Primitive<${
71
+ name ??
72
+ TypeFactory.getFullName(project.checker)(type)
73
+ }>`,
74
+ ),
75
+ undefined,
76
+ ts.factory.createBlock(
77
+ [
78
+ ...importer.declare(modulo),
79
+ ...functions.objects,
80
+ ...functions.arrays,
81
+ ...functions.tuples,
82
+ ts.factory.createReturnStatement(output),
83
+ ],
84
+ true,
85
+ ),
86
+ );
87
+ };
88
+ };
89
+
90
+ const write_object_functions =
91
+ (importer: FunctionImporter) =>
92
+ (collection: MetadataCollection): ts.VariableStatement[] =>
93
+ collection.objects().map((obj, i) =>
94
+ StatementFactory.constant(
95
+ PREFIX.object(i),
96
+ ts.factory.createArrowFunction(
97
+ undefined,
98
+ undefined,
99
+ [
100
+ IdentifierFactory.parameter(
101
+ "_recursive",
102
+ TypeFactory.keyword("boolean"),
103
+ ts.factory.createIdentifier(
104
+ String(obj.recursive),
105
+ ),
106
+ ),
107
+ IdentifierFactory.parameter(
108
+ "_depth",
109
+ TypeFactory.keyword("number"),
110
+ ts.factory.createNumericLiteral(0),
111
+ ),
112
+ ],
113
+ TypeFactory.keyword("any"),
114
+ undefined,
115
+ RandomJoiner.object(COALESCE(importer))(
116
+ decode(importer)({
117
+ recursive: obj.recursive,
118
+ function: true,
119
+ }),
120
+ )(obj),
121
+ ),
122
+ ),
123
+ );
124
+
125
+ const write_array_functions =
126
+ (importer: FunctionImporter) =>
127
+ (collection: MetadataCollection): ts.VariableStatement[] =>
128
+ collection
129
+ .arrays()
130
+ .filter((a) => a.recursive)
131
+ .map((array, i) =>
132
+ StatementFactory.constant(
133
+ PREFIX.array(i),
134
+ ts.factory.createArrowFunction(
135
+ undefined,
136
+ undefined,
137
+ [
138
+ IdentifierFactory.parameter(
139
+ "length",
140
+ TypeFactory.keyword("number"),
141
+ ),
142
+ IdentifierFactory.parameter(
143
+ "_recursive",
144
+ TypeFactory.keyword("boolean"),
145
+ ts.factory.createTrue(),
146
+ ),
147
+ IdentifierFactory.parameter(
148
+ "_depth",
149
+ TypeFactory.keyword("number"),
150
+ ts.factory.createNumericLiteral(0),
151
+ ),
152
+ ],
153
+ TypeFactory.keyword("any"),
154
+ undefined,
155
+ RandomJoiner.array(COALESCE(importer))(
156
+ decode(importer)({
157
+ recursive: true,
158
+ function: true,
159
+ }),
160
+ )({
161
+ recursive: true,
162
+ function: true,
163
+ })(ts.factory.createIdentifier("length"))(
164
+ array.value,
165
+ [],
166
+ [],
167
+ ),
168
+ ),
169
+ ),
170
+ );
171
+
172
+ const write_tuple_functions =
173
+ (importer: FunctionImporter) =>
174
+ (collection: MetadataCollection): ts.VariableStatement[] =>
175
+ collection
176
+ .tuples()
177
+ .filter((a) => a.recursive)
178
+ .map((tuple, i) =>
179
+ StatementFactory.constant(
180
+ PREFIX.tuple(i),
181
+ ts.factory.createArrowFunction(
182
+ undefined,
183
+ undefined,
184
+ [
185
+ IdentifierFactory.parameter(
186
+ "_recursive",
187
+ TypeFactory.keyword("boolean"),
188
+ ts.factory.createTrue(),
189
+ ),
190
+ IdentifierFactory.parameter(
191
+ "_depth",
192
+ TypeFactory.keyword("number"),
193
+ ts.factory.createNumericLiteral(0),
194
+ ),
195
+ ],
196
+ TypeFactory.keyword("any"),
197
+ undefined,
198
+ RandomJoiner.tuple(
199
+ decode(importer)({
200
+ function: true,
201
+ recursive: true,
202
+ }),
203
+ )(tuple.elements, [], []),
204
+ ),
205
+ ),
206
+ );
207
+
208
+ /* -----------------------------------------------------------
209
+ DECODERS
210
+ ----------------------------------------------------------- */
211
+ const decode =
212
+ (importer: FunctionImporter) =>
213
+ (explore: IExplore) =>
214
+ (
215
+ meta: Metadata,
216
+ tags: IMetadataTag[],
217
+ comments: ICommentTag[],
218
+ ): ts.Expression => {
219
+ const expressions: ts.Expression[] = [];
220
+ if (meta.any)
221
+ expressions.push(
222
+ ts.factory.createStringLiteral(
223
+ "fucking any type exists...",
224
+ ),
225
+ );
226
+
227
+ // NULL COALESCING
228
+ if (meta.required === false)
229
+ expressions.push(ts.factory.createIdentifier("undefined"));
230
+ if (meta.nullable === true)
231
+ expressions.push(ts.factory.createNull());
232
+
233
+ // CONSTANT TYPES
234
+ for (const constant of meta.constants)
235
+ for (const value of constant.values)
236
+ expressions.push(decode_atomic(value));
237
+
238
+ // ATOMIC VARIABLES
239
+ for (const template of meta.templates)
240
+ expressions.push(decode_template(importer)(explore)(template));
241
+ for (const atomic of meta.atomics)
242
+ if (atomic === "boolean")
243
+ expressions.push(decode_boolean(importer));
244
+ else if (atomic === "number")
245
+ expressions.push(decode_number(importer)(tags)(comments));
246
+ else if (atomic === "string")
247
+ expressions.push(decode_string(importer)(tags)(comments));
248
+ else if (atomic === "bigint")
249
+ expressions.push(decode_bigint(importer)(tags)(comments));
250
+
251
+ // INSTANCE TYPES
252
+ if (meta.resolved)
253
+ expressions.push(
254
+ decode(importer)(explore)(
255
+ meta.resolved.returns,
256
+ tags,
257
+ comments,
258
+ ),
259
+ );
260
+ for (const array of meta.arrays)
261
+ expressions.push(
262
+ decode_array(importer)(explore)(array, tags, comments),
263
+ );
264
+ for (const tuple of meta.tuples)
265
+ expressions.push(
266
+ decode_tuple(importer)(explore)(tuple, tags, comments),
267
+ );
268
+ for (const o of meta.objects)
269
+ expressions.push(decode_object(importer)(explore)(o));
270
+ for (const native of meta.natives)
271
+ if (native === "Boolean")
272
+ expressions.push(decode_boolean(importer));
273
+ else if (native === "Number")
274
+ expressions.push(decode_number(importer)(tags)(comments));
275
+ else if (native === "String")
276
+ expressions.push(decode_string(importer)(tags)(comments));
277
+ else expressions.push(ts.factory.createIdentifier("{}"));
278
+ if (meta.sets.length || meta.maps.length)
279
+ expressions.push(ts.factory.createIdentifier("{}"));
280
+
281
+ // PRIMITIVE TYPES
282
+ if (expressions.length === 1) return expressions[0]!;
283
+ return ts.factory.createCallExpression(
284
+ ts.factory.createCallExpression(
285
+ importer.use("pick"),
286
+ undefined,
287
+ [
288
+ ts.factory.createArrayLiteralExpression(
289
+ expressions.map((expr) =>
290
+ ts.factory.createArrowFunction(
291
+ undefined,
292
+ undefined,
293
+ [],
294
+ undefined,
295
+ undefined,
296
+ expr,
297
+ ),
298
+ ),
299
+ true,
300
+ ),
301
+ ],
302
+ ),
303
+ undefined,
304
+ undefined,
305
+ );
306
+ };
307
+
308
+ const decode_boolean = (importer: FunctionImporter) =>
309
+ ts.factory.createCallExpression(
310
+ COALESCE(importer)("boolean"),
311
+ undefined,
312
+ undefined,
313
+ );
314
+
315
+ const decode_atomic = (value: Atomic) =>
316
+ typeof value === "boolean"
317
+ ? ts.factory.createIdentifier(value.toString())
318
+ : typeof value === "number"
319
+ ? ts.factory.createNumericLiteral(value)
320
+ : typeof value === "string"
321
+ ? ts.factory.createStringLiteral(value)
322
+ : ts.factory.createBigIntLiteral(value.toString());
323
+
324
+ const decode_template =
325
+ (importer: FunctionImporter) =>
326
+ (explore: IExplore) =>
327
+ (template: Metadata[]) =>
328
+ TemplateFactory.generate(
329
+ template.map((meta) => decode(importer)(explore)(meta, [], [])),
330
+ );
331
+
332
+ const decode_number =
333
+ (importer: FunctionImporter) =>
334
+ (tags: IMetadataTag[]) =>
335
+ (comments: ICommentTag[]): ts.Expression => {
336
+ const type = tags.find(
337
+ (t) => t.kind === "type" && t.value === "uint",
338
+ )
339
+ ? "int"
340
+ : tags.find((t) => t.kind === "type" && t.value === "int")
341
+ ? "uint"
342
+ : "double";
343
+ return random_custom(COALESCE(importer))("number")(comments)(
344
+ RandomRanger.number({
345
+ type,
346
+ transform: (value) =>
347
+ ts.factory.createNumericLiteral(value),
348
+ setter: (args) =>
349
+ ts.factory.createCallExpression(
350
+ type === "double" &&
351
+ tags.every(
352
+ (t) =>
353
+ t.kind !== "multipleOf" &&
354
+ t.kind !== "step",
355
+ )
356
+ ? COALESCE(importer)("number")
357
+ : COALESCE(importer)("integer"),
358
+ undefined,
359
+ args.map((val) =>
360
+ ts.factory.createNumericLiteral(val),
361
+ ),
362
+ ),
363
+ })({
364
+ minimum: 0,
365
+ maximum: 100,
366
+ gap: 10,
367
+ })(tags),
368
+ );
369
+ };
370
+
371
+ const decode_bigint =
372
+ (importer: FunctionImporter) =>
373
+ (tags: IMetadataTag[]) =>
374
+ (comments: ICommentTag[]): ts.Expression =>
375
+ random_custom(COALESCE(importer))("bigint")(comments)(
376
+ RandomRanger.number({
377
+ type: tags.find(
378
+ (t) => t.kind === "type" && t.value === "uint",
379
+ )
380
+ ? "uint"
381
+ : "int",
382
+ transform: (value) =>
383
+ ts.factory.createCallExpression(
384
+ ts.factory.createIdentifier("BigInt"),
385
+ undefined,
386
+ [ts.factory.createStringLiteral(value.toString())],
387
+ ),
388
+ setter: (args) =>
389
+ ts.factory.createCallExpression(
390
+ COALESCE(importer)("bigint"),
391
+ undefined,
392
+ args.map((value) =>
393
+ ts.factory.createCallExpression(
394
+ ts.factory.createIdentifier("BigInt"),
395
+ undefined,
396
+ [
397
+ ts.factory.createStringLiteral(
398
+ value.toString(),
399
+ ),
400
+ ],
401
+ ),
402
+ ),
403
+ ),
404
+ })({
405
+ minimum: 0,
406
+ maximum: 100,
407
+ gap: 10,
408
+ })(tags),
409
+ );
410
+
411
+ const decode_string =
412
+ (importer: FunctionImporter) =>
413
+ (tags: IMetadataTag[]) =>
414
+ (comments: ICommentTag[]): ts.Expression =>
415
+ random_custom(COALESCE(importer))("string")(comments)(
416
+ (() => {
417
+ for (const t of tags)
418
+ if (t.kind === "format")
419
+ return ts.factory.createCallExpression(
420
+ COALESCE(importer)(t.value),
421
+ undefined,
422
+ undefined,
423
+ );
424
+ else if (t.kind === "pattern")
425
+ return ts.factory.createCallExpression(
426
+ COALESCE(importer)("pattern"),
427
+ undefined,
428
+ [ts.factory.createIdentifier(`/${t.value}/`)],
429
+ );
430
+
431
+ const tail = RandomRanger.length(COALESCE(importer))({
432
+ minimum: 5,
433
+ maximum: 25,
434
+ gap: 5,
435
+ })({
436
+ fixed: "length",
437
+ minimum: "minLength",
438
+ maximum: "maxLength",
439
+ })(tags);
440
+ return ts.factory.createCallExpression(
441
+ COALESCE(importer)("string"),
442
+ undefined,
443
+ tail ? [tail] : undefined,
444
+ );
445
+ })(),
446
+ );
447
+
448
+ const decode_array =
449
+ (importer: FunctionImporter) =>
450
+ (explore: IExplore) =>
451
+ (
452
+ array: MetadataArray,
453
+ tags: IMetadataTag[],
454
+ comments: ICommentTag[],
455
+ ) => {
456
+ const length: ts.Expression | undefined = RandomRanger.length(
457
+ COALESCE(importer),
458
+ )({
459
+ minimum: 0,
460
+ maximum: 3,
461
+ gap: 3,
462
+ })({
463
+ fixed: "items",
464
+ minimum: "minItems",
465
+ maximum: "maxItems",
466
+ })(tags);
467
+ if (array.recursive)
468
+ return ts.factory.createCallExpression(
469
+ ts.factory.createIdentifier(
470
+ importer.useLocal(PREFIX.array(array.index!)),
471
+ ),
472
+ undefined,
473
+ [
474
+ length ?? COALESCE(importer)("length"),
475
+ ts.factory.createTrue(),
476
+ explore.recursive
477
+ ? ts.factory.createAdd(
478
+ ts.factory.createNumericLiteral(1),
479
+ ts.factory.createIdentifier("_depth"),
480
+ )
481
+ : ts.factory.createNumericLiteral(0),
482
+ ],
483
+ );
484
+ const expr: ts.Expression = RandomJoiner.array(COALESCE(importer))(
485
+ decode(importer)(explore),
486
+ )(explore)(length)(array.value, tags, comments);
487
+ return explore.recursive
488
+ ? ts.factory.createConditionalExpression(
489
+ ts.factory.createLogicalAnd(
490
+ ts.factory.createIdentifier("_recursive"),
491
+ ts.factory.createLessThan(
492
+ ts.factory.createNumericLiteral(5),
493
+ ts.factory.createIdentifier("_depth"),
494
+ ),
495
+ ),
496
+ undefined,
497
+ ts.factory.createIdentifier("[]"),
498
+ undefined,
499
+ expr,
500
+ )
501
+ : expr;
502
+ };
503
+
504
+ const decode_tuple =
505
+ (importer: FunctionImporter) =>
506
+ (explore: IExplore) =>
507
+ (
508
+ tuple: MetadataTuple,
509
+ tags: IMetadataTag[],
510
+ comments: ICommentTag[],
511
+ ): ts.Expression =>
512
+ tuple.recursive
513
+ ? ts.factory.createCallExpression(
514
+ ts.factory.createIdentifier(
515
+ importer.useLocal(PREFIX.tuple(tuple.index!)),
516
+ ),
517
+ undefined,
518
+ [
519
+ ts.factory.createTrue(),
520
+ explore.recursive
521
+ ? ts.factory.createAdd(
522
+ ts.factory.createNumericLiteral(1),
523
+ ts.factory.createIdentifier("_depth"),
524
+ )
525
+ : ts.factory.createNumericLiteral(0),
526
+ ],
527
+ )
528
+ : RandomJoiner.tuple(decode(importer)(explore))(
529
+ tuple.elements,
530
+ tags,
531
+ comments,
532
+ );
533
+
534
+ const decode_object =
535
+ (importer: FunctionImporter) =>
536
+ (explore: IExplore) =>
537
+ (object: MetadataObject) =>
538
+ ts.factory.createCallExpression(
539
+ ts.factory.createIdentifier(
540
+ importer.useLocal(PREFIX.object(object.index)),
541
+ ),
542
+ undefined,
543
+ explore.function
544
+ ? [
545
+ explore.recursive
546
+ ? ts.factory.createTrue()
547
+ : ts.factory.createIdentifier("_recursive"),
548
+ ts.factory.createConditionalExpression(
549
+ ts.factory.createIdentifier("_recursive"),
550
+ undefined,
551
+ ts.factory.createAdd(
552
+ ts.factory.createNumericLiteral(1),
553
+ ts.factory.createIdentifier("_depth"),
554
+ ),
555
+ undefined,
556
+ ts.factory.createIdentifier("_depth"),
557
+ ),
558
+ ]
559
+ : undefined,
560
+ );
561
+ }
562
+
563
+ type Atomic = boolean | number | string | bigint;
564
+ interface IExplore {
565
+ function: boolean;
566
+ recursive: boolean;
567
+ }
568
+
569
+ const PREFIX = {
570
+ object: (i: number) => `$ro${i}`,
571
+ array: (i: number) => `$ra${i}`,
572
+ tuple: (i: number) => `$rt${i}`,
573
+ };
574
+ const COALESCE = (importer: FunctionImporter) => (name: string) =>
575
+ ExpressionFactory.coalesce(
576
+ ts.factory.createPropertyAccessChain(
577
+ ts.factory.createIdentifier("generator"),
578
+ ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
579
+ ts.factory.createIdentifier(name),
580
+ ),
581
+ )(IdentifierFactory.access(importer.use("generator"))(name));