typia 4.0.0 → 4.0.1-dev.20230604

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 (87) hide show
  1. package/lib/programmers/ApplicationProgrammer.js +1 -1
  2. package/lib/programmers/internal/application_array.js +19 -41
  3. package/lib/programmers/internal/application_array.js.map +1 -1
  4. package/lib/programmers/internal/application_definition.d.ts +1 -1
  5. package/lib/programmers/internal/application_definition.js +8 -8
  6. package/lib/programmers/internal/application_definition.js.map +1 -1
  7. package/lib/programmers/internal/application_native.js +3 -3
  8. package/lib/programmers/internal/application_object.js +4 -4
  9. package/lib/programmers/internal/application_schema.js +3 -3
  10. package/lib/programmers/internal/application_schema.js.map +1 -1
  11. package/lib/programmers/internal/application_tuple.js +8 -30
  12. package/lib/programmers/internal/application_tuple.js.map +1 -1
  13. package/lib/schemas/IJsonComponents.d.ts +1 -12
  14. package/lib/schemas/IJsonSchema.d.ts +1 -1
  15. package/package.json +1 -1
  16. package/src/IRandomGenerator.ts +34 -34
  17. package/src/factories/IdentifierFactory.ts +65 -65
  18. package/src/factories/MetadataCollection.ts +254 -254
  19. package/src/factories/MetadataFactory.ts +40 -40
  20. package/src/factories/MetadataTagFactory.ts +300 -300
  21. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  22. package/src/factories/internal/metadata/emplace_metadata_definition.ts +35 -35
  23. package/src/factories/internal/metadata/emplace_metadata_object.ts +142 -142
  24. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  25. package/src/factories/internal/metadata/explore_metadata.ts +66 -66
  26. package/src/factories/internal/metadata/iterate_metadata.ts +77 -77
  27. package/src/factories/internal/metadata/iterate_metadata_array.ts +25 -25
  28. package/src/factories/internal/metadata/iterate_metadata_collection.ts +130 -130
  29. package/src/factories/internal/metadata/iterate_metadata_definition.ts +30 -30
  30. package/src/factories/internal/metadata/iterate_metadata_object.ts +49 -49
  31. package/src/factories/internal/metadata/iterate_metadata_sort.ts +68 -68
  32. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  33. package/src/factories/internal/metadata/iterate_metadata_union.ts +68 -68
  34. package/src/metadata/IMetadata.ts +27 -27
  35. package/src/metadata/IMetadataAlias.ts +12 -12
  36. package/src/metadata/IMetadataApplication.ts +7 -7
  37. package/src/metadata/IMetadataArray.ts +10 -10
  38. package/src/metadata/IMetadataCollection.ts +11 -11
  39. package/src/metadata/IMetadataDictionary.ts +14 -14
  40. package/src/metadata/IMetadataObject.ts +18 -18
  41. package/src/metadata/IMetadataProperty.ts +11 -11
  42. package/src/metadata/IMetadataTuple.ts +10 -10
  43. package/src/metadata/Metadata.ts +585 -585
  44. package/src/metadata/MetadataAlias.ts +61 -61
  45. package/src/metadata/MetadataArray.ts +52 -52
  46. package/src/metadata/MetadataObject.ts +114 -114
  47. package/src/metadata/MetadataProperty.ts +64 -64
  48. package/src/metadata/MetadataTuple.ts +53 -53
  49. package/src/programmers/ApplicationProgrammer.ts +55 -55
  50. package/src/programmers/AssertProgrammer.ts +291 -291
  51. package/src/programmers/CheckerProgrammer.ts +1182 -1182
  52. package/src/programmers/CloneProgrammer.ts +595 -595
  53. package/src/programmers/FeatureProgrammer.ts +495 -495
  54. package/src/programmers/IsProgrammer.ts +250 -250
  55. package/src/programmers/LiteralsProgrammer.ts +66 -66
  56. package/src/programmers/PruneProgrammer.ts +550 -550
  57. package/src/programmers/RandomProgrammer.ts +589 -589
  58. package/src/programmers/StringifyProgrammer.ts +990 -990
  59. package/src/programmers/ValidateProgrammer.ts +313 -313
  60. package/src/programmers/helpers/FunctionImporeter.ts +78 -78
  61. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  62. package/src/programmers/helpers/UnionExplorer.ts +301 -301
  63. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  64. package/src/programmers/helpers/disable_function_importer_declare.ts +26 -26
  65. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  66. package/src/programmers/internal/application_array.ts +30 -64
  67. package/src/programmers/internal/application_boolean.ts +15 -15
  68. package/src/programmers/internal/application_constant.ts +26 -26
  69. package/src/programmers/internal/application_default.ts +17 -17
  70. package/src/programmers/internal/application_definition.ts +45 -45
  71. package/src/programmers/internal/application_native.ts +39 -39
  72. package/src/programmers/internal/application_number.ts +74 -74
  73. package/src/programmers/internal/application_object.ts +167 -167
  74. package/src/programmers/internal/application_schema.ts +156 -164
  75. package/src/programmers/internal/application_tuple.ts +47 -77
  76. package/src/programmers/internal/check_union_array_like.ts +329 -329
  77. package/src/programmers/internal/feature_object_entries.ts +63 -63
  78. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  79. package/src/schemas/IJsonApplication.ts +8 -8
  80. package/src/schemas/IJsonComponents.ts +34 -45
  81. package/src/schemas/IJsonSchema.ts +134 -134
  82. package/src/transformers/CallExpressionTransformer.ts +179 -179
  83. package/src/transformers/FileTransformer.ts +47 -47
  84. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +104 -104
  85. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +53 -53
  86. package/src/utils/ArrayUtil.ts +45 -45
  87. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,550 +1,550 @@
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 { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
11
- import { IMetadataTag } from "../metadata/IMetadataTag";
12
- import { Metadata } from "../metadata/Metadata";
13
- import { MetadataArray } from "../metadata/MetadataArray";
14
- import { MetadataTuple } from "../metadata/MetadataTuple";
15
-
16
- import { IProject } from "../transformers/IProject";
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 PruneProgrammer {
27
- /**
28
- * @deprecated Use `write()` function instead
29
- */
30
- export const generate =
31
- (project: IProject, modulo: ts.LeftHandSideExpression) =>
32
- (type: ts.Type, name?: string) =>
33
- write(project)(modulo)(type, name);
34
-
35
- export const write =
36
- (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
37
- const importer: FunctionImporter = new FunctionImporter();
38
- return FeatureProgrammer.write(project)({
39
- ...configure(project)(importer),
40
- addition: (collection) => [
41
- ...IsProgrammer.write_function_statements(project)(
42
- importer,
43
- )(collection),
44
- ...importer.declare(modulo),
45
- ],
46
- })(importer);
47
- };
48
-
49
- const write_array_functions =
50
- (config: FeatureProgrammer.IConfig) =>
51
- (importer: FunctionImporter) =>
52
- (collection: MetadataCollection): ts.VariableStatement[] =>
53
- collection
54
- .arrays()
55
- .filter((a) => a.recursive)
56
- .map((array, i) =>
57
- StatementFactory.constant(
58
- `${config.prefix}a${i}`,
59
- ts.factory.createArrowFunction(
60
- undefined,
61
- undefined,
62
- FeatureProgrammer.parameterDeclarations(config)(
63
- TypeFactory.keyword("any"),
64
- )(ts.factory.createIdentifier("input")),
65
- TypeFactory.keyword("any"),
66
- undefined,
67
- decode_array_inline(config)(importer)(
68
- ts.factory.createIdentifier("input"),
69
- array,
70
- {
71
- tracable: config.trace,
72
- source: "function",
73
- from: "array",
74
- postfix: "",
75
- },
76
- ),
77
- ),
78
- ),
79
- );
80
-
81
- const write_tuple_functions =
82
- (project: IProject) =>
83
- (config: FeatureProgrammer.IConfig) =>
84
- (importer: FunctionImporter) =>
85
- (collection: MetadataCollection): ts.VariableStatement[] =>
86
- collection
87
- .tuples()
88
- .filter((t) => t.recursive)
89
- .map((tuple, i) =>
90
- StatementFactory.constant(
91
- `${config.prefix}t${i}`,
92
- ts.factory.createArrowFunction(
93
- undefined,
94
- undefined,
95
- FeatureProgrammer.parameterDeclarations(config)(
96
- TypeFactory.keyword("any"),
97
- )(ts.factory.createIdentifier("input")),
98
- TypeFactory.keyword("any"),
99
- undefined,
100
- decode_tuple_inline(project)(config)(importer)(
101
- ts.factory.createIdentifier("input"),
102
- tuple,
103
- {
104
- tracable: config.trace,
105
- source: "function",
106
- from: "array",
107
- postfix: "",
108
- },
109
- ),
110
- ),
111
- ),
112
- );
113
-
114
- /* -----------------------------------------------------------
115
- DECODERS
116
- ----------------------------------------------------------- */
117
- const decode =
118
- (project: IProject) =>
119
- (config: FeatureProgrammer.IConfig) =>
120
- (importer: FunctionImporter) =>
121
- (
122
- input: ts.Expression,
123
- meta: Metadata,
124
- explore: FeatureProgrammer.IExplore,
125
- ): ts.ConciseBody => {
126
- if (filter(meta) === false) return ts.factory.createBlock([]);
127
-
128
- interface IUnion {
129
- type: string;
130
- is: () => ts.Expression;
131
- value: () => ts.Expression | ts.Block | ts.ReturnStatement;
132
- }
133
- const unions: IUnion[] = [];
134
-
135
- //----
136
- // LIST UP UNION TYPES
137
- //----
138
- // TUPLES
139
- for (const tuple of meta.tuples.filter((t) =>
140
- t.elements.some((e) => filter(e.rest ?? e)),
141
- ))
142
- unions.push({
143
- type: "tuple",
144
- is: () =>
145
- IsProgrammer.decode(project)(importer)(
146
- input,
147
- (() => {
148
- const partial = Metadata.initialize();
149
- partial.tuples.push(tuple);
150
- return partial;
151
- })(),
152
- explore,
153
- [],
154
- [],
155
- ),
156
- value: () =>
157
- decode_tuple(project)(config)(importer)(
158
- input,
159
- tuple,
160
- explore,
161
- ),
162
- });
163
-
164
- // ARRAYS
165
- if (meta.arrays.filter((a) => filter(a.value)).length)
166
- unions.push({
167
- type: "array",
168
- is: () => ExpressionFactory.isArray(input),
169
- value: () =>
170
- explore_arrays(project)(config)(importer)(
171
- input,
172
- meta.arrays,
173
- {
174
- ...explore,
175
- from: "array",
176
- },
177
- ),
178
- });
179
-
180
- // BUILT-IN CLASSES
181
- if (meta.natives.length)
182
- for (const native of meta.natives)
183
- unions.push({
184
- type: "native",
185
- is: () => ExpressionFactory.isInstanceOf(native)(input),
186
- value: () => ts.factory.createReturnStatement(),
187
- });
188
- if (meta.sets.length)
189
- unions.push({
190
- type: "set",
191
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
192
- value: () => ts.factory.createReturnStatement(),
193
- });
194
- if (meta.maps.length)
195
- unions.push({
196
- type: "map",
197
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
198
- value: () => ts.factory.createReturnStatement(),
199
- });
200
-
201
- // OBJECTS
202
- if (meta.objects.length)
203
- unions.push({
204
- type: "object",
205
- is: () =>
206
- ExpressionFactory.isObject({
207
- checkNull: true,
208
- checkArray: false,
209
- })(input),
210
- value: () =>
211
- explore_objects(config)(importer)(input, meta, {
212
- ...explore,
213
- from: "object",
214
- }),
215
- });
216
-
217
- //----
218
- // STATEMENTS
219
- //----
220
- const converter = (
221
- v: ts.Expression | ts.Block | ts.ReturnStatement,
222
- ) =>
223
- ts.isReturnStatement(v) || ts.isBlock(v)
224
- ? v
225
- : ts.factory.createExpressionStatement(v);
226
-
227
- const statements: ts.Statement[] = unions.map((u) =>
228
- ts.factory.createIfStatement(u.is(), converter(u.value())),
229
- );
230
- return ts.factory.createBlock(statements, true);
231
- };
232
-
233
- const decode_object = (importer: FunctionImporter) =>
234
- FeatureProgrammer.decode_object({
235
- trace: false,
236
- path: false,
237
- prefix: PREFIX,
238
- })(importer);
239
-
240
- const decode_array =
241
- (config: FeatureProgrammer.IConfig) =>
242
- (importer: FunctionImporter) =>
243
- (
244
- input: ts.Expression,
245
- array: MetadataArray,
246
- explore: FeatureProgrammer.IExplore,
247
- ) =>
248
- array.recursive
249
- ? ts.factory.createCallExpression(
250
- ts.factory.createIdentifier(
251
- importer.useLocal(`${config.prefix}a${array.index}`),
252
- ),
253
- undefined,
254
- FeatureProgrammer.argumentsArray(config)({
255
- ...explore,
256
- source: "function",
257
- from: "array",
258
- })(input),
259
- )
260
- : decode_array_inline(config)(importer)(input, array, explore);
261
-
262
- const decode_array_inline =
263
- (config: FeatureProgrammer.IConfig) =>
264
- (importer: FunctionImporter) =>
265
- (
266
- input: ts.Expression,
267
- array: MetadataArray,
268
- explore: FeatureProgrammer.IExplore,
269
- ): ts.Expression =>
270
- FeatureProgrammer.decode_array(config)(importer)(PruneJoiner.array)(
271
- input,
272
- array,
273
- explore,
274
- [],
275
- [],
276
- );
277
-
278
- const decode_tuple =
279
- (project: IProject) =>
280
- (config: FeatureProgrammer.IConfig) =>
281
- (importer: FunctionImporter) =>
282
- (
283
- input: ts.Expression,
284
- tuple: MetadataTuple,
285
- explore: FeatureProgrammer.IExplore,
286
- ): ts.Expression | ts.Block =>
287
- tuple.recursive
288
- ? ts.factory.createCallExpression(
289
- ts.factory.createIdentifier(
290
- importer.useLocal(`${config.prefix}t${tuple.index}`),
291
- ),
292
- undefined,
293
- FeatureProgrammer.argumentsArray(config)({
294
- ...explore,
295
- source: "function",
296
- })(input),
297
- )
298
- : decode_tuple_inline(project)(config)(importer)(
299
- input,
300
- tuple,
301
- explore,
302
- );
303
-
304
- const decode_tuple_inline =
305
- (project: IProject) =>
306
- (config: FeatureProgrammer.IConfig) =>
307
- (importer: FunctionImporter) =>
308
- (
309
- input: ts.Expression,
310
- tuple: MetadataTuple,
311
- explore: FeatureProgrammer.IExplore,
312
- ): ts.Block => {
313
- const children: ts.ConciseBody[] = tuple.elements
314
- .map((elem, index) => [elem, index] as const)
315
- .filter(([elem]) => filter(elem) && elem.rest === null)
316
- .map(([elem, index]) =>
317
- decode(project)(config)(importer)(
318
- ts.factory.createElementAccessExpression(input, index),
319
- elem,
320
- {
321
- ...explore,
322
- from: "array",
323
- postfix: explore.postfix.length
324
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
325
- : `"[${index}]"`,
326
- },
327
- ),
328
- );
329
- const rest = (() => {
330
- if (tuple.elements.length === 0) return null;
331
-
332
- const last: Metadata = tuple.elements.at(-1)!;
333
- const rest: Metadata | null = last.rest;
334
- if (rest === null || filter(rest) === false) return null;
335
-
336
- return decode(project)(config)(importer)(
337
- ts.factory.createCallExpression(
338
- IdentifierFactory.access(input)("slice"),
339
- undefined,
340
- [
341
- ts.factory.createNumericLiteral(
342
- tuple.elements.length - 1,
343
- ),
344
- ],
345
- ),
346
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
347
- {
348
- ...explore,
349
- start: tuple.elements.length - 1,
350
- },
351
- );
352
- })();
353
- return PruneJoiner.tuple(children, rest);
354
- };
355
-
356
- /* -----------------------------------------------------------
357
- UNION TYPE EXPLORERS
358
- ----------------------------------------------------------- */
359
- const explore_objects =
360
- (config: FeatureProgrammer.IConfig) =>
361
- (importer: FunctionImporter) =>
362
- (
363
- input: ts.Expression,
364
- meta: Metadata,
365
- explore: FeatureProgrammer.IExplore,
366
- ) => {
367
- if (meta.objects.length === 1)
368
- return decode_object(importer)(
369
- input,
370
- meta.objects[0]!,
371
- explore,
372
- );
373
-
374
- return ts.factory.createCallExpression(
375
- ts.factory.createIdentifier(
376
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
377
- ),
378
- undefined,
379
- FeatureProgrammer.argumentsArray(config)(explore)(input),
380
- );
381
- };
382
-
383
- const explore_arrays =
384
- (project: IProject) =>
385
- (config: FeatureProgrammer.IConfig) =>
386
- (importer: FunctionImporter) =>
387
- (
388
- input: ts.Expression,
389
- elements: MetadataArray[],
390
- explore: FeatureProgrammer.IExplore,
391
- ): ts.Expression =>
392
- explore_array_like_union_types(config)(importer)(
393
- UnionExplorer.array({
394
- checker: IsProgrammer.decode(project)(importer),
395
- decoder: decode_array(config)(importer),
396
- empty: ts.factory.createStringLiteral("[]"),
397
- success: ts.factory.createTrue(),
398
- failure: (input, expected) =>
399
- create_throw_error(importer)(expected)(input),
400
- }),
401
- )(input, elements, explore);
402
-
403
- const explore_array_like_union_types =
404
- (config: FeatureProgrammer.IConfig) =>
405
- (importer: FunctionImporter) =>
406
- <T extends MetadataArray | MetadataTuple>(
407
- factory: (
408
- parameters: ts.ParameterDeclaration[],
409
- ) => (
410
- input: ts.Expression,
411
- elements: T[],
412
- explore: FeatureProgrammer.IExplore,
413
- tags: IMetadataTag[],
414
- jsDocTags: IJsDocTagInfo[],
415
- ) => ts.ArrowFunction,
416
- ) =>
417
- (
418
- input: ts.Expression,
419
- elements: T[],
420
- explore: FeatureProgrammer.IExplore,
421
- ): ts.Expression => {
422
- const arrow =
423
- (parameters: ts.ParameterDeclaration[]) =>
424
- (explore: FeatureProgrammer.IExplore) =>
425
- (input: ts.Expression): ts.ArrowFunction =>
426
- factory(parameters)(input, elements, explore, [], []);
427
- if (elements.every((e) => e.recursive === false))
428
- ts.factory.createCallExpression(
429
- arrow([])(explore)(input),
430
- undefined,
431
- [],
432
- );
433
-
434
- explore = {
435
- ...explore,
436
- source: "function",
437
- from: "array",
438
- };
439
- return ts.factory.createCallExpression(
440
- ts.factory.createIdentifier(
441
- importer.emplaceUnion(
442
- config.prefix,
443
- elements.map((e) => e.name).join(" | "),
444
- () =>
445
- arrow(
446
- FeatureProgrammer.parameterDeclarations(config)(
447
- TypeFactory.keyword("any"),
448
- )(ts.factory.createIdentifier("input")),
449
- )({
450
- ...explore,
451
- postfix: "",
452
- })(ts.factory.createIdentifier("input")),
453
- ),
454
- ),
455
- undefined,
456
- FeatureProgrammer.argumentsArray(config)(explore)(input),
457
- );
458
- };
459
-
460
- // @todo -> must filter out recursive visit
461
- const filter = (meta: Metadata): boolean =>
462
- meta.any === false &&
463
- (meta.objects.length !== 0 ||
464
- meta.tuples.some((t) =>
465
- t.elements.some((e) => filter(e.rest ?? e)),
466
- ) ||
467
- meta.arrays.some((e) => filter(e.value)));
468
-
469
- /* -----------------------------------------------------------
470
- CONFIGURATIONS
471
- ----------------------------------------------------------- */
472
- const PREFIX = "$p";
473
-
474
- const configure =
475
- (project: IProject) =>
476
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
477
- const config: FeatureProgrammer.IConfig = {
478
- types: {
479
- input: (type, name) =>
480
- ts.factory.createTypeReferenceNode(
481
- name ??
482
- TypeFactory.getFullName(project.checker)(type),
483
- ),
484
- output: () => TypeFactory.keyword("void"),
485
- },
486
- prefix: PREFIX,
487
- trace: false,
488
- path: false,
489
- initializer,
490
- decoder: () => decode(project)(config)(importer),
491
- objector: {
492
- checker: () => IsProgrammer.decode(project)(importer),
493
- decoder: () => decode_object(importer),
494
- joiner: PruneJoiner.object,
495
- unionizer: decode_union_object(
496
- IsProgrammer.decode_object(importer),
497
- )(decode_object(importer))((exp) => exp)(
498
- (value, expected) =>
499
- create_throw_error(importer)(expected)(value),
500
- ),
501
- failure: (input, expected) =>
502
- create_throw_error(importer)(expected)(input),
503
- },
504
- generator: {
505
- arrays: () => write_array_functions(config)(importer),
506
- tuples: () =>
507
- write_tuple_functions(project)(config)(importer),
508
- },
509
- };
510
- return config;
511
- };
512
-
513
- const initializer: FeatureProgrammer.IConfig["initializer"] =
514
- ({ checker }) =>
515
- (type) => {
516
- const collection = new MetadataCollection();
517
- const meta = MetadataFactory.analyze(checker)({
518
- resolve: false,
519
- constant: true,
520
- absorb: true,
521
- })(collection)(type);
522
- return [collection, meta];
523
- };
524
-
525
- const create_throw_error =
526
- (importer: FunctionImporter) =>
527
- (expected: string) =>
528
- (value: ts.Expression) =>
529
- ts.factory.createExpressionStatement(
530
- ts.factory.createCallExpression(
531
- importer.use("throws"),
532
- [],
533
- [
534
- ts.factory.createObjectLiteralExpression(
535
- [
536
- ts.factory.createPropertyAssignment(
537
- "expected",
538
- ts.factory.createStringLiteral(expected),
539
- ),
540
- ts.factory.createPropertyAssignment(
541
- "value",
542
- value,
543
- ),
544
- ],
545
- true,
546
- ),
547
- ],
548
- ),
549
- );
550
- }
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 { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
11
+ import { IMetadataTag } from "../metadata/IMetadataTag";
12
+ import { Metadata } from "../metadata/Metadata";
13
+ import { MetadataArray } from "../metadata/MetadataArray";
14
+ import { MetadataTuple } from "../metadata/MetadataTuple";
15
+
16
+ import { IProject } from "../transformers/IProject";
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 PruneProgrammer {
27
+ /**
28
+ * @deprecated Use `write()` function instead
29
+ */
30
+ export const generate =
31
+ (project: IProject, modulo: ts.LeftHandSideExpression) =>
32
+ (type: ts.Type, name?: string) =>
33
+ write(project)(modulo)(type, name);
34
+
35
+ export const write =
36
+ (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
37
+ const importer: FunctionImporter = new FunctionImporter();
38
+ return FeatureProgrammer.write(project)({
39
+ ...configure(project)(importer),
40
+ addition: (collection) => [
41
+ ...IsProgrammer.write_function_statements(project)(
42
+ importer,
43
+ )(collection),
44
+ ...importer.declare(modulo),
45
+ ],
46
+ })(importer);
47
+ };
48
+
49
+ const write_array_functions =
50
+ (config: FeatureProgrammer.IConfig) =>
51
+ (importer: FunctionImporter) =>
52
+ (collection: MetadataCollection): ts.VariableStatement[] =>
53
+ collection
54
+ .arrays()
55
+ .filter((a) => a.recursive)
56
+ .map((array, i) =>
57
+ StatementFactory.constant(
58
+ `${config.prefix}a${i}`,
59
+ ts.factory.createArrowFunction(
60
+ undefined,
61
+ undefined,
62
+ FeatureProgrammer.parameterDeclarations(config)(
63
+ TypeFactory.keyword("any"),
64
+ )(ts.factory.createIdentifier("input")),
65
+ TypeFactory.keyword("any"),
66
+ undefined,
67
+ decode_array_inline(config)(importer)(
68
+ ts.factory.createIdentifier("input"),
69
+ array,
70
+ {
71
+ tracable: config.trace,
72
+ source: "function",
73
+ from: "array",
74
+ postfix: "",
75
+ },
76
+ ),
77
+ ),
78
+ ),
79
+ );
80
+
81
+ const write_tuple_functions =
82
+ (project: IProject) =>
83
+ (config: FeatureProgrammer.IConfig) =>
84
+ (importer: FunctionImporter) =>
85
+ (collection: MetadataCollection): ts.VariableStatement[] =>
86
+ collection
87
+ .tuples()
88
+ .filter((t) => t.recursive)
89
+ .map((tuple, i) =>
90
+ StatementFactory.constant(
91
+ `${config.prefix}t${i}`,
92
+ ts.factory.createArrowFunction(
93
+ undefined,
94
+ undefined,
95
+ FeatureProgrammer.parameterDeclarations(config)(
96
+ TypeFactory.keyword("any"),
97
+ )(ts.factory.createIdentifier("input")),
98
+ TypeFactory.keyword("any"),
99
+ undefined,
100
+ decode_tuple_inline(project)(config)(importer)(
101
+ ts.factory.createIdentifier("input"),
102
+ tuple,
103
+ {
104
+ tracable: config.trace,
105
+ source: "function",
106
+ from: "array",
107
+ postfix: "",
108
+ },
109
+ ),
110
+ ),
111
+ ),
112
+ );
113
+
114
+ /* -----------------------------------------------------------
115
+ DECODERS
116
+ ----------------------------------------------------------- */
117
+ const decode =
118
+ (project: IProject) =>
119
+ (config: FeatureProgrammer.IConfig) =>
120
+ (importer: FunctionImporter) =>
121
+ (
122
+ input: ts.Expression,
123
+ meta: Metadata,
124
+ explore: FeatureProgrammer.IExplore,
125
+ ): ts.ConciseBody => {
126
+ if (filter(meta) === false) return ts.factory.createBlock([]);
127
+
128
+ interface IUnion {
129
+ type: string;
130
+ is: () => ts.Expression;
131
+ value: () => ts.Expression | ts.Block | ts.ReturnStatement;
132
+ }
133
+ const unions: IUnion[] = [];
134
+
135
+ //----
136
+ // LIST UP UNION TYPES
137
+ //----
138
+ // TUPLES
139
+ for (const tuple of meta.tuples.filter((t) =>
140
+ t.elements.some((e) => filter(e.rest ?? e)),
141
+ ))
142
+ unions.push({
143
+ type: "tuple",
144
+ is: () =>
145
+ IsProgrammer.decode(project)(importer)(
146
+ input,
147
+ (() => {
148
+ const partial = Metadata.initialize();
149
+ partial.tuples.push(tuple);
150
+ return partial;
151
+ })(),
152
+ explore,
153
+ [],
154
+ [],
155
+ ),
156
+ value: () =>
157
+ decode_tuple(project)(config)(importer)(
158
+ input,
159
+ tuple,
160
+ explore,
161
+ ),
162
+ });
163
+
164
+ // ARRAYS
165
+ if (meta.arrays.filter((a) => filter(a.value)).length)
166
+ unions.push({
167
+ type: "array",
168
+ is: () => ExpressionFactory.isArray(input),
169
+ value: () =>
170
+ explore_arrays(project)(config)(importer)(
171
+ input,
172
+ meta.arrays,
173
+ {
174
+ ...explore,
175
+ from: "array",
176
+ },
177
+ ),
178
+ });
179
+
180
+ // BUILT-IN CLASSES
181
+ if (meta.natives.length)
182
+ for (const native of meta.natives)
183
+ unions.push({
184
+ type: "native",
185
+ is: () => ExpressionFactory.isInstanceOf(native)(input),
186
+ value: () => ts.factory.createReturnStatement(),
187
+ });
188
+ if (meta.sets.length)
189
+ unions.push({
190
+ type: "set",
191
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
192
+ value: () => ts.factory.createReturnStatement(),
193
+ });
194
+ if (meta.maps.length)
195
+ unions.push({
196
+ type: "map",
197
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
198
+ value: () => ts.factory.createReturnStatement(),
199
+ });
200
+
201
+ // OBJECTS
202
+ if (meta.objects.length)
203
+ unions.push({
204
+ type: "object",
205
+ is: () =>
206
+ ExpressionFactory.isObject({
207
+ checkNull: true,
208
+ checkArray: false,
209
+ })(input),
210
+ value: () =>
211
+ explore_objects(config)(importer)(input, meta, {
212
+ ...explore,
213
+ from: "object",
214
+ }),
215
+ });
216
+
217
+ //----
218
+ // STATEMENTS
219
+ //----
220
+ const converter = (
221
+ v: ts.Expression | ts.Block | ts.ReturnStatement,
222
+ ) =>
223
+ ts.isReturnStatement(v) || ts.isBlock(v)
224
+ ? v
225
+ : ts.factory.createExpressionStatement(v);
226
+
227
+ const statements: ts.Statement[] = unions.map((u) =>
228
+ ts.factory.createIfStatement(u.is(), converter(u.value())),
229
+ );
230
+ return ts.factory.createBlock(statements, true);
231
+ };
232
+
233
+ const decode_object = (importer: FunctionImporter) =>
234
+ FeatureProgrammer.decode_object({
235
+ trace: false,
236
+ path: false,
237
+ prefix: PREFIX,
238
+ })(importer);
239
+
240
+ const decode_array =
241
+ (config: FeatureProgrammer.IConfig) =>
242
+ (importer: FunctionImporter) =>
243
+ (
244
+ input: ts.Expression,
245
+ array: MetadataArray,
246
+ explore: FeatureProgrammer.IExplore,
247
+ ) =>
248
+ array.recursive
249
+ ? ts.factory.createCallExpression(
250
+ ts.factory.createIdentifier(
251
+ importer.useLocal(`${config.prefix}a${array.index}`),
252
+ ),
253
+ undefined,
254
+ FeatureProgrammer.argumentsArray(config)({
255
+ ...explore,
256
+ source: "function",
257
+ from: "array",
258
+ })(input),
259
+ )
260
+ : decode_array_inline(config)(importer)(input, array, explore);
261
+
262
+ const decode_array_inline =
263
+ (config: FeatureProgrammer.IConfig) =>
264
+ (importer: FunctionImporter) =>
265
+ (
266
+ input: ts.Expression,
267
+ array: MetadataArray,
268
+ explore: FeatureProgrammer.IExplore,
269
+ ): ts.Expression =>
270
+ FeatureProgrammer.decode_array(config)(importer)(PruneJoiner.array)(
271
+ input,
272
+ array,
273
+ explore,
274
+ [],
275
+ [],
276
+ );
277
+
278
+ const decode_tuple =
279
+ (project: IProject) =>
280
+ (config: FeatureProgrammer.IConfig) =>
281
+ (importer: FunctionImporter) =>
282
+ (
283
+ input: ts.Expression,
284
+ tuple: MetadataTuple,
285
+ explore: FeatureProgrammer.IExplore,
286
+ ): ts.Expression | ts.Block =>
287
+ tuple.recursive
288
+ ? ts.factory.createCallExpression(
289
+ ts.factory.createIdentifier(
290
+ importer.useLocal(`${config.prefix}t${tuple.index}`),
291
+ ),
292
+ undefined,
293
+ FeatureProgrammer.argumentsArray(config)({
294
+ ...explore,
295
+ source: "function",
296
+ })(input),
297
+ )
298
+ : decode_tuple_inline(project)(config)(importer)(
299
+ input,
300
+ tuple,
301
+ explore,
302
+ );
303
+
304
+ const decode_tuple_inline =
305
+ (project: IProject) =>
306
+ (config: FeatureProgrammer.IConfig) =>
307
+ (importer: FunctionImporter) =>
308
+ (
309
+ input: ts.Expression,
310
+ tuple: MetadataTuple,
311
+ explore: FeatureProgrammer.IExplore,
312
+ ): ts.Block => {
313
+ const children: ts.ConciseBody[] = tuple.elements
314
+ .map((elem, index) => [elem, index] as const)
315
+ .filter(([elem]) => filter(elem) && elem.rest === null)
316
+ .map(([elem, index]) =>
317
+ decode(project)(config)(importer)(
318
+ ts.factory.createElementAccessExpression(input, index),
319
+ elem,
320
+ {
321
+ ...explore,
322
+ from: "array",
323
+ postfix: explore.postfix.length
324
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
325
+ : `"[${index}]"`,
326
+ },
327
+ ),
328
+ );
329
+ const rest = (() => {
330
+ if (tuple.elements.length === 0) return null;
331
+
332
+ const last: Metadata = tuple.elements.at(-1)!;
333
+ const rest: Metadata | null = last.rest;
334
+ if (rest === null || filter(rest) === false) return null;
335
+
336
+ return decode(project)(config)(importer)(
337
+ ts.factory.createCallExpression(
338
+ IdentifierFactory.access(input)("slice"),
339
+ undefined,
340
+ [
341
+ ts.factory.createNumericLiteral(
342
+ tuple.elements.length - 1,
343
+ ),
344
+ ],
345
+ ),
346
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
347
+ {
348
+ ...explore,
349
+ start: tuple.elements.length - 1,
350
+ },
351
+ );
352
+ })();
353
+ return PruneJoiner.tuple(children, rest);
354
+ };
355
+
356
+ /* -----------------------------------------------------------
357
+ UNION TYPE EXPLORERS
358
+ ----------------------------------------------------------- */
359
+ const explore_objects =
360
+ (config: FeatureProgrammer.IConfig) =>
361
+ (importer: FunctionImporter) =>
362
+ (
363
+ input: ts.Expression,
364
+ meta: Metadata,
365
+ explore: FeatureProgrammer.IExplore,
366
+ ) => {
367
+ if (meta.objects.length === 1)
368
+ return decode_object(importer)(
369
+ input,
370
+ meta.objects[0]!,
371
+ explore,
372
+ );
373
+
374
+ return ts.factory.createCallExpression(
375
+ ts.factory.createIdentifier(
376
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
377
+ ),
378
+ undefined,
379
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
380
+ );
381
+ };
382
+
383
+ const explore_arrays =
384
+ (project: IProject) =>
385
+ (config: FeatureProgrammer.IConfig) =>
386
+ (importer: FunctionImporter) =>
387
+ (
388
+ input: ts.Expression,
389
+ elements: MetadataArray[],
390
+ explore: FeatureProgrammer.IExplore,
391
+ ): ts.Expression =>
392
+ explore_array_like_union_types(config)(importer)(
393
+ UnionExplorer.array({
394
+ checker: IsProgrammer.decode(project)(importer),
395
+ decoder: decode_array(config)(importer),
396
+ empty: ts.factory.createStringLiteral("[]"),
397
+ success: ts.factory.createTrue(),
398
+ failure: (input, expected) =>
399
+ create_throw_error(importer)(expected)(input),
400
+ }),
401
+ )(input, elements, explore);
402
+
403
+ const explore_array_like_union_types =
404
+ (config: FeatureProgrammer.IConfig) =>
405
+ (importer: FunctionImporter) =>
406
+ <T extends MetadataArray | MetadataTuple>(
407
+ factory: (
408
+ parameters: ts.ParameterDeclaration[],
409
+ ) => (
410
+ input: ts.Expression,
411
+ elements: T[],
412
+ explore: FeatureProgrammer.IExplore,
413
+ tags: IMetadataTag[],
414
+ jsDocTags: IJsDocTagInfo[],
415
+ ) => ts.ArrowFunction,
416
+ ) =>
417
+ (
418
+ input: ts.Expression,
419
+ elements: T[],
420
+ explore: FeatureProgrammer.IExplore,
421
+ ): ts.Expression => {
422
+ const arrow =
423
+ (parameters: ts.ParameterDeclaration[]) =>
424
+ (explore: FeatureProgrammer.IExplore) =>
425
+ (input: ts.Expression): ts.ArrowFunction =>
426
+ factory(parameters)(input, elements, explore, [], []);
427
+ if (elements.every((e) => e.recursive === false))
428
+ ts.factory.createCallExpression(
429
+ arrow([])(explore)(input),
430
+ undefined,
431
+ [],
432
+ );
433
+
434
+ explore = {
435
+ ...explore,
436
+ source: "function",
437
+ from: "array",
438
+ };
439
+ return ts.factory.createCallExpression(
440
+ ts.factory.createIdentifier(
441
+ importer.emplaceUnion(
442
+ config.prefix,
443
+ elements.map((e) => e.name).join(" | "),
444
+ () =>
445
+ arrow(
446
+ FeatureProgrammer.parameterDeclarations(config)(
447
+ TypeFactory.keyword("any"),
448
+ )(ts.factory.createIdentifier("input")),
449
+ )({
450
+ ...explore,
451
+ postfix: "",
452
+ })(ts.factory.createIdentifier("input")),
453
+ ),
454
+ ),
455
+ undefined,
456
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
457
+ );
458
+ };
459
+
460
+ // @todo -> must filter out recursive visit
461
+ const filter = (meta: Metadata): boolean =>
462
+ meta.any === false &&
463
+ (meta.objects.length !== 0 ||
464
+ meta.tuples.some((t) =>
465
+ t.elements.some((e) => filter(e.rest ?? e)),
466
+ ) ||
467
+ meta.arrays.some((e) => filter(e.value)));
468
+
469
+ /* -----------------------------------------------------------
470
+ CONFIGURATIONS
471
+ ----------------------------------------------------------- */
472
+ const PREFIX = "$p";
473
+
474
+ const configure =
475
+ (project: IProject) =>
476
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
477
+ const config: FeatureProgrammer.IConfig = {
478
+ types: {
479
+ input: (type, name) =>
480
+ ts.factory.createTypeReferenceNode(
481
+ name ??
482
+ TypeFactory.getFullName(project.checker)(type),
483
+ ),
484
+ output: () => TypeFactory.keyword("void"),
485
+ },
486
+ prefix: PREFIX,
487
+ trace: false,
488
+ path: false,
489
+ initializer,
490
+ decoder: () => decode(project)(config)(importer),
491
+ objector: {
492
+ checker: () => IsProgrammer.decode(project)(importer),
493
+ decoder: () => decode_object(importer),
494
+ joiner: PruneJoiner.object,
495
+ unionizer: decode_union_object(
496
+ IsProgrammer.decode_object(importer),
497
+ )(decode_object(importer))((exp) => exp)(
498
+ (value, expected) =>
499
+ create_throw_error(importer)(expected)(value),
500
+ ),
501
+ failure: (input, expected) =>
502
+ create_throw_error(importer)(expected)(input),
503
+ },
504
+ generator: {
505
+ arrays: () => write_array_functions(config)(importer),
506
+ tuples: () =>
507
+ write_tuple_functions(project)(config)(importer),
508
+ },
509
+ };
510
+ return config;
511
+ };
512
+
513
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
514
+ ({ checker }) =>
515
+ (type) => {
516
+ const collection = new MetadataCollection();
517
+ const meta = MetadataFactory.analyze(checker)({
518
+ resolve: false,
519
+ constant: true,
520
+ absorb: true,
521
+ })(collection)(type);
522
+ return [collection, meta];
523
+ };
524
+
525
+ const create_throw_error =
526
+ (importer: FunctionImporter) =>
527
+ (expected: string) =>
528
+ (value: ts.Expression) =>
529
+ ts.factory.createExpressionStatement(
530
+ ts.factory.createCallExpression(
531
+ importer.use("throws"),
532
+ [],
533
+ [
534
+ ts.factory.createObjectLiteralExpression(
535
+ [
536
+ ts.factory.createPropertyAssignment(
537
+ "expected",
538
+ ts.factory.createStringLiteral(expected),
539
+ ),
540
+ ts.factory.createPropertyAssignment(
541
+ "value",
542
+ value,
543
+ ),
544
+ ],
545
+ true,
546
+ ),
547
+ ],
548
+ ),
549
+ );
550
+ }