typia 4.0.0 → 4.0.1

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,1182 +1,1182 @@
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
- import { ValueFactory } from "../factories/ValueFactory";
10
-
11
- import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
12
- import { IMetadataTag } from "../metadata/IMetadataTag";
13
- import { Metadata } from "../metadata/Metadata";
14
- import { MetadataArray } from "../metadata/MetadataArray";
15
- import { MetadataObject } from "../metadata/MetadataObject";
16
- import { MetadataTuple } from "../metadata/MetadataTuple";
17
-
18
- import { IProject } from "../transformers/IProject";
19
-
20
- import { FeatureProgrammer } from "./FeatureProgrammer";
21
- import { AtomicPredicator } from "./helpers/AtomicPredicator";
22
- import { FunctionImporter } from "./helpers/FunctionImporeter";
23
- import { ICheckEntry } from "./helpers/ICheckEntry";
24
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
25
- import { OptionPredicator } from "./helpers/OptionPredicator";
26
- import { UnionExplorer } from "./helpers/UnionExplorer";
27
- import { check_array } from "./internal/check_array";
28
- import { check_array_length } from "./internal/check_array_length";
29
- import { check_bigint } from "./internal/check_bigint";
30
- import { check_native } from "./internal/check_native";
31
- import { check_number } from "./internal/check_number";
32
- import { check_string } from "./internal/check_string";
33
- import { check_template } from "./internal/check_template";
34
- import { decode_union_object } from "./internal/decode_union_object";
35
- import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
36
-
37
- export namespace CheckerProgrammer {
38
- export interface IConfig {
39
- prefix: string;
40
- path: boolean;
41
- trace: boolean;
42
- equals: boolean;
43
- numeric: boolean;
44
- addition?: () => ts.Statement[];
45
- decoder?: () => FeatureProgrammer.Decoder<Metadata, ts.Expression>;
46
- combiner: IConfig.Combiner;
47
- atomist: (
48
- explore: IExplore,
49
- ) => (check: ICheckEntry) => (input: ts.Expression) => ts.Expression;
50
- joiner: IConfig.IJoiner;
51
- success: ts.Expression;
52
- }
53
- export namespace IConfig {
54
- export interface Combiner {
55
- (explorer: IExplore): {
56
- (logic: "and" | "or"): {
57
- (
58
- input: ts.Expression,
59
- binaries: IBinary[],
60
- expected: string,
61
- ): ts.Expression;
62
- };
63
- };
64
- }
65
- export interface IJoiner {
66
- object(
67
- input: ts.Expression,
68
- entries: IExpressionEntry[],
69
- ): ts.Expression;
70
- array(input: ts.Expression, arrow: ts.ArrowFunction): ts.Expression;
71
- tuple?(exprs: ts.Expression[]): ts.Expression;
72
-
73
- failure(
74
- value: ts.Expression,
75
- expected: string,
76
- explore?: FeatureProgrammer.IExplore,
77
- ): ts.Expression;
78
- is?(expression: ts.Expression): ts.Expression;
79
- required?(exp: ts.Expression): ts.Expression;
80
- full?: (
81
- condition: ts.Expression,
82
- ) => (
83
- input: ts.Expression,
84
- expected: string,
85
- explore: IExplore,
86
- ) => ts.Expression;
87
- }
88
- }
89
- export type IExplore = FeatureProgrammer.IExplore;
90
-
91
- export interface IBinary {
92
- expression: ts.Expression;
93
- combined: boolean;
94
- }
95
-
96
- /* -----------------------------------------------------------
97
- WRITERS
98
- ----------------------------------------------------------- */
99
- export const write =
100
- (project: IProject) =>
101
- (config: IConfig) =>
102
- (importer: FunctionImporter) =>
103
- FeatureProgrammer.write(project)(
104
- configure(project)(config)(importer),
105
- )(importer);
106
-
107
- export const write_object_functions =
108
- (project: IProject) =>
109
- (config: IConfig) =>
110
- (importer: FunctionImporter) =>
111
- FeatureProgrammer.write_object_functions(
112
- configure(project)(config)(importer),
113
- )(importer);
114
-
115
- export const write_union_functions =
116
- (project: IProject) =>
117
- (config: IConfig) =>
118
- (importer: FunctionImporter) =>
119
- FeatureProgrammer.write_union_functions(
120
- configure(project)({ ...config, numeric: false })(importer),
121
- );
122
-
123
- export const write_array_functions =
124
- (project: IProject) =>
125
- (config: IConfig) =>
126
- (importer: FunctionImporter) =>
127
- (collection: MetadataCollection): ts.VariableStatement[] =>
128
- collection
129
- .arrays()
130
- .filter((a) => a.recursive)
131
- .map((array, i) =>
132
- StatementFactory.constant(
133
- `${config.prefix}a${i}`,
134
- ts.factory.createArrowFunction(
135
- undefined,
136
- undefined,
137
- FeatureProgrammer.parameterDeclarations(config)(
138
- TypeFactory.keyword("any"),
139
- )(ts.factory.createIdentifier("input")),
140
- TypeFactory.keyword("any"),
141
- undefined,
142
- decode_array_inline(project)(config)(importer)(
143
- ts.factory.createIdentifier("input"),
144
- array,
145
- {
146
- tracable: config.trace,
147
- source: "function",
148
- from: "array",
149
- postfix: "",
150
- },
151
- [],
152
- [],
153
- ),
154
- ),
155
- ),
156
- );
157
-
158
- export const write_tuple_functions =
159
- (project: IProject) =>
160
- (config: IConfig) =>
161
- (importer: FunctionImporter) =>
162
- (collection: MetadataCollection): ts.VariableStatement[] =>
163
- collection
164
- .tuples()
165
- .filter((t) => t.recursive)
166
- .map((tuple, i) =>
167
- StatementFactory.constant(
168
- `${config.prefix}t${i}`,
169
- ts.factory.createArrowFunction(
170
- undefined,
171
- undefined,
172
- FeatureProgrammer.parameterDeclarations(config)(
173
- TypeFactory.keyword("any"),
174
- )(ts.factory.createIdentifier("input")),
175
- TypeFactory.keyword("any"),
176
- undefined,
177
- decode_tuple_inline(project)(config)(importer)(
178
- ts.factory.createIdentifier("input"),
179
- tuple,
180
- {
181
- tracable: config.trace,
182
- source: "function",
183
- from: "array",
184
- postfix: "",
185
- },
186
- [],
187
- [],
188
- ),
189
- ),
190
- ),
191
- );
192
-
193
- const configure =
194
- (project: IProject) =>
195
- (config: IConfig) =>
196
- (importer: FunctionImporter): FeatureProgrammer.IConfig => ({
197
- types: {
198
- input: () => TypeFactory.keyword("any"),
199
- output: (type, name) =>
200
- ts.factory.createTypePredicateNode(
201
- undefined,
202
- "input",
203
- ts.factory.createTypeReferenceNode(
204
- name ??
205
- TypeFactory.getFullName(project.checker)(type),
206
- ),
207
- ),
208
- },
209
- trace: config.trace,
210
- path: config.path,
211
- prefix: config.prefix,
212
- initializer:
213
- ({ checker }) =>
214
- (type) => {
215
- const collection: MetadataCollection =
216
- new MetadataCollection();
217
- const meta: Metadata = MetadataFactory.analyze(checker)({
218
- resolve: false,
219
- constant: true,
220
- absorb: true,
221
- validate: (meta) => {
222
- if (
223
- meta.arrays.length > 1 &&
224
- meta.arrays.some((a) => a.recursive)
225
- )
226
- throw new Error(
227
- `Repeated union array types are not supported yet.`,
228
- );
229
- },
230
- })(collection)(type);
231
- return [collection, meta];
232
- },
233
- addition: config.addition,
234
- decoder: () =>
235
- config.decoder?.() ?? decode(project)(config)(importer),
236
- objector: {
237
- checker: () =>
238
- config.decoder?.() ?? decode(project)(config)(importer),
239
- decoder: () => decode_object(config)(importer),
240
- joiner: config.joiner.object,
241
- unionizer: config.equals
242
- ? decode_union_object(decode_object(config)(importer))(
243
- (input, obj, explore) =>
244
- decode_object(config)(importer)(input, obj, {
245
- ...explore,
246
- tracable: true,
247
- }),
248
- )(config.joiner.is ?? ((expr) => expr))(
249
- (value, expected) =>
250
- ts.factory.createReturnStatement(
251
- config.joiner.failure(value, expected),
252
- ),
253
- )
254
- : (input, targets, explore) =>
255
- config.combiner(explore)("or")(
256
- input,
257
- targets.map((obj) => ({
258
- expression: decode_object(config)(importer)(
259
- input,
260
- obj,
261
- explore,
262
- ),
263
- combined: true,
264
- })),
265
- `(${targets.map((t) => t.name).join(" | ")})`,
266
- ),
267
- failure: (value, expected) =>
268
- ts.factory.createReturnStatement(
269
- config.joiner.failure(value, expected),
270
- ),
271
- is: config.joiner.is,
272
- required: config.joiner.required,
273
- full: config.joiner.full,
274
- type: TypeFactory.keyword("boolean"),
275
- },
276
- generator: {
277
- unions: config.numeric
278
- ? () =>
279
- FeatureProgrammer.write_union_functions(
280
- configure(project)({ ...config, numeric: false })(
281
- importer,
282
- ),
283
- )
284
- : undefined,
285
- arrays: () => write_array_functions(project)(config)(importer),
286
- tuples: () => write_tuple_functions(project)(config)(importer),
287
- },
288
- });
289
-
290
- /* -----------------------------------------------------------
291
- DECODERS
292
- ----------------------------------------------------------- */
293
- /**
294
- * @internal
295
- */
296
- export const decode =
297
- (project: IProject) =>
298
- (config: IConfig) =>
299
- (importer: FunctionImporter) =>
300
- (
301
- input: ts.Expression,
302
- meta: Metadata,
303
- explore: IExplore,
304
- metaTags: IMetadataTag[],
305
- jsDocTags: ts.JSDocTagInfo[],
306
- ): ts.Expression => {
307
- if (meta.any) return config.success;
308
-
309
- const top: IBinary[] = [];
310
- const binaries: IBinary[] = [];
311
- const add = create_add(binaries)(input);
312
- const getConstantValue = (
313
- value: number | string | bigint | boolean,
314
- ) =>
315
- typeof value === "string"
316
- ? ts.factory.createStringLiteral(value)
317
- : ts.factory.createIdentifier(value.toString());
318
-
319
- //----
320
- // CHECK OPTIONAL
321
- //----
322
- // @todo -> should be elaborated
323
- const checkOptional: boolean = meta.empty() || meta.isUnionBucket();
324
-
325
- // NULLABLE
326
- if (checkOptional || meta.nullable)
327
- (meta.nullable ? add : create_add(top)(input))(
328
- meta.nullable,
329
- ValueFactory.NULL(),
330
- );
331
-
332
- // UNDEFINDABLE
333
- if (checkOptional || !meta.required)
334
- (meta.required ? create_add(top)(input) : add)(
335
- !meta.required,
336
- ValueFactory.UNDEFINED(),
337
- );
338
-
339
- // FUNCTIONAL
340
- if (meta.functional === true)
341
- if (
342
- OptionPredicator.functional(project.options) ||
343
- meta.size() !== 1
344
- )
345
- add(
346
- true,
347
- ts.factory.createStringLiteral("function"),
348
- ValueFactory.TYPEOF(input),
349
- );
350
- else
351
- binaries.push({
352
- combined: false,
353
- expression: config.success,
354
- });
355
-
356
- //----
357
- // VALUES
358
- //----
359
- // CONSTANT VALUES
360
- for (const constant of meta.constants)
361
- if (AtomicPredicator.constant(meta)(constant.type))
362
- for (const val of constant.values)
363
- add(true, getConstantValue(val));
364
-
365
- // ATOMIC VALUES
366
- for (const type of meta.atomics)
367
- if (AtomicPredicator.atomic(meta)(type) === false) continue;
368
- else if (type === "number")
369
- binaries.push({
370
- expression: config.atomist(explore)(
371
- check_number(project, config.numeric)(importer)(
372
- metaTags,
373
- )(jsDocTags)(input),
374
- )(input),
375
- combined: false,
376
- });
377
- else if (type === "bigint")
378
- binaries.push({
379
- expression: config.atomist(explore)(
380
- check_bigint(importer)(metaTags)(jsDocTags)(input),
381
- )(input),
382
- combined: false,
383
- });
384
- else if (type === "string")
385
- binaries.push({
386
- expression: config.atomist(explore)(
387
- check_string(importer)(metaTags)(jsDocTags)(input),
388
- )(input),
389
- combined: false,
390
- });
391
- else
392
- add(
393
- true,
394
- ts.factory.createStringLiteral(type),
395
- ValueFactory.TYPEOF(input),
396
- );
397
-
398
- // TEMPLATE LITERAL VALUES
399
- if (meta.templates.length)
400
- if (AtomicPredicator.template(meta))
401
- binaries.push({
402
- expression: config.atomist(explore)(
403
- check_template(importer)(metaTags)(jsDocTags)(
404
- meta.templates,
405
- )(input),
406
- )(input),
407
- combined: false,
408
- });
409
-
410
- // NATIVE CLASSES
411
- for (const native of meta.natives)
412
- binaries.push({
413
- expression: check_native(native)(input),
414
- combined: false,
415
- });
416
-
417
- //----
418
- // INSTANCES
419
- //----
420
- interface IInstance {
421
- pre: ts.Expression;
422
- body: ts.Expression | null;
423
- expected: string;
424
- }
425
- const instances: IInstance[] = [];
426
- const prepare =
427
- (pre: ts.Expression, expected: string) =>
428
- (body: ts.Expression | null) =>
429
- instances.push({
430
- pre,
431
- expected,
432
- body,
433
- });
434
-
435
- // SETS
436
- if (meta.sets.length) {
437
- const install = prepare(
438
- check_native("Set")(input),
439
- meta.sets
440
- .map((elem) => `Set<${elem.getName()}>`)
441
- .join(" | "),
442
- );
443
- if (meta.sets.some((elem) => elem.any)) install(null);
444
- else
445
- install(
446
- explore_sets(project)(config)(importer)(
447
- input,
448
- meta.sets,
449
- {
450
- ...explore,
451
- from: "array",
452
- },
453
- [],
454
- [],
455
- ),
456
- );
457
- }
458
-
459
- // MAPS
460
- if (meta.maps.length) {
461
- const install = prepare(
462
- check_native("Map")(input),
463
- meta.maps
464
- .map(({ key, value }) => `Map<${key}, ${value}>`)
465
- .join(" | "),
466
- );
467
- if (meta.maps.some((elem) => elem.key.any && elem.value.any))
468
- install(null);
469
- else
470
- install(
471
- explore_maps(project)(config)(importer)(
472
- input,
473
- meta.maps,
474
- {
475
- ...explore,
476
- from: "array",
477
- },
478
- [],
479
- [],
480
- ),
481
- );
482
- }
483
-
484
- // ARRAYS AND TUPLES
485
- if (meta.tuples.length + meta.arrays.length > 0) {
486
- const install = prepare(
487
- config.atomist(explore)(
488
- check_array(importer)(
489
- meta.tuples.length === 0 ? metaTags : [],
490
- )(jsDocTags)(input),
491
- )(input),
492
- [...meta.tuples, ...meta.arrays]
493
- .map((elem) => elem.name)
494
- .join(" | "),
495
- );
496
- if (meta.arrays.length === 0)
497
- if (meta.tuples.length === 1)
498
- install(
499
- decode_tuple(project)(config)(importer)(
500
- input,
501
- meta.tuples[0]!,
502
- {
503
- ...explore,
504
- from: "array",
505
- },
506
- metaTags,
507
- jsDocTags,
508
- ),
509
- );
510
- // TUPLE ONLY
511
- else
512
- install(
513
- explore_tuples(project)(config)(importer)(
514
- input,
515
- meta.tuples,
516
- {
517
- ...explore,
518
- from: "array",
519
- },
520
- metaTags,
521
- jsDocTags,
522
- ),
523
- );
524
- else if (meta.arrays.some((elem) => elem.value.any))
525
- install(null);
526
- else if (meta.tuples.length === 0)
527
- if (meta.arrays.length === 1)
528
- // ARRAY ONLY
529
- install(
530
- decode_array(project)(config)(importer)(
531
- input,
532
- meta.arrays[0]!,
533
- {
534
- ...explore,
535
- from: "array",
536
- },
537
- metaTags,
538
- jsDocTags,
539
- ),
540
- );
541
- else
542
- install(
543
- explore_arrays(project)(config)(importer)(
544
- input,
545
- meta.arrays,
546
- {
547
- ...explore,
548
- from: "array",
549
- },
550
- metaTags,
551
- jsDocTags,
552
- ),
553
- );
554
- else
555
- install(
556
- explore_arrays_and_tuples(project)(config)(importer)(
557
- input,
558
- [...meta.tuples, ...meta.arrays],
559
- explore,
560
- metaTags,
561
- jsDocTags,
562
- ),
563
- );
564
- }
565
-
566
- // OBJECT
567
- if (meta.objects.length > 0)
568
- prepare(
569
- ExpressionFactory.isObject({
570
- checkNull: true,
571
- checkArray: meta.objects.some((obj) =>
572
- obj.properties.every(
573
- (prop) =>
574
- !prop.key.isSoleLiteral() ||
575
- !prop.value.required,
576
- ),
577
- ),
578
- })(input),
579
- meta.objects.map((obj) => obj.name).join(" | "),
580
- )(
581
- explore_objects(config)(importer)(input, meta, {
582
- ...explore,
583
- from: "object",
584
- }),
585
- );
586
-
587
- if (instances.length) {
588
- const transformer =
589
- (
590
- merger: (
591
- x: ts.Expression,
592
- y: ts.Expression,
593
- ) => ts.Expression,
594
- ) =>
595
- (ins: IInstance) =>
596
- ins.body
597
- ? {
598
- expression: merger(ins.pre, ins.body),
599
- combined: true,
600
- }
601
- : {
602
- expression: ins.pre,
603
- combined: false,
604
- };
605
- if (instances.length === 1)
606
- binaries.push(
607
- transformer((pre, body) =>
608
- config.combiner(explore)("and")(
609
- input,
610
- [pre, body].map((expression) => ({
611
- expression,
612
- combined: expression !== pre,
613
- })),
614
- meta.getName(),
615
- ),
616
- )(instances[0]!),
617
- );
618
- else
619
- binaries.push({
620
- expression: config.combiner(explore)("or")(
621
- input,
622
- instances.map(
623
- transformer(ts.factory.createLogicalAnd),
624
- ),
625
- meta.getName(),
626
- ),
627
- combined: true,
628
- });
629
- }
630
-
631
- //----
632
- // COMBINE CONDITIONS
633
- //----
634
- return top.length && binaries.length
635
- ? config.combiner(explore)("and")(
636
- input,
637
- [
638
- ...top,
639
- {
640
- expression: config.combiner(explore)("or")(
641
- input,
642
- binaries,
643
- meta.getName(),
644
- ),
645
- combined: true,
646
- },
647
- ],
648
- meta.getName(),
649
- )
650
- : binaries.length
651
- ? config.combiner(explore)("or")(
652
- input,
653
- binaries,
654
- meta.getName(),
655
- )
656
- : config.success;
657
- };
658
-
659
- export const decode_object =
660
- (config: IConfig) => (importer: FunctionImporter) => {
661
- const func = FeatureProgrammer.decode_object(config)(importer);
662
- return (
663
- input: ts.Expression,
664
- obj: MetadataObject,
665
- explore: IExplore,
666
- ) => {
667
- obj.validated = true;
668
- return func(input, obj, explore);
669
- };
670
- };
671
-
672
- const decode_array =
673
- (project: IProject) =>
674
- (config: IConfig) =>
675
- (importer: FunctionImporter) =>
676
- (
677
- input: ts.Expression,
678
- array: MetadataArray,
679
- explore: IExplore,
680
- metaTags: IMetadataTag[],
681
- jsDocTags: IJsDocTagInfo[],
682
- ) => {
683
- if (array.recursive === false)
684
- return decode_array_inline(project)(config)(importer)(
685
- input,
686
- array,
687
- explore,
688
- metaTags,
689
- jsDocTags,
690
- );
691
-
692
- explore = {
693
- ...explore,
694
- source: "function",
695
- from: "array",
696
- };
697
- return ts.factory.createLogicalOr(
698
- ts.factory.createCallExpression(
699
- ts.factory.createIdentifier(
700
- importer.useLocal(`${config.prefix}a${array.index}`),
701
- ),
702
- undefined,
703
- FeatureProgrammer.argumentsArray(config)({
704
- ...explore,
705
- source: "function",
706
- from: "array",
707
- })(input),
708
- ),
709
- config.joiner.failure(input, array.name, explore),
710
- );
711
- };
712
-
713
- const decode_array_inline =
714
- (project: IProject) =>
715
- (config: IConfig) =>
716
- (importer: FunctionImporter) =>
717
- FeatureProgrammer.decode_array({
718
- prefix: config.prefix,
719
- trace: config.trace,
720
- path: config.path,
721
- decoder: () => decode(project)(config)(importer),
722
- })(importer)(config.joiner.array);
723
-
724
- const decode_tuple =
725
- (project: IProject) =>
726
- (config: IConfig) =>
727
- (importer: FunctionImporter) =>
728
- (
729
- input: ts.Expression,
730
- tuple: MetadataTuple,
731
- explore: IExplore,
732
- tagList: IMetadataTag[],
733
- jsDocTags: ts.JSDocTagInfo[],
734
- ): ts.Expression => {
735
- if (tuple.recursive === false)
736
- return decode_tuple_inline(project)(config)(importer)(
737
- input,
738
- tuple,
739
- explore,
740
- tagList,
741
- jsDocTags,
742
- );
743
- explore = {
744
- ...explore,
745
- source: "function",
746
- from: "array",
747
- };
748
- return ts.factory.createLogicalOr(
749
- ts.factory.createCallExpression(
750
- ts.factory.createIdentifier(
751
- importer.useLocal(`${config.prefix}t${tuple.index}`),
752
- ),
753
- undefined,
754
- FeatureProgrammer.argumentsArray(config)({
755
- ...explore,
756
- source: "function",
757
- })(input),
758
- ),
759
- config.joiner.failure(input, tuple.name, explore),
760
- );
761
- };
762
-
763
- const decode_tuple_inline =
764
- (project: IProject) =>
765
- (config: IConfig) =>
766
- (importer: FunctionImporter) =>
767
- (
768
- input: ts.Expression,
769
- tuple: MetadataTuple,
770
- explore: IExplore,
771
- tagList: IMetadataTag[],
772
- jsDocTags: ts.JSDocTagInfo[],
773
- ): ts.Expression => {
774
- const binaries: ts.Expression[] = tuple.elements
775
- .filter((meta) => meta.rest === null)
776
- .map((meta, index) =>
777
- decode(project)(config)(importer)(
778
- ts.factory.createElementAccessExpression(input, index),
779
- meta,
780
- {
781
- ...explore,
782
- from: "array",
783
- postfix: explore.postfix.length
784
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
785
- : `"[${index}]"`,
786
- },
787
- tagList,
788
- jsDocTags,
789
- ),
790
- );
791
- const rest: ts.Expression | null =
792
- tuple.elements.length && tuple.elements.at(-1)!.rest !== null
793
- ? decode(project)(config)(importer)(
794
- ts.factory.createCallExpression(
795
- IdentifierFactory.access(input)("slice"),
796
- undefined,
797
- [
798
- ts.factory.createNumericLiteral(
799
- tuple.elements.length - 1,
800
- ),
801
- ],
802
- ),
803
- wrap_metadata_rest_tuple(
804
- tuple.elements.at(-1)!.rest!,
805
- ),
806
- {
807
- ...explore,
808
- start: tuple.elements.length - 1,
809
- },
810
- tagList,
811
- jsDocTags,
812
- )
813
- : null;
814
-
815
- const arrayLength = ts.factory.createPropertyAccessExpression(
816
- input,
817
- "length",
818
- );
819
- return config.combiner(explore)("and")(
820
- input,
821
- [
822
- ...(rest === null
823
- ? tuple.elements.every((t) => t.optional === false)
824
- ? [
825
- {
826
- combined: false,
827
- expression:
828
- ts.factory.createStrictEquality(
829
- arrayLength,
830
- ts.factory.createNumericLiteral(
831
- tuple.elements.length,
832
- ),
833
- ),
834
- },
835
- ]
836
- : [
837
- {
838
- combined: false,
839
- expression: ts.factory.createLogicalAnd(
840
- ts.factory.createLessThanEquals(
841
- ts.factory.createNumericLiteral(
842
- tuple.elements.filter(
843
- (t) =>
844
- t.optional === false,
845
- ).length,
846
- ),
847
- arrayLength,
848
- ),
849
- ts.factory.createGreaterThanEquals(
850
- ts.factory.createNumericLiteral(
851
- tuple.elements.length,
852
- ),
853
- arrayLength,
854
- ),
855
- ),
856
- },
857
- ]
858
- : []),
859
- ...(config.joiner.tuple
860
- ? [
861
- {
862
- expression: config.joiner.tuple(binaries),
863
- combined: true,
864
- },
865
- ]
866
- : binaries.map((expression) => ({
867
- expression,
868
- combined: true,
869
- }))),
870
- ...(rest !== null
871
- ? [
872
- {
873
- expression: rest,
874
- combined: true,
875
- },
876
- ]
877
- : []),
878
- ],
879
- `[${tuple.elements.map((t) => t.getName()).join(", ")}]`,
880
- );
881
- };
882
-
883
- /* -----------------------------------------------------------
884
- UNION TYPE EXPLORERS
885
- ----------------------------------------------------------- */
886
- const explore_sets =
887
- (project: IProject) =>
888
- (config: IConfig) =>
889
- (importer: FunctionImporter) =>
890
- (
891
- input: ts.Expression,
892
- sets: Metadata[],
893
- explore: IExplore,
894
- tags: IMetadataTag[],
895
- jsDocTags: IJsDocTagInfo[],
896
- ): ts.Expression =>
897
- ts.factory.createCallExpression(
898
- UnionExplorer.set({
899
- checker: decode(project)(config)(importer),
900
- decoder: decode_array(project)(config)(importer),
901
- empty: config.success,
902
- success: config.success,
903
- failure: (input, expected, explore) =>
904
- ts.factory.createReturnStatement(
905
- config.joiner.failure(input, expected, explore),
906
- ),
907
- })([])(input, sets, explore, tags, jsDocTags),
908
- undefined,
909
- undefined,
910
- );
911
-
912
- const explore_maps =
913
- (project: IProject) =>
914
- (config: IConfig) =>
915
- (importer: FunctionImporter) =>
916
- (
917
- input: ts.Expression,
918
- maps: Metadata.Entry[],
919
- explore: IExplore,
920
- tags: IMetadataTag[],
921
- jsDocTags: IJsDocTagInfo[],
922
- ): ts.Expression =>
923
- ts.factory.createCallExpression(
924
- UnionExplorer.map({
925
- checker: (input, entry, explore) => {
926
- const func = decode(project)(config)(importer);
927
- return ts.factory.createLogicalAnd(
928
- func(
929
- ts.factory.createElementAccessExpression(
930
- input,
931
- 0,
932
- ),
933
- entry[0],
934
- {
935
- ...explore,
936
- postfix: `${explore.postfix}[0]`,
937
- },
938
- [],
939
- [],
940
- ),
941
- func(
942
- ts.factory.createElementAccessExpression(
943
- input,
944
- 1,
945
- ),
946
- entry[1],
947
- {
948
- ...explore,
949
- postfix: `${explore.postfix}[1]`,
950
- },
951
- [],
952
- [],
953
- ),
954
- );
955
- },
956
- decoder: decode_array(project)(config)(importer),
957
- empty: config.success,
958
- success: config.success,
959
- failure: (input, expected, explore) =>
960
- ts.factory.createReturnStatement(
961
- config.joiner.failure(input, expected, explore),
962
- ),
963
- })([])(input, maps, explore, tags, jsDocTags),
964
- undefined,
965
- undefined,
966
- );
967
-
968
- const explore_tuples =
969
- (project: IProject) =>
970
- (config: IConfig) =>
971
- (importer: FunctionImporter) =>
972
- (
973
- input: ts.Expression,
974
- tuples: MetadataTuple[],
975
- explore: IExplore,
976
- tags: IMetadataTag[],
977
- jsDocTags: IJsDocTagInfo[],
978
- ): ts.Expression =>
979
- explore_array_like_union_types(config)(importer)(
980
- UnionExplorer.tuple({
981
- checker: decode_tuple(project)(config)(importer),
982
- decoder: decode_tuple(project)(config)(importer),
983
- empty: config.success,
984
- success: config.success,
985
- failure: (input, expected, explore) =>
986
- ts.factory.createReturnStatement(
987
- config.joiner.failure(input, expected, explore),
988
- ),
989
- }),
990
- )(input, tuples, explore, tags, jsDocTags);
991
-
992
- const explore_arrays =
993
- (project: IProject) =>
994
- (config: IConfig) =>
995
- (importer: FunctionImporter) =>
996
- (
997
- input: ts.Expression,
998
- arrays: MetadataArray[],
999
- explore: IExplore,
1000
- tags: IMetadataTag[],
1001
- jsDocTags: IJsDocTagInfo[],
1002
- ): ts.Expression =>
1003
- explore_array_like_union_types(config)(importer)(
1004
- UnionExplorer.array({
1005
- checker: decode(project)(config)(importer),
1006
- decoder: decode_array(project)(config)(importer),
1007
- empty: config.success,
1008
- success: config.success,
1009
- failure: (input, expected, explore) =>
1010
- ts.factory.createReturnStatement(
1011
- config.joiner.failure(input, expected, explore),
1012
- ),
1013
- }),
1014
- )(input, arrays, explore, tags, jsDocTags);
1015
-
1016
- const explore_arrays_and_tuples =
1017
- (project: IProject) =>
1018
- (config: IConfig) =>
1019
- (importer: FunctionImporter) =>
1020
- (
1021
- input: ts.Expression,
1022
- elements: Array<MetadataArray | MetadataTuple>,
1023
- explore: IExplore,
1024
- tags: IMetadataTag[],
1025
- jsDocTags: IJsDocTagInfo[],
1026
- ): ts.Expression =>
1027
- explore_array_like_union_types(config)(importer)(
1028
- UnionExplorer.array_or_tuple({
1029
- checker: (front, target, explore, tags, jsDocTags, array) =>
1030
- target instanceof MetadataTuple
1031
- ? decode_tuple(project)(config)(importer)(
1032
- front,
1033
- target,
1034
- explore,
1035
- tags,
1036
- jsDocTags,
1037
- )
1038
- : config.atomist(explore)({
1039
- expression: decode(project)(config)(importer)(
1040
- front,
1041
- target,
1042
- explore,
1043
- tags,
1044
- jsDocTags,
1045
- ),
1046
- tags: check_array_length(tags)(array),
1047
- })(array),
1048
- decoder: (input, target, explore, tags, jsDocTags) =>
1049
- target instanceof MetadataTuple
1050
- ? decode_tuple(project)(config)(importer)(
1051
- input,
1052
- target,
1053
- explore,
1054
- tags,
1055
- jsDocTags,
1056
- )
1057
- : decode_array(project)(config)(importer)(
1058
- input,
1059
- target,
1060
- explore,
1061
- tags,
1062
- jsDocTags,
1063
- ),
1064
- empty: config.success,
1065
- success: config.success,
1066
- failure: (input, expected, explore) =>
1067
- ts.factory.createReturnStatement(
1068
- config.joiner.failure(input, expected, explore),
1069
- ),
1070
- }),
1071
- )(input, elements, explore, tags, jsDocTags);
1072
-
1073
- const explore_array_like_union_types =
1074
- (config: IConfig) =>
1075
- (importer: FunctionImporter) =>
1076
- <T extends MetadataArray | MetadataTuple>(
1077
- factory: (
1078
- parameters: ts.ParameterDeclaration[],
1079
- ) => (
1080
- input: ts.Expression,
1081
- elements: T[],
1082
- explore: IExplore,
1083
- tags: IMetadataTag[],
1084
- jsDocTags: IJsDocTagInfo[],
1085
- ) => ts.ArrowFunction,
1086
- ) =>
1087
- (
1088
- input: ts.Expression,
1089
- elements: T[],
1090
- explore: IExplore,
1091
- tags: IMetadataTag[],
1092
- jsDocTags: IJsDocTagInfo[],
1093
- ): ts.Expression => {
1094
- const arrow =
1095
- (parameters: ts.ParameterDeclaration[]) =>
1096
- (explore: IExplore) =>
1097
- (input: ts.Expression): ts.ArrowFunction =>
1098
- factory(parameters)(
1099
- input,
1100
- elements,
1101
- explore,
1102
- tags,
1103
- jsDocTags,
1104
- );
1105
- if (elements.every((e) => e.recursive === false))
1106
- ts.factory.createCallExpression(
1107
- arrow([])(explore)(input),
1108
- undefined,
1109
- [],
1110
- );
1111
- explore = {
1112
- ...explore,
1113
- source: "function",
1114
- from: "array",
1115
- };
1116
- return ts.factory.createLogicalOr(
1117
- ts.factory.createCallExpression(
1118
- ts.factory.createIdentifier(
1119
- importer.emplaceUnion(
1120
- config.prefix,
1121
- elements.map((e) => e.name).join(" | "),
1122
- () =>
1123
- arrow(
1124
- FeatureProgrammer.parameterDeclarations(
1125
- config,
1126
- )(TypeFactory.keyword("any"))(
1127
- ts.factory.createIdentifier("input"),
1128
- ),
1129
- )({
1130
- ...explore,
1131
- postfix: "",
1132
- })(ts.factory.createIdentifier("input")),
1133
- ),
1134
- ),
1135
- undefined,
1136
- FeatureProgrammer.argumentsArray(config)(explore)(input),
1137
- ),
1138
- config.joiner.failure(
1139
- input,
1140
- elements.map((e) => e.name).join(" | "),
1141
- explore,
1142
- ),
1143
- );
1144
- };
1145
-
1146
- const explore_objects =
1147
- (config: IConfig) =>
1148
- (importer: FunctionImporter) =>
1149
- (input: ts.Expression, meta: Metadata, explore: IExplore) =>
1150
- meta.objects.length === 1
1151
- ? decode_object(config)(importer)(
1152
- input,
1153
- meta.objects[0]!,
1154
- explore,
1155
- )
1156
- : ts.factory.createCallExpression(
1157
- ts.factory.createIdentifier(
1158
- importer.useLocal(
1159
- `${config.prefix}u${meta.union_index!}`,
1160
- ),
1161
- ),
1162
- undefined,
1163
- FeatureProgrammer.argumentsArray(config)(explore)(input),
1164
- );
1165
- }
1166
-
1167
- const create_add =
1168
- (binaries: CheckerProgrammer.IBinary[]) =>
1169
- (defaultInput: ts.Expression) =>
1170
- (
1171
- exact: boolean,
1172
- left: ts.Expression,
1173
- right: ts.Expression = defaultInput,
1174
- ) => {
1175
- const factory = exact
1176
- ? ts.factory.createStrictEquality
1177
- : ts.factory.createStrictInequality;
1178
- binaries.push({
1179
- expression: factory(left, right),
1180
- combined: false,
1181
- });
1182
- };
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
+ import { ValueFactory } from "../factories/ValueFactory";
10
+
11
+ import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
12
+ import { IMetadataTag } from "../metadata/IMetadataTag";
13
+ import { Metadata } from "../metadata/Metadata";
14
+ import { MetadataArray } from "../metadata/MetadataArray";
15
+ import { MetadataObject } from "../metadata/MetadataObject";
16
+ import { MetadataTuple } from "../metadata/MetadataTuple";
17
+
18
+ import { IProject } from "../transformers/IProject";
19
+
20
+ import { FeatureProgrammer } from "./FeatureProgrammer";
21
+ import { AtomicPredicator } from "./helpers/AtomicPredicator";
22
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
23
+ import { ICheckEntry } from "./helpers/ICheckEntry";
24
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
25
+ import { OptionPredicator } from "./helpers/OptionPredicator";
26
+ import { UnionExplorer } from "./helpers/UnionExplorer";
27
+ import { check_array } from "./internal/check_array";
28
+ import { check_array_length } from "./internal/check_array_length";
29
+ import { check_bigint } from "./internal/check_bigint";
30
+ import { check_native } from "./internal/check_native";
31
+ import { check_number } from "./internal/check_number";
32
+ import { check_string } from "./internal/check_string";
33
+ import { check_template } from "./internal/check_template";
34
+ import { decode_union_object } from "./internal/decode_union_object";
35
+ import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
36
+
37
+ export namespace CheckerProgrammer {
38
+ export interface IConfig {
39
+ prefix: string;
40
+ path: boolean;
41
+ trace: boolean;
42
+ equals: boolean;
43
+ numeric: boolean;
44
+ addition?: () => ts.Statement[];
45
+ decoder?: () => FeatureProgrammer.Decoder<Metadata, ts.Expression>;
46
+ combiner: IConfig.Combiner;
47
+ atomist: (
48
+ explore: IExplore,
49
+ ) => (check: ICheckEntry) => (input: ts.Expression) => ts.Expression;
50
+ joiner: IConfig.IJoiner;
51
+ success: ts.Expression;
52
+ }
53
+ export namespace IConfig {
54
+ export interface Combiner {
55
+ (explorer: IExplore): {
56
+ (logic: "and" | "or"): {
57
+ (
58
+ input: ts.Expression,
59
+ binaries: IBinary[],
60
+ expected: string,
61
+ ): ts.Expression;
62
+ };
63
+ };
64
+ }
65
+ export interface IJoiner {
66
+ object(
67
+ input: ts.Expression,
68
+ entries: IExpressionEntry[],
69
+ ): ts.Expression;
70
+ array(input: ts.Expression, arrow: ts.ArrowFunction): ts.Expression;
71
+ tuple?(exprs: ts.Expression[]): ts.Expression;
72
+
73
+ failure(
74
+ value: ts.Expression,
75
+ expected: string,
76
+ explore?: FeatureProgrammer.IExplore,
77
+ ): ts.Expression;
78
+ is?(expression: ts.Expression): ts.Expression;
79
+ required?(exp: ts.Expression): ts.Expression;
80
+ full?: (
81
+ condition: ts.Expression,
82
+ ) => (
83
+ input: ts.Expression,
84
+ expected: string,
85
+ explore: IExplore,
86
+ ) => ts.Expression;
87
+ }
88
+ }
89
+ export type IExplore = FeatureProgrammer.IExplore;
90
+
91
+ export interface IBinary {
92
+ expression: ts.Expression;
93
+ combined: boolean;
94
+ }
95
+
96
+ /* -----------------------------------------------------------
97
+ WRITERS
98
+ ----------------------------------------------------------- */
99
+ export const write =
100
+ (project: IProject) =>
101
+ (config: IConfig) =>
102
+ (importer: FunctionImporter) =>
103
+ FeatureProgrammer.write(project)(
104
+ configure(project)(config)(importer),
105
+ )(importer);
106
+
107
+ export const write_object_functions =
108
+ (project: IProject) =>
109
+ (config: IConfig) =>
110
+ (importer: FunctionImporter) =>
111
+ FeatureProgrammer.write_object_functions(
112
+ configure(project)(config)(importer),
113
+ )(importer);
114
+
115
+ export const write_union_functions =
116
+ (project: IProject) =>
117
+ (config: IConfig) =>
118
+ (importer: FunctionImporter) =>
119
+ FeatureProgrammer.write_union_functions(
120
+ configure(project)({ ...config, numeric: false })(importer),
121
+ );
122
+
123
+ export const write_array_functions =
124
+ (project: IProject) =>
125
+ (config: IConfig) =>
126
+ (importer: FunctionImporter) =>
127
+ (collection: MetadataCollection): ts.VariableStatement[] =>
128
+ collection
129
+ .arrays()
130
+ .filter((a) => a.recursive)
131
+ .map((array, i) =>
132
+ StatementFactory.constant(
133
+ `${config.prefix}a${i}`,
134
+ ts.factory.createArrowFunction(
135
+ undefined,
136
+ undefined,
137
+ FeatureProgrammer.parameterDeclarations(config)(
138
+ TypeFactory.keyword("any"),
139
+ )(ts.factory.createIdentifier("input")),
140
+ TypeFactory.keyword("any"),
141
+ undefined,
142
+ decode_array_inline(project)(config)(importer)(
143
+ ts.factory.createIdentifier("input"),
144
+ array,
145
+ {
146
+ tracable: config.trace,
147
+ source: "function",
148
+ from: "array",
149
+ postfix: "",
150
+ },
151
+ [],
152
+ [],
153
+ ),
154
+ ),
155
+ ),
156
+ );
157
+
158
+ export const write_tuple_functions =
159
+ (project: IProject) =>
160
+ (config: IConfig) =>
161
+ (importer: FunctionImporter) =>
162
+ (collection: MetadataCollection): ts.VariableStatement[] =>
163
+ collection
164
+ .tuples()
165
+ .filter((t) => t.recursive)
166
+ .map((tuple, i) =>
167
+ StatementFactory.constant(
168
+ `${config.prefix}t${i}`,
169
+ ts.factory.createArrowFunction(
170
+ undefined,
171
+ undefined,
172
+ FeatureProgrammer.parameterDeclarations(config)(
173
+ TypeFactory.keyword("any"),
174
+ )(ts.factory.createIdentifier("input")),
175
+ TypeFactory.keyword("any"),
176
+ undefined,
177
+ decode_tuple_inline(project)(config)(importer)(
178
+ ts.factory.createIdentifier("input"),
179
+ tuple,
180
+ {
181
+ tracable: config.trace,
182
+ source: "function",
183
+ from: "array",
184
+ postfix: "",
185
+ },
186
+ [],
187
+ [],
188
+ ),
189
+ ),
190
+ ),
191
+ );
192
+
193
+ const configure =
194
+ (project: IProject) =>
195
+ (config: IConfig) =>
196
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => ({
197
+ types: {
198
+ input: () => TypeFactory.keyword("any"),
199
+ output: (type, name) =>
200
+ ts.factory.createTypePredicateNode(
201
+ undefined,
202
+ "input",
203
+ ts.factory.createTypeReferenceNode(
204
+ name ??
205
+ TypeFactory.getFullName(project.checker)(type),
206
+ ),
207
+ ),
208
+ },
209
+ trace: config.trace,
210
+ path: config.path,
211
+ prefix: config.prefix,
212
+ initializer:
213
+ ({ checker }) =>
214
+ (type) => {
215
+ const collection: MetadataCollection =
216
+ new MetadataCollection();
217
+ const meta: Metadata = MetadataFactory.analyze(checker)({
218
+ resolve: false,
219
+ constant: true,
220
+ absorb: true,
221
+ validate: (meta) => {
222
+ if (
223
+ meta.arrays.length > 1 &&
224
+ meta.arrays.some((a) => a.recursive)
225
+ )
226
+ throw new Error(
227
+ `Repeated union array types are not supported yet.`,
228
+ );
229
+ },
230
+ })(collection)(type);
231
+ return [collection, meta];
232
+ },
233
+ addition: config.addition,
234
+ decoder: () =>
235
+ config.decoder?.() ?? decode(project)(config)(importer),
236
+ objector: {
237
+ checker: () =>
238
+ config.decoder?.() ?? decode(project)(config)(importer),
239
+ decoder: () => decode_object(config)(importer),
240
+ joiner: config.joiner.object,
241
+ unionizer: config.equals
242
+ ? decode_union_object(decode_object(config)(importer))(
243
+ (input, obj, explore) =>
244
+ decode_object(config)(importer)(input, obj, {
245
+ ...explore,
246
+ tracable: true,
247
+ }),
248
+ )(config.joiner.is ?? ((expr) => expr))(
249
+ (value, expected) =>
250
+ ts.factory.createReturnStatement(
251
+ config.joiner.failure(value, expected),
252
+ ),
253
+ )
254
+ : (input, targets, explore) =>
255
+ config.combiner(explore)("or")(
256
+ input,
257
+ targets.map((obj) => ({
258
+ expression: decode_object(config)(importer)(
259
+ input,
260
+ obj,
261
+ explore,
262
+ ),
263
+ combined: true,
264
+ })),
265
+ `(${targets.map((t) => t.name).join(" | ")})`,
266
+ ),
267
+ failure: (value, expected) =>
268
+ ts.factory.createReturnStatement(
269
+ config.joiner.failure(value, expected),
270
+ ),
271
+ is: config.joiner.is,
272
+ required: config.joiner.required,
273
+ full: config.joiner.full,
274
+ type: TypeFactory.keyword("boolean"),
275
+ },
276
+ generator: {
277
+ unions: config.numeric
278
+ ? () =>
279
+ FeatureProgrammer.write_union_functions(
280
+ configure(project)({ ...config, numeric: false })(
281
+ importer,
282
+ ),
283
+ )
284
+ : undefined,
285
+ arrays: () => write_array_functions(project)(config)(importer),
286
+ tuples: () => write_tuple_functions(project)(config)(importer),
287
+ },
288
+ });
289
+
290
+ /* -----------------------------------------------------------
291
+ DECODERS
292
+ ----------------------------------------------------------- */
293
+ /**
294
+ * @internal
295
+ */
296
+ export const decode =
297
+ (project: IProject) =>
298
+ (config: IConfig) =>
299
+ (importer: FunctionImporter) =>
300
+ (
301
+ input: ts.Expression,
302
+ meta: Metadata,
303
+ explore: IExplore,
304
+ metaTags: IMetadataTag[],
305
+ jsDocTags: ts.JSDocTagInfo[],
306
+ ): ts.Expression => {
307
+ if (meta.any) return config.success;
308
+
309
+ const top: IBinary[] = [];
310
+ const binaries: IBinary[] = [];
311
+ const add = create_add(binaries)(input);
312
+ const getConstantValue = (
313
+ value: number | string | bigint | boolean,
314
+ ) =>
315
+ typeof value === "string"
316
+ ? ts.factory.createStringLiteral(value)
317
+ : ts.factory.createIdentifier(value.toString());
318
+
319
+ //----
320
+ // CHECK OPTIONAL
321
+ //----
322
+ // @todo -> should be elaborated
323
+ const checkOptional: boolean = meta.empty() || meta.isUnionBucket();
324
+
325
+ // NULLABLE
326
+ if (checkOptional || meta.nullable)
327
+ (meta.nullable ? add : create_add(top)(input))(
328
+ meta.nullable,
329
+ ValueFactory.NULL(),
330
+ );
331
+
332
+ // UNDEFINDABLE
333
+ if (checkOptional || !meta.required)
334
+ (meta.required ? create_add(top)(input) : add)(
335
+ !meta.required,
336
+ ValueFactory.UNDEFINED(),
337
+ );
338
+
339
+ // FUNCTIONAL
340
+ if (meta.functional === true)
341
+ if (
342
+ OptionPredicator.functional(project.options) ||
343
+ meta.size() !== 1
344
+ )
345
+ add(
346
+ true,
347
+ ts.factory.createStringLiteral("function"),
348
+ ValueFactory.TYPEOF(input),
349
+ );
350
+ else
351
+ binaries.push({
352
+ combined: false,
353
+ expression: config.success,
354
+ });
355
+
356
+ //----
357
+ // VALUES
358
+ //----
359
+ // CONSTANT VALUES
360
+ for (const constant of meta.constants)
361
+ if (AtomicPredicator.constant(meta)(constant.type))
362
+ for (const val of constant.values)
363
+ add(true, getConstantValue(val));
364
+
365
+ // ATOMIC VALUES
366
+ for (const type of meta.atomics)
367
+ if (AtomicPredicator.atomic(meta)(type) === false) continue;
368
+ else if (type === "number")
369
+ binaries.push({
370
+ expression: config.atomist(explore)(
371
+ check_number(project, config.numeric)(importer)(
372
+ metaTags,
373
+ )(jsDocTags)(input),
374
+ )(input),
375
+ combined: false,
376
+ });
377
+ else if (type === "bigint")
378
+ binaries.push({
379
+ expression: config.atomist(explore)(
380
+ check_bigint(importer)(metaTags)(jsDocTags)(input),
381
+ )(input),
382
+ combined: false,
383
+ });
384
+ else if (type === "string")
385
+ binaries.push({
386
+ expression: config.atomist(explore)(
387
+ check_string(importer)(metaTags)(jsDocTags)(input),
388
+ )(input),
389
+ combined: false,
390
+ });
391
+ else
392
+ add(
393
+ true,
394
+ ts.factory.createStringLiteral(type),
395
+ ValueFactory.TYPEOF(input),
396
+ );
397
+
398
+ // TEMPLATE LITERAL VALUES
399
+ if (meta.templates.length)
400
+ if (AtomicPredicator.template(meta))
401
+ binaries.push({
402
+ expression: config.atomist(explore)(
403
+ check_template(importer)(metaTags)(jsDocTags)(
404
+ meta.templates,
405
+ )(input),
406
+ )(input),
407
+ combined: false,
408
+ });
409
+
410
+ // NATIVE CLASSES
411
+ for (const native of meta.natives)
412
+ binaries.push({
413
+ expression: check_native(native)(input),
414
+ combined: false,
415
+ });
416
+
417
+ //----
418
+ // INSTANCES
419
+ //----
420
+ interface IInstance {
421
+ pre: ts.Expression;
422
+ body: ts.Expression | null;
423
+ expected: string;
424
+ }
425
+ const instances: IInstance[] = [];
426
+ const prepare =
427
+ (pre: ts.Expression, expected: string) =>
428
+ (body: ts.Expression | null) =>
429
+ instances.push({
430
+ pre,
431
+ expected,
432
+ body,
433
+ });
434
+
435
+ // SETS
436
+ if (meta.sets.length) {
437
+ const install = prepare(
438
+ check_native("Set")(input),
439
+ meta.sets
440
+ .map((elem) => `Set<${elem.getName()}>`)
441
+ .join(" | "),
442
+ );
443
+ if (meta.sets.some((elem) => elem.any)) install(null);
444
+ else
445
+ install(
446
+ explore_sets(project)(config)(importer)(
447
+ input,
448
+ meta.sets,
449
+ {
450
+ ...explore,
451
+ from: "array",
452
+ },
453
+ [],
454
+ [],
455
+ ),
456
+ );
457
+ }
458
+
459
+ // MAPS
460
+ if (meta.maps.length) {
461
+ const install = prepare(
462
+ check_native("Map")(input),
463
+ meta.maps
464
+ .map(({ key, value }) => `Map<${key}, ${value}>`)
465
+ .join(" | "),
466
+ );
467
+ if (meta.maps.some((elem) => elem.key.any && elem.value.any))
468
+ install(null);
469
+ else
470
+ install(
471
+ explore_maps(project)(config)(importer)(
472
+ input,
473
+ meta.maps,
474
+ {
475
+ ...explore,
476
+ from: "array",
477
+ },
478
+ [],
479
+ [],
480
+ ),
481
+ );
482
+ }
483
+
484
+ // ARRAYS AND TUPLES
485
+ if (meta.tuples.length + meta.arrays.length > 0) {
486
+ const install = prepare(
487
+ config.atomist(explore)(
488
+ check_array(importer)(
489
+ meta.tuples.length === 0 ? metaTags : [],
490
+ )(jsDocTags)(input),
491
+ )(input),
492
+ [...meta.tuples, ...meta.arrays]
493
+ .map((elem) => elem.name)
494
+ .join(" | "),
495
+ );
496
+ if (meta.arrays.length === 0)
497
+ if (meta.tuples.length === 1)
498
+ install(
499
+ decode_tuple(project)(config)(importer)(
500
+ input,
501
+ meta.tuples[0]!,
502
+ {
503
+ ...explore,
504
+ from: "array",
505
+ },
506
+ metaTags,
507
+ jsDocTags,
508
+ ),
509
+ );
510
+ // TUPLE ONLY
511
+ else
512
+ install(
513
+ explore_tuples(project)(config)(importer)(
514
+ input,
515
+ meta.tuples,
516
+ {
517
+ ...explore,
518
+ from: "array",
519
+ },
520
+ metaTags,
521
+ jsDocTags,
522
+ ),
523
+ );
524
+ else if (meta.arrays.some((elem) => elem.value.any))
525
+ install(null);
526
+ else if (meta.tuples.length === 0)
527
+ if (meta.arrays.length === 1)
528
+ // ARRAY ONLY
529
+ install(
530
+ decode_array(project)(config)(importer)(
531
+ input,
532
+ meta.arrays[0]!,
533
+ {
534
+ ...explore,
535
+ from: "array",
536
+ },
537
+ metaTags,
538
+ jsDocTags,
539
+ ),
540
+ );
541
+ else
542
+ install(
543
+ explore_arrays(project)(config)(importer)(
544
+ input,
545
+ meta.arrays,
546
+ {
547
+ ...explore,
548
+ from: "array",
549
+ },
550
+ metaTags,
551
+ jsDocTags,
552
+ ),
553
+ );
554
+ else
555
+ install(
556
+ explore_arrays_and_tuples(project)(config)(importer)(
557
+ input,
558
+ [...meta.tuples, ...meta.arrays],
559
+ explore,
560
+ metaTags,
561
+ jsDocTags,
562
+ ),
563
+ );
564
+ }
565
+
566
+ // OBJECT
567
+ if (meta.objects.length > 0)
568
+ prepare(
569
+ ExpressionFactory.isObject({
570
+ checkNull: true,
571
+ checkArray: meta.objects.some((obj) =>
572
+ obj.properties.every(
573
+ (prop) =>
574
+ !prop.key.isSoleLiteral() ||
575
+ !prop.value.required,
576
+ ),
577
+ ),
578
+ })(input),
579
+ meta.objects.map((obj) => obj.name).join(" | "),
580
+ )(
581
+ explore_objects(config)(importer)(input, meta, {
582
+ ...explore,
583
+ from: "object",
584
+ }),
585
+ );
586
+
587
+ if (instances.length) {
588
+ const transformer =
589
+ (
590
+ merger: (
591
+ x: ts.Expression,
592
+ y: ts.Expression,
593
+ ) => ts.Expression,
594
+ ) =>
595
+ (ins: IInstance) =>
596
+ ins.body
597
+ ? {
598
+ expression: merger(ins.pre, ins.body),
599
+ combined: true,
600
+ }
601
+ : {
602
+ expression: ins.pre,
603
+ combined: false,
604
+ };
605
+ if (instances.length === 1)
606
+ binaries.push(
607
+ transformer((pre, body) =>
608
+ config.combiner(explore)("and")(
609
+ input,
610
+ [pre, body].map((expression) => ({
611
+ expression,
612
+ combined: expression !== pre,
613
+ })),
614
+ meta.getName(),
615
+ ),
616
+ )(instances[0]!),
617
+ );
618
+ else
619
+ binaries.push({
620
+ expression: config.combiner(explore)("or")(
621
+ input,
622
+ instances.map(
623
+ transformer(ts.factory.createLogicalAnd),
624
+ ),
625
+ meta.getName(),
626
+ ),
627
+ combined: true,
628
+ });
629
+ }
630
+
631
+ //----
632
+ // COMBINE CONDITIONS
633
+ //----
634
+ return top.length && binaries.length
635
+ ? config.combiner(explore)("and")(
636
+ input,
637
+ [
638
+ ...top,
639
+ {
640
+ expression: config.combiner(explore)("or")(
641
+ input,
642
+ binaries,
643
+ meta.getName(),
644
+ ),
645
+ combined: true,
646
+ },
647
+ ],
648
+ meta.getName(),
649
+ )
650
+ : binaries.length
651
+ ? config.combiner(explore)("or")(
652
+ input,
653
+ binaries,
654
+ meta.getName(),
655
+ )
656
+ : config.success;
657
+ };
658
+
659
+ export const decode_object =
660
+ (config: IConfig) => (importer: FunctionImporter) => {
661
+ const func = FeatureProgrammer.decode_object(config)(importer);
662
+ return (
663
+ input: ts.Expression,
664
+ obj: MetadataObject,
665
+ explore: IExplore,
666
+ ) => {
667
+ obj.validated = true;
668
+ return func(input, obj, explore);
669
+ };
670
+ };
671
+
672
+ const decode_array =
673
+ (project: IProject) =>
674
+ (config: IConfig) =>
675
+ (importer: FunctionImporter) =>
676
+ (
677
+ input: ts.Expression,
678
+ array: MetadataArray,
679
+ explore: IExplore,
680
+ metaTags: IMetadataTag[],
681
+ jsDocTags: IJsDocTagInfo[],
682
+ ) => {
683
+ if (array.recursive === false)
684
+ return decode_array_inline(project)(config)(importer)(
685
+ input,
686
+ array,
687
+ explore,
688
+ metaTags,
689
+ jsDocTags,
690
+ );
691
+
692
+ explore = {
693
+ ...explore,
694
+ source: "function",
695
+ from: "array",
696
+ };
697
+ return ts.factory.createLogicalOr(
698
+ ts.factory.createCallExpression(
699
+ ts.factory.createIdentifier(
700
+ importer.useLocal(`${config.prefix}a${array.index}`),
701
+ ),
702
+ undefined,
703
+ FeatureProgrammer.argumentsArray(config)({
704
+ ...explore,
705
+ source: "function",
706
+ from: "array",
707
+ })(input),
708
+ ),
709
+ config.joiner.failure(input, array.name, explore),
710
+ );
711
+ };
712
+
713
+ const decode_array_inline =
714
+ (project: IProject) =>
715
+ (config: IConfig) =>
716
+ (importer: FunctionImporter) =>
717
+ FeatureProgrammer.decode_array({
718
+ prefix: config.prefix,
719
+ trace: config.trace,
720
+ path: config.path,
721
+ decoder: () => decode(project)(config)(importer),
722
+ })(importer)(config.joiner.array);
723
+
724
+ const decode_tuple =
725
+ (project: IProject) =>
726
+ (config: IConfig) =>
727
+ (importer: FunctionImporter) =>
728
+ (
729
+ input: ts.Expression,
730
+ tuple: MetadataTuple,
731
+ explore: IExplore,
732
+ tagList: IMetadataTag[],
733
+ jsDocTags: ts.JSDocTagInfo[],
734
+ ): ts.Expression => {
735
+ if (tuple.recursive === false)
736
+ return decode_tuple_inline(project)(config)(importer)(
737
+ input,
738
+ tuple,
739
+ explore,
740
+ tagList,
741
+ jsDocTags,
742
+ );
743
+ explore = {
744
+ ...explore,
745
+ source: "function",
746
+ from: "array",
747
+ };
748
+ return ts.factory.createLogicalOr(
749
+ ts.factory.createCallExpression(
750
+ ts.factory.createIdentifier(
751
+ importer.useLocal(`${config.prefix}t${tuple.index}`),
752
+ ),
753
+ undefined,
754
+ FeatureProgrammer.argumentsArray(config)({
755
+ ...explore,
756
+ source: "function",
757
+ })(input),
758
+ ),
759
+ config.joiner.failure(input, tuple.name, explore),
760
+ );
761
+ };
762
+
763
+ const decode_tuple_inline =
764
+ (project: IProject) =>
765
+ (config: IConfig) =>
766
+ (importer: FunctionImporter) =>
767
+ (
768
+ input: ts.Expression,
769
+ tuple: MetadataTuple,
770
+ explore: IExplore,
771
+ tagList: IMetadataTag[],
772
+ jsDocTags: ts.JSDocTagInfo[],
773
+ ): ts.Expression => {
774
+ const binaries: ts.Expression[] = tuple.elements
775
+ .filter((meta) => meta.rest === null)
776
+ .map((meta, index) =>
777
+ decode(project)(config)(importer)(
778
+ ts.factory.createElementAccessExpression(input, index),
779
+ meta,
780
+ {
781
+ ...explore,
782
+ from: "array",
783
+ postfix: explore.postfix.length
784
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
785
+ : `"[${index}]"`,
786
+ },
787
+ tagList,
788
+ jsDocTags,
789
+ ),
790
+ );
791
+ const rest: ts.Expression | null =
792
+ tuple.elements.length && tuple.elements.at(-1)!.rest !== null
793
+ ? decode(project)(config)(importer)(
794
+ ts.factory.createCallExpression(
795
+ IdentifierFactory.access(input)("slice"),
796
+ undefined,
797
+ [
798
+ ts.factory.createNumericLiteral(
799
+ tuple.elements.length - 1,
800
+ ),
801
+ ],
802
+ ),
803
+ wrap_metadata_rest_tuple(
804
+ tuple.elements.at(-1)!.rest!,
805
+ ),
806
+ {
807
+ ...explore,
808
+ start: tuple.elements.length - 1,
809
+ },
810
+ tagList,
811
+ jsDocTags,
812
+ )
813
+ : null;
814
+
815
+ const arrayLength = ts.factory.createPropertyAccessExpression(
816
+ input,
817
+ "length",
818
+ );
819
+ return config.combiner(explore)("and")(
820
+ input,
821
+ [
822
+ ...(rest === null
823
+ ? tuple.elements.every((t) => t.optional === false)
824
+ ? [
825
+ {
826
+ combined: false,
827
+ expression:
828
+ ts.factory.createStrictEquality(
829
+ arrayLength,
830
+ ts.factory.createNumericLiteral(
831
+ tuple.elements.length,
832
+ ),
833
+ ),
834
+ },
835
+ ]
836
+ : [
837
+ {
838
+ combined: false,
839
+ expression: ts.factory.createLogicalAnd(
840
+ ts.factory.createLessThanEquals(
841
+ ts.factory.createNumericLiteral(
842
+ tuple.elements.filter(
843
+ (t) =>
844
+ t.optional === false,
845
+ ).length,
846
+ ),
847
+ arrayLength,
848
+ ),
849
+ ts.factory.createGreaterThanEquals(
850
+ ts.factory.createNumericLiteral(
851
+ tuple.elements.length,
852
+ ),
853
+ arrayLength,
854
+ ),
855
+ ),
856
+ },
857
+ ]
858
+ : []),
859
+ ...(config.joiner.tuple
860
+ ? [
861
+ {
862
+ expression: config.joiner.tuple(binaries),
863
+ combined: true,
864
+ },
865
+ ]
866
+ : binaries.map((expression) => ({
867
+ expression,
868
+ combined: true,
869
+ }))),
870
+ ...(rest !== null
871
+ ? [
872
+ {
873
+ expression: rest,
874
+ combined: true,
875
+ },
876
+ ]
877
+ : []),
878
+ ],
879
+ `[${tuple.elements.map((t) => t.getName()).join(", ")}]`,
880
+ );
881
+ };
882
+
883
+ /* -----------------------------------------------------------
884
+ UNION TYPE EXPLORERS
885
+ ----------------------------------------------------------- */
886
+ const explore_sets =
887
+ (project: IProject) =>
888
+ (config: IConfig) =>
889
+ (importer: FunctionImporter) =>
890
+ (
891
+ input: ts.Expression,
892
+ sets: Metadata[],
893
+ explore: IExplore,
894
+ tags: IMetadataTag[],
895
+ jsDocTags: IJsDocTagInfo[],
896
+ ): ts.Expression =>
897
+ ts.factory.createCallExpression(
898
+ UnionExplorer.set({
899
+ checker: decode(project)(config)(importer),
900
+ decoder: decode_array(project)(config)(importer),
901
+ empty: config.success,
902
+ success: config.success,
903
+ failure: (input, expected, explore) =>
904
+ ts.factory.createReturnStatement(
905
+ config.joiner.failure(input, expected, explore),
906
+ ),
907
+ })([])(input, sets, explore, tags, jsDocTags),
908
+ undefined,
909
+ undefined,
910
+ );
911
+
912
+ const explore_maps =
913
+ (project: IProject) =>
914
+ (config: IConfig) =>
915
+ (importer: FunctionImporter) =>
916
+ (
917
+ input: ts.Expression,
918
+ maps: Metadata.Entry[],
919
+ explore: IExplore,
920
+ tags: IMetadataTag[],
921
+ jsDocTags: IJsDocTagInfo[],
922
+ ): ts.Expression =>
923
+ ts.factory.createCallExpression(
924
+ UnionExplorer.map({
925
+ checker: (input, entry, explore) => {
926
+ const func = decode(project)(config)(importer);
927
+ return ts.factory.createLogicalAnd(
928
+ func(
929
+ ts.factory.createElementAccessExpression(
930
+ input,
931
+ 0,
932
+ ),
933
+ entry[0],
934
+ {
935
+ ...explore,
936
+ postfix: `${explore.postfix}[0]`,
937
+ },
938
+ [],
939
+ [],
940
+ ),
941
+ func(
942
+ ts.factory.createElementAccessExpression(
943
+ input,
944
+ 1,
945
+ ),
946
+ entry[1],
947
+ {
948
+ ...explore,
949
+ postfix: `${explore.postfix}[1]`,
950
+ },
951
+ [],
952
+ [],
953
+ ),
954
+ );
955
+ },
956
+ decoder: decode_array(project)(config)(importer),
957
+ empty: config.success,
958
+ success: config.success,
959
+ failure: (input, expected, explore) =>
960
+ ts.factory.createReturnStatement(
961
+ config.joiner.failure(input, expected, explore),
962
+ ),
963
+ })([])(input, maps, explore, tags, jsDocTags),
964
+ undefined,
965
+ undefined,
966
+ );
967
+
968
+ const explore_tuples =
969
+ (project: IProject) =>
970
+ (config: IConfig) =>
971
+ (importer: FunctionImporter) =>
972
+ (
973
+ input: ts.Expression,
974
+ tuples: MetadataTuple[],
975
+ explore: IExplore,
976
+ tags: IMetadataTag[],
977
+ jsDocTags: IJsDocTagInfo[],
978
+ ): ts.Expression =>
979
+ explore_array_like_union_types(config)(importer)(
980
+ UnionExplorer.tuple({
981
+ checker: decode_tuple(project)(config)(importer),
982
+ decoder: decode_tuple(project)(config)(importer),
983
+ empty: config.success,
984
+ success: config.success,
985
+ failure: (input, expected, explore) =>
986
+ ts.factory.createReturnStatement(
987
+ config.joiner.failure(input, expected, explore),
988
+ ),
989
+ }),
990
+ )(input, tuples, explore, tags, jsDocTags);
991
+
992
+ const explore_arrays =
993
+ (project: IProject) =>
994
+ (config: IConfig) =>
995
+ (importer: FunctionImporter) =>
996
+ (
997
+ input: ts.Expression,
998
+ arrays: MetadataArray[],
999
+ explore: IExplore,
1000
+ tags: IMetadataTag[],
1001
+ jsDocTags: IJsDocTagInfo[],
1002
+ ): ts.Expression =>
1003
+ explore_array_like_union_types(config)(importer)(
1004
+ UnionExplorer.array({
1005
+ checker: decode(project)(config)(importer),
1006
+ decoder: decode_array(project)(config)(importer),
1007
+ empty: config.success,
1008
+ success: config.success,
1009
+ failure: (input, expected, explore) =>
1010
+ ts.factory.createReturnStatement(
1011
+ config.joiner.failure(input, expected, explore),
1012
+ ),
1013
+ }),
1014
+ )(input, arrays, explore, tags, jsDocTags);
1015
+
1016
+ const explore_arrays_and_tuples =
1017
+ (project: IProject) =>
1018
+ (config: IConfig) =>
1019
+ (importer: FunctionImporter) =>
1020
+ (
1021
+ input: ts.Expression,
1022
+ elements: Array<MetadataArray | MetadataTuple>,
1023
+ explore: IExplore,
1024
+ tags: IMetadataTag[],
1025
+ jsDocTags: IJsDocTagInfo[],
1026
+ ): ts.Expression =>
1027
+ explore_array_like_union_types(config)(importer)(
1028
+ UnionExplorer.array_or_tuple({
1029
+ checker: (front, target, explore, tags, jsDocTags, array) =>
1030
+ target instanceof MetadataTuple
1031
+ ? decode_tuple(project)(config)(importer)(
1032
+ front,
1033
+ target,
1034
+ explore,
1035
+ tags,
1036
+ jsDocTags,
1037
+ )
1038
+ : config.atomist(explore)({
1039
+ expression: decode(project)(config)(importer)(
1040
+ front,
1041
+ target,
1042
+ explore,
1043
+ tags,
1044
+ jsDocTags,
1045
+ ),
1046
+ tags: check_array_length(tags)(array),
1047
+ })(array),
1048
+ decoder: (input, target, explore, tags, jsDocTags) =>
1049
+ target instanceof MetadataTuple
1050
+ ? decode_tuple(project)(config)(importer)(
1051
+ input,
1052
+ target,
1053
+ explore,
1054
+ tags,
1055
+ jsDocTags,
1056
+ )
1057
+ : decode_array(project)(config)(importer)(
1058
+ input,
1059
+ target,
1060
+ explore,
1061
+ tags,
1062
+ jsDocTags,
1063
+ ),
1064
+ empty: config.success,
1065
+ success: config.success,
1066
+ failure: (input, expected, explore) =>
1067
+ ts.factory.createReturnStatement(
1068
+ config.joiner.failure(input, expected, explore),
1069
+ ),
1070
+ }),
1071
+ )(input, elements, explore, tags, jsDocTags);
1072
+
1073
+ const explore_array_like_union_types =
1074
+ (config: IConfig) =>
1075
+ (importer: FunctionImporter) =>
1076
+ <T extends MetadataArray | MetadataTuple>(
1077
+ factory: (
1078
+ parameters: ts.ParameterDeclaration[],
1079
+ ) => (
1080
+ input: ts.Expression,
1081
+ elements: T[],
1082
+ explore: IExplore,
1083
+ tags: IMetadataTag[],
1084
+ jsDocTags: IJsDocTagInfo[],
1085
+ ) => ts.ArrowFunction,
1086
+ ) =>
1087
+ (
1088
+ input: ts.Expression,
1089
+ elements: T[],
1090
+ explore: IExplore,
1091
+ tags: IMetadataTag[],
1092
+ jsDocTags: IJsDocTagInfo[],
1093
+ ): ts.Expression => {
1094
+ const arrow =
1095
+ (parameters: ts.ParameterDeclaration[]) =>
1096
+ (explore: IExplore) =>
1097
+ (input: ts.Expression): ts.ArrowFunction =>
1098
+ factory(parameters)(
1099
+ input,
1100
+ elements,
1101
+ explore,
1102
+ tags,
1103
+ jsDocTags,
1104
+ );
1105
+ if (elements.every((e) => e.recursive === false))
1106
+ ts.factory.createCallExpression(
1107
+ arrow([])(explore)(input),
1108
+ undefined,
1109
+ [],
1110
+ );
1111
+ explore = {
1112
+ ...explore,
1113
+ source: "function",
1114
+ from: "array",
1115
+ };
1116
+ return ts.factory.createLogicalOr(
1117
+ ts.factory.createCallExpression(
1118
+ ts.factory.createIdentifier(
1119
+ importer.emplaceUnion(
1120
+ config.prefix,
1121
+ elements.map((e) => e.name).join(" | "),
1122
+ () =>
1123
+ arrow(
1124
+ FeatureProgrammer.parameterDeclarations(
1125
+ config,
1126
+ )(TypeFactory.keyword("any"))(
1127
+ ts.factory.createIdentifier("input"),
1128
+ ),
1129
+ )({
1130
+ ...explore,
1131
+ postfix: "",
1132
+ })(ts.factory.createIdentifier("input")),
1133
+ ),
1134
+ ),
1135
+ undefined,
1136
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
1137
+ ),
1138
+ config.joiner.failure(
1139
+ input,
1140
+ elements.map((e) => e.name).join(" | "),
1141
+ explore,
1142
+ ),
1143
+ );
1144
+ };
1145
+
1146
+ const explore_objects =
1147
+ (config: IConfig) =>
1148
+ (importer: FunctionImporter) =>
1149
+ (input: ts.Expression, meta: Metadata, explore: IExplore) =>
1150
+ meta.objects.length === 1
1151
+ ? decode_object(config)(importer)(
1152
+ input,
1153
+ meta.objects[0]!,
1154
+ explore,
1155
+ )
1156
+ : ts.factory.createCallExpression(
1157
+ ts.factory.createIdentifier(
1158
+ importer.useLocal(
1159
+ `${config.prefix}u${meta.union_index!}`,
1160
+ ),
1161
+ ),
1162
+ undefined,
1163
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
1164
+ );
1165
+ }
1166
+
1167
+ const create_add =
1168
+ (binaries: CheckerProgrammer.IBinary[]) =>
1169
+ (defaultInput: ts.Expression) =>
1170
+ (
1171
+ exact: boolean,
1172
+ left: ts.Expression,
1173
+ right: ts.Expression = defaultInput,
1174
+ ) => {
1175
+ const factory = exact
1176
+ ? ts.factory.createStrictEquality
1177
+ : ts.factory.createStrictInequality;
1178
+ binaries.push({
1179
+ expression: factory(left, right),
1180
+ combined: false,
1181
+ });
1182
+ };