typia 5.2.2 → 5.2.3

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 (74) hide show
  1. package/lib/Primitive.d.ts +1 -1
  2. package/lib/executable/setup/CommandExecutor.js +2 -2
  3. package/lib/executable/setup/CommandExecutor.js.map +1 -1
  4. package/package.json +1 -2
  5. package/src/CamelCase.ts +116 -116
  6. package/src/PascalCase.ts +116 -116
  7. package/src/Primitive.ts +135 -135
  8. package/src/Resolved.ts +116 -116
  9. package/src/SnakeCase.ts +156 -156
  10. package/src/executable/TypiaSetupWizard.ts +142 -142
  11. package/src/executable/setup/CommandExecutor.ts +2 -2
  12. package/src/factories/JsonMetadataFactory.ts +50 -50
  13. package/src/factories/MetadataCollection.ts +282 -282
  14. package/src/functional/$convention.ts +40 -40
  15. package/src/functional/Namespace.ts +164 -164
  16. package/src/module.ts +662 -662
  17. package/src/notations.ts +855 -855
  18. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  19. package/src/programmers/helpers/NotationJoiner.ts +146 -146
  20. package/src/programmers/misc/MiscCloneProgrammer.ts +790 -790
  21. package/src/programmers/misc/MiscPruneProgrammer.ts +552 -552
  22. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +72 -72
  23. package/src/programmers/notations/NotationGeneralProgrammer.ts +720 -720
  24. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +79 -79
  25. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +88 -88
  26. package/src/transformers/CallExpressionTransformer.ts +380 -380
  27. package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
  28. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  29. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +12 -12
  30. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  31. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  32. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +12 -12
  33. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  34. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +12 -12
  35. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  36. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  37. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
  38. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  39. package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
  40. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  41. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
  42. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
  43. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  44. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +12 -12
  45. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +12 -12
  46. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  47. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  48. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  49. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  50. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +12 -12
  51. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +12 -12
  52. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  53. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  54. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  55. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
  56. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
  57. package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +15 -15
  58. package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +15 -15
  59. package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +15 -15
  60. package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +15 -15
  61. package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +17 -17
  62. package/src/transformers/features/notations/NotationGeneralTransformer.ts +11 -11
  63. package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +15 -15
  64. package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +17 -17
  65. package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +12 -12
  66. package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +12 -12
  67. package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +9 -9
  68. package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +9 -9
  69. package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +12 -12
  70. package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +12 -12
  71. package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +12 -12
  72. package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +12 -12
  73. package/src/utils/NamingConvention.ts +91 -91
  74. package/src/utils/StringUtil.ts +4 -4
@@ -1,790 +1,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
- [
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(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
+ [
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(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
+ }