typia 5.2.6 → 5.3.0-dev.20231112

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 (202) hide show
  1. package/lib/factories/ExpressionFactory.js +14 -16
  2. package/lib/factories/ExpressionFactory.js.map +1 -1
  3. package/lib/factories/JsonMetadataFactory.d.ts +1 -1
  4. package/lib/factories/JsonMetadataFactory.js +2 -2
  5. package/lib/factories/JsonMetadataFactory.js.map +1 -1
  6. package/lib/factories/LiteralFactory.js +2 -0
  7. package/lib/factories/LiteralFactory.js.map +1 -1
  8. package/lib/factories/MetadataCollection.d.ts +2 -2
  9. package/lib/factories/MetadataFactory.d.ts +1 -1
  10. package/lib/factories/MetadataFactory.js +236 -170
  11. package/lib/factories/MetadataFactory.js.map +1 -1
  12. package/lib/factories/NumericRangeFactory.d.ts +2 -2
  13. package/lib/factories/NumericRangeFactory.js +41 -21
  14. package/lib/factories/NumericRangeFactory.js.map +1 -1
  15. package/lib/factories/ProtobufFactory.d.ts +1 -1
  16. package/lib/factories/ProtobufFactory.js +2 -2
  17. package/lib/factories/ProtobufFactory.js.map +1 -1
  18. package/lib/factories/internal/metadata/MetadataHelper.js +3 -2
  19. package/lib/factories/internal/metadata/MetadataHelper.js.map +1 -1
  20. package/lib/factories/internal/metadata/iterate_metadata_constant.js +13 -8
  21. package/lib/factories/internal/metadata/iterate_metadata_constant.js.map +1 -1
  22. package/lib/factories/internal/metadata/iterate_metadata_intersection.js +8 -5
  23. package/lib/factories/internal/metadata/iterate_metadata_intersection.js.map +1 -1
  24. package/lib/functional/$from.d.ts +3 -0
  25. package/lib/functional/$from.js +9 -0
  26. package/lib/functional/$from.js.map +1 -0
  27. package/lib/functional/Namespace.js +7 -0
  28. package/lib/functional/Namespace.js.map +1 -1
  29. package/lib/module.d.ts +1 -0
  30. package/lib/module.js +2 -5
  31. package/lib/module.js.map +1 -1
  32. package/lib/programmers/CheckerProgrammer.js +11 -16
  33. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  34. package/lib/programmers/RandomProgrammer.js +1 -1
  35. package/lib/programmers/RandomProgrammer.js.map +1 -1
  36. package/lib/programmers/helpers/ProtobufUtil.js.map +1 -1
  37. package/lib/programmers/http/HttpHeadersProgrammer.js +1 -1
  38. package/lib/programmers/http/HttpHeadersProgrammer.js.map +1 -1
  39. package/lib/programmers/http/HttpParameterProgrammer.js +1 -1
  40. package/lib/programmers/http/HttpParameterProgrammer.js.map +1 -1
  41. package/lib/programmers/http/HttpQueryProgrammer.js +1 -1
  42. package/lib/programmers/http/HttpQueryProgrammer.js.map +1 -1
  43. package/lib/programmers/internal/application_array.js +8 -0
  44. package/lib/programmers/internal/application_array.js.map +1 -1
  45. package/lib/programmers/internal/application_number.js +8 -1
  46. package/lib/programmers/internal/application_number.js.map +1 -1
  47. package/lib/programmers/internal/application_string.js +8 -1
  48. package/lib/programmers/internal/application_string.js.map +1 -1
  49. package/lib/programmers/internal/check_array_length.js +7 -4
  50. package/lib/programmers/internal/check_array_length.js.map +1 -1
  51. package/lib/programmers/internal/check_bigint.js +7 -4
  52. package/lib/programmers/internal/check_bigint.js.map +1 -1
  53. package/lib/programmers/internal/check_number.js +7 -4
  54. package/lib/programmers/internal/check_number.js.map +1 -1
  55. package/lib/programmers/internal/check_string.js +7 -4
  56. package/lib/programmers/internal/check_string.js.map +1 -1
  57. package/lib/programmers/internal/metadata_to_pattern.js +3 -1
  58. package/lib/programmers/internal/metadata_to_pattern.js.map +1 -1
  59. package/lib/programmers/json/JsonAssertParseProgrammer.js +1 -1
  60. package/lib/programmers/json/JsonAssertParseProgrammer.js.map +1 -1
  61. package/lib/programmers/json/JsonIsParseProgrammer.js +1 -1
  62. package/lib/programmers/json/JsonIsParseProgrammer.js.map +1 -1
  63. package/lib/programmers/json/JsonStringifyProgrammer.js +3 -8
  64. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  65. package/lib/programmers/json/JsonValidateParseProgrammer.js +1 -1
  66. package/lib/programmers/json/JsonValidateParseProgrammer.js.map +1 -1
  67. package/lib/programmers/misc/MiscCloneProgrammer.js +19 -24
  68. package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
  69. package/lib/programmers/misc/MiscLiteralsProgrammer.js +1 -1
  70. package/lib/programmers/misc/MiscLiteralsProgrammer.js.map +1 -1
  71. package/lib/programmers/misc/MiscPruneProgrammer.js +11 -16
  72. package/lib/programmers/misc/MiscPruneProgrammer.js.map +1 -1
  73. package/lib/programmers/notations/NotationGeneralProgrammer.js +11 -16
  74. package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
  75. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +1 -1
  76. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
  77. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +38 -42
  78. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
  79. package/lib/programmers/protobuf/ProtobufMessageProgrammer.d.ts +1 -1
  80. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +22 -25
  81. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
  82. package/lib/reflect.d.ts +3 -0
  83. package/lib/reflect.js +13 -0
  84. package/lib/reflect.js.map +1 -0
  85. package/lib/schemas/json/IJsonSchema.d.ts +1 -0
  86. package/lib/schemas/metadata/IMetadataApplication.d.ts +2 -2
  87. package/lib/schemas/metadata/{IMetadataCollection.d.ts → IMetadataComponents.d.ts} +1 -1
  88. package/lib/schemas/metadata/{IMetadataCollection.js → IMetadataComponents.js} +1 -1
  89. package/lib/schemas/metadata/IMetadataComponents.js.map +1 -0
  90. package/lib/schemas/metadata/IMetadataConstant.d.ts +1 -1
  91. package/lib/schemas/metadata/IMetadataTypeTag.d.ts +2 -2
  92. package/lib/schemas/metadata/Metadata.d.ts +2 -2
  93. package/lib/schemas/metadata/Metadata.js +82 -173
  94. package/lib/schemas/metadata/Metadata.js.map +1 -1
  95. package/lib/schemas/metadata/MetadataAlias.js +1 -1
  96. package/lib/schemas/metadata/MetadataAlias.js.map +1 -1
  97. package/lib/schemas/metadata/MetadataApplication.d.ts +13 -0
  98. package/lib/schemas/metadata/MetadataApplication.js +30 -0
  99. package/lib/schemas/metadata/MetadataApplication.js.map +1 -0
  100. package/lib/schemas/metadata/MetadataArrayType.js +1 -1
  101. package/lib/schemas/metadata/MetadataArrayType.js.map +1 -1
  102. package/lib/schemas/metadata/MetadataAtomic.d.ts +3 -0
  103. package/lib/schemas/metadata/MetadataAtomic.js +42 -0
  104. package/lib/schemas/metadata/MetadataAtomic.js.map +1 -1
  105. package/lib/schemas/metadata/MetadataComponents.d.ts +16 -0
  106. package/lib/schemas/metadata/MetadataComponents.js +151 -0
  107. package/lib/schemas/metadata/MetadataComponents.js.map +1 -0
  108. package/lib/schemas/metadata/MetadataConstant.d.ts +9 -1
  109. package/lib/schemas/metadata/MetadataConstant.js +29 -0
  110. package/lib/schemas/metadata/MetadataConstant.js.map +1 -1
  111. package/lib/schemas/metadata/MetadataEscaped.js +4 -4
  112. package/lib/schemas/metadata/MetadataEscaped.js.map +1 -1
  113. package/lib/schemas/metadata/MetadataObject.js +2 -2
  114. package/lib/schemas/metadata/MetadataObject.js.map +1 -1
  115. package/lib/schemas/metadata/MetadataProperty.js +4 -4
  116. package/lib/schemas/metadata/MetadataProperty.js.map +1 -1
  117. package/lib/schemas/metadata/MetadataTupleType.js +1 -1
  118. package/lib/schemas/metadata/MetadataTupleType.js.map +1 -1
  119. package/lib/transformers/CallExpressionTransformer.js +5 -2
  120. package/lib/transformers/CallExpressionTransformer.js.map +1 -1
  121. package/lib/transformers/features/json/JsonApplicationTransformer.d.ts +1 -1
  122. package/lib/transformers/features/json/JsonApplicationTransformer.js +4 -5
  123. package/lib/transformers/features/json/JsonApplicationTransformer.js.map +1 -1
  124. package/lib/transformers/features/reflect/ReflectMetadataTransformer.d.ts +5 -0
  125. package/lib/transformers/features/reflect/ReflectMetadataTransformer.js +58 -0
  126. package/lib/transformers/features/reflect/ReflectMetadataTransformer.js.map +1 -0
  127. package/package.json +2 -2
  128. package/src/Primitive.ts +135 -135
  129. package/src/executable/TypiaSetupWizard.ts +142 -142
  130. package/src/executable/setup/CommandExecutor.ts +8 -8
  131. package/src/factories/ExpressionFactory.ts +12 -13
  132. package/src/factories/JsonMetadataFactory.ts +53 -50
  133. package/src/factories/LiteralFactory.ts +2 -0
  134. package/src/factories/MetadataCollection.ts +282 -282
  135. package/src/factories/MetadataFactory.ts +55 -22
  136. package/src/factories/NumericRangeFactory.ts +56 -17
  137. package/src/factories/ProtobufFactory.ts +5 -2
  138. package/src/factories/internal/metadata/MetadataHelper.ts +7 -4
  139. package/src/factories/internal/metadata/emplace_metadata_object.ts +178 -178
  140. package/src/factories/internal/metadata/iterate_metadata_constant.ts +10 -8
  141. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +9 -8
  142. package/src/functional/$from.ts +5 -0
  143. package/src/functional/$stoll.ts +8 -8
  144. package/src/functional/Namespace.ts +175 -168
  145. package/src/module.ts +1 -19
  146. package/src/programmers/AssertProgrammer.ts +322 -322
  147. package/src/programmers/CheckerProgrammer.ts +16 -17
  148. package/src/programmers/IsProgrammer.ts +258 -258
  149. package/src/programmers/RandomProgrammer.ts +4 -1
  150. package/src/programmers/ValidateProgrammer.ts +350 -350
  151. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  152. package/src/programmers/helpers/ProtobufUtil.ts +3 -2
  153. package/src/programmers/http/HttpHeadersProgrammer.ts +4 -1
  154. package/src/programmers/http/HttpParameterProgrammer.ts +4 -1
  155. package/src/programmers/http/HttpQueryProgrammer.ts +4 -1
  156. package/src/programmers/internal/application_array.ts +8 -0
  157. package/src/programmers/internal/application_number.ts +8 -1
  158. package/src/programmers/internal/application_string.ts +8 -1
  159. package/src/programmers/internal/check_array_length.ts +5 -2
  160. package/src/programmers/internal/check_bigint.ts +5 -2
  161. package/src/programmers/internal/check_dynamic_key.ts +178 -178
  162. package/src/programmers/internal/check_dynamic_properties.ts +202 -202
  163. package/src/programmers/internal/check_number.ts +5 -2
  164. package/src/programmers/internal/check_object.ts +62 -62
  165. package/src/programmers/internal/check_string.ts +5 -3
  166. package/src/programmers/internal/metadata_to_pattern.ts +3 -1
  167. package/src/programmers/json/JsonAssertParseProgrammer.ts +1 -0
  168. package/src/programmers/json/JsonIsParseProgrammer.ts +1 -0
  169. package/src/programmers/json/JsonStringifyProgrammer.ts +959 -960
  170. package/src/programmers/json/JsonValidateParseProgrammer.ts +1 -0
  171. package/src/programmers/misc/MiscCloneProgrammer.ts +787 -786
  172. package/src/programmers/misc/MiscLiteralsProgrammer.ts +4 -1
  173. package/src/programmers/misc/MiscPruneProgrammer.ts +549 -548
  174. package/src/programmers/notations/NotationGeneralProgrammer.ts +717 -716
  175. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +1 -0
  176. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +872 -882
  177. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +21 -21
  178. package/src/reflect.ts +24 -0
  179. package/src/schemas/json/IJsonSchema.ts +1 -0
  180. package/src/schemas/metadata/IMetadataApplication.ts +2 -2
  181. package/src/schemas/metadata/{IMetadataCollection.ts → IMetadataComponents.ts} +1 -1
  182. package/src/schemas/metadata/IMetadataConstant.ts +1 -1
  183. package/src/schemas/metadata/IMetadataTypeTag.ts +9 -2
  184. package/src/schemas/metadata/Metadata.ts +18 -91
  185. package/src/schemas/metadata/MetadataAlias.ts +1 -1
  186. package/src/schemas/metadata/MetadataApplication.ts +44 -0
  187. package/src/schemas/metadata/MetadataArrayType.ts +1 -1
  188. package/src/schemas/metadata/MetadataAtomic.ts +44 -0
  189. package/src/schemas/metadata/MetadataComponents.ts +98 -0
  190. package/src/schemas/metadata/MetadataConstant.ts +44 -1
  191. package/src/schemas/metadata/MetadataEscaped.ts +4 -4
  192. package/src/schemas/metadata/MetadataObject.ts +2 -2
  193. package/src/schemas/metadata/MetadataProperty.ts +4 -7
  194. package/src/schemas/metadata/MetadataTupleType.ts +1 -1
  195. package/src/transform.ts +35 -35
  196. package/src/transformers/CallExpressionTransformer.ts +5 -2
  197. package/src/transformers/features/json/JsonApplicationTransformer.ts +7 -4
  198. package/src/transformers/features/{misc/MetadataTransformer.ts → reflect/ReflectMetadataTransformer.ts} +25 -8
  199. package/lib/schemas/metadata/IMetadataCollection.js.map +0 -1
  200. package/lib/transformers/features/misc/MetadataTransformer.d.ts +0 -5
  201. package/lib/transformers/features/misc/MetadataTransformer.js +0 -55
  202. package/lib/transformers/features/misc/MetadataTransformer.js.map +0 -1
