typia 3.8.3 → 3.8.4-dev.20230502

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 (157) hide show
  1. package/lib/functional/$dictionary.js +3 -2
  2. package/lib/functional/$dictionary.js.map +1 -1
  3. package/package.json +1 -1
  4. package/src/Primitive.ts +123 -123
  5. package/src/executable/TypiaGenerateWizard.ts +85 -85
  6. package/src/executable/TypiaSetupWizard.ts +133 -133
  7. package/src/executable/setup/CommandExecutor.ts +8 -8
  8. package/src/executable/setup/PackageManager.ts +71 -71
  9. package/src/executable/setup/PluginConfigurator.ts +70 -70
  10. package/src/executable/typia.ts +52 -52
  11. package/src/factories/CommentFactory.ts +14 -14
  12. package/src/factories/ExpressionFactory.ts +70 -70
  13. package/src/factories/IdentifierFactory.ts +63 -63
  14. package/src/factories/LiteralFactory.ts +39 -39
  15. package/src/factories/MetadataCollection.ts +121 -121
  16. package/src/factories/MetadataFactory.ts +62 -62
  17. package/src/factories/MetadataTagFactory.ts +297 -297
  18. package/src/factories/StatementFactory.ts +24 -24
  19. package/src/factories/TemplateFactory.ts +58 -58
  20. package/src/factories/TypeFactory.ts +124 -124
  21. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  22. package/src/factories/internal/metadata/emplace_metadata_object.ts +143 -143
  23. package/src/factories/internal/metadata/iterate_metadata.ts +67 -67
  24. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  25. package/src/factories/internal/metadata/iterate_metadata_native.ts +219 -219
  26. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +27 -27
  27. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  28. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +48 -48
  29. package/src/factories/internal/metadata/iterate_metadata_union.ts +57 -57
  30. package/src/functional/$any.ts +2 -2
  31. package/src/functional/$dictionary.ts +25 -20
  32. package/src/functional/$is_between.ts +2 -2
  33. package/src/functional/$is_custom.ts +14 -14
  34. package/src/functional/$is_date.ts +3 -3
  35. package/src/functional/$is_datetime.ts +2 -2
  36. package/src/functional/$is_email.ts +4 -4
  37. package/src/functional/$is_ipv4.ts +4 -4
  38. package/src/functional/$is_ipv6.ts +4 -4
  39. package/src/functional/$is_url.ts +4 -4
  40. package/src/functional/$is_uuid.ts +4 -4
  41. package/src/functional/$join.ts +46 -46
  42. package/src/functional/$number.ts +12 -12
  43. package/src/functional/$rest.ts +3 -3
  44. package/src/functional/$string.ts +37 -37
  45. package/src/functional/$tail.ts +5 -5
  46. package/src/metadata/IMetadata.ts +26 -26
  47. package/src/metadata/Metadata.ts +539 -539
  48. package/src/metadata/MetadataConstant.ts +3 -3
  49. package/src/metadata/MetadataObject.ts +127 -127
  50. package/src/module.ts +2043 -2043
  51. package/src/programmers/ApplicationProgrammer.ts +62 -62
  52. package/src/programmers/AssertCloneProgrammer.ts +79 -79
  53. package/src/programmers/AssertParseProgrammer.ts +74 -74
  54. package/src/programmers/AssertProgrammer.ts +285 -285
  55. package/src/programmers/AssertPruneProgrammer.ts +76 -76
  56. package/src/programmers/AssertStringifyProgrammer.ts +74 -74
  57. package/src/programmers/CheckerProgrammer.ts +901 -901
  58. package/src/programmers/CloneProgrammer.ts +389 -389
  59. package/src/programmers/FeatureProgrammer.ts +500 -500
  60. package/src/programmers/IsCloneProgrammer.ts +86 -86
  61. package/src/programmers/IsParseProgrammer.ts +80 -80
  62. package/src/programmers/IsProgrammer.ts +212 -212
  63. package/src/programmers/IsPruneProgrammer.ts +81 -81
  64. package/src/programmers/IsStringifyProgrammer.ts +84 -84
  65. package/src/programmers/LiteralsProgrammer.ts +65 -65
  66. package/src/programmers/PruneProgrammer.ts +347 -347
  67. package/src/programmers/RandomProgrammer.ts +423 -423
  68. package/src/programmers/StringifyProgrammer.ts +798 -798
  69. package/src/programmers/TypiaProgrammer.ts +129 -129
  70. package/src/programmers/ValidateCloneProgrammer.ts +93 -93
  71. package/src/programmers/ValidateParseProgrammer.ts +78 -78
  72. package/src/programmers/ValidateProgrammer.ts +316 -316
  73. package/src/programmers/ValidatePruneProgrammer.ts +86 -86
  74. package/src/programmers/ValidateStringifyProgrammer.ts +92 -92
  75. package/src/programmers/helpers/CloneJoiner.ts +131 -131
  76. package/src/programmers/helpers/FunctionImporeter.ts +54 -54
  77. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  78. package/src/programmers/helpers/PruneJoiner.ts +144 -144
  79. package/src/programmers/helpers/StringifyJoinder.ts +113 -113
  80. package/src/programmers/helpers/StringifyPredicator.ts +12 -12
  81. package/src/programmers/helpers/UnionExplorer.ts +275 -275
  82. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  83. package/src/programmers/internal/application_array.ts +36 -36
  84. package/src/programmers/internal/application_boolean.ts +17 -17
  85. package/src/programmers/internal/application_constant.ts +30 -30
  86. package/src/programmers/internal/application_default_string.ts +33 -33
  87. package/src/programmers/internal/application_native.ts +32 -32
  88. package/src/programmers/internal/application_number.ts +73 -73
  89. package/src/programmers/internal/application_object.ts +155 -155
  90. package/src/programmers/internal/application_schema.ts +213 -213
  91. package/src/programmers/internal/application_string.ts +45 -45
  92. package/src/programmers/internal/application_templates.ts +26 -26
  93. package/src/programmers/internal/application_tuple.ts +31 -31
  94. package/src/programmers/internal/check_array.ts +30 -30
  95. package/src/programmers/internal/check_custom.ts +31 -31
  96. package/src/programmers/internal/check_dynamic_properties.ts +194 -194
  97. package/src/programmers/internal/check_everything.ts +28 -28
  98. package/src/programmers/internal/check_native.ts +21 -21
  99. package/src/programmers/internal/check_string_tags.ts +67 -67
  100. package/src/programmers/internal/check_union_array_like.ts +271 -271
  101. package/src/programmers/internal/check_union_tuple.ts +33 -33
  102. package/src/programmers/internal/random_custom.ts +29 -29
  103. package/src/programmers/internal/stringify_dynamic_properties.ts +167 -167
  104. package/src/programmers/internal/stringify_native.ts +7 -7
  105. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  106. package/src/schemas/IJsonSchema.ts +130 -130
  107. package/src/transform.ts +17 -17
  108. package/src/transformers/CallExpressionTransformer.ts +177 -177
  109. package/src/transformers/FileTransformer.ts +47 -47
  110. package/src/transformers/NodeTransformer.ts +13 -13
  111. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +114 -114
  112. package/src/transformers/features/miscellaneous/AssertCloneTransformer.ts +9 -9
  113. package/src/transformers/features/miscellaneous/AssertPruneTransformer.ts +9 -9
  114. package/src/transformers/features/miscellaneous/CloneTransformer.ts +9 -9
  115. package/src/transformers/features/miscellaneous/CreateAssertCloneTransformer.ts +9 -9
  116. package/src/transformers/features/miscellaneous/CreateAssertPruneTransformer.ts +9 -9
  117. package/src/transformers/features/miscellaneous/CreateCloneTransformer.ts +9 -9
  118. package/src/transformers/features/miscellaneous/CreateIsCloneTransformer.ts +9 -9
  119. package/src/transformers/features/miscellaneous/CreateIsPruneTransformer.ts +9 -9
  120. package/src/transformers/features/miscellaneous/CreatePruneTransformer.ts +9 -9
  121. package/src/transformers/features/miscellaneous/CreateRandomTransformer.ts +39 -39
  122. package/src/transformers/features/miscellaneous/CreateValidateCloneTransformer.ts +9 -9
  123. package/src/transformers/features/miscellaneous/CreateValidatePruneTransformer.ts +9 -9
  124. package/src/transformers/features/miscellaneous/IsCloneTransformer.ts +9 -9
  125. package/src/transformers/features/miscellaneous/IsPruneTransformer.ts +9 -9
  126. package/src/transformers/features/miscellaneous/LiteralsTransformer.ts +28 -28
  127. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +52 -52
  128. package/src/transformers/features/miscellaneous/PruneTransformer.ts +9 -9
  129. package/src/transformers/features/miscellaneous/RandomTransformer.ts +42 -42
  130. package/src/transformers/features/miscellaneous/ValidateCloneTransformer.ts +9 -9
  131. package/src/transformers/features/miscellaneous/ValidatePruneTransformer.ts +9 -9
  132. package/src/transformers/features/parsers/AssertParseTransformer.ts +9 -9
  133. package/src/transformers/features/parsers/CreateAssertParseTransformer.ts +9 -9
  134. package/src/transformers/features/parsers/CreateIsParseTransformer.ts +9 -9
  135. package/src/transformers/features/parsers/CreateValidateParseTransformer.ts +9 -9
  136. package/src/transformers/features/parsers/IsParseTransformer.ts +9 -9
  137. package/src/transformers/features/parsers/ValidateParseTransformer.ts +9 -9
  138. package/src/transformers/features/stringifiers/AssertStringifyTransformer.ts +10 -10
  139. package/src/transformers/features/stringifiers/CreateAssertStringifyTransformer.ts +12 -12
  140. package/src/transformers/features/stringifiers/CreateIsStringifyTransformer.ts +9 -9
  141. package/src/transformers/features/stringifiers/CreateStringifyTransformer.ts +9 -9
  142. package/src/transformers/features/stringifiers/CreateValidateStringifyProgrammer.ts +12 -12
  143. package/src/transformers/features/stringifiers/IsStringifyTransformer.ts +9 -9
  144. package/src/transformers/features/stringifiers/StringifyTransformer.ts +9 -9
  145. package/src/transformers/features/stringifiers/ValidateStringifyTransformer.ts +10 -10
  146. package/src/transformers/features/validators/AssertTransformer.ts +11 -11
  147. package/src/transformers/features/validators/CreateAssertTransformer.ts +13 -13
  148. package/src/transformers/features/validators/CreateIsTransformer.ts +11 -11
  149. package/src/transformers/features/validators/CreateValidateTransformer.ts +13 -13
  150. package/src/transformers/features/validators/IsTransformer.ts +11 -11
  151. package/src/transformers/features/validators/ValidateTransformer.ts +11 -11
  152. package/src/transformers/internal/GenericTransformer.ts +97 -97
  153. package/src/utils/ArrayUtil.ts +44 -44
  154. package/src/utils/Escaper.ts +45 -45
  155. package/src/utils/MapUtil.ts +12 -12
  156. package/src/utils/PatternUtil.ts +30 -30
  157. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,500 +1,500 @@
