typia 5.2.5 → 5.2.6

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 (64) hide show
  1. package/lib/factories/ExpressionFactory.d.ts +1 -0
  2. package/lib/factories/ExpressionFactory.js +5 -0
  3. package/lib/factories/ExpressionFactory.js.map +1 -1
  4. package/lib/programmers/CheckerProgrammer.js +4 -4
  5. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  6. package/lib/programmers/RandomProgrammer.js +16 -18
  7. package/lib/programmers/RandomProgrammer.js.map +1 -1
  8. package/lib/programmers/ValidateProgrammer.js +2 -1
  9. package/lib/programmers/ValidateProgrammer.js.map +1 -1
  10. package/lib/programmers/helpers/RandomJoiner.js +4 -6
  11. package/lib/programmers/helpers/RandomJoiner.js.map +1 -1
  12. package/lib/programmers/helpers/RandomRanger.js +3 -2
  13. package/lib/programmers/helpers/RandomRanger.js.map +1 -1
  14. package/lib/programmers/http/HttpHeadersProgrammer.js +1 -1
  15. package/lib/programmers/http/HttpHeadersProgrammer.js.map +1 -1
  16. package/lib/programmers/internal/check_dynamic_properties.js +4 -3
  17. package/lib/programmers/internal/check_dynamic_properties.js.map +1 -1
  18. package/lib/programmers/internal/check_union_array_like.js +3 -2
  19. package/lib/programmers/internal/check_union_array_like.js.map +1 -1
  20. package/lib/programmers/json/JsonStringifyProgrammer.js +1 -3
  21. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  22. package/lib/programmers/misc/MiscCloneProgrammer.js +1 -3
  23. package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
  24. package/lib/programmers/misc/MiscLiteralsProgrammer.js +1 -1
  25. package/lib/programmers/misc/MiscLiteralsProgrammer.js.map +1 -1
  26. package/lib/programmers/misc/MiscPruneProgrammer.js +1 -3
  27. package/lib/programmers/misc/MiscPruneProgrammer.js.map +1 -1
  28. package/lib/programmers/notations/NotationGeneralProgrammer.js +1 -3
  29. package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
  30. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +6 -6
  31. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
  32. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +2 -2
  33. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/Primitive.ts +135 -135
  36. package/src/executable/TypiaSetupWizard.ts +142 -142
  37. package/src/executable/setup/CommandExecutor.ts +8 -8
  38. package/src/factories/ExpressionFactory.ts +8 -0
  39. package/src/factories/JsonMetadataFactory.ts +50 -50
  40. package/src/factories/MetadataCollection.ts +282 -282
  41. package/src/factories/internal/metadata/emplace_metadata_object.ts +178 -178
  42. package/src/functional/$stoll.ts +8 -8
  43. package/src/functional/Namespace.ts +168 -168
  44. package/src/programmers/AssertProgrammer.ts +322 -322
  45. package/src/programmers/CheckerProgrammer.ts +4 -4
  46. package/src/programmers/IsProgrammer.ts +258 -258
  47. package/src/programmers/RandomProgrammer.ts +16 -17
  48. package/src/programmers/ValidateProgrammer.ts +350 -349
  49. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  50. package/src/programmers/helpers/RandomJoiner.ts +4 -6
  51. package/src/programmers/helpers/RandomRanger.ts +4 -2
  52. package/src/programmers/http/HttpHeadersProgrammer.ts +1 -1
  53. package/src/programmers/internal/check_dynamic_key.ts +178 -178
  54. package/src/programmers/internal/check_dynamic_properties.ts +202 -201
  55. package/src/programmers/internal/check_object.ts +62 -62
  56. package/src/programmers/internal/check_union_array_like.ts +4 -3
  57. package/src/programmers/json/JsonStringifyProgrammer.ts +960 -964
  58. package/src/programmers/misc/MiscCloneProgrammer.ts +786 -790
  59. package/src/programmers/misc/MiscLiteralsProgrammer.ts +1 -1
  60. package/src/programmers/misc/MiscPruneProgrammer.ts +548 -552
  61. package/src/programmers/notations/NotationGeneralProgrammer.ts +716 -720
  62. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +7 -9
  63. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +882 -882
  64. package/src/transform.ts +35 -35
