typia 4.2.0 → 4.2.1-dev.20230808

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.
@@ -1,581 +1,579 @@
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
- "any type used...",
224
- ),
225
- );
226
-
227
- // NULL COALESCING
228
- if (meta.isRequired() === 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("any type used..."),
223
+ );
224
+
225
+ // NULL COALESCING
226
+ if (meta.isRequired() === false)
227
+ expressions.push(ts.factory.createIdentifier("undefined"));
228
+ if (meta.nullable === true)
229
+ expressions.push(ts.factory.createNull());
230
+
231
+ // CONSTANT TYPES
232
+ for (const constant of meta.constants)
233
+ for (const value of constant.values)
234
+ expressions.push(decode_atomic(value));
235
+
236
+ // ATOMIC VARIABLES
237
+ for (const template of meta.templates)
238
+ expressions.push(decode_template(importer)(explore)(template));
239
+ for (const atomic of meta.atomics)
240
+ if (atomic === "boolean")
241
+ expressions.push(decode_boolean(importer));
242
+ else if (atomic === "number")
243
+ expressions.push(decode_number(importer)(tags)(comments));
244
+ else if (atomic === "string")
245
+ expressions.push(decode_string(importer)(tags)(comments));
246
+ else if (atomic === "bigint")
247
+ expressions.push(decode_bigint(importer)(tags)(comments));
248
+
249
+ // INSTANCE TYPES
250
+ if (meta.resolved)
251
+ expressions.push(
252
+ decode(importer)(explore)(
253
+ meta.resolved.returns,
254
+ tags,
255
+ comments,
256
+ ),
257
+ );
258
+ for (const array of meta.arrays)
259
+ expressions.push(
260
+ decode_array(importer)(explore)(array, tags, comments),
261
+ );
262
+ for (const tuple of meta.tuples)
263
+ expressions.push(
264
+ decode_tuple(importer)(explore)(tuple, tags, comments),
265
+ );
266
+ for (const o of meta.objects)
267
+ expressions.push(decode_object(importer)(explore)(o));
268
+ for (const native of meta.natives)
269
+ if (native === "Boolean")
270
+ expressions.push(decode_boolean(importer));
271
+ else if (native === "Number")
272
+ expressions.push(decode_number(importer)(tags)(comments));
273
+ else if (native === "String")
274
+ expressions.push(decode_string(importer)(tags)(comments));
275
+ else expressions.push(ts.factory.createIdentifier("{}"));
276
+ if (meta.sets.length || meta.maps.length)
277
+ expressions.push(ts.factory.createIdentifier("{}"));
278
+
279
+ // PRIMITIVE TYPES
280
+ if (expressions.length === 1) return expressions[0]!;
281
+ return ts.factory.createCallExpression(
282
+ ts.factory.createCallExpression(
283
+ importer.use("pick"),
284
+ undefined,
285
+ [
286
+ ts.factory.createArrayLiteralExpression(
287
+ expressions.map((expr) =>
288
+ ts.factory.createArrowFunction(
289
+ undefined,
290
+ undefined,
291
+ [],
292
+ undefined,
293
+ undefined,
294
+ expr,
295
+ ),
296
+ ),
297
+ true,
298
+ ),
299
+ ],
300
+ ),
301
+ undefined,
302
+ undefined,
303
+ );
304
+ };
305
+
306
+ const decode_boolean = (importer: FunctionImporter) =>
307
+ ts.factory.createCallExpression(
308
+ COALESCE(importer)("boolean"),
309
+ undefined,
310
+ undefined,
311
+ );
312
+
313
+ const decode_atomic = (value: Atomic) =>
314
+ typeof value === "boolean"
315
+ ? ts.factory.createIdentifier(value.toString())
316
+ : typeof value === "number"
317
+ ? ts.factory.createNumericLiteral(value)
318
+ : typeof value === "string"
319
+ ? ts.factory.createStringLiteral(value)
320
+ : ts.factory.createBigIntLiteral(value.toString());
321
+
322
+ const decode_template =
323
+ (importer: FunctionImporter) =>
324
+ (explore: IExplore) =>
325
+ (template: Metadata[]) =>
326
+ TemplateFactory.generate(
327
+ template.map((meta) => decode(importer)(explore)(meta, [], [])),
328
+ );
329
+
330
+ const decode_number =
331
+ (importer: FunctionImporter) =>
332
+ (tags: IMetadataTag[]) =>
333
+ (comments: ICommentTag[]): ts.Expression => {
334
+ const type = tags.find(
335
+ (t) => t.kind === "type" && t.value === "uint",
336
+ )
337
+ ? "int"
338
+ : tags.find((t) => t.kind === "type" && t.value === "int")
339
+ ? "uint"
340
+ : "double";
341
+ return random_custom(COALESCE(importer))("number")(comments)(
342
+ RandomRanger.number({
343
+ type,
344
+ transform: (value) =>
345
+ ts.factory.createNumericLiteral(value),
346
+ setter: (args) =>
347
+ ts.factory.createCallExpression(
348
+ type === "double" &&
349
+ tags.every(
350
+ (t) =>
351
+ t.kind !== "multipleOf" &&
352
+ t.kind !== "step",
353
+ )
354
+ ? COALESCE(importer)("number")
355
+ : COALESCE(importer)("integer"),
356
+ undefined,
357
+ args.map((val) =>
358
+ ts.factory.createNumericLiteral(val),
359
+ ),
360
+ ),
361
+ })({
362
+ minimum: 0,
363
+ maximum: 100,
364
+ gap: 10,
365
+ })(tags),
366
+ );
367
+ };
368
+
369
+ const decode_bigint =
370
+ (importer: FunctionImporter) =>
371
+ (tags: IMetadataTag[]) =>
372
+ (comments: ICommentTag[]): ts.Expression =>
373
+ random_custom(COALESCE(importer))("bigint")(comments)(
374
+ RandomRanger.number({
375
+ type: tags.find(
376
+ (t) => t.kind === "type" && t.value === "uint",
377
+ )
378
+ ? "uint"
379
+ : "int",
380
+ transform: (value) =>
381
+ ts.factory.createCallExpression(
382
+ ts.factory.createIdentifier("BigInt"),
383
+ undefined,
384
+ [ts.factory.createStringLiteral(value.toString())],
385
+ ),
386
+ setter: (args) =>
387
+ ts.factory.createCallExpression(
388
+ COALESCE(importer)("bigint"),
389
+ undefined,
390
+ args.map((value) =>
391
+ ts.factory.createCallExpression(
392
+ ts.factory.createIdentifier("BigInt"),
393
+ undefined,
394
+ [
395
+ ts.factory.createStringLiteral(
396
+ value.toString(),
397
+ ),
398
+ ],
399
+ ),
400
+ ),
401
+ ),
402
+ })({
403
+ minimum: 0,
404
+ maximum: 100,
405
+ gap: 10,
406
+ })(tags),
407
+ );
408
+
409
+ const decode_string =
410
+ (importer: FunctionImporter) =>
411
+ (tags: IMetadataTag[]) =>
412
+ (comments: ICommentTag[]): ts.Expression =>
413
+ random_custom(COALESCE(importer))("string")(comments)(
414
+ (() => {
415
+ for (const t of tags)
416
+ if (t.kind === "format")
417
+ return ts.factory.createCallExpression(
418
+ COALESCE(importer)(t.value),
419
+ undefined,
420
+ undefined,
421
+ );
422
+ else if (t.kind === "pattern")
423
+ return ts.factory.createCallExpression(
424
+ COALESCE(importer)("pattern"),
425
+ undefined,
426
+ [ts.factory.createIdentifier(`/${t.value}/`)],
427
+ );
428
+
429
+ const tail = RandomRanger.length(COALESCE(importer))({
430
+ minimum: 5,
431
+ maximum: 25,
432
+ gap: 5,
433
+ })({
434
+ fixed: "length",
435
+ minimum: "minLength",
436
+ maximum: "maxLength",
437
+ })(tags);
438
+ return ts.factory.createCallExpression(
439
+ COALESCE(importer)("string"),
440
+ undefined,
441
+ tail ? [tail] : undefined,
442
+ );
443
+ })(),
444
+ );
445
+
446
+ const decode_array =
447
+ (importer: FunctionImporter) =>
448
+ (explore: IExplore) =>
449
+ (
450
+ array: MetadataArray,
451
+ tags: IMetadataTag[],
452
+ comments: ICommentTag[],
453
+ ) => {
454
+ const length: ts.Expression | undefined = RandomRanger.length(
455
+ COALESCE(importer),
456
+ )({
457
+ minimum: 0,
458
+ maximum: 3,
459
+ gap: 3,
460
+ })({
461
+ fixed: "items",
462
+ minimum: "minItems",
463
+ maximum: "maxItems",
464
+ })(tags);
465
+ if (array.recursive)
466
+ return ts.factory.createCallExpression(
467
+ ts.factory.createIdentifier(
468
+ importer.useLocal(PREFIX.array(array.index!)),
469
+ ),
470
+ undefined,
471
+ [
472
+ length ?? COALESCE(importer)("length"),
473
+ ts.factory.createTrue(),
474
+ explore.recursive
475
+ ? ts.factory.createAdd(
476
+ ts.factory.createNumericLiteral(1),
477
+ ts.factory.createIdentifier("_depth"),
478
+ )
479
+ : ts.factory.createNumericLiteral(0),
480
+ ],
481
+ );
482
+ const expr: ts.Expression = RandomJoiner.array(COALESCE(importer))(
483
+ decode(importer)(explore),
484
+ )(explore)(length)(array.value, tags, comments);
485
+ return explore.recursive
486
+ ? ts.factory.createConditionalExpression(
487
+ ts.factory.createLogicalAnd(
488
+ ts.factory.createIdentifier("_recursive"),
489
+ ts.factory.createLessThan(
490
+ ts.factory.createNumericLiteral(5),
491
+ ts.factory.createIdentifier("_depth"),
492
+ ),
493
+ ),
494
+ undefined,
495
+ ts.factory.createIdentifier("[]"),
496
+ undefined,
497
+ expr,
498
+ )
499
+ : expr;
500
+ };
501
+
502
+ const decode_tuple =
503
+ (importer: FunctionImporter) =>
504
+ (explore: IExplore) =>
505
+ (
506
+ tuple: MetadataTuple,
507
+ tags: IMetadataTag[],
508
+ comments: ICommentTag[],
509
+ ): ts.Expression =>
510
+ tuple.recursive
511
+ ? ts.factory.createCallExpression(
512
+ ts.factory.createIdentifier(
513
+ importer.useLocal(PREFIX.tuple(tuple.index!)),
514
+ ),
515
+ undefined,
516
+ [
517
+ ts.factory.createTrue(),
518
+ explore.recursive
519
+ ? ts.factory.createAdd(
520
+ ts.factory.createNumericLiteral(1),
521
+ ts.factory.createIdentifier("_depth"),
522
+ )
523
+ : ts.factory.createNumericLiteral(0),
524
+ ],
525
+ )
526
+ : RandomJoiner.tuple(decode(importer)(explore))(
527
+ tuple.elements,
528
+ tags,
529
+ comments,
530
+ );
531
+
532
+ const decode_object =
533
+ (importer: FunctionImporter) =>
534
+ (explore: IExplore) =>
535
+ (object: MetadataObject) =>
536
+ ts.factory.createCallExpression(
537
+ ts.factory.createIdentifier(
538
+ importer.useLocal(PREFIX.object(object.index)),
539
+ ),
540
+ undefined,
541
+ explore.function
542
+ ? [
543
+ explore.recursive
544
+ ? ts.factory.createTrue()
545
+ : ts.factory.createIdentifier("_recursive"),
546
+ ts.factory.createConditionalExpression(
547
+ ts.factory.createIdentifier("_recursive"),
548
+ undefined,
549
+ ts.factory.createAdd(
550
+ ts.factory.createNumericLiteral(1),
551
+ ts.factory.createIdentifier("_depth"),
552
+ ),
553
+ undefined,
554
+ ts.factory.createIdentifier("_depth"),
555
+ ),
556
+ ]
557
+ : undefined,
558
+ );
559
+ }
560
+
561
+ type Atomic = boolean | number | string | bigint;
562
+ interface IExplore {
563
+ function: boolean;
564
+ recursive: boolean;
565
+ }
566
+
567
+ const PREFIX = {
568
+ object: (i: number) => `$ro${i}`,
569
+ array: (i: number) => `$ra${i}`,
570
+ tuple: (i: number) => `$rt${i}`,
571
+ };
572
+ const COALESCE = (importer: FunctionImporter) => (name: string) =>
573
+ ExpressionFactory.coalesce(
574
+ ts.factory.createPropertyAccessChain(
575
+ ts.factory.createIdentifier("generator"),
576
+ ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
577
+ ts.factory.createIdentifier(name),
578
+ ),
579
+ )(IdentifierFactory.access(importer.use("generator"))(name));