typia 5.0.0-dev.20230823-2 → 5.0.0-dev.2023084

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