@@ -1,790 +1,786 @@
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 { TypeFactory } from "../../factories/TypeFactory";
9
-
10
- import { Metadata } from "../../schemas/metadata/Metadata";
11
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
- import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
- import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
-
15
- import { IProject } from "../../transformers/IProject";
16
- import { TransformerError } from "../../transformers/TransformerError";
17
-
18
- import { FeatureProgrammer } from "../FeatureProgrammer";
19
- import { IsProgrammer } from "../IsProgrammer";
20
- import { CloneJoiner } from "../helpers/CloneJoiner";
21
- import { FunctionImporter } from "../helpers/FunctionImporeter";
22
- import { UnionExplorer } from "../helpers/UnionExplorer";
23
- import { decode_union_object } from "../internal/decode_union_object";
24
- import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
25
-
26
- export namespace MiscCloneProgrammer {
27
- export const write =
28
- (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
29
- const importer: FunctionImporter = new FunctionImporter(
30
- modulo.getText(),
31
- );
32
- return FeatureProgrammer.write(project)({
33
- ...configure(project)(importer),
34
- addition: (collection) => [
35
- ...IsProgrammer.write_function_statements(project)(
36
- importer,
37
- )(collection),
38
- ...importer.declare(modulo),
39
- ],
40
- })(importer);
41
- };
42
-
43
- const write_array_functions =
44
- (config: FeatureProgrammer.IConfig) =>
45
- (importer: FunctionImporter) =>
46
- (collection: MetadataCollection): ts.VariableStatement[] =>
47
- collection
48
- .arrays()
49
- .filter((a) => a.recursive)
50
- .map((type, i) =>
51
- StatementFactory.constant(
52
- `${config.prefix}a${i}`,
53
- ts.factory.createArrowFunction(
54
- undefined,
55
- undefined,
56
- FeatureProgrammer.parameterDeclarations(config)(
57
- TypeFactory.keyword("any"),
58
- )(ts.factory.createIdentifier("input")),
59
- TypeFactory.keyword("any"),
60
- undefined,
61
- decode_array_inline(config)(importer)(
62
- ts.factory.createIdentifier("input"),
63
- MetadataArray.create({
64
- type,
65
- tags: [],
66
- }),
67
- {
68
- tracable: config.trace,
69
- source: "function",
70
- from: "array",
71
- postfix: "",
72
- },
73
- ),
74
- ),
75
- ),
76
- );
77
-
78
- const write_tuple_functions =
79
- (project: IProject) =>
80
- (config: FeatureProgrammer.IConfig) =>
81
- (importer: FunctionImporter) =>
82
- (collection: MetadataCollection): ts.VariableStatement[] =>
83
- collection
84
- .tuples()
85
- .filter((t) => t.recursive)
86
- .map((tuple, i) =>
87
- StatementFactory.constant(
88
- `${config.prefix}t${i}`,
89
- ts.factory.createArrowFunction(
90
- undefined,
91
- undefined,
92
- FeatureProgrammer.parameterDeclarations(config)(
93
- TypeFactory.keyword("any"),
94
- )(ts.factory.createIdentifier("input")),
95
- TypeFactory.keyword("any"),
96
- undefined,
97
- decode_tuple_inline(project)(config)(importer)(
98
- ts.factory.createIdentifier("input"),
99
- tuple,
100
- {
101
- tracable: config.trace,
102
- source: "function",
103
- from: "array",
104
- postfix: "",
105
- },
106
- ),
107
- ),
108
- ),
109
- );
110
-
111
- /* -----------------------------------------------------------
112
- DECODERS
113
- ----------------------------------------------------------- */
114
- const decode =
115
- (project: IProject) =>
116
- (config: FeatureProgrammer.IConfig) =>
117
- (importer: FunctionImporter) =>
118
- (
119
- input: ts.Expression,
120
- meta: Metadata,
121
- explore: FeatureProgrammer.IExplore,
122
- ): ts.Expression => {
123
- // ANY TYPE
124
- if (
125
- meta.any ||
126
- meta.arrays.some((a) => a.type.value.any) ||
127
- meta.tuples.some(
128
- (t) =>
129
- !!t.type.elements.length &&
130
- t.type.elements.every((e) => e.any),
131
- )
132
- )
133
- return ts.factory.createCallExpression(
134
- importer.use("any"),
135
- undefined,
136
- [input],
137
- );
138
-
139
- interface IUnion {
140
- type: string;
141
- is: () => ts.Expression;
142
- value: () => ts.Expression;
143
- }
144
- const unions: IUnion[] = [];
145
-
146
- //----
147
- // LIST UP UNION TYPES
148
- //----
149
- // FUNCTIONAL
150
- if (meta.functional)
151
- unions.push({
152
- type: "functional",
153
- is: () =>
154
- ts.factory.createStrictEquality(
155
- ts.factory.createStringLiteral("function"),
156
- ts.factory.createTypeOfExpression(input),
157
- ),
158
- value: () => ts.factory.createIdentifier("undefined"),
159
- });
160
-
161
- // TUPLES
162
- for (const tuple of meta.tuples)
163
- unions.push({
164
- type: "tuple",
165
- is: () =>
166
- IsProgrammer.decode(project)(importer)(
167
- input,
168
- (() => {
169
- const partial = Metadata.initialize();
170
- partial.tuples.push(tuple);
171
- return partial;
172
- })(),
173
- explore,
174
- ),
175
- value: () =>
176
- decode_tuple(project)(config)(importer)(
177
- input,
178
- tuple,
179
- explore,
180
- ),
181
- });
182
-
183
- // ARRAYS
184
- if (meta.arrays.length)
185
- unions.push({
186
- type: "array",
187
- is: () => ExpressionFactory.isArray(input),
188
- value: () =>
189
- explore_arrays(project)(config)(importer)(
190
- input,
191
- meta.arrays,
192
- {
193
- ...explore,
194
- from: "array",
195
- },
196
- ),
197
- });
198
-
199
- // NATIVE TYPES
200
- if (meta.sets.length)
201
- unions.push({
202
- type: "set",
203
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
204
- value: () =>
205
- explore_sets(project)(config)(importer)(
206
- input,
207
- meta.sets,
208
- { ...explore, from: "array" },
209
- ),
210
- });
211
- if (meta.maps.length)
212
- unions.push({
213
- type: "map",
214
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
215
- value: () =>
216
- explore_maps(project)(config)(importer)(
217
- input,
218
- meta.maps,
219
- {
220
- ...explore,
221
- from: "array",
222
- },
223
- ),
224
- });
225
- for (const native of meta.natives)
226
- unions.push({
227
- type: "native",
228
- is: () => ExpressionFactory.isInstanceOf(native)(input),
229
- value: () =>
230
- native === "Boolean" ||
231
- native === "Number" ||
232
- native === "String"
233
- ? ts.factory.createCallExpression(
234
- IdentifierFactory.access(input)("valueOf"),
235
- undefined,
236
- undefined,
237
- )
238
- : decode_native(native)(input),
239
- });
240
-
241
- // OBJECTS
242
- if (meta.objects.length)
243
- unions.push({
244
- type: "object",
245
- is: () =>
246
- ExpressionFactory.isObject({
247
- checkNull: true,
248
- checkArray: false,
249
- })(input),
250
- value: () =>
251
- explore_objects(config)(importer)(input, meta, {
252
- ...explore,
253
- from: "object",
254
- }),
255
- });
256
-
257
- // COMPOSITION
258
- let last: ts.Expression = input;
259
- for (const u of unions.reverse())
260
- last = ts.factory.createConditionalExpression(
261
- u.is(),
262
- undefined,
263
- u.value(),
264
- undefined,
265
- last,
266
- );
267
- return ts.factory.createAsExpression(
268
- last,
269
- TypeFactory.keyword("any"),
270
- );
271
- };
272
-
273
- const decode_object = (importer: FunctionImporter) =>
274
- FeatureProgrammer.decode_object({
275
- trace: false,
276
- path: false,
277
- prefix: PREFIX,
278
- })(importer);
279
-
280
- const decode_array =
281
- (config: FeatureProgrammer.IConfig) =>
282
- (importer: FunctionImporter) =>
283
- (
284
- input: ts.Expression,
285
- array: MetadataArray,
286
- explore: FeatureProgrammer.IExplore,
287
- ) =>
288
- array.type.recursive
289
- ? ts.factory.createCallExpression(
290
- ts.factory.createIdentifier(
291
- importer.useLocal(
292
- `${config.prefix}a${array.type.index}`,
293
- ),
294
- ),
295
- undefined,
296
- FeatureProgrammer.argumentsArray(config)({
297
- ...explore,
298
- source: "function",
299
- from: "array",
300
- })(input),
301
- )
302
- : decode_array_inline(config)(importer)(input, array, explore);
303
-
304
- const decode_array_inline =
305
- (config: FeatureProgrammer.IConfig) =>
306
- (importer: FunctionImporter) =>
307
- (
308
- input: ts.Expression,
309
- array: MetadataArray,
310
- explore: FeatureProgrammer.IExplore,
311
- ) =>
312
- FeatureProgrammer.decode_array(config)(importer)(CloneJoiner.array)(
313
- input,
314
- array,
315
- explore,
316
- );
317
-
318
- const decode_tuple =
319
- (project: IProject) =>
320
- (config: FeatureProgrammer.IConfig) =>
321
- (importer: FunctionImporter) =>
322
- (
323
- input: ts.Expression,
324
- tuple: MetadataTuple,
325
- explore: FeatureProgrammer.IExplore,
326
- ): ts.Expression =>
327
- tuple.type.recursive
328
- ? ts.factory.createCallExpression(
329
- ts.factory.createIdentifier(
330
- importer.useLocal(
331
- `${config.prefix}t${tuple.type.index}`,
332
- ),
333
- ),
334
- undefined,
335
- FeatureProgrammer.argumentsArray(config)({
336
- ...explore,
337
- source: "function",
338
- })(input),
339
- )
340
- : decode_tuple_inline(project)(config)(importer)(
341
- input,
342
- tuple.type,
343
- explore,
344
- );
345
-
346
- const decode_tuple_inline =
347
- (project: IProject) =>
348
- (config: FeatureProgrammer.IConfig) =>
349
- (importer: FunctionImporter) =>
350
- (
351
- input: ts.Expression,
352
- tuple: MetadataTupleType,
353
- explore: FeatureProgrammer.IExplore,
354
- ): ts.Expression => {
355
- const children: ts.Expression[] = tuple.elements
356
- .filter((m) => m.rest === null)
357
- .map((elem, index) =>
358
- decode(project)(config)(importer)(
359
- ts.factory.createElementAccessExpression(input, index),
360
- elem,
361
- {
362
- ...explore,
363
- from: "array",
364
- postfix: explore.postfix.length
365
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
366
- : `"[${index}]"`,
367
- },
368
- ),
369
- );
370
- const rest = (() => {
371
- if (tuple.elements.length === 0) return null;
372
-
373
- const last: Metadata = tuple.elements.at(-1)!;
374
- const rest: Metadata | null = last.rest;
375
- if (rest === null) return null;
376
-
377
- return decode(project)(config)(importer)(
378
- ts.factory.createCallExpression(
379
- IdentifierFactory.access(input)("slice"),
380
- undefined,
381
- [
382
- ts.factory.createNumericLiteral(
383
- tuple.elements.length - 1,
384
- ),
385
- ],
386
- ),
387
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
388
- {
389
- ...explore,
390
- start: tuple.elements.length - 1,
391
- },
392
- );
393
- })();
394
- return CloneJoiner.tuple(children, rest);
395
- };
396
-
397
- /* -----------------------------------------------------------
398
- NATIVE CLASSES
399
- ----------------------------------------------------------- */
400
- const decode_native = (type: string) => (input: ts.Expression) =>
401
- type === "Date" ||
402
- type === "Uint8Array" ||
403
- type === "Uint8ClampedArray" ||
404
- type === "Uint16Array" ||
405
- type === "Uint32Array" ||
406
- type === "BigUint64Array" ||
407
- type === "Int8Array" ||
408
- type === "Int16Array" ||
409
- type === "Int32Array" ||
410
- type === "BigInt64Array" ||
411
- type === "Float32Array" ||
412
- type === "Float64Array"
413
- ? decode_native_copyable(type)(input)
414
- : type === "ArrayBuffer" || type === "SharedArrayBuffer"
415
- ? decode_native_buffer(type)(input)
416
- : type === "DataView"
417
- ? decode_native_data_view(input)
418
- : ts.factory.createCallExpression(
419
- ts.factory.createIdentifier(type),
420
- undefined,
421
- [],
422
- );
423
-
424
- const decode_native_copyable = (type: string) => (input: ts.Expression) =>
425
- ts.factory.createNewExpression(
426
- ts.factory.createIdentifier(type),
427
- undefined,
428
- [input],
429
- );
430
-
431
- const decode_native_buffer =
432
- (type: "ArrayBuffer" | "SharedArrayBuffer") => (input: ts.Expression) =>
433
- ExpressionFactory.selfCall(
434
- ts.factory.createBlock(
435
- [
436
- StatementFactory.constant(
437
- "buffer",
438
- ts.factory.createNewExpression(
439
- ts.factory.createIdentifier(type),
440
- undefined,
441
- [IdentifierFactory.access(input)("byteLength")],
442
- ),
443
- ),
444
- ts.factory.createExpressionStatement(
445
- ts.factory.createCallExpression(
446
- IdentifierFactory.access(
447
- ts.factory.createNewExpression(
448
- ts.factory.createIdentifier(
449
- "Uint8Array",
450
- ),
451
- undefined,
452
- [ts.factory.createIdentifier("buffer")],
453
- ),
454
- )("set"),
455
- undefined,
456
- [
457
- ts.factory.createNewExpression(
458
- ts.factory.createIdentifier(
459
- "Uint8Array",
460
- ),
461
- undefined,
462
- [input],
463
- ),
464
- ],
465
- ),
466
- ),
467
- ts.factory.createReturnStatement(
468
- ts.factory.createIdentifier("buffer"),
469
- ),
470
- ],
471
- true,
472
- ),
473
- );
474
-
475
- const decode_native_data_view = (input: ts.Expression) =>
476
- ts.factory.createNewExpression(
477
- ts.factory.createIdentifier("DataView"),
478
- undefined,
479
- [IdentifierFactory.access(input)("buffer")],
480
- );
481
-
482
- /* -----------------------------------------------------------
483
- EXPLORERS FOR UNION TYPES
484
- ----------------------------------------------------------- */
485
- const explore_sets =
486
- (project: IProject) =>
487
- (config: FeatureProgrammer.IConfig) =>
488
- (importer: FunctionImporter) =>
489
- (
490
- input: ts.Expression,
491
- sets: Metadata[],
492
- explore: FeatureProgrammer.IExplore,
493
- ): ts.Expression =>
494
- ts.factory.createCallExpression(
495
- UnionExplorer.set({
496
- checker: IsProgrammer.decode(project)(importer),
497
- decoder: (input, array, explore) =>
498
- ts.factory.createNewExpression(
499
- ts.factory.createIdentifier("Set"),
500
- [TypeFactory.keyword("any")],
501
- [
502
- decode_array(config)(importer)(
503
- input,
504
- array,
505
- explore,
506
- ),
507
- ],
508
- ),
509
- empty: ts.factory.createNewExpression(
510
- ts.factory.createIdentifier("Set"),
511
- [TypeFactory.keyword("any")],
512
- [],
513
- ),
514
- success: ts.factory.createTrue(),
515
- failure: (input, expected) =>
516
- create_throw_error(importer)(expected)(input),
517
- })([])(input, sets, explore),
518
- undefined,
519
- undefined,
520
- );
521
-
522
- const explore_maps =
523
- (project: IProject) =>
524
- (config: FeatureProgrammer.IConfig) =>
525
- (importer: FunctionImporter) =>
526
- (
527
- input: ts.Expression,
528
- maps: Metadata.Entry[],
529
- explore: FeatureProgrammer.IExplore,
530
- ): ts.Expression =>
531
- ts.factory.createCallExpression(
532
- UnionExplorer.map({
533
- checker: (top, entry, explore) => {
534
- const func = IsProgrammer.decode(project)(importer);
535
- return ts.factory.createLogicalAnd(
536
- func(
537
- ts.factory.createElementAccessExpression(
538
- top,
539
- 0,
540
- ),
541
- entry[0],
542
- {
543
- ...explore,
544
- postfix: `${explore.postfix}[0]`,
545
- },
546
- ),
547
- func(
548
- ts.factory.createElementAccessExpression(
549
- top,
550
- 1,
551
- ),
552
- entry[1],
553
- {
554
- ...explore,
555
- postfix: `${explore.postfix}[1]`,
556
- },
557
- ),
558
- );
559
- },
560
- decoder: (input, array, explore) =>
561
- ts.factory.createNewExpression(
562
- ts.factory.createIdentifier("Map"),
563
- [
564
- TypeFactory.keyword("any"),
565
- TypeFactory.keyword("any"),
566
- ],
567
- [
568
- decode_array(config)(importer)(
569
- input,
570
- array,
571
- explore,
572
- ),
573
- ],
574
- ),
575
- empty: ts.factory.createNewExpression(
576
- ts.factory.createIdentifier("Map"),
577
- [
578
- TypeFactory.keyword("any"),
579
- TypeFactory.keyword("any"),
580
- ],
581
- [],
582
- ),
583
- success: ts.factory.createTrue(),
584
- failure: (input, expected) =>
585
- create_throw_error(importer)(expected)(input),
586
- })([])(input, maps, explore),
587
- undefined,
588
- undefined,
589
- );
590
-
591
- const explore_objects =
592
- (config: FeatureProgrammer.IConfig) =>
593
- (importer: FunctionImporter) =>
594
- (
595
- input: ts.Expression,
596
- meta: Metadata,
597
- explore: FeatureProgrammer.IExplore,
598
- ) => {
599
- if (meta.objects.length === 1)
600
- return decode_object(importer)(
601
- input,
602
- meta.objects[0]!,
603
- explore,
604
- );
605
-
606
- return ts.factory.createCallExpression(
607
- ts.factory.createIdentifier(
608
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
609
- ),
610
- undefined,
611
- FeatureProgrammer.argumentsArray(config)(explore)(input),
612
- );
613
- };
614
-
615
- const explore_arrays =
616
- (project: IProject) =>
617
- (config: FeatureProgrammer.IConfig) =>
618
- (importer: FunctionImporter) =>
619
- (
620
- input: ts.Expression,
621
- elements: MetadataArray[],
622
- explore: FeatureProgrammer.IExplore,
623
- ): ts.Expression =>
624
- explore_array_like_union_types(config)(importer)(
625
- UnionExplorer.array({
626
- checker: IsProgrammer.decode(project)(importer),
627
- decoder: decode_array(config)(importer),
628
- empty: ts.factory.createIdentifier("[]"),
629
- success: ts.factory.createTrue(),
630
- failure: (input, expected) =>
631
- create_throw_error(importer)(expected)(input),
632
- }),
633
- )(input, elements, explore);
634
-
635
- const explore_array_like_union_types =
636
- (config: FeatureProgrammer.IConfig) =>
637
- (importer: FunctionImporter) =>
638
- <T extends MetadataArray | MetadataTuple>(
639
- factory: (
640
- parameters: ts.ParameterDeclaration[],
641
- ) => (
642
- input: ts.Expression,
643
- elements: T[],
644
- explore: FeatureProgrammer.IExplore,
645
- ) => ts.ArrowFunction,
646
- ) =>
647
- (
648
- input: ts.Expression,
649
- elements: T[],
650
- explore: FeatureProgrammer.IExplore,
651
- ): ts.Expression => {
652
- const arrow =
653
- (parameters: ts.ParameterDeclaration[]) =>
654
- (explore: FeatureProgrammer.IExplore) =>
655
- (input: ts.Expression): ts.ArrowFunction =>
656
- factory(parameters)(input, elements, explore);
657
- if (elements.every((e) => e.type.recursive === false))
658
- ts.factory.createCallExpression(
659
- arrow([])(explore)(input),
660
- undefined,
661
- [],
662
- );
663
-
664
- explore = {
665
- ...explore,
666
- source: "function",
667
- from: "array",
668
- };
669
- return ts.factory.createCallExpression(
670
- ts.factory.createIdentifier(
671
- importer.emplaceUnion(
672
- config.prefix,
673
- elements.map((e) => e.type.name).join(" | "),
674
- () =>
675
- arrow(
676
- FeatureProgrammer.parameterDeclarations(config)(
677
- TypeFactory.keyword("any"),
678
- )(ts.factory.createIdentifier("input")),
679
- )({
680
- ...explore,
681
- postfix: "",
682
- })(ts.factory.createIdentifier("input")),
683
- ),
684
- ),
685
- undefined,
686
- FeatureProgrammer.argumentsArray(config)(explore)(input),
687
- );
688
- };
689
-
690
- /* -----------------------------------------------------------
691
- CONFIGURATIONS
692
- ----------------------------------------------------------- */
693
- const PREFIX = "$c";
694
-
695
- const configure =
696
- (project: IProject) =>
697
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
698
- const config: FeatureProgrammer.IConfig = {
699
- types: {
700
- input: (type, name) =>
701
- ts.factory.createTypeReferenceNode(
702
- name ??
703
- TypeFactory.getFullName(project.checker)(type),
704
- ),
705
- output: (type, name) =>
706
- ts.factory.createTypeReferenceNode(
707
- `typia.Resolved<${
708
- name ??
709
- TypeFactory.getFullName(project.checker)(type)
710
- }>`,
711
- ),
712
- },
713
- prefix: PREFIX,
714
- trace: false,
715
- path: false,
716
- initializer,
717
- decoder: () => decode(project)(config)(importer),
718
- objector: {
719
- checker: () => IsProgrammer.decode(project)(importer),
720
- decoder: () => decode_object(importer),
721
- joiner: CloneJoiner.object,
722
- unionizer: decode_union_object(
723
- IsProgrammer.decode_object(project)(importer),
724
- )(decode_object(importer))((exp) => exp)(
725
- (input, expected) =>
726
- create_throw_error(importer)(expected)(input),
727
- ),
728
- failure: (input, expected) =>
729
- create_throw_error(importer)(expected)(input),
730
- },
731
- generator: {
732
- arrays: () => write_array_functions(config)(importer),
733
- tuples: () =>
734
- write_tuple_functions(project)(config)(importer),
735
- },
736
- };
737
- return config;
738
- };
739
-
740
- const initializer: FeatureProgrammer.IConfig["initializer"] =
741
- ({ checker }) =>
742
- (importer) =>
743
- (type) => {
744
- const collection = new MetadataCollection();
745
- const result = MetadataFactory.analyze(checker)({
746
- escape: false,
747
- constant: true,
748
- absorb: true,
749
- validate: (meta) => {
750
- const output: string[] = [];
751
- if (meta.natives.some((n) => n === "WeakSet"))
752
- output.push("unable to clone WeakSet");
753
- else if (meta.natives.some((n) => n === "WeakMap"))
754
- output.push("unable to clone WeakMap");
755
- return output;
756
- },
757
- })(collection)(type);
758
- if (result.success === false)
759
- throw TransformerError.from(`typia.misc.${importer.method}`)(
760
- result.errors,
761
- );
762
- return [collection, result.data];
763
- };
764
-
765
- const create_throw_error =
766
- (importer: FunctionImporter) =>
767
- (expected: string) =>
768
- (value: ts.Expression) =>
769
- ts.factory.createExpressionStatement(
770
- ts.factory.createCallExpression(
771
- importer.use("throws"),
772
- [],
773
- [
774
- ts.factory.createObjectLiteralExpression(
775
- [
776
- ts.factory.createPropertyAssignment(
777
- "expected",
778
- ts.factory.createStringLiteral(expected),
779
- ),
780
- ts.factory.createPropertyAssignment(
781
- "value",
782
- value,
783
- ),
784
- ],
785
- true,
786
- ),
787
- ],
788
- ),
789
- );
790
- }
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 { TypeFactory } from "../../factories/TypeFactory";
9
+
10
+ import { Metadata } from "../../schemas/metadata/Metadata";
11
+ import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
+ import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
+ import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
+
15
+ import { IProject } from "../../transformers/IProject";
16
+ import { TransformerError } from "../../transformers/TransformerError";
17
+
18
+ import { FeatureProgrammer } from "../FeatureProgrammer";
19
+ import { IsProgrammer } from "../IsProgrammer";
20
+ import { CloneJoiner } from "../helpers/CloneJoiner";
21
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
22
+ import { UnionExplorer } from "../helpers/UnionExplorer";
23
+ import { decode_union_object } from "../internal/decode_union_object";
24
+ import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
25
+
26
+ export namespace MiscCloneProgrammer {
27
+ export const write =
28
+ (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
29
+ const importer: FunctionImporter = new FunctionImporter(
30
+ modulo.getText(),
31
+ );
32
+ return FeatureProgrammer.write(project)({
33
+ ...configure(project)(importer),
34
+ addition: (collection) => [
35
+ ...IsProgrammer.write_function_statements(project)(
36
+ importer,
37
+ )(collection),
38
+ ...importer.declare(modulo),
39
+ ],
40
+ })(importer);
41
+ };
42
+
43
+ const write_array_functions =
44
+ (config: FeatureProgrammer.IConfig) =>
45
+ (importer: FunctionImporter) =>
46
+ (collection: MetadataCollection): ts.VariableStatement[] =>
47
+ collection
48
+ .arrays()
49
+ .filter((a) => a.recursive)
50
+ .map((type, i) =>
51
+ StatementFactory.constant(
52
+ `${config.prefix}a${i}`,
53
+ ts.factory.createArrowFunction(
54
+ undefined,
55
+ undefined,
56
+ FeatureProgrammer.parameterDeclarations(config)(
57
+ TypeFactory.keyword("any"),
58
+ )(ts.factory.createIdentifier("input")),
59
+ TypeFactory.keyword("any"),
60
+ undefined,
61
+ decode_array_inline(config)(importer)(
62
+ ts.factory.createIdentifier("input"),
63
+ MetadataArray.create({
64
+ type,
65
+ tags: [],
66
+ }),
67
+ {
68
+ tracable: config.trace,
69
+ source: "function",
70
+ from: "array",
71
+ postfix: "",
72
+ },
73
+ ),
74
+ ),
75
+ ),
76
+ );
77
+
78
+ const write_tuple_functions =
79
+ (project: IProject) =>
80
+ (config: FeatureProgrammer.IConfig) =>
81
+ (importer: FunctionImporter) =>
82
+ (collection: MetadataCollection): ts.VariableStatement[] =>
83
+ collection
84
+ .tuples()
85
+ .filter((t) => t.recursive)
86
+ .map((tuple, i) =>
87
+ StatementFactory.constant(
88
+ `${config.prefix}t${i}`,
89
+ ts.factory.createArrowFunction(
90
+ undefined,
91
+ undefined,
92
+ FeatureProgrammer.parameterDeclarations(config)(
93
+ TypeFactory.keyword("any"),
94
+ )(ts.factory.createIdentifier("input")),
95
+ TypeFactory.keyword("any"),
96
+ undefined,
97
+ decode_tuple_inline(project)(config)(importer)(
98
+ ts.factory.createIdentifier("input"),
99
+ tuple,
100
+ {
101
+ tracable: config.trace,
102
+ source: "function",
103
+ from: "array",
104
+ postfix: "",
105
+ },
106
+ ),
107
+ ),
108
+ ),
109
+ );
110
+
111
+ /* -----------------------------------------------------------
112
+ DECODERS
113
+ ----------------------------------------------------------- */
114
+ const decode =
115
+ (project: IProject) =>
116
+ (config: FeatureProgrammer.IConfig) =>
117
+ (importer: FunctionImporter) =>
118
+ (
119
+ input: ts.Expression,
120
+ meta: Metadata,
121
+ explore: FeatureProgrammer.IExplore,
122
+ ): ts.Expression => {
123
+ // ANY TYPE
124
+ if (
125
+ meta.any ||
126
+ meta.arrays.some((a) => a.type.value.any) ||
127
+ meta.tuples.some(
128
+ (t) =>
129
+ !!t.type.elements.length &&
130
+ t.type.elements.every((e) => e.any),
131
+ )
132
+ )
133
+ return ts.factory.createCallExpression(
134
+ importer.use("any"),
135
+ undefined,
136
+ [input],
137
+ );
138
+
139
+ interface IUnion {
140
+ type: string;
141
+ is: () => ts.Expression;
142
+ value: () => ts.Expression;
143
+ }
144
+ const unions: IUnion[] = [];
145
+
146
+ //----
147
+ // LIST UP UNION TYPES
148
+ //----
149
+ // FUNCTIONAL
150
+ if (meta.functional)
151
+ unions.push({
152
+ type: "functional",
153
+ is: () =>
154
+ ts.factory.createStrictEquality(
155
+ ts.factory.createStringLiteral("function"),
156
+ ts.factory.createTypeOfExpression(input),
157
+ ),
158
+ value: () => ts.factory.createIdentifier("undefined"),
159
+ });
160
+
161
+ // TUPLES
162
+ for (const tuple of meta.tuples)
163
+ unions.push({
164
+ type: "tuple",
165
+ is: () =>
166
+ IsProgrammer.decode(project)(importer)(
167
+ input,
168
+ (() => {
169
+ const partial = Metadata.initialize();
170
+ partial.tuples.push(tuple);
171
+ return partial;
172
+ })(),
173
+ explore,
174
+ ),
175
+ value: () =>
176
+ decode_tuple(project)(config)(importer)(
177
+ input,
178
+ tuple,
179
+ explore,
180
+ ),
181
+ });
182
+
183
+ // ARRAYS
184
+ if (meta.arrays.length)
185
+ unions.push({
186
+ type: "array",
187
+ is: () => ExpressionFactory.isArray(input),
188
+ value: () =>
189
+ explore_arrays(project)(config)(importer)(
190
+ input,
191
+ meta.arrays,
192
+ {
193
+ ...explore,
194
+ from: "array",
195
+ },
196
+ ),
197
+ });
198
+
199
+ // NATIVE TYPES
200
+ if (meta.sets.length)
201
+ unions.push({
202
+ type: "set",
203
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
204
+ value: () =>
205
+ explore_sets(project)(config)(importer)(
206
+ input,
207
+ meta.sets,
208
+ { ...explore, from: "array" },
209
+ ),
210
+ });
211
+ if (meta.maps.length)
212
+ unions.push({
213
+ type: "map",
214
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
215
+ value: () =>
216
+ explore_maps(project)(config)(importer)(
217
+ input,
218
+ meta.maps,
219
+ {
220
+ ...explore,
221
+ from: "array",
222
+ },
223
+ ),
224
+ });
225
+ for (const native of meta.natives)
226
+ unions.push({
227
+ type: "native",
228
+ is: () => ExpressionFactory.isInstanceOf(native)(input),
229
+ value: () =>
230
+ native === "Boolean" ||
231
+ native === "Number" ||
232
+ native === "String"
233
+ ? ts.factory.createCallExpression(
234
+ IdentifierFactory.access(input)("valueOf"),
235
+ undefined,
236
+ undefined,
237
+ )
238
+ : decode_native(native)(input),
239
+ });
240
+
241
+ // OBJECTS
242
+ if (meta.objects.length)
243
+ unions.push({
244
+ type: "object",
245
+ is: () =>
246
+ ExpressionFactory.isObject({
247
+ checkNull: true,
248
+ checkArray: false,
249
+ })(input),
250
+ value: () =>
251
+ explore_objects(config)(importer)(input, meta, {
252
+ ...explore,
253
+ from: "object",
254
+ }),
255
+ });
256
+
257
+ // COMPOSITION
258
+ let last: ts.Expression = input;
259
+ for (const u of unions.reverse())
260
+ last = ts.factory.createConditionalExpression(
261
+ u.is(),
262
+ undefined,
263
+ u.value(),
264
+ undefined,
265
+ last,
266
+ );
267
+ return ts.factory.createAsExpression(
268
+ last,
269
+ TypeFactory.keyword("any"),
270
+ );
271
+ };
272
+
273
+ const decode_object = (importer: FunctionImporter) =>
274
+ FeatureProgrammer.decode_object({
275
+ trace: false,
276
+ path: false,
277
+ prefix: PREFIX,
278
+ })(importer);
279
+
280
+ const decode_array =
281
+ (config: FeatureProgrammer.IConfig) =>
282
+ (importer: FunctionImporter) =>
283
+ (
284
+ input: ts.Expression,
285
+ array: MetadataArray,
286
+ explore: FeatureProgrammer.IExplore,
287
+ ) =>
288
+ array.type.recursive
289
+ ? ts.factory.createCallExpression(
290
+ ts.factory.createIdentifier(
291
+ importer.useLocal(
292
+ `${config.prefix}a${array.type.index}`,
293
+ ),
294
+ ),
295
+ undefined,
296
+ FeatureProgrammer.argumentsArray(config)({
297
+ ...explore,
298
+ source: "function",
299
+ from: "array",
300
+ })(input),
301
+ )
302
+ : decode_array_inline(config)(importer)(input, array, explore);
303
+
304
+ const decode_array_inline =
305
+ (config: FeatureProgrammer.IConfig) =>
306
+ (importer: FunctionImporter) =>
307
+ (
308
+ input: ts.Expression,
309
+ array: MetadataArray,
310
+ explore: FeatureProgrammer.IExplore,
311
+ ) =>
312
+ FeatureProgrammer.decode_array(config)(importer)(CloneJoiner.array)(
313
+ input,
314
+ array,
315
+ explore,
316
+ );
317
+
318
+ const decode_tuple =
319
+ (project: IProject) =>
320
+ (config: FeatureProgrammer.IConfig) =>
321
+ (importer: FunctionImporter) =>
322
+ (
323
+ input: ts.Expression,
324
+ tuple: MetadataTuple,
325
+ explore: FeatureProgrammer.IExplore,
326
+ ): ts.Expression =>
327
+ tuple.type.recursive
328
+ ? ts.factory.createCallExpression(
329
+ ts.factory.createIdentifier(
330
+ importer.useLocal(
331
+ `${config.prefix}t${tuple.type.index}`,
332
+ ),
333
+ ),
334
+ undefined,
335
+ FeatureProgrammer.argumentsArray(config)({
336
+ ...explore,
337
+ source: "function",
338
+ })(input),
339
+ )
340
+ : decode_tuple_inline(project)(config)(importer)(
341
+ input,
342
+ tuple.type,
343
+ explore,
344
+ );
345
+
346
+ const decode_tuple_inline =
347
+ (project: IProject) =>
348
+ (config: FeatureProgrammer.IConfig) =>
349
+ (importer: FunctionImporter) =>
350
+ (
351
+ input: ts.Expression,
352
+ tuple: MetadataTupleType,
353
+ explore: FeatureProgrammer.IExplore,
354
+ ): ts.Expression => {
355
+ const children: ts.Expression[] = tuple.elements
356
+ .filter((m) => m.rest === null)
357
+ .map((elem, index) =>
358
+ decode(project)(config)(importer)(
359
+ ts.factory.createElementAccessExpression(input, index),
360
+ elem,
361
+ {
362
+ ...explore,
363
+ from: "array",
364
+ postfix: explore.postfix.length
365
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
366
+ : `"[${index}]"`,
367
+ },
368
+ ),
369
+ );
370
+ const rest = (() => {
371
+ if (tuple.elements.length === 0) return null;
372
+
373
+ const last: Metadata = tuple.elements.at(-1)!;
374
+ const rest: Metadata | null = last.rest;
375
+ if (rest === null) return null;
376
+
377
+ return decode(project)(config)(importer)(
378
+ ts.factory.createCallExpression(
379
+ IdentifierFactory.access(input)("slice"),
380
+ undefined,
381
+ [ExpressionFactory.number(tuple.elements.length - 1)],
382
+ ),
383
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
384
+ {
385
+ ...explore,
386
+ start: tuple.elements.length - 1,
387
+ },
388
+ );
389
+ })();
390
+ return CloneJoiner.tuple(children, rest);
391
+ };
392
+
393
+ /* -----------------------------------------------------------
394
+ NATIVE CLASSES
395
+ ----------------------------------------------------------- */
396
+ const decode_native = (type: string) => (input: ts.Expression) =>
397
+ type === "Date" ||
398
+ type === "Uint8Array" ||
399
+ type === "Uint8ClampedArray" ||
400
+ type === "Uint16Array" ||
401
+ type === "Uint32Array" ||
402
+ type === "BigUint64Array" ||
403
+ type === "Int8Array" ||
404
+ type === "Int16Array" ||
405
+ type === "Int32Array" ||
406
+ type === "BigInt64Array" ||
407
+ type === "Float32Array" ||
408
+ type === "Float64Array"
409
+ ? decode_native_copyable(type)(input)
410
+ : type === "ArrayBuffer" || type === "SharedArrayBuffer"
411
+ ? decode_native_buffer(type)(input)
412
+ : type === "DataView"
413
+ ? decode_native_data_view(input)
414
+ : ts.factory.createCallExpression(
415
+ ts.factory.createIdentifier(type),
416
+ undefined,
417
+ [],
418
+ );
419
+
420
+ const decode_native_copyable = (type: string) => (input: ts.Expression) =>
421
+ ts.factory.createNewExpression(
422
+ ts.factory.createIdentifier(type),
423
+ undefined,
424
+ [input],
425
+ );
426
+
427
+ const decode_native_buffer =
428
+ (type: "ArrayBuffer" | "SharedArrayBuffer") => (input: ts.Expression) =>
429
+ ExpressionFactory.selfCall(
430
+ ts.factory.createBlock(
431
+ [
432
+ StatementFactory.constant(
433
+ "buffer",
434
+ ts.factory.createNewExpression(
435
+ ts.factory.createIdentifier(type),
436
+ undefined,
437
+ [IdentifierFactory.access(input)("byteLength")],
438
+ ),
439
+ ),
440
+ ts.factory.createExpressionStatement(
441
+ ts.factory.createCallExpression(
442
+ IdentifierFactory.access(
443
+ ts.factory.createNewExpression(
444
+ ts.factory.createIdentifier(
445
+ "Uint8Array",
446
+ ),
447
+ undefined,
448
+ [ts.factory.createIdentifier("buffer")],
449
+ ),
450
+ )("set"),
451
+ undefined,
452
+ [
453
+ ts.factory.createNewExpression(
454
+ ts.factory.createIdentifier(
455
+ "Uint8Array",
456
+ ),
457
+ undefined,
458
+ [input],
459
+ ),
460
+ ],
461
+ ),
462
+ ),
463
+ ts.factory.createReturnStatement(
464
+ ts.factory.createIdentifier("buffer"),
465
+ ),
466
+ ],
467
+ true,
468
+ ),
469
+ );
470
+
471
+ const decode_native_data_view = (input: ts.Expression) =>
472
+ ts.factory.createNewExpression(
473
+ ts.factory.createIdentifier("DataView"),
474
+ undefined,
475
+ [IdentifierFactory.access(input)("buffer")],
476
+ );
477
+
478
+ /* -----------------------------------------------------------
479
+ EXPLORERS FOR UNION TYPES
480
+ ----------------------------------------------------------- */
481
+ const explore_sets =
482
+ (project: IProject) =>
483
+ (config: FeatureProgrammer.IConfig) =>
484
+ (importer: FunctionImporter) =>
485
+ (
486
+ input: ts.Expression,
487
+ sets: Metadata[],
488
+ explore: FeatureProgrammer.IExplore,
489
+ ): ts.Expression =>
490
+ ts.factory.createCallExpression(
491
+ UnionExplorer.set({
492
+ checker: IsProgrammer.decode(project)(importer),
493
+ decoder: (input, array, explore) =>
494
+ ts.factory.createNewExpression(
495
+ ts.factory.createIdentifier("Set"),
496
+ [TypeFactory.keyword("any")],
497
+ [
498
+ decode_array(config)(importer)(
499
+ input,
500
+ array,
501
+ explore,
502
+ ),
503
+ ],
504
+ ),
505
+ empty: ts.factory.createNewExpression(
506
+ ts.factory.createIdentifier("Set"),
507
+ [TypeFactory.keyword("any")],
508
+ [],
509
+ ),
510
+ success: ts.factory.createTrue(),
511
+ failure: (input, expected) =>
512
+ create_throw_error(importer)(expected)(input),
513
+ })([])(input, sets, explore),
514
+ undefined,
515
+ undefined,
516
+ );
517
+
518
+ const explore_maps =
519
+ (project: IProject) =>
520
+ (config: FeatureProgrammer.IConfig) =>
521
+ (importer: FunctionImporter) =>
522
+ (
523
+ input: ts.Expression,
524
+ maps: Metadata.Entry[],
525
+ explore: FeatureProgrammer.IExplore,
526
+ ): ts.Expression =>
527
+ ts.factory.createCallExpression(
528
+ UnionExplorer.map({
529
+ checker: (top, entry, explore) => {
530
+ const func = IsProgrammer.decode(project)(importer);
531
+ return ts.factory.createLogicalAnd(
532
+ func(
533
+ ts.factory.createElementAccessExpression(
534
+ top,
535
+ 0,
536
+ ),
537
+ entry[0],
538
+ {
539
+ ...explore,
540
+ postfix: `${explore.postfix}[0]`,
541
+ },
542
+ ),
543
+ func(
544
+ ts.factory.createElementAccessExpression(
545
+ top,
546
+ 1,
547
+ ),
548
+ entry[1],
549
+ {
550
+ ...explore,
551
+ postfix: `${explore.postfix}[1]`,
552
+ },
553
+ ),
554
+ );
555
+ },
556
+ decoder: (input, array, explore) =>
557
+ ts.factory.createNewExpression(
558
+ ts.factory.createIdentifier("Map"),
559
+ [
560
+ TypeFactory.keyword("any"),
561
+ TypeFactory.keyword("any"),
562
+ ],
563
+ [
564
+ decode_array(config)(importer)(
565
+ input,
566
+ array,
567
+ explore,
568
+ ),
569
+ ],
570
+ ),
571
+ empty: ts.factory.createNewExpression(
572
+ ts.factory.createIdentifier("Map"),
573
+ [
574
+ TypeFactory.keyword("any"),
575
+ TypeFactory.keyword("any"),
576
+ ],
577
+ [],
578
+ ),
579
+ success: ts.factory.createTrue(),
580
+ failure: (input, expected) =>
581
+ create_throw_error(importer)(expected)(input),
582
+ })([])(input, maps, explore),
583
+ undefined,
584
+ undefined,
585
+ );
586
+
587
+ const explore_objects =
588
+ (config: FeatureProgrammer.IConfig) =>
589
+ (importer: FunctionImporter) =>
590
+ (
591
+ input: ts.Expression,
592
+ meta: Metadata,
593
+ explore: FeatureProgrammer.IExplore,
594
+ ) => {
595
+ if (meta.objects.length === 1)
596
+ return decode_object(importer)(
597
+ input,
598
+ meta.objects[0]!,
599
+ explore,
600
+ );
601
+
602
+ return ts.factory.createCallExpression(
603
+ ts.factory.createIdentifier(
604
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
605
+ ),
606
+ undefined,
607
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
608
+ );
609
+ };
610
+
611
+ const explore_arrays =
612
+ (project: IProject) =>
613
+ (config: FeatureProgrammer.IConfig) =>
614
+ (importer: FunctionImporter) =>
615
+ (
616
+ input: ts.Expression,
617
+ elements: MetadataArray[],
618
+ explore: FeatureProgrammer.IExplore,
619
+ ): ts.Expression =>
620
+ explore_array_like_union_types(config)(importer)(
621
+ UnionExplorer.array({
622
+ checker: IsProgrammer.decode(project)(importer),
623
+ decoder: decode_array(config)(importer),
624
+ empty: ts.factory.createIdentifier("[]"),
625
+ success: ts.factory.createTrue(),
626
+ failure: (input, expected) =>
627
+ create_throw_error(importer)(expected)(input),
628
+ }),
629
+ )(input, elements, explore);
630
+
631
+ const explore_array_like_union_types =
632
+ (config: FeatureProgrammer.IConfig) =>
633
+ (importer: FunctionImporter) =>
634
+ <T extends MetadataArray | MetadataTuple>(
635
+ factory: (
636
+ parameters: ts.ParameterDeclaration[],
637
+ ) => (
638
+ input: ts.Expression,
639
+ elements: T[],
640
+ explore: FeatureProgrammer.IExplore,
641
+ ) => ts.ArrowFunction,
642
+ ) =>
643
+ (
644
+ input: ts.Expression,
645
+ elements: T[],
646
+ explore: FeatureProgrammer.IExplore,
647
+ ): ts.Expression => {
648
+ const arrow =
649
+ (parameters: ts.ParameterDeclaration[]) =>
650
+ (explore: FeatureProgrammer.IExplore) =>
651
+ (input: ts.Expression): ts.ArrowFunction =>
652
+ factory(parameters)(input, elements, explore);
653
+ if (elements.every((e) => e.type.recursive === false))
654
+ ts.factory.createCallExpression(
655
+ arrow([])(explore)(input),
656
+ undefined,
657
+ [],
658
+ );
659
+
660
+ explore = {
661
+ ...explore,
662
+ source: "function",
663
+ from: "array",
664
+ };
665
+ return ts.factory.createCallExpression(
666
+ ts.factory.createIdentifier(
667
+ importer.emplaceUnion(
668
+ config.prefix,
669
+ elements.map((e) => e.type.name).join(" | "),
670
+ () =>
671
+ arrow(
672
+ FeatureProgrammer.parameterDeclarations(config)(
673
+ TypeFactory.keyword("any"),
674
+ )(ts.factory.createIdentifier("input")),
675
+ )({
676
+ ...explore,
677
+ postfix: "",
678
+ })(ts.factory.createIdentifier("input")),
679
+ ),
680
+ ),
681
+ undefined,
682
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
683
+ );
684
+ };
685
+
686
+ /* -----------------------------------------------------------
687
+ CONFIGURATIONS
688
+ ----------------------------------------------------------- */
689
+ const PREFIX = "$c";
690
+
691
+ const configure =
692
+ (project: IProject) =>
693
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
694
+ const config: FeatureProgrammer.IConfig = {
695
+ types: {
696
+ input: (type, name) =>
697
+ ts.factory.createTypeReferenceNode(
698
+ name ??
699
+ TypeFactory.getFullName(project.checker)(type),
700
+ ),
701
+ output: (type, name) =>
702
+ ts.factory.createTypeReferenceNode(
703
+ `typia.Resolved<${
704
+ name ??
705
+ TypeFactory.getFullName(project.checker)(type)
706
+ }>`,
707
+ ),
708
+ },
709
+ prefix: PREFIX,
710
+ trace: false,
711
+ path: false,
712
+ initializer,
713
+ decoder: () => decode(project)(config)(importer),
714
+ objector: {
715
+ checker: () => IsProgrammer.decode(project)(importer),
716
+ decoder: () => decode_object(importer),
717
+ joiner: CloneJoiner.object,
718
+ unionizer: decode_union_object(
719
+ IsProgrammer.decode_object(project)(importer),
720
+ )(decode_object(importer))((exp) => exp)(
721
+ (input, expected) =>
722
+ create_throw_error(importer)(expected)(input),
723
+ ),
724
+ failure: (input, expected) =>
725
+ create_throw_error(importer)(expected)(input),
726
+ },
727
+ generator: {
728
+ arrays: () => write_array_functions(config)(importer),
729
+ tuples: () =>
730
+ write_tuple_functions(project)(config)(importer),
731
+ },
732
+ };
733
+ return config;
734
+ };
735
+
736
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
737
+ ({ checker }) =>
738
+ (importer) =>
739
+ (type) => {
740
+ const collection = new MetadataCollection();
741
+ const result = MetadataFactory.analyze(checker)({
742
+ escape: false,
743
+ constant: true,
744
+ absorb: true,
745
+ validate: (meta) => {
746
+ const output: string[] = [];
747
+ if (meta.natives.some((n) => n === "WeakSet"))
748
+ output.push("unable to clone WeakSet");
749
+ else if (meta.natives.some((n) => n === "WeakMap"))
750
+ output.push("unable to clone WeakMap");
751
+ return output;
752
+ },
753
+ })(collection)(type);
754
+ if (result.success === false)
755
+ throw TransformerError.from(`typia.misc.${importer.method}`)(
756
+ result.errors,
757
+ );
758
+ return [collection, result.data];
759
+ };
760
+
761
+ const create_throw_error =
762
+ (importer: FunctionImporter) =>
763
+ (expected: string) =>
764
+ (value: ts.Expression) =>
765
+ ts.factory.createExpressionStatement(
766
+ ts.factory.createCallExpression(
767
+ importer.use("throws"),
768
+ [],
769
+ [
770
+ ts.factory.createObjectLiteralExpression(
771
+ [
772
+ ts.factory.createPropertyAssignment(
773
+ "expected",
774
+ ts.factory.createStringLiteral(expected),
775
+ ),
776
+ ts.factory.createPropertyAssignment(
777
+ "value",
778
+ value,
779
+ ),
780
+ ],
781
+ true,
782
+ ),
783
+ ],
784
+ ),
785
+ );
786
+ }