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,552 +1,552 @@
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 { FunctionImporter } from "../helpers/FunctionImporeter";
21
- import { PruneJoiner } from "../helpers/PruneJoiner";
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 MiscPruneProgrammer {
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.ConciseBody => {
123
- if (filter(meta) === false) return ts.factory.createBlock([]);
124
-
125
- interface IUnion {
126
- type: string;
127
- is: () => ts.Expression;
128
- value: () => ts.Expression | ts.Block | ts.ReturnStatement;
129
- }
130
- const unions: IUnion[] = [];
131
-
132
- //----
133
- // LIST UP UNION TYPES
134
- //----
135
- // TUPLES
136
- for (const tuple of meta.tuples.filter((tuple) =>
137
- tuple.type.elements.some((e) => filter(e.rest ?? e)),
138
- ))
139
- unions.push({
140
- type: "tuple",
141
- is: () =>
142
- IsProgrammer.decode(project)(importer)(
143
- input,
144
- (() => {
145
- const partial = Metadata.initialize();
146
- partial.tuples.push(tuple);
147
- return partial;
148
- })(),
149
- explore,
150
- ),
151
- value: () =>
152
- decode_tuple(project)(config)(importer)(
153
- input,
154
- tuple,
155
- explore,
156
- ),
157
- });
158
-
159
- // ARRAYS
160
- if (meta.arrays.filter((a) => filter(a.type.value)).length)
161
- unions.push({
162
- type: "array",
163
- is: () => ExpressionFactory.isArray(input),
164
- value: () =>
165
- explore_arrays(project)(config)(importer)(
166
- input,
167
- meta.arrays,
168
- {
169
- ...explore,
170
- from: "array",
171
- },
172
- ),
173
- });
174
-
175
- // BUILT-IN CLASSES
176
- if (meta.natives.length)
177
- for (const native of meta.natives)
178
- unions.push({
179
- type: "native",
180
- is: () => ExpressionFactory.isInstanceOf(native)(input),
181
- value: () => ts.factory.createReturnStatement(),
182
- });
183
- if (meta.sets.length)
184
- unions.push({
185
- type: "set",
186
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
187
- value: () => ts.factory.createReturnStatement(),
188
- });
189
- if (meta.maps.length)
190
- unions.push({
191
- type: "map",
192
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
193
- value: () => ts.factory.createReturnStatement(),
194
- });
195
-
196
- // OBJECTS
197
- if (meta.objects.length)
198
- unions.push({
199
- type: "object",
200
- is: () =>
201
- ExpressionFactory.isObject({
202
- checkNull: true,
203
- checkArray: false,
204
- })(input),
205
- value: () =>
206
- explore_objects(config)(importer)(input, meta, {
207
- ...explore,
208
- from: "object",
209
- }),
210
- });
211
-
212
- //----
213
- // STATEMENTS
214
- //----
215
- const converter = (
216
- v: ts.Expression | ts.Block | ts.ReturnStatement,
217
- ) =>
218
- ts.isReturnStatement(v) || ts.isBlock(v)
219
- ? v
220
- : ts.factory.createExpressionStatement(v);
221
-
222
- const statements: ts.Statement[] = unions.map((u) =>
223
- ts.factory.createIfStatement(u.is(), converter(u.value())),
224
- );
225
- return ts.factory.createBlock(statements, true);
226
- };
227
-
228
- const decode_object = (importer: FunctionImporter) =>
229
- FeatureProgrammer.decode_object({
230
- trace: false,
231
- path: false,
232
- prefix: PREFIX,
233
- })(importer);
234
-
235
- const decode_array =
236
- (config: FeatureProgrammer.IConfig) =>
237
- (importer: FunctionImporter) =>
238
- (
239
- input: ts.Expression,
240
- array: MetadataArray,
241
- explore: FeatureProgrammer.IExplore,
242
- ) =>
243
- array.type.recursive
244
- ? ts.factory.createCallExpression(
245
- ts.factory.createIdentifier(
246
- importer.useLocal(
247
- `${config.prefix}a${array.type.index}`,
248
- ),
249
- ),
250
- undefined,
251
- FeatureProgrammer.argumentsArray(config)({
252
- ...explore,
253
- source: "function",
254
- from: "array",
255
- })(input),
256
- )
257
- : decode_array_inline(config)(importer)(input, array, explore);
258
-
259
- const decode_array_inline =
260
- (config: FeatureProgrammer.IConfig) =>
261
- (importer: FunctionImporter) =>
262
- (
263
- input: ts.Expression,
264
- array: MetadataArray,
265
- explore: FeatureProgrammer.IExplore,
266
- ): ts.Expression =>
267
- FeatureProgrammer.decode_array(config)(importer)(PruneJoiner.array)(
268
- input,
269
- array,
270
- explore,
271
- );
272
-
273
- const decode_tuple =
274
- (project: IProject) =>
275
- (config: FeatureProgrammer.IConfig) =>
276
- (importer: FunctionImporter) =>
277
- (
278
- input: ts.Expression,
279
- tuple: MetadataTuple,
280
- explore: FeatureProgrammer.IExplore,
281
- ): ts.Expression | ts.Block =>
282
- tuple.type.recursive
283
- ? ts.factory.createCallExpression(
284
- ts.factory.createIdentifier(
285
- importer.useLocal(
286
- `${config.prefix}t${tuple.type.index}`,
287
- ),
288
- ),
289
- undefined,
290
- FeatureProgrammer.argumentsArray(config)({
291
- ...explore,
292
- source: "function",
293
- })(input),
294
- )
295
- : decode_tuple_inline(project)(config)(importer)(
296
- input,
297
- tuple.type,
298
- explore,
299
- );
300
-
301
- const decode_tuple_inline =
302
- (project: IProject) =>
303
- (config: FeatureProgrammer.IConfig) =>
304
- (importer: FunctionImporter) =>
305
- (
306
- input: ts.Expression,
307
- tuple: MetadataTupleType,
308
- explore: FeatureProgrammer.IExplore,
309
- ): ts.Block => {
310
- const children: ts.ConciseBody[] = tuple.elements
311
- .map((elem, index) => [elem, index] as const)
312
- .filter(([elem]) => filter(elem) && elem.rest === null)
313
- .map(([elem, index]) =>
314
- decode(project)(config)(importer)(
315
- ts.factory.createElementAccessExpression(input, index),
316
- elem,
317
- {
318
- ...explore,
319
- from: "array",
320
- postfix: explore.postfix.length
321
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
322
- : `"[${index}]"`,
323
- },
324
- ),
325
- );
326
- const rest = (() => {
327
- if (tuple.elements.length === 0) return null;
328
-
329
- const last: Metadata = tuple.elements.at(-1)!;
330
- const rest: Metadata | null = last.rest;
331
- if (rest === null || filter(rest) === false) return null;
332
-
333
- return decode(project)(config)(importer)(
334
- ts.factory.createCallExpression(
335
- IdentifierFactory.access(input)("slice"),
336
- undefined,
337
- [
338
- ts.factory.createNumericLiteral(
339
- tuple.elements.length - 1,
340
- ),
341
- ],
342
- ),
343
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
344
- {
345
- ...explore,
346
- start: tuple.elements.length - 1,
347
- },
348
- );
349
- })();
350
- return PruneJoiner.tuple(children, rest);
351
- };
352
-
353
- /* -----------------------------------------------------------
354
- UNION TYPE EXPLORERS
355
- ----------------------------------------------------------- */
356
- const explore_objects =
357
- (config: FeatureProgrammer.IConfig) =>
358
- (importer: FunctionImporter) =>
359
- (
360
- input: ts.Expression,
361
- meta: Metadata,
362
- explore: FeatureProgrammer.IExplore,
363
- ) => {
364
- if (meta.objects.length === 1)
365
- return decode_object(importer)(
366
- input,
367
- meta.objects[0]!,
368
- explore,
369
- );
370
-
371
- return ts.factory.createCallExpression(
372
- ts.factory.createIdentifier(
373
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
374
- ),
375
- undefined,
376
- FeatureProgrammer.argumentsArray(config)(explore)(input),
377
- );
378
- };
379
-
380
- const explore_arrays =
381
- (project: IProject) =>
382
- (config: FeatureProgrammer.IConfig) =>
383
- (importer: FunctionImporter) =>
384
- (
385
- input: ts.Expression,
386
- elements: MetadataArray[],
387
- explore: FeatureProgrammer.IExplore,
388
- ): ts.Expression =>
389
- explore_array_like_union_types(config)(importer)(
390
- UnionExplorer.array({
391
- checker: IsProgrammer.decode(project)(importer),
392
- decoder: decode_array(config)(importer),
393
- empty: ts.factory.createStringLiteral("[]"),
394
- success: ts.factory.createTrue(),
395
- failure: (input, expected) =>
396
- create_throw_error(importer)(expected)(input),
397
- }),
398
- )(input, elements, explore);
399
-
400
- const explore_array_like_union_types =
401
- (config: FeatureProgrammer.IConfig) =>
402
- (importer: FunctionImporter) =>
403
- <T extends MetadataArray | MetadataTuple>(
404
- factory: (
405
- parameters: ts.ParameterDeclaration[],
406
- ) => (
407
- input: ts.Expression,
408
- elements: T[],
409
- explore: FeatureProgrammer.IExplore,
410
- ) => ts.ArrowFunction,
411
- ) =>
412
- (
413
- input: ts.Expression,
414
- elements: T[],
415
- explore: FeatureProgrammer.IExplore,
416
- ): ts.Expression => {
417
- const arrow =
418
- (parameters: ts.ParameterDeclaration[]) =>
419
- (explore: FeatureProgrammer.IExplore) =>
420
- (input: ts.Expression): ts.ArrowFunction =>
421
- factory(parameters)(input, elements, explore);
422
- if (elements.every((e) => e.type.recursive === false))
423
- ts.factory.createCallExpression(
424
- arrow([])(explore)(input),
425
- undefined,
426
- [],
427
- );
428
-
429
- explore = {
430
- ...explore,
431
- source: "function",
432
- from: "array",
433
- };
434
- return ts.factory.createCallExpression(
435
- ts.factory.createIdentifier(
436
- importer.emplaceUnion(
437
- config.prefix,
438
- elements.map((e) => e.type.name).join(" | "),
439
- () =>
440
- arrow(
441
- FeatureProgrammer.parameterDeclarations(config)(
442
- TypeFactory.keyword("any"),
443
- )(ts.factory.createIdentifier("input")),
444
- )({
445
- ...explore,
446
- postfix: "",
447
- })(ts.factory.createIdentifier("input")),
448
- ),
449
- ),
450
- undefined,
451
- FeatureProgrammer.argumentsArray(config)(explore)(input),
452
- );
453
- };
454
-
455
- // @todo -> must filter out recursive visit
456
- const filter = (meta: Metadata): boolean =>
457
- meta.any === false &&
458
- (meta.objects.length !== 0 ||
459
- meta.tuples.some(
460
- (t) =>
461
- !!t.type.elements.length &&
462
- t.type.elements.some((e) => filter(e.rest ?? e)),
463
- ) ||
464
- meta.arrays.some((e) => filter(e.type.value)));
465
-
466
- /* -----------------------------------------------------------
467
- CONFIGURATIONS
468
- ----------------------------------------------------------- */
469
- const PREFIX = "$p";
470
-
471
- const configure =
472
- (project: IProject) =>
473
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
474
- const config: FeatureProgrammer.IConfig = {
475
- types: {
476
- input: (type, name) =>
477
- ts.factory.createTypeReferenceNode(
478
- name ??
479
- TypeFactory.getFullName(project.checker)(type),
480
- ),
481
- output: () => TypeFactory.keyword("void"),
482
- },
483
- prefix: PREFIX,
484
- trace: false,
485
- path: false,
486
- initializer,
487
- decoder: () => decode(project)(config)(importer),
488
- objector: {
489
- checker: () => IsProgrammer.decode(project)(importer),
490
- decoder: () => decode_object(importer),
491
- joiner: PruneJoiner.object,
492
- unionizer: decode_union_object(
493
- IsProgrammer.decode_object(importer),
494
- )(decode_object(importer))((exp) => exp)(
495
- (value, expected) =>
496
- create_throw_error(importer)(expected)(value),
497
- ),
498
- failure: (input, expected) =>
499
- create_throw_error(importer)(expected)(input),
500
- },
501
- generator: {
502
- arrays: () => write_array_functions(config)(importer),
503
- tuples: () =>
504
- write_tuple_functions(project)(config)(importer),
505
- },
506
- };
507
- return config;
508
- };
509
-
510
- const initializer: FeatureProgrammer.IConfig["initializer"] =
511
- ({ checker }) =>
512
- (importer) =>
513
- (type) => {
514
- const collection = new MetadataCollection();
515
- const result = MetadataFactory.analyze(checker)({
516
- escape: false,
517
- constant: true,
518
- absorb: true,
519
- })(collection)(type);
520
- if (result.success === false)
521
- throw TransformerError.from(`typia.misc.${importer.method}`)(
522
- result.errors,
523
- );
524
- return [collection, result.data];
525
- };
526
-
527
- const create_throw_error =
528
- (importer: FunctionImporter) =>
529
- (expected: string) =>
530
- (value: ts.Expression) =>
531
- ts.factory.createExpressionStatement(
532
- ts.factory.createCallExpression(
533
- importer.use("throws"),
534
- [],
535
- [
536
- ts.factory.createObjectLiteralExpression(
537
- [
538
- ts.factory.createPropertyAssignment(
539
- "expected",
540
- ts.factory.createStringLiteral(expected),
541
- ),
542
- ts.factory.createPropertyAssignment(
543
- "value",
544
- value,
545
- ),
546
- ],
547
- true,
548
- ),
549
- ],
550
- ),
551
- );
552
- }
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 { FunctionImporter } from "../helpers/FunctionImporeter";
21
+ import { PruneJoiner } from "../helpers/PruneJoiner";
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 MiscPruneProgrammer {
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.ConciseBody => {
123
+ if (filter(meta) === false) return ts.factory.createBlock([]);
124
+
125
+ interface IUnion {
126
+ type: string;
127
+ is: () => ts.Expression;
128
+ value: () => ts.Expression | ts.Block | ts.ReturnStatement;
129
+ }
130
+ const unions: IUnion[] = [];
131
+
132
+ //----
133
+ // LIST UP UNION TYPES
134
+ //----
135
+ // TUPLES
136
+ for (const tuple of meta.tuples.filter((tuple) =>
137
+ tuple.type.elements.some((e) => filter(e.rest ?? e)),
138
+ ))
139
+ unions.push({
140
+ type: "tuple",
141
+ is: () =>
142
+ IsProgrammer.decode(project)(importer)(
143
+ input,
144
+ (() => {
145
+ const partial = Metadata.initialize();
146
+ partial.tuples.push(tuple);
147
+ return partial;
148
+ })(),
149
+ explore,
150
+ ),
151
+ value: () =>
152
+ decode_tuple(project)(config)(importer)(
153
+ input,
154
+ tuple,
155
+ explore,
156
+ ),
157
+ });
158
+
159
+ // ARRAYS
160
+ if (meta.arrays.filter((a) => filter(a.type.value)).length)
161
+ unions.push({
162
+ type: "array",
163
+ is: () => ExpressionFactory.isArray(input),
164
+ value: () =>
165
+ explore_arrays(project)(config)(importer)(
166
+ input,
167
+ meta.arrays,
168
+ {
169
+ ...explore,
170
+ from: "array",
171
+ },
172
+ ),
173
+ });
174
+
175
+ // BUILT-IN CLASSES
176
+ if (meta.natives.length)
177
+ for (const native of meta.natives)
178
+ unions.push({
179
+ type: "native",
180
+ is: () => ExpressionFactory.isInstanceOf(native)(input),
181
+ value: () => ts.factory.createReturnStatement(),
182
+ });
183
+ if (meta.sets.length)
184
+ unions.push({
185
+ type: "set",
186
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
187
+ value: () => ts.factory.createReturnStatement(),
188
+ });
189
+ if (meta.maps.length)
190
+ unions.push({
191
+ type: "map",
192
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
193
+ value: () => ts.factory.createReturnStatement(),
194
+ });
195
+
196
+ // OBJECTS
197
+ if (meta.objects.length)
198
+ unions.push({
199
+ type: "object",
200
+ is: () =>
201
+ ExpressionFactory.isObject({
202
+ checkNull: true,
203
+ checkArray: false,
204
+ })(input),
205
+ value: () =>
206
+ explore_objects(config)(importer)(input, meta, {
207
+ ...explore,
208
+ from: "object",
209
+ }),
210
+ });
211
+
212
+ //----
213
+ // STATEMENTS
214
+ //----
215
+ const converter = (
216
+ v: ts.Expression | ts.Block | ts.ReturnStatement,
217
+ ) =>
218
+ ts.isReturnStatement(v) || ts.isBlock(v)
219
+ ? v
220
+ : ts.factory.createExpressionStatement(v);
221
+
222
+ const statements: ts.Statement[] = unions.map((u) =>
223
+ ts.factory.createIfStatement(u.is(), converter(u.value())),
224
+ );
225
+ return ts.factory.createBlock(statements, true);
226
+ };
227
+
228
+ const decode_object = (importer: FunctionImporter) =>
229
+ FeatureProgrammer.decode_object({
230
+ trace: false,
231
+ path: false,
232
+ prefix: PREFIX,
233
+ })(importer);
234
+
235
+ const decode_array =
236
+ (config: FeatureProgrammer.IConfig) =>
237
+ (importer: FunctionImporter) =>
238
+ (
239
+ input: ts.Expression,
240
+ array: MetadataArray,
241
+ explore: FeatureProgrammer.IExplore,
242
+ ) =>
243
+ array.type.recursive
244
+ ? ts.factory.createCallExpression(
245
+ ts.factory.createIdentifier(
246
+ importer.useLocal(
247
+ `${config.prefix}a${array.type.index}`,
248
+ ),
249
+ ),
250
+ undefined,
251
+ FeatureProgrammer.argumentsArray(config)({
252
+ ...explore,
253
+ source: "function",
254
+ from: "array",
255
+ })(input),
256
+ )
257
+ : decode_array_inline(config)(importer)(input, array, explore);
258
+
259
+ const decode_array_inline =
260
+ (config: FeatureProgrammer.IConfig) =>
261
+ (importer: FunctionImporter) =>
262
+ (
263
+ input: ts.Expression,
264
+ array: MetadataArray,
265
+ explore: FeatureProgrammer.IExplore,
266
+ ): ts.Expression =>
267
+ FeatureProgrammer.decode_array(config)(importer)(PruneJoiner.array)(
268
+ input,
269
+ array,
270
+ explore,
271
+ );
272
+
273
+ const decode_tuple =
274
+ (project: IProject) =>
275
+ (config: FeatureProgrammer.IConfig) =>
276
+ (importer: FunctionImporter) =>
277
+ (
278
+ input: ts.Expression,
279
+ tuple: MetadataTuple,
280
+ explore: FeatureProgrammer.IExplore,
281
+ ): ts.Expression | ts.Block =>
282
+ tuple.type.recursive
283
+ ? ts.factory.createCallExpression(
284
+ ts.factory.createIdentifier(
285
+ importer.useLocal(
286
+ `${config.prefix}t${tuple.type.index}`,
287
+ ),
288
+ ),
289
+ undefined,
290
+ FeatureProgrammer.argumentsArray(config)({
291
+ ...explore,
292
+ source: "function",
293
+ })(input),
294
+ )
295
+ : decode_tuple_inline(project)(config)(importer)(
296
+ input,
297
+ tuple.type,
298
+ explore,
299
+ );
300
+
301
+ const decode_tuple_inline =
302
+ (project: IProject) =>
303
+ (config: FeatureProgrammer.IConfig) =>
304
+ (importer: FunctionImporter) =>
305
+ (
306
+ input: ts.Expression,
307
+ tuple: MetadataTupleType,
308
+ explore: FeatureProgrammer.IExplore,
309
+ ): ts.Block => {
310
+ const children: ts.ConciseBody[] = tuple.elements
311
+ .map((elem, index) => [elem, index] as const)
312
+ .filter(([elem]) => filter(elem) && elem.rest === null)
313
+ .map(([elem, index]) =>
314
+ decode(project)(config)(importer)(
315
+ ts.factory.createElementAccessExpression(input, index),
316
+ elem,
317
+ {
318
+ ...explore,
319
+ from: "array",
320
+ postfix: explore.postfix.length
321
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
322
+ : `"[${index}]"`,
323
+ },
324
+ ),
325
+ );
326
+ const rest = (() => {
327
+ if (tuple.elements.length === 0) return null;
328
+
329
+ const last: Metadata = tuple.elements.at(-1)!;
330
+ const rest: Metadata | null = last.rest;
331
+ if (rest === null || filter(rest) === false) return null;
332
+
333
+ return decode(project)(config)(importer)(
334
+ ts.factory.createCallExpression(
335
+ IdentifierFactory.access(input)("slice"),
336
+ undefined,
337
+ [
338
+ ts.factory.createNumericLiteral(
339
+ tuple.elements.length - 1,
340
+ ),
341
+ ],
342
+ ),
343
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
344
+ {
345
+ ...explore,
346
+ start: tuple.elements.length - 1,
347
+ },
348
+ );
349
+ })();
350
+ return PruneJoiner.tuple(children, rest);
351
+ };
352
+
353
+ /* -----------------------------------------------------------
354
+ UNION TYPE EXPLORERS
355
+ ----------------------------------------------------------- */
356
+ const explore_objects =
357
+ (config: FeatureProgrammer.IConfig) =>
358
+ (importer: FunctionImporter) =>
359
+ (
360
+ input: ts.Expression,
361
+ meta: Metadata,
362
+ explore: FeatureProgrammer.IExplore,
363
+ ) => {
364
+ if (meta.objects.length === 1)
365
+ return decode_object(importer)(
366
+ input,
367
+ meta.objects[0]!,
368
+ explore,
369
+ );
370
+
371
+ return ts.factory.createCallExpression(
372
+ ts.factory.createIdentifier(
373
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
374
+ ),
375
+ undefined,
376
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
377
+ );
378
+ };
379
+
380
+ const explore_arrays =
381
+ (project: IProject) =>
382
+ (config: FeatureProgrammer.IConfig) =>
383
+ (importer: FunctionImporter) =>
384
+ (
385
+ input: ts.Expression,
386
+ elements: MetadataArray[],
387
+ explore: FeatureProgrammer.IExplore,
388
+ ): ts.Expression =>
389
+ explore_array_like_union_types(config)(importer)(
390
+ UnionExplorer.array({
391
+ checker: IsProgrammer.decode(project)(importer),
392
+ decoder: decode_array(config)(importer),
393
+ empty: ts.factory.createStringLiteral("[]"),
394
+ success: ts.factory.createTrue(),
395
+ failure: (input, expected) =>
396
+ create_throw_error(importer)(expected)(input),
397
+ }),
398
+ )(input, elements, explore);
399
+
400
+ const explore_array_like_union_types =
401
+ (config: FeatureProgrammer.IConfig) =>
402
+ (importer: FunctionImporter) =>
403
+ <T extends MetadataArray | MetadataTuple>(
404
+ factory: (
405
+ parameters: ts.ParameterDeclaration[],
406
+ ) => (
407
+ input: ts.Expression,
408
+ elements: T[],
409
+ explore: FeatureProgrammer.IExplore,
410
+ ) => ts.ArrowFunction,
411
+ ) =>
412
+ (
413
+ input: ts.Expression,
414
+ elements: T[],
415
+ explore: FeatureProgrammer.IExplore,
416
+ ): ts.Expression => {
417
+ const arrow =
418
+ (parameters: ts.ParameterDeclaration[]) =>
419
+ (explore: FeatureProgrammer.IExplore) =>
420
+ (input: ts.Expression): ts.ArrowFunction =>
421
+ factory(parameters)(input, elements, explore);
422
+ if (elements.every((e) => e.type.recursive === false))
423
+ ts.factory.createCallExpression(
424
+ arrow([])(explore)(input),
425
+ undefined,
426
+ [],
427
+ );
428
+
429
+ explore = {
430
+ ...explore,
431
+ source: "function",
432
+ from: "array",
433
+ };
434
+ return ts.factory.createCallExpression(
435
+ ts.factory.createIdentifier(
436
+ importer.emplaceUnion(
437
+ config.prefix,
438
+ elements.map((e) => e.type.name).join(" | "),
439
+ () =>
440
+ arrow(
441
+ FeatureProgrammer.parameterDeclarations(config)(
442
+ TypeFactory.keyword("any"),
443
+ )(ts.factory.createIdentifier("input")),
444
+ )({
445
+ ...explore,
446
+ postfix: "",
447
+ })(ts.factory.createIdentifier("input")),
448
+ ),
449
+ ),
450
+ undefined,
451
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
452
+ );
453
+ };
454
+
455
+ // @todo -> must filter out recursive visit
456
+ const filter = (meta: Metadata): boolean =>
457
+ meta.any === false &&
458
+ (meta.objects.length !== 0 ||
459
+ meta.tuples.some(
460
+ (t) =>
461
+ !!t.type.elements.length &&
462
+ t.type.elements.some((e) => filter(e.rest ?? e)),
463
+ ) ||
464
+ meta.arrays.some((e) => filter(e.type.value)));
465
+
466
+ /* -----------------------------------------------------------
467
+ CONFIGURATIONS
468
+ ----------------------------------------------------------- */
469
+ const PREFIX = "$p";
470
+
471
+ const configure =
472
+ (project: IProject) =>
473
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
474
+ const config: FeatureProgrammer.IConfig = {
475
+ types: {
476
+ input: (type, name) =>
477
+ ts.factory.createTypeReferenceNode(
478
+ name ??
479
+ TypeFactory.getFullName(project.checker)(type),
480
+ ),
481
+ output: () => TypeFactory.keyword("void"),
482
+ },
483
+ prefix: PREFIX,
484
+ trace: false,
485
+ path: false,
486
+ initializer,
487
+ decoder: () => decode(project)(config)(importer),
488
+ objector: {
489
+ checker: () => IsProgrammer.decode(project)(importer),
490
+ decoder: () => decode_object(importer),
491
+ joiner: PruneJoiner.object,
492
+ unionizer: decode_union_object(
493
+ IsProgrammer.decode_object(importer),
494
+ )(decode_object(importer))((exp) => exp)(
495
+ (value, expected) =>
496
+ create_throw_error(importer)(expected)(value),
497
+ ),
498
+ failure: (input, expected) =>
499
+ create_throw_error(importer)(expected)(input),
500
+ },
501
+ generator: {
502
+ arrays: () => write_array_functions(config)(importer),
503
+ tuples: () =>
504
+ write_tuple_functions(project)(config)(importer),
505
+ },
506
+ };
507
+ return config;
508
+ };
509
+
510
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
511
+ ({ checker }) =>
512
+ (importer) =>
513
+ (type) => {
514
+ const collection = new MetadataCollection();
515
+ const result = MetadataFactory.analyze(checker)({
516
+ escape: false,
517
+ constant: true,
518
+ absorb: true,
519
+ })(collection)(type);
520
+ if (result.success === false)
521
+ throw TransformerError.from(`typia.misc.${importer.method}`)(
522
+ result.errors,
523
+ );
524
+ return [collection, result.data];
525
+ };
526
+
527
+ const create_throw_error =
528
+ (importer: FunctionImporter) =>
529
+ (expected: string) =>
530
+ (value: ts.Expression) =>
531
+ ts.factory.createExpressionStatement(
532
+ ts.factory.createCallExpression(
533
+ importer.use("throws"),
534
+ [],
535
+ [
536
+ ts.factory.createObjectLiteralExpression(
537
+ [
538
+ ts.factory.createPropertyAssignment(
539
+ "expected",
540
+ ts.factory.createStringLiteral(expected),
541
+ ),
542
+ ts.factory.createPropertyAssignment(
543
+ "value",
544
+ value,
545
+ ),
546
+ ],
547
+ true,
548
+ ),
549
+ ],
550
+ ),
551
+ );
552
+ }