@@ -1,786 +1,787 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../../factories/MetadataCollection";
6
- import { MetadataFactory } from "../../factories/MetadataFactory";
7
- import { StatementFactory } from "../../factories/StatementFactory";
8
- import { TypeFactory } from "../../factories/TypeFactory";
9
-
10
- import { Metadata } from "../../schemas/metadata/Metadata";
11
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
- import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
- import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
-
15
- import { IProject } from "../../transformers/IProject";
16
- import { TransformerError } from "../../transformers/TransformerError";
17
-
18
- import { FeatureProgrammer } from "../FeatureProgrammer";
19
- import { IsProgrammer } from "../IsProgrammer";
20
- import { CloneJoiner } from "../helpers/CloneJoiner";
21
- import { FunctionImporter } from "../helpers/FunctionImporeter";
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 MiscCloneProgrammer {
27
- export const write =
28
- (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
29
- const importer: FunctionImporter = new FunctionImporter(
30
- modulo.getText(),
31
- );
32
- return FeatureProgrammer.write(project)({
33
- ...configure(project)(importer),
34
- addition: (collection) => [
35
- ...IsProgrammer.write_function_statements(project)(
36
- importer,
37
- )(collection),
38
- ...importer.declare(modulo),
39
- ],
40
- })(importer);
41
- };
42
-
43
- const write_array_functions =
44
- (config: FeatureProgrammer.IConfig) =>
45
- (importer: FunctionImporter) =>
46
- (collection: MetadataCollection): ts.VariableStatement[] =>
47
- collection
48
- .arrays()
49
- .filter((a) => a.recursive)
50
- .map((type, i) =>
51
- StatementFactory.constant(
52
- `${config.prefix}a${i}`,
53
- ts.factory.createArrowFunction(
54
- undefined,
55
- undefined,
56
- FeatureProgrammer.parameterDeclarations(config)(
57
- TypeFactory.keyword("any"),
58
- )(ts.factory.createIdentifier("input")),
59
- TypeFactory.keyword("any"),
60
- undefined,
61
- decode_array_inline(config)(importer)(
62
- ts.factory.createIdentifier("input"),
63
- MetadataArray.create({
64
- type,
65
- tags: [],
66
- }),
67
- {
68
- tracable: config.trace,
69
- source: "function",
70
- from: "array",
71
- postfix: "",
72
- },
73
- ),
74
- ),
75
- ),
76
- );
77
-
78
- const write_tuple_functions =
79
- (project: IProject) =>
80
- (config: FeatureProgrammer.IConfig) =>
81
- (importer: FunctionImporter) =>
82
- (collection: MetadataCollection): ts.VariableStatement[] =>
83
- collection
84
- .tuples()
85
- .filter((t) => t.recursive)
86
- .map((tuple, i) =>
87
- StatementFactory.constant(
88
- `${config.prefix}t${i}`,
89
- ts.factory.createArrowFunction(
90
- undefined,
91
- undefined,
92
- FeatureProgrammer.parameterDeclarations(config)(
93
- TypeFactory.keyword("any"),
94
- )(ts.factory.createIdentifier("input")),
95
- TypeFactory.keyword("any"),
96
- undefined,
97
- decode_tuple_inline(project)(config)(importer)(
98
- ts.factory.createIdentifier("input"),
99
- tuple,
100
- {
101
- tracable: config.trace,
102
- source: "function",
103
- from: "array",
104
- postfix: "",
105
- },
106
- ),
107
- ),
108
- ),
109
- );
110
-
111
- /* -----------------------------------------------------------
112
- DECODERS
113
- ----------------------------------------------------------- */
114
- const decode =
115
- (project: IProject) =>
116
- (config: FeatureProgrammer.IConfig) =>
117
- (importer: FunctionImporter) =>
118
- (
119
- input: ts.Expression,
120
- meta: Metadata,
121
- explore: FeatureProgrammer.IExplore,
122
- ): ts.Expression => {
123
- // ANY TYPE
124
- if (
125
- meta.any ||
126
- meta.arrays.some((a) => a.type.value.any) ||
127
- meta.tuples.some(
128
- (t) =>
129
- !!t.type.elements.length &&
130
- t.type.elements.every((e) => e.any),
131
- )
132
- )
133
- return ts.factory.createCallExpression(
134
- importer.use("any"),
135
- undefined,
136
- [input],
137
- );
138
-
139
- interface IUnion {
140
- type: string;
141
- is: () => ts.Expression;
142
- value: () => ts.Expression;
143
- }
144
- const unions: IUnion[] = [];
145
-
146
- //----
147
- // LIST UP UNION TYPES
148
- //----
149
- // FUNCTIONAL
150
- if (meta.functional)
151
- unions.push({
152
- type: "functional",
153
- is: () =>
154
- ts.factory.createStrictEquality(
155
- ts.factory.createStringLiteral("function"),
156
- ts.factory.createTypeOfExpression(input),
157
- ),
158
- value: () => ts.factory.createIdentifier("undefined"),
159
- });
160
-
161
- // TUPLES
162
- for (const tuple of meta.tuples)
163
- unions.push({
164
- type: "tuple",
165
- is: () =>
166
- IsProgrammer.decode(project)(importer)(
167
- input,
168
- (() => {
169
- const partial = Metadata.initialize();
170
- partial.tuples.push(tuple);
171
- return partial;
172
- })(),
173
- explore,
174
- ),
175
- value: () =>
176
- decode_tuple(project)(config)(importer)(
177
- input,
178
- tuple,
179
- explore,
180
- ),
181
- });
182
-
183
- // ARRAYS
184
- if (meta.arrays.length)
185
- unions.push({
186
- type: "array",
187
- is: () => ExpressionFactory.isArray(input),
188
- value: () =>
189
- explore_arrays(project)(config)(importer)(
190
- input,
191
- meta.arrays,
192
- {
193
- ...explore,
194
- from: "array",
195
- },
196
- ),
197
- });
198
-
199
- // NATIVE TYPES
200
- if (meta.sets.length)
201
- unions.push({
202
- type: "set",
203
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
204
- value: () =>
205
- explore_sets(project)(config)(importer)(
206
- input,
207
- meta.sets,
208
- { ...explore, from: "array" },
209
- ),
210
- });
211
- if (meta.maps.length)
212
- unions.push({
213
- type: "map",
214
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
215
- value: () =>
216
- explore_maps(project)(config)(importer)(
217
- input,
218
- meta.maps,
219
- {
220
- ...explore,
221
- from: "array",
222
- },
223
- ),
224
- });
225
- for (const native of meta.natives)
226
- unions.push({
227
- type: "native",
228
- is: () => ExpressionFactory.isInstanceOf(native)(input),
229
- value: () =>
230
- native === "Boolean" ||
231
- native === "Number" ||
232
- native === "String"
233
- ? ts.factory.createCallExpression(
234
- IdentifierFactory.access(input)("valueOf"),
235
- undefined,
236
- undefined,
237
- )
238
- : decode_native(native)(input),
239
- });
240
-
241
- // OBJECTS
242
- if (meta.objects.length)
243
- unions.push({
244
- type: "object",
245
- is: () =>
246
- ExpressionFactory.isObject({
247
- checkNull: true,
248
- checkArray: false,
249
- })(input),
250
- value: () =>
251
- explore_objects(config)(importer)(input, meta, {
252
- ...explore,
253
- from: "object",
254
- }),
255
- });
256
-
257
- // COMPOSITION
258
- let last: ts.Expression = input;
259
- for (const u of unions.reverse())
260
- last = ts.factory.createConditionalExpression(
261
- u.is(),
262
- undefined,
263
- u.value(),
264
- undefined,
265
- last,
266
- );
267
- return ts.factory.createAsExpression(
268
- last,
269
- TypeFactory.keyword("any"),
270
- );
271
- };
272
-
273
- const decode_object = (importer: FunctionImporter) =>
274
- FeatureProgrammer.decode_object({
275
- trace: false,
276
- path: false,
277
- prefix: PREFIX,
278
- })(importer);
279
-
280
- const decode_array =
281
- (config: FeatureProgrammer.IConfig) =>
282
- (importer: FunctionImporter) =>
283
- (
284
- input: ts.Expression,
285
- array: MetadataArray,
286
- explore: FeatureProgrammer.IExplore,
287
- ) =>
288
- array.type.recursive
289
- ? ts.factory.createCallExpression(
290
- ts.factory.createIdentifier(
291
- importer.useLocal(
292
- `${config.prefix}a${array.type.index}`,
293
- ),
294
- ),
295
- undefined,
296
- FeatureProgrammer.argumentsArray(config)({
297
- ...explore,
298
- source: "function",
299
- from: "array",
300
- })(input),
301
- )
302
- : decode_array_inline(config)(importer)(input, array, explore);
303
-
304
- const decode_array_inline =
305
- (config: FeatureProgrammer.IConfig) =>
306
- (importer: FunctionImporter) =>
307
- (
308
- input: ts.Expression,
309
- array: MetadataArray,
310
- explore: FeatureProgrammer.IExplore,
311
- ) =>
312
- FeatureProgrammer.decode_array(config)(importer)(CloneJoiner.array)(
313
- input,
314
- array,
315
- explore,
316
- );
317
-
318
- const decode_tuple =
319
- (project: IProject) =>
320
- (config: FeatureProgrammer.IConfig) =>
321
- (importer: FunctionImporter) =>
322
- (
323
- input: ts.Expression,
324
- tuple: MetadataTuple,
325
- explore: FeatureProgrammer.IExplore,
326
- ): ts.Expression =>
327
- tuple.type.recursive
328
- ? ts.factory.createCallExpression(
329
- ts.factory.createIdentifier(
330
- importer.useLocal(
331
- `${config.prefix}t${tuple.type.index}`,
332
- ),
333
- ),
334
- undefined,
335
- FeatureProgrammer.argumentsArray(config)({
336
- ...explore,
337
- source: "function",
338
- })(input),
339
- )
340
- : decode_tuple_inline(project)(config)(importer)(
341
- input,
342
- tuple.type,
343
- explore,
344
- );
345
-
346
- const decode_tuple_inline =
347
- (project: IProject) =>
348
- (config: FeatureProgrammer.IConfig) =>
349
- (importer: FunctionImporter) =>
350
- (
351
- input: ts.Expression,
352
- tuple: MetadataTupleType,
353
- explore: FeatureProgrammer.IExplore,
354
- ): ts.Expression => {
355
- const children: ts.Expression[] = tuple.elements
356
- .filter((m) => m.rest === null)
357
- .map((elem, index) =>
358
- decode(project)(config)(importer)(
359
- ts.factory.createElementAccessExpression(input, index),
360
- elem,
361
- {
362
- ...explore,
363
- from: "array",
364
- postfix: explore.postfix.length
365
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
366
- : `"[${index}]"`,
367
- },
368
- ),
369
- );
370
- const rest = (() => {
371
- if (tuple.elements.length === 0) return null;
372
-
373
- const last: Metadata = tuple.elements.at(-1)!;
374
- const rest: Metadata | null = last.rest;
375
- if (rest === null) return null;
376
-
377
- return decode(project)(config)(importer)(
378
- ts.factory.createCallExpression(
379
- IdentifierFactory.access(input)("slice"),
380
- undefined,
381
- [ExpressionFactory.number(tuple.elements.length - 1)],
382
- ),
383
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
384
- {
385
- ...explore,
386
- start: tuple.elements.length - 1,
387
- },
388
- );
389
- })();
390
- return CloneJoiner.tuple(children, rest);
391
- };
392
-
393
- /* -----------------------------------------------------------
394
- NATIVE CLASSES
395
- ----------------------------------------------------------- */
396
- const decode_native = (type: string) => (input: ts.Expression) =>
397
- type === "Date" ||
398
- type === "Uint8Array" ||
399
- type === "Uint8ClampedArray" ||
400
- type === "Uint16Array" ||
401
- type === "Uint32Array" ||
402
- type === "BigUint64Array" ||
403
- type === "Int8Array" ||
404
- type === "Int16Array" ||
405
- type === "Int32Array" ||
406
- type === "BigInt64Array" ||
407
- type === "Float32Array" ||
408
- type === "Float64Array"
409
- ? decode_native_copyable(type)(input)
410
- : type === "ArrayBuffer" || type === "SharedArrayBuffer"
411
- ? decode_native_buffer(type)(input)
412
- : type === "DataView"
413
- ? decode_native_data_view(input)
414
- : ts.factory.createCallExpression(
415
- ts.factory.createIdentifier(type),
416
- undefined,
417
- [],
418
- );
419
-
420
- const decode_native_copyable = (type: string) => (input: ts.Expression) =>
421
- ts.factory.createNewExpression(
422
- ts.factory.createIdentifier(type),
423
- undefined,
424
- [input],
425
- );
426
-
427
- const decode_native_buffer =
428
- (type: "ArrayBuffer" | "SharedArrayBuffer") => (input: ts.Expression) =>
429
- ExpressionFactory.selfCall(
430
- ts.factory.createBlock(
431
- [
432
- StatementFactory.constant(
433
- "buffer",
434
- ts.factory.createNewExpression(
435
- ts.factory.createIdentifier(type),
436
- undefined,
437
- [IdentifierFactory.access(input)("byteLength")],
438
- ),
439
- ),
440
- ts.factory.createExpressionStatement(
441
- ts.factory.createCallExpression(
442
- IdentifierFactory.access(
443
- ts.factory.createNewExpression(
444
- ts.factory.createIdentifier(
445
- "Uint8Array",
446
- ),
447
- undefined,
448
- [ts.factory.createIdentifier("buffer")],
449
- ),
450
- )("set"),
451
- undefined,
452
- [
453
- ts.factory.createNewExpression(
454
- ts.factory.createIdentifier(
455
- "Uint8Array",
456
- ),
457
- undefined,
458
- [input],
459
- ),
460
- ],
461
- ),
462
- ),
463
- ts.factory.createReturnStatement(
464
- ts.factory.createIdentifier("buffer"),
465
- ),
466
- ],
467
- true,
468
- ),
469
- );
470
-
471
- const decode_native_data_view = (input: ts.Expression) =>
472
- ts.factory.createNewExpression(
473
- ts.factory.createIdentifier("DataView"),
474
- undefined,
475
- [IdentifierFactory.access(input)("buffer")],
476
- );
477
-
478
- /* -----------------------------------------------------------
479
- EXPLORERS FOR UNION TYPES
480
- ----------------------------------------------------------- */
481
- const explore_sets =
482
- (project: IProject) =>
483
- (config: FeatureProgrammer.IConfig) =>
484
- (importer: FunctionImporter) =>
485
- (
486
- input: ts.Expression,
487
- sets: Metadata[],
488
- explore: FeatureProgrammer.IExplore,
489
- ): ts.Expression =>
490
- ts.factory.createCallExpression(
491
- UnionExplorer.set({
492
- checker: IsProgrammer.decode(project)(importer),
493
- decoder: (input, array, explore) =>
494
- ts.factory.createNewExpression(
495
- ts.factory.createIdentifier("Set"),
496
- [TypeFactory.keyword("any")],
497
- [
498
- decode_array(config)(importer)(
499
- input,
500
- array,
501
- explore,
502
- ),
503
- ],
504
- ),
505
- empty: ts.factory.createNewExpression(
506
- ts.factory.createIdentifier("Set"),
507
- [TypeFactory.keyword("any")],
508
- [],
509
- ),
510
- success: ts.factory.createTrue(),
511
- failure: (input, expected) =>
512
- create_throw_error(importer)(expected)(input),
513
- })([])(input, sets, explore),
514
- undefined,
515
- undefined,
516
- );
517
-
518
- const explore_maps =
519
- (project: IProject) =>
520
- (config: FeatureProgrammer.IConfig) =>
521
- (importer: FunctionImporter) =>
522
- (
523
- input: ts.Expression,
524
- maps: Metadata.Entry[],
525
- explore: FeatureProgrammer.IExplore,
526
- ): ts.Expression =>
527
- ts.factory.createCallExpression(
528
- UnionExplorer.map({
529
- checker: (top, entry, explore) => {
530
- const func = IsProgrammer.decode(project)(importer);
531
- return ts.factory.createLogicalAnd(
532
- func(
533
- ts.factory.createElementAccessExpression(
534
- top,
535
- 0,
536
- ),
537
- entry[0],
538
- {
539
- ...explore,
540
- postfix: `${explore.postfix}[0]`,
541
- },
542
- ),
543
- func(
544
- ts.factory.createElementAccessExpression(
545
- top,
546
- 1,
547
- ),
548
- entry[1],
549
- {
550
- ...explore,
551
- postfix: `${explore.postfix}[1]`,
552
- },
553
- ),
554
- );
555
- },
556
- decoder: (input, array, explore) =>
557
- ts.factory.createNewExpression(
558
- ts.factory.createIdentifier("Map"),
559
- [
560
- TypeFactory.keyword("any"),
561
- TypeFactory.keyword("any"),
562
- ],
563
- [
564
- decode_array(config)(importer)(
565
- input,
566
- array,
567
- explore,
568
- ),
569
- ],
570
- ),
571
- empty: ts.factory.createNewExpression(
572
- ts.factory.createIdentifier("Map"),
573
- [
574
- TypeFactory.keyword("any"),
575
- TypeFactory.keyword("any"),
576
- ],
577
- [],
578
- ),
579
- success: ts.factory.createTrue(),
580
- failure: (input, expected) =>
581
- create_throw_error(importer)(expected)(input),
582
- })([])(input, maps, explore),
583
- undefined,
584
- undefined,
585
- );
586
-
587
- const explore_objects =
588
- (config: FeatureProgrammer.IConfig) =>
589
- (importer: FunctionImporter) =>
590
- (
591
- input: ts.Expression,
592
- meta: Metadata,
593
- explore: FeatureProgrammer.IExplore,
594
- ) => {
595
- if (meta.objects.length === 1)
596
- return decode_object(importer)(
597
- input,
598
- meta.objects[0]!,
599
- explore,
600
- );
601
-
602
- return ts.factory.createCallExpression(
603
- ts.factory.createIdentifier(
604
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
605
- ),
606
- undefined,
607
- FeatureProgrammer.argumentsArray(config)(explore)(input),
608
- );
609
- };
610
-
611
- const explore_arrays =
612
- (project: IProject) =>
613
- (config: FeatureProgrammer.IConfig) =>
614
- (importer: FunctionImporter) =>
615
- (
616
- input: ts.Expression,
617
- elements: MetadataArray[],
618
- explore: FeatureProgrammer.IExplore,
619
- ): ts.Expression =>
620
- explore_array_like_union_types(config)(importer)(
621
- UnionExplorer.array({
622
- checker: IsProgrammer.decode(project)(importer),
623
- decoder: decode_array(config)(importer),
624
- empty: ts.factory.createIdentifier("[]"),
625
- success: ts.factory.createTrue(),
626
- failure: (input, expected) =>
627
- create_throw_error(importer)(expected)(input),
628
- }),
629
- )(input, elements, explore);
630
-
631
- const explore_array_like_union_types =
632
- (config: FeatureProgrammer.IConfig) =>
633
- (importer: FunctionImporter) =>
634
- <T extends MetadataArray | MetadataTuple>(
635
- factory: (
636
- parameters: ts.ParameterDeclaration[],
637
- ) => (
638
- input: ts.Expression,
639
- elements: T[],
640
- explore: FeatureProgrammer.IExplore,
641
- ) => ts.ArrowFunction,
642
- ) =>
643
- (
644
- input: ts.Expression,
645
- elements: T[],
646
- explore: FeatureProgrammer.IExplore,
647
- ): ts.Expression => {
648
- const arrow =
649
- (parameters: ts.ParameterDeclaration[]) =>
650
- (explore: FeatureProgrammer.IExplore) =>
651
- (input: ts.Expression): ts.ArrowFunction =>
652
- factory(parameters)(input, elements, explore);
653
- if (elements.every((e) => e.type.recursive === false))
654
- ts.factory.createCallExpression(
655
- arrow([])(explore)(input),
656
- undefined,
657
- [],
658
- );
659
-
660
- explore = {
661
- ...explore,
662
- source: "function",
663
- from: "array",
664
- };
665
- return ts.factory.createCallExpression(
666
- ts.factory.createIdentifier(
667
- importer.emplaceUnion(
668
- config.prefix,
669
- elements.map((e) => e.type.name).join(" | "),
670
- () =>
671
- arrow(
672
- FeatureProgrammer.parameterDeclarations(config)(
673
- TypeFactory.keyword("any"),
674
- )(ts.factory.createIdentifier("input")),
675
- )({
676
- ...explore,
677
- postfix: "",
678
- })(ts.factory.createIdentifier("input")),
679
- ),
680
- ),
681
- undefined,
682
- FeatureProgrammer.argumentsArray(config)(explore)(input),
683
- );
684
- };
685
-
686
- /* -----------------------------------------------------------
687
- CONFIGURATIONS
688
- ----------------------------------------------------------- */
689
- const PREFIX = "$c";
690
-
691
- const configure =
692
- (project: IProject) =>
693
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
694
- const config: FeatureProgrammer.IConfig = {
695
- types: {
696
- input: (type, name) =>
697
- ts.factory.createTypeReferenceNode(
698
- name ??
699
- TypeFactory.getFullName(project.checker)(type),
700
- ),
701
- output: (type, name) =>
702
- ts.factory.createTypeReferenceNode(
703
- `typia.Resolved<${
704
- name ??
705
- TypeFactory.getFullName(project.checker)(type)
706
- }>`,
707
- ),
708
- },
709
- prefix: PREFIX,
710
- trace: false,
711
- path: false,
712
- initializer,
713
- decoder: () => decode(project)(config)(importer),
714
- objector: {
715
- checker: () => IsProgrammer.decode(project)(importer),
716
- decoder: () => decode_object(importer),
717
- joiner: CloneJoiner.object,
718
- unionizer: decode_union_object(
719
- IsProgrammer.decode_object(project)(importer),
720
- )(decode_object(importer))((exp) => exp)(
721
- (input, expected) =>
722
- create_throw_error(importer)(expected)(input),
723
- ),
724
- failure: (input, expected) =>
725
- create_throw_error(importer)(expected)(input),
726
- },
727
- generator: {
728
- arrays: () => write_array_functions(config)(importer),
729
- tuples: () =>
730
- write_tuple_functions(project)(config)(importer),
731
- },
732
- };
733
- return config;
734
- };
735
-
736
- const initializer: FeatureProgrammer.IConfig["initializer"] =
737
- ({ checker }) =>
738
- (importer) =>
739
- (type) => {
740
- const collection = new MetadataCollection();
741
- const result = MetadataFactory.analyze(checker)({
742
- escape: false,
743
- constant: true,
744
- absorb: true,
745
- validate: (meta) => {
746
- const output: string[] = [];
747
- if (meta.natives.some((n) => n === "WeakSet"))
748
- output.push("unable to clone WeakSet");
749
- else if (meta.natives.some((n) => n === "WeakMap"))
750
- output.push("unable to clone WeakMap");
751
- return output;
752
- },
753
- })(collection)(type);
754
- if (result.success === false)
755
- throw TransformerError.from(`typia.misc.${importer.method}`)(
756
- result.errors,
757
- );
758
- return [collection, result.data];
759
- };
760
-
761
- const create_throw_error =
762
- (importer: FunctionImporter) =>
763
- (expected: string) =>
764
- (value: ts.Expression) =>
765
- ts.factory.createExpressionStatement(
766
- ts.factory.createCallExpression(
767
- importer.use("throws"),
768
- [],
769
- [
770
- ts.factory.createObjectLiteralExpression(
771
- [
772
- ts.factory.createPropertyAssignment(
773
- "expected",
774
- ts.factory.createStringLiteral(expected),
775
- ),
776
- ts.factory.createPropertyAssignment(
777
- "value",
778
- value,
779
- ),
780
- ],
781
- true,
782
- ),
783
- ],
784
- ),
785
- );
786
- }
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../../factories/MetadataCollection";
6
+ import { MetadataFactory } from "../../factories/MetadataFactory";
7
+ import { StatementFactory } from "../../factories/StatementFactory";
8
+ import { TypeFactory } from "../../factories/TypeFactory";
9
+
10
+ import { Metadata } from "../../schemas/metadata/Metadata";
11
+ import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
+ import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
+ import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
+
15
+ import { IProject } from "../../transformers/IProject";
16
+ import { TransformerError } from "../../transformers/TransformerError";
17
+
18
+ import { FeatureProgrammer } from "../FeatureProgrammer";
19
+ import { IsProgrammer } from "../IsProgrammer";
20
+ import { CloneJoiner } from "../helpers/CloneJoiner";
21
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
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 MiscCloneProgrammer {
27
+ export const write =
28
+ (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
29
+ const importer: FunctionImporter = new FunctionImporter(
30
+ modulo.getText(),
31
+ );
32
+ return FeatureProgrammer.write(project)({
33
+ ...configure(project)(importer),
34
+ addition: (collection) => [
35
+ ...IsProgrammer.write_function_statements(project)(
36
+ importer,
37
+ )(collection),
38
+ ...importer.declare(modulo),
39
+ ],
40
+ })(importer);
41
+ };
42
+
43
+ const write_array_functions =
44
+ (config: FeatureProgrammer.IConfig) =>
45
+ (importer: FunctionImporter) =>
46
+ (collection: MetadataCollection): ts.VariableStatement[] =>
47
+ collection
48
+ .arrays()
49
+ .filter((a) => a.recursive)
50
+ .map((type, i) =>
51
+ StatementFactory.constant(
52
+ `${config.prefix}a${i}`,
53
+ ts.factory.createArrowFunction(
54
+ undefined,
55
+ undefined,
56
+ FeatureProgrammer.parameterDeclarations(config)(
57
+ TypeFactory.keyword("any"),
58
+ )(ts.factory.createIdentifier("input")),
59
+ TypeFactory.keyword("any"),
60
+ undefined,
61
+ decode_array_inline(config)(importer)(
62
+ ts.factory.createIdentifier("input"),
63
+ MetadataArray.create({
64
+ type,
65
+ tags: [],
66
+ }),
67
+ {
68
+ tracable: config.trace,
69
+ source: "function",
70
+ from: "array",
71
+ postfix: "",
72
+ },
73
+ ),
74
+ ),
75
+ ),
76
+ );
77
+
78
+ const write_tuple_functions =
79
+ (project: IProject) =>
80
+ (config: FeatureProgrammer.IConfig) =>
81
+ (importer: FunctionImporter) =>
82
+ (collection: MetadataCollection): ts.VariableStatement[] =>
83
+ collection
84
+ .tuples()
85
+ .filter((t) => t.recursive)
86
+ .map((tuple, i) =>
87
+ StatementFactory.constant(
88
+ `${config.prefix}t${i}`,
89
+ ts.factory.createArrowFunction(
90
+ undefined,
91
+ undefined,
92
+ FeatureProgrammer.parameterDeclarations(config)(
93
+ TypeFactory.keyword("any"),
94
+ )(ts.factory.createIdentifier("input")),
95
+ TypeFactory.keyword("any"),
96
+ undefined,
97
+ decode_tuple_inline(project)(config)(importer)(
98
+ ts.factory.createIdentifier("input"),
99
+ tuple,
100
+ {
101
+ tracable: config.trace,
102
+ source: "function",
103
+ from: "array",
104
+ postfix: "",
105
+ },
106
+ ),
107
+ ),
108
+ ),
109
+ );
110
+
111
+ /* -----------------------------------------------------------
112
+ DECODERS
113
+ ----------------------------------------------------------- */
114
+ const decode =
115
+ (project: IProject) =>
116
+ (config: FeatureProgrammer.IConfig) =>
117
+ (importer: FunctionImporter) =>
118
+ (
119
+ input: ts.Expression,
120
+ meta: Metadata,
121
+ explore: FeatureProgrammer.IExplore,
122
+ ): ts.Expression => {
123
+ // ANY TYPE
124
+ if (
125
+ meta.any ||
126
+ meta.arrays.some((a) => a.type.value.any) ||
127
+ meta.tuples.some(
128
+ (t) =>
129
+ !!t.type.elements.length &&
130
+ t.type.elements.every((e) => e.any),
131
+ )
132
+ )
133
+ return ts.factory.createCallExpression(
134
+ importer.use("any"),
135
+ undefined,
136
+ [input],
137
+ );
138
+
139
+ interface IUnion {
140
+ type: string;
141
+ is: () => ts.Expression;
142
+ value: () => ts.Expression;
143
+ }
144
+ const unions: IUnion[] = [];
145
+
146
+ //----
147
+ // LIST UP UNION TYPES
148
+ //----
149
+ // FUNCTIONAL
150
+ if (meta.functional)
151
+ unions.push({
152
+ type: "functional",
153
+ is: () =>
154
+ ts.factory.createStrictEquality(
155
+ ts.factory.createStringLiteral("function"),
156
+ ts.factory.createTypeOfExpression(input),
157
+ ),
158
+ value: () => ts.factory.createIdentifier("undefined"),
159
+ });
160
+
161
+ // TUPLES
162
+ for (const tuple of meta.tuples)
163
+ unions.push({
164
+ type: "tuple",
165
+ is: () =>
166
+ IsProgrammer.decode(project)(importer)(
167
+ input,
168
+ (() => {
169
+ const partial = Metadata.initialize();
170
+ partial.tuples.push(tuple);
171
+ return partial;
172
+ })(),
173
+ explore,
174
+ ),
175
+ value: () =>
176
+ decode_tuple(project)(config)(importer)(
177
+ input,
178
+ tuple,
179
+ explore,
180
+ ),
181
+ });
182
+
183
+ // ARRAYS
184
+ if (meta.arrays.length)
185
+ unions.push({
186
+ type: "array",
187
+ is: () => ExpressionFactory.isArray(input),
188
+ value: () =>
189
+ explore_arrays(project)(config)(importer)(
190
+ input,
191
+ meta.arrays,
192
+ {
193
+ ...explore,
194
+ from: "array",
195
+ },
196
+ ),
197
+ });
198
+
199
+ // NATIVE TYPES
200
+ if (meta.sets.length)
201
+ unions.push({
202
+ type: "set",
203
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
204
+ value: () =>
205
+ explore_sets(project)(config)(importer)(
206
+ input,
207
+ meta.sets,
208
+ { ...explore, from: "array" },
209
+ ),
210
+ });
211
+ if (meta.maps.length)
212
+ unions.push({
213
+ type: "map",
214
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
215
+ value: () =>
216
+ explore_maps(project)(config)(importer)(
217
+ input,
218
+ meta.maps,
219
+ {
220
+ ...explore,
221
+ from: "array",
222
+ },
223
+ ),
224
+ });
225
+ for (const native of meta.natives)
226
+ unions.push({
227
+ type: "native",
228
+ is: () => ExpressionFactory.isInstanceOf(native)(input),
229
+ value: () =>
230
+ native === "Boolean" ||
231
+ native === "Number" ||
232
+ native === "String"
233
+ ? ts.factory.createCallExpression(
234
+ IdentifierFactory.access(input)("valueOf"),
235
+ undefined,
236
+ undefined,
237
+ )
238
+ : decode_native(native)(input),
239
+ });
240
+
241
+ // OBJECTS
242
+ if (meta.objects.length)
243
+ unions.push({
244
+ type: "object",
245
+ is: () =>
246
+ ExpressionFactory.isObject({
247
+ checkNull: true,
248
+ checkArray: false,
249
+ })(input),
250
+ value: () =>
251
+ explore_objects(config)(importer)(input, meta, {
252
+ ...explore,
253
+ from: "object",
254
+ }),
255
+ });
256
+
257
+ // COMPOSITION
258
+ let last: ts.Expression = input;
259
+ for (const u of unions.reverse())
260
+ last = ts.factory.createConditionalExpression(
261
+ u.is(),
262
+ undefined,
263
+ u.value(),
264
+ undefined,
265
+ last,
266
+ );
267
+ return ts.factory.createAsExpression(
268
+ last,
269
+ TypeFactory.keyword("any"),
270
+ );
271
+ };
272
+
273
+ const decode_object = (importer: FunctionImporter) =>
274
+ FeatureProgrammer.decode_object({
275
+ trace: false,
276
+ path: false,
277
+ prefix: PREFIX,
278
+ })(importer);
279
+
280
+ const decode_array =
281
+ (config: FeatureProgrammer.IConfig) =>
282
+ (importer: FunctionImporter) =>
283
+ (
284
+ input: ts.Expression,
285
+ array: MetadataArray,
286
+ explore: FeatureProgrammer.IExplore,
287
+ ) =>
288
+ array.type.recursive
289
+ ? ts.factory.createCallExpression(
290
+ ts.factory.createIdentifier(
291
+ importer.useLocal(
292
+ `${config.prefix}a${array.type.index}`,
293
+ ),
294
+ ),
295
+ undefined,
296
+ FeatureProgrammer.argumentsArray(config)({
297
+ ...explore,
298
+ source: "function",
299
+ from: "array",
300
+ })(input),
301
+ )
302
+ : decode_array_inline(config)(importer)(input, array, explore);
303
+
304
+ const decode_array_inline =
305
+ (config: FeatureProgrammer.IConfig) =>
306
+ (importer: FunctionImporter) =>
307
+ (
308
+ input: ts.Expression,
309
+ array: MetadataArray,
310
+ explore: FeatureProgrammer.IExplore,
311
+ ) =>
312
+ FeatureProgrammer.decode_array(config)(importer)(CloneJoiner.array)(
313
+ input,
314
+ array,
315
+ explore,
316
+ );
317
+
318
+ const decode_tuple =
319
+ (project: IProject) =>
320
+ (config: FeatureProgrammer.IConfig) =>
321
+ (importer: FunctionImporter) =>
322
+ (
323
+ input: ts.Expression,
324
+ tuple: MetadataTuple,
325
+ explore: FeatureProgrammer.IExplore,
326
+ ): ts.Expression =>
327
+ tuple.type.recursive
328
+ ? ts.factory.createCallExpression(
329
+ ts.factory.createIdentifier(
330
+ importer.useLocal(
331
+ `${config.prefix}t${tuple.type.index}`,
332
+ ),
333
+ ),
334
+ undefined,
335
+ FeatureProgrammer.argumentsArray(config)({
336
+ ...explore,
337
+ source: "function",
338
+ })(input),
339
+ )
340
+ : decode_tuple_inline(project)(config)(importer)(
341
+ input,
342
+ tuple.type,
343
+ explore,
344
+ );
345
+
346
+ const decode_tuple_inline =
347
+ (project: IProject) =>
348
+ (config: FeatureProgrammer.IConfig) =>
349
+ (importer: FunctionImporter) =>
350
+ (
351
+ input: ts.Expression,
352
+ tuple: MetadataTupleType,
353
+ explore: FeatureProgrammer.IExplore,
354
+ ): ts.Expression => {
355
+ const children: ts.Expression[] = tuple.elements
356
+ .filter((m) => m.rest === null)
357
+ .map((elem, index) =>
358
+ decode(project)(config)(importer)(
359
+ ts.factory.createElementAccessExpression(input, index),
360
+ elem,
361
+ {
362
+ ...explore,
363
+ from: "array",
364
+ postfix: explore.postfix.length
365
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
366
+ : `"[${index}]"`,
367
+ },
368
+ ),
369
+ );
370
+ const rest = (() => {
371
+ if (tuple.elements.length === 0) return null;
372
+
373
+ const last: Metadata = tuple.elements.at(-1)!;
374
+ const rest: Metadata | null = last.rest;
375
+ if (rest === null) return null;
376
+
377
+ return decode(project)(config)(importer)(
378
+ ts.factory.createCallExpression(
379
+ IdentifierFactory.access(input)("slice"),
380
+ undefined,
381
+ [ExpressionFactory.number(tuple.elements.length - 1)],
382
+ ),
383
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
384
+ {
385
+ ...explore,
386
+ start: tuple.elements.length - 1,
387
+ },
388
+ );
389
+ })();
390
+ return CloneJoiner.tuple(children, rest);
391
+ };
392
+
393
+ /* -----------------------------------------------------------
394
+ NATIVE CLASSES
395
+ ----------------------------------------------------------- */
396
+ const decode_native = (type: string) => (input: ts.Expression) =>
397
+ type === "Date" ||
398
+ type === "Uint8Array" ||
399
+ type === "Uint8ClampedArray" ||
400
+ type === "Uint16Array" ||
401
+ type === "Uint32Array" ||
402
+ type === "BigUint64Array" ||
403
+ type === "Int8Array" ||
404
+ type === "Int16Array" ||
405
+ type === "Int32Array" ||
406
+ type === "BigInt64Array" ||
407
+ type === "Float32Array" ||
408
+ type === "Float64Array"
409
+ ? decode_native_copyable(type)(input)
410
+ : type === "ArrayBuffer" || type === "SharedArrayBuffer"
411
+ ? decode_native_buffer(type)(input)
412
+ : type === "DataView"
413
+ ? decode_native_data_view(input)
414
+ : ts.factory.createCallExpression(
415
+ ts.factory.createIdentifier(type),
416
+ undefined,
417
+ [],
418
+ );
419
+
420
+ const decode_native_copyable = (type: string) => (input: ts.Expression) =>
421
+ ts.factory.createNewExpression(
422
+ ts.factory.createIdentifier(type),
423
+ undefined,
424
+ [input],
425
+ );
426
+
427
+ const decode_native_buffer =
428
+ (type: "ArrayBuffer" | "SharedArrayBuffer") => (input: ts.Expression) =>
429
+ ExpressionFactory.selfCall(
430
+ ts.factory.createBlock(
431
+ [
432
+ StatementFactory.constant(
433
+ "buffer",
434
+ ts.factory.createNewExpression(
435
+ ts.factory.createIdentifier(type),
436
+ undefined,
437
+ [IdentifierFactory.access(input)("byteLength")],
438
+ ),
439
+ ),
440
+ ts.factory.createExpressionStatement(
441
+ ts.factory.createCallExpression(
442
+ IdentifierFactory.access(
443
+ ts.factory.createNewExpression(
444
+ ts.factory.createIdentifier(
445
+ "Uint8Array",
446
+ ),
447
+ undefined,
448
+ [ts.factory.createIdentifier("buffer")],
449
+ ),
450
+ )("set"),
451
+ undefined,
452
+ [
453
+ ts.factory.createNewExpression(
454
+ ts.factory.createIdentifier(
455
+ "Uint8Array",
456
+ ),
457
+ undefined,
458
+ [input],
459
+ ),
460
+ ],
461
+ ),
462
+ ),
463
+ ts.factory.createReturnStatement(
464
+ ts.factory.createIdentifier("buffer"),
465
+ ),
466
+ ],
467
+ true,
468
+ ),
469
+ );
470
+
471
+ const decode_native_data_view = (input: ts.Expression) =>
472
+ ts.factory.createNewExpression(
473
+ ts.factory.createIdentifier("DataView"),
474
+ undefined,
475
+ [IdentifierFactory.access(input)("buffer")],
476
+ );
477
+
478
+ /* -----------------------------------------------------------
479
+ EXPLORERS FOR UNION TYPES
480
+ ----------------------------------------------------------- */
481
+ const explore_sets =
482
+ (project: IProject) =>
483
+ (config: FeatureProgrammer.IConfig) =>
484
+ (importer: FunctionImporter) =>
485
+ (
486
+ input: ts.Expression,
487
+ sets: Metadata[],
488
+ explore: FeatureProgrammer.IExplore,
489
+ ): ts.Expression =>
490
+ ts.factory.createCallExpression(
491
+ UnionExplorer.set({
492
+ checker: IsProgrammer.decode(project)(importer),
493
+ decoder: (input, array, explore) =>
494
+ ts.factory.createNewExpression(
495
+ ts.factory.createIdentifier("Set"),
496
+ [TypeFactory.keyword("any")],
497
+ [
498
+ decode_array(config)(importer)(
499
+ input,
500
+ array,
501
+ explore,
502
+ ),
503
+ ],
504
+ ),
505
+ empty: ts.factory.createNewExpression(
506
+ ts.factory.createIdentifier("Set"),
507
+ [TypeFactory.keyword("any")],
508
+ [],
509
+ ),
510
+ success: ts.factory.createTrue(),
511
+ failure: (input, expected) =>
512
+ create_throw_error(importer)(expected)(input),
513
+ })([])(input, sets, explore),
514
+ undefined,
515
+ undefined,
516
+ );
517
+
518
+ const explore_maps =
519
+ (project: IProject) =>
520
+ (config: FeatureProgrammer.IConfig) =>
521
+ (importer: FunctionImporter) =>
522
+ (
523
+ input: ts.Expression,
524
+ maps: Metadata.Entry[],
525
+ explore: FeatureProgrammer.IExplore,
526
+ ): ts.Expression =>
527
+ ts.factory.createCallExpression(
528
+ UnionExplorer.map({
529
+ checker: (top, entry, explore) => {
530
+ const func = IsProgrammer.decode(project)(importer);
531
+ return ts.factory.createLogicalAnd(
532
+ func(
533
+ ts.factory.createElementAccessExpression(
534
+ top,
535
+ 0,
536
+ ),
537
+ entry[0],
538
+ {
539
+ ...explore,
540
+ postfix: `${explore.postfix}[0]`,
541
+ },
542
+ ),
543
+ func(
544
+ ts.factory.createElementAccessExpression(
545
+ top,
546
+ 1,
547
+ ),
548
+ entry[1],
549
+ {
550
+ ...explore,
551
+ postfix: `${explore.postfix}[1]`,
552
+ },
553
+ ),
554
+ );
555
+ },
556
+ decoder: (input, array, explore) =>
557
+ ts.factory.createNewExpression(
558
+ ts.factory.createIdentifier("Map"),
559
+ [
560
+ TypeFactory.keyword("any"),
561
+ TypeFactory.keyword("any"),
562
+ ],
563
+ [
564
+ decode_array(config)(importer)(
565
+ input,
566
+ array,
567
+ explore,
568
+ ),
569
+ ],
570
+ ),
571
+ empty: ts.factory.createNewExpression(
572
+ ts.factory.createIdentifier("Map"),
573
+ [
574
+ TypeFactory.keyword("any"),
575
+ TypeFactory.keyword("any"),
576
+ ],
577
+ [],
578
+ ),
579
+ success: ts.factory.createTrue(),
580
+ failure: (input, expected) =>
581
+ create_throw_error(importer)(expected)(input),
582
+ })([])(input, maps, explore),
583
+ undefined,
584
+ undefined,
585
+ );
586
+
587
+ const explore_objects =
588
+ (config: FeatureProgrammer.IConfig) =>
589
+ (importer: FunctionImporter) =>
590
+ (
591
+ input: ts.Expression,
592
+ meta: Metadata,
593
+ explore: FeatureProgrammer.IExplore,
594
+ ) => {
595
+ if (meta.objects.length === 1)
596
+ return decode_object(importer)(
597
+ input,
598
+ meta.objects[0]!,
599
+ explore,
600
+ );
601
+
602
+ return ts.factory.createCallExpression(
603
+ ts.factory.createIdentifier(
604
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
605
+ ),
606
+ undefined,
607
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
608
+ );
609
+ };
610
+
611
+ const explore_arrays =
612
+ (project: IProject) =>
613
+ (config: FeatureProgrammer.IConfig) =>
614
+ (importer: FunctionImporter) =>
615
+ (
616
+ input: ts.Expression,
617
+ elements: MetadataArray[],
618
+ explore: FeatureProgrammer.IExplore,
619
+ ): ts.Expression =>
620
+ explore_array_like_union_types(config)(importer)(
621
+ UnionExplorer.array({
622
+ checker: IsProgrammer.decode(project)(importer),
623
+ decoder: decode_array(config)(importer),
624
+ empty: ts.factory.createIdentifier("[]"),
625
+ success: ts.factory.createTrue(),
626
+ failure: (input, expected) =>
627
+ create_throw_error(importer)(expected)(input),
628
+ }),
629
+ )(input, elements, explore);
630
+
631
+ const explore_array_like_union_types =
632
+ (config: FeatureProgrammer.IConfig) =>
633
+ (importer: FunctionImporter) =>
634
+ <T extends MetadataArray | MetadataTuple>(
635
+ factory: (
636
+ parameters: ts.ParameterDeclaration[],
637
+ ) => (
638
+ input: ts.Expression,
639
+ elements: T[],
640
+ explore: FeatureProgrammer.IExplore,
641
+ ) => ts.ArrowFunction,
642
+ ) =>
643
+ (
644
+ input: ts.Expression,
645
+ elements: T[],
646
+ explore: FeatureProgrammer.IExplore,
647
+ ): ts.Expression => {
648
+ const arrow =
649
+ (parameters: ts.ParameterDeclaration[]) =>
650
+ (explore: FeatureProgrammer.IExplore) =>
651
+ (input: ts.Expression): ts.ArrowFunction =>
652
+ factory(parameters)(input, elements, explore);
653
+ if (elements.every((e) => e.type.recursive === false))
654
+ ts.factory.createCallExpression(
655
+ arrow([])(explore)(input),
656
+ undefined,
657
+ [],
658
+ );
659
+
660
+ explore = {
661
+ ...explore,
662
+ source: "function",
663
+ from: "array",
664
+ };
665
+ return ts.factory.createCallExpression(
666
+ ts.factory.createIdentifier(
667
+ importer.emplaceUnion(
668
+ config.prefix,
669
+ elements.map((e) => e.type.name).join(" | "),
670
+ () =>
671
+ arrow(
672
+ FeatureProgrammer.parameterDeclarations(config)(
673
+ TypeFactory.keyword("any"),
674
+ )(ts.factory.createIdentifier("input")),
675
+ )({
676
+ ...explore,
677
+ postfix: "",
678
+ })(ts.factory.createIdentifier("input")),
679
+ ),
680
+ ),
681
+ undefined,
682
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
683
+ );
684
+ };
685
+
686
+ /* -----------------------------------------------------------
687
+ CONFIGURATIONS
688
+ ----------------------------------------------------------- */
689
+ const PREFIX = "$c";
690
+
691
+ const configure =
692
+ (project: IProject) =>
693
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
694
+ const config: FeatureProgrammer.IConfig = {
695
+ types: {
696
+ input: (type, name) =>
697
+ ts.factory.createTypeReferenceNode(
698
+ name ??
699
+ TypeFactory.getFullName(project.checker)(type),
700
+ ),
701
+ output: (type, name) =>
702
+ ts.factory.createTypeReferenceNode(
703
+ `typia.Resolved<${
704
+ name ??
705
+ TypeFactory.getFullName(project.checker)(type)
706
+ }>`,
707
+ ),
708
+ },
709
+ prefix: PREFIX,
710
+ trace: false,
711
+ path: false,
712
+ initializer,
713
+ decoder: () => decode(project)(config)(importer),
714
+ objector: {
715
+ checker: () => IsProgrammer.decode(project)(importer),
716
+ decoder: () => decode_object(importer),
717
+ joiner: CloneJoiner.object,
718
+ unionizer: decode_union_object(
719
+ IsProgrammer.decode_object(project)(importer),
720
+ )(decode_object(importer))((exp) => exp)(
721
+ (input, expected) =>
722
+ create_throw_error(importer)(expected)(input),
723
+ ),
724
+ failure: (input, expected) =>
725
+ create_throw_error(importer)(expected)(input),
726
+ },
727
+ generator: {
728
+ arrays: () => write_array_functions(config)(importer),
729
+ tuples: () =>
730
+ write_tuple_functions(project)(config)(importer),
731
+ },
732
+ };
733
+ return config;
734
+ };
735
+
736
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
737
+ (project) => (importer) => (type) => {
738
+ const collection = new MetadataCollection();
739
+ const result = MetadataFactory.analyze(
740
+ project.checker,
741
+ project.context,
742
+ )({
743
+ escape: false,
744
+ constant: true,
745
+ absorb: true,
746
+ validate: (meta) => {
747
+ const output: string[] = [];
748
+ if (meta.natives.some((n) => n === "WeakSet"))
749
+ output.push("unable to clone WeakSet");
750
+ else if (meta.natives.some((n) => n === "WeakMap"))
751
+ output.push("unable to clone WeakMap");
752
+ return output;
753
+ },
754
+ })(collection)(type);
755
+ if (result.success === false)
756
+ throw TransformerError.from(`typia.misc.${importer.method}`)(
757
+ result.errors,
758
+ );
759
+ return [collection, result.data];
760
+ };
761
+
762
+ const create_throw_error =
763
+ (importer: FunctionImporter) =>
764
+ (expected: string) =>
765
+ (value: ts.Expression) =>
766
+ ts.factory.createExpressionStatement(
767
+ ts.factory.createCallExpression(
768
+ importer.use("throws"),
769
+ [],
770
+ [
771
+ ts.factory.createObjectLiteralExpression(
772
+ [
773
+ ts.factory.createPropertyAssignment(
774
+ "expected",
775
+ ts.factory.createStringLiteral(expected),
776
+ ),
777
+ ts.factory.createPropertyAssignment(
778
+ "value",
779
+ value,
780
+ ),
781
+ ],
782
+ true,
783
+ ),
784
+ ],
785
+ ),
786
+ );
787
+ }