1
- import ts from "typescript";
2
-
3
- import { IdentifierFactory } from "../factories/IdentifierFactory";
4
- import { MetadataCollection } from "../factories/MetadataCollection";
5
- import { StatementFactory } from "../factories/StatementFactory";
6
- import { TypeFactory } from "../factories/TypeFactory";
7
- import { ValueFactory } from "../factories/ValueFactory";
8
-
9
- import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
10
- import { IMetadataTag } from "../metadata/IMetadataTag";
11
- import { Metadata } from "../metadata/Metadata";
12
- import { MetadataObject } from "../metadata/MetadataObject";
13
-
14
- import { IProject } from "../transformers/IProject";
15
-
16
- import { CheckerProgrammer } from "./CheckerProgrammer";
17
- import { FunctionImporter } from "./helpers/FunctionImporeter";
18
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
19
- import { UnionExplorer } from "./helpers/UnionExplorer";
20
- import { feature_object_entries } from "./internal/feature_object_entries";
21
-
22
- export namespace FeatureProgrammer {
23
- /* -----------------------------------------------------------
24
- PARAMETERS
25
- ----------------------------------------------------------- */
26
- export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
27
- types: IConfig.ITypes;
28
-
29
- /**
30
- * Prefix name of functions for specific object types.
31
- */
32
- functors: string;
33
-
34
- /**
35
- * Prefix name of functions for union object types.
36
- */
37
- unioners: string;
38
-
39
- /**
40
- * Whether to archive access path or not.
41
- */
42
- path: boolean;
43
-
44
- /**
45
- * Whether to trace exception or not.
46
- */
47
- trace: boolean;
48
-
49
- addition?(collection: MetadataCollection): ts.Statement[];
50
-
51
- /**
52
- * Initializer of metadata.
53
- */
54
- initializer: (
55
- project: IProject,
56
- ) => (type: ts.Type) => [MetadataCollection, Metadata];
57
-
58
- /**
59
- * Decoder, station of every types.
60
- */
61
- decoder: Decoder<Metadata, Output>;
62
-
63
- /**
64
- * Object configurator.
65
- */
66
- objector: IConfig.IObjector;
67
-
68
- /**
69
- * Generator of functions for object types.
70
- */
71
- generator?: Partial<IConfig.IGenerator>;
72
- }
73
- export namespace IConfig {
74
- export interface ITypes {
75
- input: (type: ts.Type, name?: string) => ts.TypeNode;
76
- output: (type: ts.Type, name?: string) => ts.TypeNode;
77
- }
78
-
79
- export interface IObjector {
80
- /**
81
- * Type checker when union object type comes.
82
- */
83
- checker: Decoder<Metadata, ts.Expression>;
84
-
85
- /**
86
- * Decoder, function call expression generator of specific typed objects.
87
- */
88
- decoder: Decoder<MetadataObject, ts.Expression>;
89
-
90
- /**
91
- * Joiner of expressions from properties.
92
- */
93
- joiner(
94
- input: ts.Expression,
95
- entries: IExpressionEntry[],
96
- parent: MetadataObject,
97
- ): ts.ConciseBody;
98
-
99
- /**
100
- * Union type specificator.
101
- *
102
- * Expression of an algorithm specifying object type and calling
103
- * the `decoder` function of the specified object type.
104
- */
105
- unionizer: Decoder<MetadataObject[], ts.Expression>;
106
-
107
- /**
108
- * Handler of union type specification failure.
109
- *
110
- * @param value Expression of input parameter
111
- * @param expected Expected type name
112
- * @param explore Exploration info
113
- * @returns Statement of failure
114
- */
115
- failure(
116
- value: ts.Expression,
117
- expected: string,
118
- explore?: IExplore,
119
- ): ts.Statement;
120
-
121
- /**
122
- * Transformer of type checking expression by discrimination.
123
- *
124
- * When an object type has been specified by a discrimination without full
125
- * iteration, the `unionizer` will decode the object instance after
126
- * the last type checking.
127
- *
128
- * In such circumtance, you can transform the last type checking function.
129
- *
130
- * @param exp Current expression about type checking
131
- * @returns Transformed expression
132
- * @deprecated
133
- */
134
- is?(exp: ts.Expression): ts.Expression;
135
-
136
- /**
137
- * Transformer of non-undefined type checking by discrimination.
138
- *
139
- * When specifying an union type of objects, `typia` tries to find
140
- * descrimination way just by checking only one property type.
141
- * If succeeded to find the discrimination way, `typia` will check the target
142
- * property type and in the checking, non-undefined type checking would be
143
- * done.
144
- *
145
- * In such process, you can transform the non-undefined type checking.
146
- *
147
- * @param exp
148
- * @returns Transformed expression
149
- * @deprecated
150
- */
151
- required?(exp: ts.Expression): ts.Expression;
152
-
153
- /**
154
- * Conditon wrapper when unable to specify any object type.
155
- *
156
- * When failed to specify an object type through discrimination, full
157
- * iteration type checking would be happend. In such circumstance, you
158
- * can wrap the condition with additional function.
159
- *
160
- * @param condition Current condition
161
- * @returns A function wrapped current condition
162
- */
163
- full?: (
164
- condition: ts.Expression,
165
- ) => (
166
- input: ts.Expression,
167
- expected: string,
168
- explore: IExplore,
169
- ) => ts.Expression;
170
-
171
- /**
172
- * Return type.
173
- */
174
- type?: ts.TypeNode;
175
- }
176
- export interface IGenerator {
177
- /**
178
- *
179
- * @param col
180
- */
181
- functors(col: MetadataCollection): ts.VariableStatement[];
182
-
183
- /**
184
- *
185
- * @param col
186
- */
187
- unioners(col: MetadataCollection): ts.VariableStatement[];
188
- }
189
- }
190
-
191
- export interface IExplore {
192
- tracable: boolean;
193
- source: "top" | "object";
194
- from: "top" | "array" | "object";
195
- postfix: string;
196
- start?: number;
197
- }
198
-
199
- export interface Decoder<
200
- T,
201
- Output extends ts.ConciseBody = ts.ConciseBody,
202
- > {
203
- (
204
- input: ts.Expression,
205
- target: T,
206
- explore: IExplore,
207
- metaTags: IMetadataTag[],
208
- jsDocTags: ts.JSDocTagInfo[],
209
- ): Output;
210
- }
211
-
212
- /* -----------------------------------------------------------
213
- GENERATORS
214
- ----------------------------------------------------------- */
215
- export const analyze =
216
- (project: IProject) =>
217
- (config: IConfig) =>
218
- (importer: FunctionImporter) =>
219
- (type: ts.Type, name?: string) => {
220
- const [collection, meta] = config.initializer(project)(type);
221
-
222
- // ITERATE OVER ALL METADATA
223
- const output: ts.ConciseBody = config.decoder(
224
- ValueFactory.INPUT(),
225
- meta,
226
- {
227
- tracable: config.path || config.trace,
228
- source: "top",
229
- from: "top",
230
- postfix: '""',
231
- },
232
- [],
233
- [],
234
- );
235
-
236
- // RETURNS THE OPTIMAL ARROW FUNCTION
237
- const functors: ts.VariableStatement[] = (
238
- config.generator?.functors ?? write_functors(config)(importer)
239
- )(collection);
240
- const unioners: ts.VariableStatement[] = (
241
- config.generator?.unioners ?? write_unioners(config)(importer)
242
- )(collection);
243
- const added: ts.Statement[] = (config.addition ?? (() => []))(
244
- collection,
245
- );
246
-
247
- return ts.factory.createArrowFunction(
248
- undefined,
249
- undefined,
250
- PARAMETERS(config)(config.types.input(type, name))(
251
- ValueFactory.INPUT(),
252
- ),
253
- config.types.output(type, name),
254
- undefined,
255
- ts.factory.createBlock(
256
- [
257
- ...added,
258
- ...functors.filter((_, i) =>
259
- importer.hasLocal(`${config.functors}${i}`),
260
- ),
261
- ...unioners.filter((_, i) =>
262
- importer.hasLocal(`${config.unioners}${i}`),
263
- ),
264
- ...(ts.isBlock(output)
265
- ? output.statements
266
- : [ts.factory.createReturnStatement(output)]),
267
- ],
268
- true,
269
- ),
270
- );
271
- };
272
-
273
- export const write_functors =
274
- (config: IConfig) =>
275
- (importer: FunctionImporter) =>
276
- (collection: MetadataCollection) =>
277
- collection
278
- .objects()
279
- .map((obj, i) =>
280
- StatementFactory.constant(
281
- `${config.functors}${i}`,
282
- write_object(config)(importer)(obj),
283
- ),
284
- );
285
-
286
- export const write_unioners =
287
- (config: IConfig) =>
288
- (importer: FunctionImporter) =>
289
- (collection: MetadataCollection) =>
290
- collection
291
- .unions()
292
- .map((union, i) =>
293
- StatementFactory.constant(
294
- importer.useLocal(`${config.unioners}${i}`),
295
- write_union(config)(union),
296
- ),
297
- );
298
-
299
- const write_object =
300
- (config: IConfig) =>
301
- (importer: FunctionImporter) =>
302
- (obj: MetadataObject) =>
303
- ts.factory.createArrowFunction(
304
- undefined,
305
- undefined,
306
- PARAMETERS(config)(TypeFactory.keyword("any"))(
307
- ValueFactory.INPUT(),
308
- ),
309
- config.objector.type ?? TypeFactory.keyword("any"),
310
- undefined,
311
- config.objector.joiner(
312
- ts.factory.createIdentifier("input"),
313
- feature_object_entries(config)(importer)(obj)(
314
- ts.factory.createIdentifier("input"),
315
- ),
316
- obj,
317
- ),
318
- );
319
-
320
- const write_union = (config: IConfig) => {
321
- const explorer = UnionExplorer.object(config);
322
- const input = ValueFactory.INPUT();
323
-
324
- return (meta: MetadataObject[]) =>
325
- ts.factory.createArrowFunction(
326
- undefined,
327
- undefined,
328
- PARAMETERS(config)(TypeFactory.keyword("any"))(
329
- ValueFactory.INPUT(),
330
- ),
331
- TypeFactory.keyword("any"),
332
- undefined,
333
- explorer(
334
- input,
335
- meta,
336
- {
337
- tracable: config.path || config.trace,
338
- source: "object",
339
- from: "object",
340
- postfix: "",
341
- },
342
- [],
343
- [],
344
- ),
345
- );
346
- };
347
-
348
- /* -----------------------------------------------------------
349
- DECODERS
350
- ----------------------------------------------------------- */
351
- export const decode_array =
352
- (config: Pick<IConfig, "trace" | "path" | "decoder">) =>
353
- (importer: FunctionImporter) =>
354
- (
355
- combiner: (
356
- input: ts.Expression,
357
- arrow: ts.ArrowFunction,
358
- metaTags: IMetadataTag[],
359
- jsDocTags: ts.JSDocTagInfo[],
360
- ) => ts.Expression,
361
- ) => {
362
- const rand: string = importer.increment().toString();
363
- const tail =
364
- config.path || config.trace
365
- ? [
366
- IdentifierFactory.parameter(
367
- "_index" + rand,
368
- TypeFactory.keyword("number"),
369
- ),
370
- ]
371
- : [];
372
-
373
- return (
374
- input: ts.Expression,
375
- meta: Metadata,
376
- explore: IExplore,
377
- metaTags: IMetadataTag[],
378
- jsDocTags: IJsDocTagInfo[],
379
- ) => {
380
- const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
381
- undefined,
382
- undefined,
383
- [
384
- IdentifierFactory.parameter(
385
- "elem",
386
- TypeFactory.keyword("any"),
387
- ),
388
- ...tail,
389
- ],
390
- undefined,
391
- undefined,
392
- config.decoder(
393
- ValueFactory.INPUT("elem"),
394
- meta,
395
- {
396
- tracable: explore.tracable,
397
- source: explore.source,
398
- from: "array",
399
- postfix: INDEX_SYMBOL(explore.start ?? null)(
400
- explore.postfix,
401
- )(rand),
402
- },
403
- metaTags,
404
- jsDocTags,
405
- ),
406
- );
407
- return combiner(input, arrow, metaTags, jsDocTags);
408
- };
409
- };
410
-
411
- export const decode_object =
412
- (config: Pick<IConfig, "trace" | "path" | "functors">) =>
413
- (importer: FunctionImporter) =>
414
- (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
415
- ts.factory.createCallExpression(
416
- ts.factory.createIdentifier(
417
- importer.useLocal(`${config.functors}${obj.index}`),
418
- ),
419
- undefined,
420
- get_object_arguments(config)(explore)(input),
421
- );
422
-
423
- export const get_object_arguments =
424
- (config: Pick<IConfig, "path" | "trace">) =>
425
- (explore: FeatureProgrammer.IExplore) => {
426
- const tail: ts.Expression[] =
427
- config.path === false && config.trace === false
428
- ? []
429
- : config.path === true && config.trace === true
430
- ? [
431
- ts.factory.createIdentifier(
432
- explore.postfix
433
- ? `_path + ${explore.postfix}`
434
- : "_path",
435
- ),
436
- explore.source === "object"
437
- ? ts.factory.createIdentifier(
438
- `${explore.tracable} && _exceptionable`,
439
- )
440
- : explore.tracable
441
- ? ts.factory.createTrue()
442
- : ts.factory.createFalse(),
443
- ]
444
- : config.path === true
445
- ? [
446
- ts.factory.createIdentifier(
447
- explore.postfix
448
- ? `_path + ${explore.postfix}`
449
- : "_path",
450
- ),
451
- ]
452
- : [
453
- explore.source === "object"
454
- ? ts.factory.createIdentifier(
455
- `${explore.tracable} && _exceptionable`,
456
- )
457
- : explore.tracable
458
- ? ts.factory.createTrue()
459
- : ts.factory.createFalse(),
460
- ];
461
- return (input: ts.Expression) => [input, ...tail];
462
- };
463
- }
464
-
465
- const INDEX_SYMBOL =
466
- (start: number | null) => (prev: string) => (rand: string) => {
467
- const tail: string =
468
- start !== null
469
- ? `"[" + (${start} + _index${rand}) + "]"`
470
- : `"[" + _index${rand} + "]"`;
471
- if (prev === "") return tail;
472
- else if (prev[prev.length - 1] === `"`)
473
- return prev.substring(0, prev.length - 1) + tail.substring(1);
474
- return prev + ` + ${tail}`;
475
- };
476
-
477
- const PARAMETERS =
478
- (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
479
- (type: ts.TypeNode) => {
480
- const tail: ts.ParameterDeclaration[] = [];
481
- if (props.path)
482
- tail.push(
483
- IdentifierFactory.parameter(
484
- "_path",
485
- TypeFactory.keyword("string"),
486
- ),
487
- );
488
- if (props.trace)
489
- tail.push(
490
- IdentifierFactory.parameter(
491
- "_exceptionable",
492
- TypeFactory.keyword("boolean"),
493
- ts.factory.createTrue(),
494
- ),
495
- );
496
- return (input: ts.Identifier) => [
497
- IdentifierFactory.parameter(input, type),
498
- ...tail,
499
- ];
500
- };
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
4
+ import { MetadataCollection } from "../factories/MetadataCollection";
5
+ import { StatementFactory } from "../factories/StatementFactory";
6
+ import { TypeFactory } from "../factories/TypeFactory";
7
+ import { ValueFactory } from "../factories/ValueFactory";
8
+
9
+ import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
10
+ import { IMetadataTag } from "../metadata/IMetadataTag";
11
+ import { Metadata } from "../metadata/Metadata";
12
+ import { MetadataObject } from "../metadata/MetadataObject";
13
+
14
+ import { IProject } from "../transformers/IProject";
15
+
16
+ import { CheckerProgrammer } from "./CheckerProgrammer";
17
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
18
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
19
+ import { UnionExplorer } from "./helpers/UnionExplorer";
20
+ import { feature_object_entries } from "./internal/feature_object_entries";
21
+
22
+ export namespace FeatureProgrammer {
23
+ /* -----------------------------------------------------------
24
+ PARAMETERS
25
+ ----------------------------------------------------------- */
26
+ export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
27
+ types: IConfig.ITypes;
28
+
29
+ /**
30
+ * Prefix name of functions for specific object types.
31
+ */
32
+ functors: string;
33
+
34
+ /**
35
+ * Prefix name of functions for union object types.
36
+ */
37
+ unioners: string;
38
+
39
+ /**
40
+ * Whether to archive access path or not.
41
+ */
42
+ path: boolean;
43
+
44
+ /**
45
+ * Whether to trace exception or not.
46
+ */
47
+ trace: boolean;
48
+
49
+ addition?(collection: MetadataCollection): ts.Statement[];
50
+
51
+ /**
52
+ * Initializer of metadata.
53
+ */
54
+ initializer: (
55
+ project: IProject,
56
+ ) => (type: ts.Type) => [MetadataCollection, Metadata];
57
+
58
+ /**
59
+ * Decoder, station of every types.
60
+ */
61
+ decoder: Decoder<Metadata, Output>;
62
+
63
+ /**
64
+ * Object configurator.
65
+ */
66
+ objector: IConfig.IObjector;
67
+
68
+ /**
69
+ * Generator of functions for object types.
70
+ */
71
+ generator?: Partial<IConfig.IGenerator>;
72
+ }
73
+ export namespace IConfig {
74
+ export interface ITypes {
75
+ input: (type: ts.Type, name?: string) => ts.TypeNode;
76
+ output: (type: ts.Type, name?: string) => ts.TypeNode;
77
+ }
78
+
79
+ export interface IObjector {
80
+ /**
81
+ * Type checker when union object type comes.
82
+ */
83
+ checker: Decoder<Metadata, ts.Expression>;
84
+
85
+ /**
86
+ * Decoder, function call expression generator of specific typed objects.
87
+ */
88
+ decoder: Decoder<MetadataObject, ts.Expression>;
89
+
90
+ /**
91
+ * Joiner of expressions from properties.
92
+ */
93
+ joiner(
94
+ input: ts.Expression,
95
+ entries: IExpressionEntry[],
96
+ parent: MetadataObject,
97
+ ): ts.ConciseBody;
98
+
99
+ /**
100
+ * Union type specificator.
101
+ *
102
+ * Expression of an algorithm specifying object type and calling
103
+ * the `decoder` function of the specified object type.
104
+ */
105
+ unionizer: Decoder<MetadataObject[], ts.Expression>;
106
+
107
+ /**
108
+ * Handler of union type specification failure.
109
+ *
110
+ * @param value Expression of input parameter
111
+ * @param expected Expected type name
112
+ * @param explore Exploration info
113
+ * @returns Statement of failure
114
+ */
115
+ failure(
116
+ value: ts.Expression,
117
+ expected: string,
118
+ explore?: IExplore,
119
+ ): ts.Statement;
120
+
121
+ /**
122
+ * Transformer of type checking expression by discrimination.
123
+ *
124
+ * When an object type has been specified by a discrimination without full
125
+ * iteration, the `unionizer` will decode the object instance after
126
+ * the last type checking.
127
+ *
128
+ * In such circumtance, you can transform the last type checking function.
129
+ *
130
+ * @param exp Current expression about type checking
131
+ * @returns Transformed expression
132
+ * @deprecated
133
+ */
134
+ is?(exp: ts.Expression): ts.Expression;
135
+
136
+ /**
137
+ * Transformer of non-undefined type checking by discrimination.
138
+ *
139
+ * When specifying an union type of objects, `typia` tries to find
140
+ * descrimination way just by checking only one property type.
141
+ * If succeeded to find the discrimination way, `typia` will check the target
142
+ * property type and in the checking, non-undefined type checking would be
143
+ * done.
144
+ *
145
+ * In such process, you can transform the non-undefined type checking.
146
+ *
147
+ * @param exp
148
+ * @returns Transformed expression
149
+ * @deprecated
150
+ */
151
+ required?(exp: ts.Expression): ts.Expression;
152
+
153
+ /**
154
+ * Conditon wrapper when unable to specify any object type.
155
+ *
156
+ * When failed to specify an object type through discrimination, full
157
+ * iteration type checking would be happend. In such circumstance, you
158
+ * can wrap the condition with additional function.
159
+ *
160
+ * @param condition Current condition
161
+ * @returns A function wrapped current condition
162
+ */
163
+ full?: (
164
+ condition: ts.Expression,
165
+ ) => (
166
+ input: ts.Expression,
167
+ expected: string,
168
+ explore: IExplore,
169
+ ) => ts.Expression;
170
+
171
+ /**
172
+ * Return type.
173
+ */
174
+ type?: ts.TypeNode;
175
+ }
176
+ export interface IGenerator {
177
+ /**
178
+ *
179
+ * @param col
180
+ */
181
+ functors(col: MetadataCollection): ts.VariableStatement[];
182
+
183
+ /**
184
+ *
185
+ * @param col
186
+ */
187
+ unioners(col: MetadataCollection): ts.VariableStatement[];
188
+ }
189
+ }
190
+
191
+ export interface IExplore {
192
+ tracable: boolean;
193
+ source: "top" | "object";
194
+ from: "top" | "array" | "object";
195
+ postfix: string;
196
+ start?: number;
197
+ }
198
+
199
+ export interface Decoder<
200
+ T,
201
+ Output extends ts.ConciseBody = ts.ConciseBody,
202
+ > {
203
+ (
204
+ input: ts.Expression,
205
+ target: T,
206
+ explore: IExplore,
207
+ metaTags: IMetadataTag[],
208
+ jsDocTags: ts.JSDocTagInfo[],
209
+ ): Output;
210
+ }
211
+
212
+ /* -----------------------------------------------------------
213
+ GENERATORS
214
+ ----------------------------------------------------------- */
215
+ export const analyze =
216
+ (project: IProject) =>
217
+ (config: IConfig) =>
218
+ (importer: FunctionImporter) =>
219
+ (type: ts.Type, name?: string) => {
220
+ const [collection, meta] = config.initializer(project)(type);
221
+
222
+ // ITERATE OVER ALL METADATA
223
+ const output: ts.ConciseBody = config.decoder(
224
+ ValueFactory.INPUT(),
225
+ meta,
226
+ {
227
+ tracable: config.path || config.trace,
228
+ source: "top",
229
+ from: "top",
230
+ postfix: '""',
231
+ },
232
+ [],
233
+ [],
234
+ );
235
+
236
+ // RETURNS THE OPTIMAL ARROW FUNCTION
237
+ const functors: ts.VariableStatement[] = (
238
+ config.generator?.functors ?? write_functors(config)(importer)
239
+ )(collection);
240
+ const unioners: ts.VariableStatement[] = (
241
+ config.generator?.unioners ?? write_unioners(config)(importer)
242
+ )(collection);
243
+ const added: ts.Statement[] = (config.addition ?? (() => []))(
244
+ collection,
245
+ );
246
+
247
+ return ts.factory.createArrowFunction(
248
+ undefined,
249
+ undefined,
250
+ PARAMETERS(config)(config.types.input(type, name))(
251
+ ValueFactory.INPUT(),
252
+ ),
253
+ config.types.output(type, name),
254
+ undefined,
255
+ ts.factory.createBlock(
256
+ [
257
+ ...added,
258
+ ...functors.filter((_, i) =>
259
+ importer.hasLocal(`${config.functors}${i}`),
260
+ ),
261
+ ...unioners.filter((_, i) =>
262
+ importer.hasLocal(`${config.unioners}${i}`),
263
+ ),
264
+ ...(ts.isBlock(output)
265
+ ? output.statements
266
+ : [ts.factory.createReturnStatement(output)]),
267
+ ],
268
+ true,
269
+ ),
270
+ );
271
+ };
272
+
273
+ export const write_functors =
274
+ (config: IConfig) =>
275
+ (importer: FunctionImporter) =>
276
+ (collection: MetadataCollection) =>
277
+ collection
278
+ .objects()
279
+ .map((obj, i) =>
280
+ StatementFactory.constant(
281
+ `${config.functors}${i}`,
282
+ write_object(config)(importer)(obj),
283
+ ),
284
+ );
285
+
286
+ export const write_unioners =
287
+ (config: IConfig) =>
288
+ (importer: FunctionImporter) =>
289
+ (collection: MetadataCollection) =>
290
+ collection
291
+ .unions()
292
+ .map((union, i) =>
293
+ StatementFactory.constant(
294
+ importer.useLocal(`${config.unioners}${i}`),
295
+ write_union(config)(union),
296
+ ),
297
+ );
298
+
299
+ const write_object =
300
+ (config: IConfig) =>
301
+ (importer: FunctionImporter) =>
302
+ (obj: MetadataObject) =>
303
+ ts.factory.createArrowFunction(
304
+ undefined,
305
+ undefined,
306
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
307
+ ValueFactory.INPUT(),
308
+ ),
309
+ config.objector.type ?? TypeFactory.keyword("any"),
310
+ undefined,
311
+ config.objector.joiner(
312
+ ts.factory.createIdentifier("input"),
313
+ feature_object_entries(config)(importer)(obj)(
314
+ ts.factory.createIdentifier("input"),
315
+ ),
316
+ obj,
317
+ ),
318
+ );
319
+
320
+ const write_union = (config: IConfig) => {
321
+ const explorer = UnionExplorer.object(config);
322
+ const input = ValueFactory.INPUT();
323
+
324
+ return (meta: MetadataObject[]) =>
325
+ ts.factory.createArrowFunction(
326
+ undefined,
327
+ undefined,
328
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
329
+ ValueFactory.INPUT(),
330
+ ),
331
+ TypeFactory.keyword("any"),
332
+ undefined,
333
+ explorer(
334
+ input,
335
+ meta,
336
+ {
337
+ tracable: config.path || config.trace,
338
+ source: "object",
339
+ from: "object",
340
+ postfix: "",
341
+ },
342
+ [],
343
+ [],
344
+ ),
345
+ );
346
+ };
347
+
348
+ /* -----------------------------------------------------------
349
+ DECODERS
350
+ ----------------------------------------------------------- */
351
+ export const decode_array =
352
+ (config: Pick<IConfig, "trace" | "path" | "decoder">) =>
353
+ (importer: FunctionImporter) =>
354
+ (
355
+ combiner: (
356
+ input: ts.Expression,
357
+ arrow: ts.ArrowFunction,
358
+ metaTags: IMetadataTag[],
359
+ jsDocTags: ts.JSDocTagInfo[],
360
+ ) => ts.Expression,
361
+ ) => {
362
+ const rand: string = importer.increment().toString();
363
+ const tail =
364
+ config.path || config.trace
365
+ ? [
366
+ IdentifierFactory.parameter(
367
+ "_index" + rand,
368
+ TypeFactory.keyword("number"),
369
+ ),
370
+ ]
371
+ : [];
372
+
373
+ return (
374
+ input: ts.Expression,
375
+ meta: Metadata,
376
+ explore: IExplore,
377
+ metaTags: IMetadataTag[],
378
+ jsDocTags: IJsDocTagInfo[],
379
+ ) => {
380
+ const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
381
+ undefined,
382
+ undefined,
383
+ [
384
+ IdentifierFactory.parameter(
385
+ "elem",
386
+ TypeFactory.keyword("any"),
387
+ ),
388
+ ...tail,
389
+ ],
390
+ undefined,
391
+ undefined,
392
+ config.decoder(
393
+ ValueFactory.INPUT("elem"),
394
+ meta,
395
+ {
396
+ tracable: explore.tracable,
397
+ source: explore.source,
398
+ from: "array",
399
+ postfix: INDEX_SYMBOL(explore.start ?? null)(
400
+ explore.postfix,
401
+ )(rand),
402
+ },
403
+ metaTags,
404
+ jsDocTags,
405
+ ),
406
+ );
407
+ return combiner(input, arrow, metaTags, jsDocTags);
408
+ };
409
+ };
410
+
411
+ export const decode_object =
412
+ (config: Pick<IConfig, "trace" | "path" | "functors">) =>
413
+ (importer: FunctionImporter) =>
414
+ (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
415
+ ts.factory.createCallExpression(
416
+ ts.factory.createIdentifier(
417
+ importer.useLocal(`${config.functors}${obj.index}`),
418
+ ),
419
+ undefined,
420
+ get_object_arguments(config)(explore)(input),
421
+ );
422
+
423
+ export const get_object_arguments =
424
+ (config: Pick<IConfig, "path" | "trace">) =>
425
+ (explore: FeatureProgrammer.IExplore) => {
426
+ const tail: ts.Expression[] =
427
+ config.path === false && config.trace === false
428
+ ? []
429
+ : config.path === true && config.trace === true
430
+ ? [
431
+ ts.factory.createIdentifier(
432
+ explore.postfix
433
+ ? `_path + ${explore.postfix}`
434
+ : "_path",
435
+ ),
436
+ explore.source === "object"
437
+ ? ts.factory.createIdentifier(
438
+ `${explore.tracable} && _exceptionable`,
439
+ )
440
+ : explore.tracable
441
+ ? ts.factory.createTrue()
442
+ : ts.factory.createFalse(),
443
+ ]
444
+ : config.path === true
445
+ ? [
446
+ ts.factory.createIdentifier(
447
+ explore.postfix
448
+ ? `_path + ${explore.postfix}`
449
+ : "_path",
450
+ ),
451
+ ]
452
+ : [
453
+ explore.source === "object"
454
+ ? ts.factory.createIdentifier(
455
+ `${explore.tracable} && _exceptionable`,
456
+ )
457
+ : explore.tracable
458
+ ? ts.factory.createTrue()
459
+ : ts.factory.createFalse(),
460
+ ];
461
+ return (input: ts.Expression) => [input, ...tail];
462
+ };
463
+ }
464
+
465
+ const INDEX_SYMBOL =
466
+ (start: number | null) => (prev: string) => (rand: string) => {
467
+ const tail: string =
468
+ start !== null
469
+ ? `"[" + (${start} + _index${rand}) + "]"`
470
+ : `"[" + _index${rand} + "]"`;
471
+ if (prev === "") return tail;
472
+ else if (prev[prev.length - 1] === `"`)
473
+ return prev.substring(0, prev.length - 1) + tail.substring(1);
474
+ return prev + ` + ${tail}`;
475
+ };
476
+
477
+ const PARAMETERS =
478
+ (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
479
+ (type: ts.TypeNode) => {
480
+ const tail: ts.ParameterDeclaration[] = [];
481
+ if (props.path)
482
+ tail.push(
483
+ IdentifierFactory.parameter(
484
+ "_path",
485
+ TypeFactory.keyword("string"),
486
+ ),
487
+ );
488
+ if (props.trace)
489
+ tail.push(
490
+ IdentifierFactory.parameter(
491
+ "_exceptionable",
492
+ TypeFactory.keyword("boolean"),
493
+ ts.factory.createTrue(),
494
+ ),
495
+ );
496
+ return (input: ts.Identifier) => [
497
+ IdentifierFactory.parameter(input, type),
498
+ ...tail,
499
+ ];
500
+ };