typia 4.0.0 → 4.0.1-dev.20230604

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/lib/programmers/ApplicationProgrammer.js +1 -1
  2. package/lib/programmers/internal/application_array.js +19 -41
  3. package/lib/programmers/internal/application_array.js.map +1 -1
  4. package/lib/programmers/internal/application_definition.d.ts +1 -1
  5. package/lib/programmers/internal/application_definition.js +8 -8
  6. package/lib/programmers/internal/application_definition.js.map +1 -1
  7. package/lib/programmers/internal/application_native.js +3 -3
  8. package/lib/programmers/internal/application_object.js +4 -4
  9. package/lib/programmers/internal/application_schema.js +3 -3
  10. package/lib/programmers/internal/application_schema.js.map +1 -1
  11. package/lib/programmers/internal/application_tuple.js +8 -30
  12. package/lib/programmers/internal/application_tuple.js.map +1 -1
  13. package/lib/schemas/IJsonComponents.d.ts +1 -12
  14. package/lib/schemas/IJsonSchema.d.ts +1 -1
  15. package/package.json +1 -1
  16. package/src/IRandomGenerator.ts +34 -34
  17. package/src/factories/IdentifierFactory.ts +65 -65
  18. package/src/factories/MetadataCollection.ts +254 -254
  19. package/src/factories/MetadataFactory.ts +40 -40
  20. package/src/factories/MetadataTagFactory.ts +300 -300
  21. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  22. package/src/factories/internal/metadata/emplace_metadata_definition.ts +35 -35
  23. package/src/factories/internal/metadata/emplace_metadata_object.ts +142 -142
  24. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  25. package/src/factories/internal/metadata/explore_metadata.ts +66 -66
  26. package/src/factories/internal/metadata/iterate_metadata.ts +77 -77
  27. package/src/factories/internal/metadata/iterate_metadata_array.ts +25 -25
  28. package/src/factories/internal/metadata/iterate_metadata_collection.ts +130 -130
  29. package/src/factories/internal/metadata/iterate_metadata_definition.ts +30 -30
  30. package/src/factories/internal/metadata/iterate_metadata_object.ts +49 -49
  31. package/src/factories/internal/metadata/iterate_metadata_sort.ts +68 -68
  32. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  33. package/src/factories/internal/metadata/iterate_metadata_union.ts +68 -68
  34. package/src/metadata/IMetadata.ts +27 -27
  35. package/src/metadata/IMetadataAlias.ts +12 -12
  36. package/src/metadata/IMetadataApplication.ts +7 -7
  37. package/src/metadata/IMetadataArray.ts +10 -10
  38. package/src/metadata/IMetadataCollection.ts +11 -11
  39. package/src/metadata/IMetadataDictionary.ts +14 -14
  40. package/src/metadata/IMetadataObject.ts +18 -18
  41. package/src/metadata/IMetadataProperty.ts +11 -11
  42. package/src/metadata/IMetadataTuple.ts +10 -10
  43. package/src/metadata/Metadata.ts +585 -585
  44. package/src/metadata/MetadataAlias.ts +61 -61
  45. package/src/metadata/MetadataArray.ts +52 -52
  46. package/src/metadata/MetadataObject.ts +114 -114
  47. package/src/metadata/MetadataProperty.ts +64 -64
  48. package/src/metadata/MetadataTuple.ts +53 -53
  49. package/src/programmers/ApplicationProgrammer.ts +55 -55
  50. package/src/programmers/AssertProgrammer.ts +291 -291
  51. package/src/programmers/CheckerProgrammer.ts +1182 -1182
  52. package/src/programmers/CloneProgrammer.ts +595 -595
  53. package/src/programmers/FeatureProgrammer.ts +495 -495
  54. package/src/programmers/IsProgrammer.ts +250 -250
  55. package/src/programmers/LiteralsProgrammer.ts +66 -66
  56. package/src/programmers/PruneProgrammer.ts +550 -550
  57. package/src/programmers/RandomProgrammer.ts +589 -589
  58. package/src/programmers/StringifyProgrammer.ts +990 -990
  59. package/src/programmers/ValidateProgrammer.ts +313 -313
  60. package/src/programmers/helpers/FunctionImporeter.ts +78 -78
  61. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  62. package/src/programmers/helpers/UnionExplorer.ts +301 -301
  63. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  64. package/src/programmers/helpers/disable_function_importer_declare.ts +26 -26
  65. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  66. package/src/programmers/internal/application_array.ts +30 -64
  67. package/src/programmers/internal/application_boolean.ts +15 -15
  68. package/src/programmers/internal/application_constant.ts +26 -26
  69. package/src/programmers/internal/application_default.ts +17 -17
  70. package/src/programmers/internal/application_definition.ts +45 -45
  71. package/src/programmers/internal/application_native.ts +39 -39
  72. package/src/programmers/internal/application_number.ts +74 -74
  73. package/src/programmers/internal/application_object.ts +167 -167
  74. package/src/programmers/internal/application_schema.ts +156 -164
  75. package/src/programmers/internal/application_tuple.ts +47 -77
  76. package/src/programmers/internal/check_union_array_like.ts +329 -329
  77. package/src/programmers/internal/feature_object_entries.ts +63 -63
  78. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  79. package/src/schemas/IJsonApplication.ts +8 -8
  80. package/src/schemas/IJsonComponents.ts +34 -45
  81. package/src/schemas/IJsonSchema.ts +134 -134
  82. package/src/transformers/CallExpressionTransformer.ts +179 -179
  83. package/src/transformers/FileTransformer.ts +47 -47
  84. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +104 -104
  85. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +53 -53
  86. package/src/utils/ArrayUtil.ts +45 -45
  87. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,990 +1,990 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../factories/MetadataCollection";
6
- import { MetadataFactory } from "../factories/MetadataFactory";
7
- import { StatementFactory } from "../factories/StatementFactory";
8
- import { TypeFactory } from "../factories/TypeFactory";
9
- import { ValueFactory } from "../factories/ValueFactory";
10
-
11
- import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
12
- import { IMetadataTag } from "../metadata/IMetadataTag";
13
- import { Metadata } from "../metadata/Metadata";
14
- import { MetadataArray } from "../metadata/MetadataArray";
15
- import { MetadataObject } from "../metadata/MetadataObject";
16
- import { MetadataTuple } from "../metadata/MetadataTuple";
17
-
18
- import { IProject } from "../transformers/IProject";
19
-
20
- import { Atomic } from "../typings/Atomic";
21
-
22
- import { ArrayUtil } from "../utils/ArrayUtil";
23
-
24
- import { FeatureProgrammer } from "./FeatureProgrammer";
25
- import { IsProgrammer } from "./IsProgrammer";
26
- import { AtomicPredicator } from "./helpers/AtomicPredicator";
27
- import { FunctionImporter } from "./helpers/FunctionImporeter";
28
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
29
- import { OptionPredicator } from "./helpers/OptionPredicator";
30
- import { StringifyJoiner } from "./helpers/StringifyJoinder";
31
- import { StringifyPredicator } from "./helpers/StringifyPredicator";
32
- import { UnionExplorer } from "./helpers/UnionExplorer";
33
- import { check_native } from "./internal/check_native";
34
- import { decode_union_object } from "./internal/decode_union_object";
35
- import { feature_object_entries } from "./internal/feature_object_entries";
36
- import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
37
-
38
- export namespace StringifyProgrammer {
39
- /* -----------------------------------------------------------
40
- WRITER
41
- ----------------------------------------------------------- */
42
- /**
43
- * @deprecated Use `write()` function instead
44
- */
45
- export const generate =
46
- (project: IProject, modulo: ts.LeftHandSideExpression) =>
47
- (type: ts.Type, name?: string) =>
48
- write(project)(modulo)(type, name);
49
-
50
- export const write =
51
- (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
52
- const importer: FunctionImporter = new FunctionImporter();
53
- const config: FeatureProgrammer.IConfig =
54
- configure(project)(importer);
55
-
56
- return FeatureProgrammer.write(project)({
57
- ...config,
58
- addition: (collection) => [
59
- ...IsProgrammer.write_function_statements(project)(
60
- importer,
61
- )(collection),
62
- ...importer.declare(modulo),
63
- ],
64
- })(importer);
65
- };
66
-
67
- const write_array_functions =
68
- (config: FeatureProgrammer.IConfig) =>
69
- (importer: FunctionImporter) =>
70
- (collection: MetadataCollection): ts.VariableStatement[] =>
71
- collection
72
- .arrays()
73
- .filter((a) => a.recursive)
74
- .map((array, i) =>
75
- StatementFactory.constant(
76
- `${config.prefix}a${i}`,
77
- ts.factory.createArrowFunction(
78
- undefined,
79
- undefined,
80
- FeatureProgrammer.parameterDeclarations(config)(
81
- TypeFactory.keyword("any"),
82
- )(ts.factory.createIdentifier("input")),
83
- TypeFactory.keyword("any"),
84
- undefined,
85
- decode_array_inline(config)(importer)(
86
- ts.factory.createIdentifier("input"),
87
- array,
88
- {
89
- tracable: config.trace,
90
- source: "function",
91
- from: "array",
92
- postfix: "",
93
- },
94
- ),
95
- ),
96
- ),
97
- );
98
-
99
- const write_tuple_functions =
100
- (project: IProject) =>
101
- (config: FeatureProgrammer.IConfig) =>
102
- (importer: FunctionImporter) =>
103
- (collection: MetadataCollection): ts.VariableStatement[] =>
104
- collection
105
- .tuples()
106
- .filter((t) => t.recursive)
107
- .map((tuple, i) =>
108
- StatementFactory.constant(
109
- `${config.prefix}t${i}`,
110
- ts.factory.createArrowFunction(
111
- undefined,
112
- undefined,
113
- FeatureProgrammer.parameterDeclarations(config)(
114
- TypeFactory.keyword("any"),
115
- )(ts.factory.createIdentifier("input")),
116
- TypeFactory.keyword("any"),
117
- undefined,
118
- decode_tuple_inline(project)(config)(importer)(
119
- ts.factory.createIdentifier("input"),
120
- tuple,
121
- {
122
- tracable: config.trace,
123
- source: "function",
124
- from: "array",
125
- postfix: "",
126
- },
127
- ),
128
- ),
129
- ),
130
- );
131
-
132
- /* -----------------------------------------------------------
133
- DECODERS
134
- ----------------------------------------------------------- */
135
- const decode =
136
- (project: IProject) =>
137
- (config: FeatureProgrammer.IConfig) =>
138
- (importer: FunctionImporter) =>
139
- (
140
- input: ts.Expression,
141
- meta: Metadata,
142
- explore: FeatureProgrammer.IExplore,
143
- ): ts.Expression => {
144
- // ANY TYPE
145
- if (meta.any === true)
146
- return wrap_required(
147
- input,
148
- meta,
149
- explore,
150
- )(
151
- wrap_functional(
152
- input,
153
- meta,
154
- explore,
155
- )(
156
- ts.factory.createCallExpression(
157
- ts.factory.createIdentifier("JSON.stringify"),
158
- undefined,
159
- [input],
160
- ),
161
- ),
162
- );
163
-
164
- // ONLY NULL OR UNDEFINED
165
- const size: number = meta.size();
166
- if (
167
- size === 0 &&
168
- (meta.required === false || meta.nullable === true)
169
- ) {
170
- if (meta.required === false && meta.nullable === true)
171
- return explore.from === "array"
172
- ? ts.factory.createStringLiteral("null")
173
- : ts.factory.createConditionalExpression(
174
- ts.factory.createStrictEquality(
175
- ts.factory.createNull(),
176
- input,
177
- ),
178
- undefined,
179
- ts.factory.createStringLiteral("null"),
180
- undefined,
181
- ts.factory.createIdentifier("undefined"),
182
- );
183
- else if (meta.required === false)
184
- return explore.from === "array"
185
- ? ts.factory.createStringLiteral("null")
186
- : ts.factory.createIdentifier("undefined");
187
- else return ts.factory.createStringLiteral("null");
188
- }
189
-
190
- //----
191
- // LIST UP UNION TYPES
192
- //----
193
- const unions: IUnion[] = [];
194
-
195
- // toJSON() METHOD
196
- if (meta.resolved !== null)
197
- if (size === 1)
198
- return decode_to_json(project)(config)(importer)(
199
- input,
200
- meta.resolved,
201
- explore,
202
- );
203
- else
204
- unions.push({
205
- type: "resolved",
206
- is: () => IsProgrammer.decode_to_json(false)(input),
207
- value: () =>
208
- decode_to_json(project)(config)(importer)(
209
- input,
210
- meta.resolved!,
211
- explore,
212
- ),
213
- });
214
- else if (meta.functional === true)
215
- unions.push({
216
- type: "functional",
217
- is: () => IsProgrammer.decode_functional(input),
218
- value: () => decode_functional(explore),
219
- });
220
-
221
- // TEMPLATES
222
- if (
223
- meta.templates.length ||
224
- ArrayUtil.has(meta.constants, (c) => c.type === "string")
225
- )
226
- if (AtomicPredicator.template(meta)) {
227
- const partial = Metadata.initialize();
228
- partial.atomics.push("string"),
229
- unions.push({
230
- type: "template literal",
231
- is: () =>
232
- IsProgrammer.decode(project)(importer)(
233
- input,
234
- partial,
235
- explore,
236
- [],
237
- [],
238
- ),
239
- value: () =>
240
- decode_atomic(project)(importer)(
241
- input,
242
- "string",
243
- explore,
244
- ),
245
- });
246
- }
247
-
248
- // CONSTANTS
249
- for (const constant of meta.constants)
250
- if (AtomicPredicator.constant(meta)(constant.type) === false)
251
- continue;
252
- else if (constant.type !== "string")
253
- unions.push({
254
- type: "atomic",
255
- is: () =>
256
- IsProgrammer.decode(project)(importer)(
257
- input,
258
- (() => {
259
- const partial = Metadata.initialize();
260
- partial.atomics.push(constant.type);
261
- return partial;
262
- })(),
263
- explore,
264
- [],
265
- [],
266
- ),
267
- value: () =>
268
- decode_atomic(project)(importer)(
269
- input,
270
- constant.type,
271
- explore,
272
- ),
273
- });
274
- else if (meta.templates.length === 0)
275
- unions.push({
276
- type: "const string",
277
- is: () =>
278
- IsProgrammer.decode(project)(importer)(
279
- input,
280
- (() => {
281
- const partial = Metadata.initialize();
282
- partial.atomics.push("string");
283
- return partial;
284
- })(),
285
- explore,
286
- [],
287
- [],
288
- ),
289
- value: () =>
290
- decode_constant_string(project)(importer)(
291
- input,
292
- [...constant.values] as string[],
293
- explore,
294
- ),
295
- });
296
-
297
- /// ATOMICS
298
- for (const type of meta.atomics)
299
- if (AtomicPredicator.atomic(meta)(type))
300
- unions.push({
301
- type: "atomic",
302
- is: () =>
303
- IsProgrammer.decode(project)(importer)(
304
- input,
305
- (() => {
306
- const partial = Metadata.initialize();
307
- partial.atomics.push(type);
308
- return partial;
309
- })(),
310
- explore,
311
- [],
312
- [],
313
- ),
314
- value: () =>
315
- decode_atomic(project)(importer)(
316
- input,
317
- type,
318
- explore,
319
- ),
320
- });
321
-
322
- // TUPLES
323
- for (const tuple of meta.tuples) {
324
- for (const child of tuple.elements)
325
- if (StringifyPredicator.undefindable(meta))
326
- throw new Error(
327
- `Error on typia.stringify(): tuple cannot contain undefined value - (${child.getName()}).`,
328
- );
329
- unions.push({
330
- type: "tuple",
331
- is: () =>
332
- IsProgrammer.decode(project)(importer)(
333
- input,
334
- (() => {
335
- const partial = Metadata.initialize();
336
- partial.tuples.push(tuple);
337
- return partial;
338
- })(),
339
- explore,
340
- [],
341
- [],
342
- ),
343
- value: () =>
344
- decode_tuple(project)(config)(importer)(
345
- input,
346
- tuple,
347
- explore,
348
- ),
349
- });
350
- }
351
-
352
- // ARRAYS
353
- if (meta.arrays.length) {
354
- for (const child of meta.arrays)
355
- if (StringifyPredicator.undefindable(child.value))
356
- throw new Error(
357
- `Error on typia.stringify(): array cannot contain undefined value (${child.value.getName()}).`,
358
- );
359
- const value: () => ts.Expression =
360
- meta.arrays.length === 1
361
- ? () =>
362
- decode_array(config)(importer)(
363
- input,
364
- meta.arrays[0]!,
365
- {
366
- ...explore,
367
- from: "array",
368
- },
369
- )
370
- : meta.arrays.some((elem) => elem.value.any)
371
- ? () =>
372
- ts.factory.createCallExpression(
373
- ts.factory.createIdentifier("JSON.stringify"),
374
- undefined,
375
- [input],
376
- )
377
- : () =>
378
- explore_arrays(project)(config)(importer)(
379
- input,
380
- meta.arrays,
381
- {
382
- ...explore,
383
- from: "array",
384
- },
385
- );
386
-
387
- unions.push({
388
- type: "array",
389
- is: () => ExpressionFactory.isArray(input),
390
- value,
391
- });
392
- }
393
-
394
- // BUILT-IN CLASSES
395
- if (meta.natives.length)
396
- for (const native of meta.natives)
397
- unions.push({
398
- type: "object",
399
- is: () => check_native(native)(input),
400
- value: () =>
401
- AtomicPredicator.native(native)
402
- ? decode_atomic(project)(importer)(
403
- input,
404
- native.toLowerCase() as Atomic.Literal,
405
- explore,
406
- )
407
- : ts.factory.createStringLiteral("{}"),
408
- });
409
-
410
- // SETS
411
- if (meta.sets.length)
412
- unions.push({
413
- type: "object",
414
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
415
- value: () => ts.factory.createStringLiteral("{}"),
416
- });
417
-
418
- // MAPS
419
- if (meta.maps.length)
420
- unions.push({
421
- type: "object",
422
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
423
- value: () => ts.factory.createStringLiteral("{}"),
424
- });
425
-
426
- // OBJECTS
427
- if (meta.objects.length)
428
- unions.push({
429
- type: "object",
430
- is: () =>
431
- ExpressionFactory.isObject({
432
- checkNull: true,
433
- checkArray: meta.objects.some((obj) =>
434
- obj.properties.every(
435
- (prop) =>
436
- !prop.key.isSoleLiteral() ||
437
- !prop.value.required,
438
- ),
439
- ),
440
- })(input),
441
- value: () =>
442
- meta.isParentResolved() === false &&
443
- meta.objects.length === 1 &&
444
- meta.objects[0]!._Is_simple()
445
- ? (() => {
446
- const obj: MetadataObject = meta.objects[0]!;
447
- const entries: IExpressionEntry<ts.Expression>[] =
448
- feature_object_entries({
449
- decoder: () =>
450
- decode(project)(config)(importer),
451
- trace: false,
452
- path: false,
453
- })(importer)(obj)(
454
- ts.factory.createAsExpression(
455
- input,
456
- TypeFactory.keyword("any"),
457
- ),
458
- );
459
- return StringifyJoiner.object(importer)(
460
- ts.factory.createAsExpression(
461
- input,
462
- TypeFactory.keyword("any"),
463
- ),
464
- entries,
465
- );
466
- })()
467
- : explore_objects(config)(importer)(input, meta, {
468
- ...explore,
469
- from: "object",
470
- }),
471
- });
472
-
473
- //----
474
- // RETURNS
475
- //----
476
- // CHECK NULL AND UNDEFINED
477
- const wrapper = (output: ts.Expression) =>
478
- wrap_required(
479
- input,
480
- meta,
481
- explore,
482
- )(wrap_nullable(input, meta)(output));
483
-
484
- // DIRECT RETURN
485
- if (unions.length === 0)
486
- return ts.factory.createCallExpression(
487
- ts.factory.createIdentifier("JSON.stringify"),
488
- undefined,
489
- [input],
490
- );
491
- else if (unions.length === 1) return wrapper(unions[0]!.value());
492
-
493
- // RETURN WITH TYPE CHECKING
494
- return wrapper(
495
- ts.factory.createCallExpression(
496
- ts.factory.createArrowFunction(
497
- undefined,
498
- undefined,
499
- [],
500
- undefined,
501
- undefined,
502
- iterate(importer, input, unions, meta.getName()),
503
- ),
504
- undefined,
505
- undefined,
506
- ),
507
- );
508
- };
509
-
510
- const decode_object = (importer: FunctionImporter) =>
511
- FeatureProgrammer.decode_object({
512
- trace: false,
513
- path: false,
514
- prefix: PREFIX,
515
- })(importer);
516
-
517
- const decode_array =
518
- (config: FeatureProgrammer.IConfig) =>
519
- (importer: FunctionImporter) =>
520
- (
521
- input: ts.Expression,
522
- array: MetadataArray,
523
- explore: FeatureProgrammer.IExplore,
524
- ) =>
525
- array.recursive
526
- ? ts.factory.createCallExpression(
527
- ts.factory.createIdentifier(
528
- importer.useLocal(`${config.prefix}a${array.index}`),
529
- ),
530
- undefined,
531
- FeatureProgrammer.argumentsArray(config)({
532
- ...explore,
533
- source: "function",
534
- from: "array",
535
- })(input),
536
- )
537
- : decode_array_inline(config)(importer)(input, array, explore);
538
-
539
- const decode_array_inline =
540
- (config: FeatureProgrammer.IConfig) =>
541
- (importer: FunctionImporter) =>
542
- (
543
- input: ts.Expression,
544
- array: MetadataArray,
545
- explore: FeatureProgrammer.IExplore,
546
- ) =>
547
- FeatureProgrammer.decode_array(config)(importer)(
548
- StringifyJoiner.array,
549
- )(input, array, explore, [], []);
550
-
551
- const decode_tuple =
552
- (project: IProject) =>
553
- (config: FeatureProgrammer.IConfig) =>
554
- (importer: FunctionImporter) =>
555
- (
556
- input: ts.Expression,
557
- tuple: MetadataTuple,
558
- explore: FeatureProgrammer.IExplore,
559
- ): ts.Expression =>
560
- tuple.recursive
561
- ? ts.factory.createCallExpression(
562
- ts.factory.createIdentifier(
563
- importer.useLocal(`${config.prefix}t${tuple.index}`),
564
- ),
565
- undefined,
566
- FeatureProgrammer.argumentsArray(config)({
567
- ...explore,
568
- source: "function",
569
- })(input),
570
- )
571
- : decode_tuple_inline(project)(config)(importer)(
572
- input,
573
- tuple,
574
- explore,
575
- );
576
-
577
- const decode_tuple_inline =
578
- (project: IProject) =>
579
- (config: FeatureProgrammer.IConfig) =>
580
- (importer: FunctionImporter) =>
581
- (
582
- input: ts.Expression,
583
- tuple: MetadataTuple,
584
- explore: FeatureProgrammer.IExplore,
585
- ): ts.Expression => {
586
- const children: ts.Expression[] = tuple.elements
587
- .filter((elem) => elem.rest === null)
588
- .map((elem, index) =>
589
- decode(project)(config)(importer)(
590
- ts.factory.createElementAccessExpression(input, index),
591
- elem,
592
- {
593
- ...explore,
594
- from: "array",
595
- postfix: explore.postfix.length
596
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
597
- : `"[${index}]"`,
598
- },
599
- ),
600
- );
601
- const rest = (() => {
602
- if (tuple.elements.length === 0) return null;
603
- const last = tuple.elements.at(-1)!;
604
- if (last.rest === null) return null;
605
-
606
- const code = decode(project)(config)(importer)(
607
- ts.factory.createCallExpression(
608
- IdentifierFactory.access(input)("slice"),
609
- undefined,
610
- [
611
- ts.factory.createNumericLiteral(
612
- tuple.elements.length - 1,
613
- ),
614
- ],
615
- ),
616
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
617
- {
618
- ...explore,
619
- start: tuple.elements.length - 1,
620
- },
621
- );
622
- return ts.factory.createCallExpression(
623
- importer.use("rest"),
624
- undefined,
625
- [code],
626
- );
627
- })();
628
- return StringifyJoiner.tuple(children, rest);
629
- };
630
-
631
- const decode_atomic =
632
- (project: IProject) =>
633
- (importer: FunctionImporter) =>
634
- (
635
- input: ts.Expression,
636
- type: string,
637
- explore: FeatureProgrammer.IExplore,
638
- ) => {
639
- if (type === "string")
640
- return ts.factory.createCallExpression(
641
- importer.use("string"),
642
- undefined,
643
- [input],
644
- );
645
- else if (
646
- type === "number" &&
647
- OptionPredicator.numeric(project.options)
648
- )
649
- input = ts.factory.createCallExpression(
650
- importer.use("number"),
651
- undefined,
652
- [input],
653
- );
654
-
655
- return explore.from !== "top"
656
- ? input
657
- : ts.factory.createCallExpression(
658
- IdentifierFactory.access(input)("toString"),
659
- undefined,
660
- undefined,
661
- );
662
- };
663
-
664
- const decode_constant_string =
665
- (project: IProject) =>
666
- (importer: FunctionImporter) =>
667
- (
668
- input: ts.Expression,
669
- values: string[],
670
- explore: FeatureProgrammer.IExplore,
671
- ): ts.Expression => {
672
- if (values.every((v) => !StringifyPredicator.require_escape(v)))
673
- return [
674
- ts.factory.createStringLiteral('"'),
675
- input,
676
- ts.factory.createStringLiteral('"'),
677
- ].reduce((x, y) => ts.factory.createAdd(x, y));
678
- else
679
- return decode_atomic(project)(importer)(
680
- input,
681
- "string",
682
- explore,
683
- );
684
- };
685
-
686
- const decode_to_json =
687
- (project: IProject) =>
688
- (config: FeatureProgrammer.IConfig) =>
689
- (importer: FunctionImporter) =>
690
- (
691
- input: ts.Expression,
692
- resolved: Metadata,
693
- explore: FeatureProgrammer.IExplore,
694
- ): ts.Expression => {
695
- return decode(project)(config)(importer)(
696
- ts.factory.createCallExpression(
697
- IdentifierFactory.access(input)("toJSON"),
698
- undefined,
699
- [],
700
- ),
701
- resolved,
702
- explore,
703
- );
704
- };
705
-
706
- const decode_functional = (explore: FeatureProgrammer.IExplore) =>
707
- explore.from === "array"
708
- ? ts.factory.createStringLiteral("null")
709
- : ts.factory.createIdentifier("undefined");
710
-
711
- /* -----------------------------------------------------------
712
- EXPLORERS
713
- ----------------------------------------------------------- */
714
- const explore_objects =
715
- (config: FeatureProgrammer.IConfig) =>
716
- (importer: FunctionImporter) =>
717
- (
718
- input: ts.Expression,
719
- meta: Metadata,
720
- explore: FeatureProgrammer.IExplore,
721
- ) =>
722
- meta.objects.length === 1
723
- ? decode_object(importer)(input, meta.objects[0]!, explore)
724
- : ts.factory.createCallExpression(
725
- ts.factory.createIdentifier(
726
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
727
- ),
728
- undefined,
729
- FeatureProgrammer.argumentsArray(config)(explore)(input),
730
- );
731
-
732
- const explore_arrays =
733
- (project: IProject) =>
734
- (config: FeatureProgrammer.IConfig) =>
735
- (importer: FunctionImporter) =>
736
- (
737
- input: ts.Expression,
738
- elements: MetadataArray[],
739
- explore: FeatureProgrammer.IExplore,
740
- ): ts.Expression =>
741
- explore_array_like_union_types(config)(importer)(
742
- UnionExplorer.array({
743
- checker: IsProgrammer.decode(project)(importer),
744
- decoder: decode_array(config)(importer),
745
- empty: ts.factory.createStringLiteral("[]"),
746
- success: ts.factory.createTrue(),
747
- failure: (input, expected) =>
748
- create_throw_error(importer)(expected)(input),
749
- }),
750
- )(input, elements, explore);
751
-
752
- const explore_array_like_union_types =
753
- (config: FeatureProgrammer.IConfig) =>
754
- (importer: FunctionImporter) =>
755
- <T extends MetadataArray | MetadataTuple>(
756
- factory: (
757
- parameters: ts.ParameterDeclaration[],
758
- ) => (
759
- input: ts.Expression,
760
- elements: T[],
761
- explore: FeatureProgrammer.IExplore,
762
- tags: IMetadataTag[],
763
- jsDocTags: IJsDocTagInfo[],
764
- ) => ts.ArrowFunction,
765
- ) =>
766
- (
767
- input: ts.Expression,
768
- elements: T[],
769
- explore: FeatureProgrammer.IExplore,
770
- ): ts.Expression => {
771
- const arrow =
772
- (parameters: ts.ParameterDeclaration[]) =>
773
- (explore: FeatureProgrammer.IExplore) =>
774
- (input: ts.Expression): ts.ArrowFunction =>
775
- factory(parameters)(input, elements, explore, [], []);
776
- if (elements.every((e) => e.recursive === false))
777
- ts.factory.createCallExpression(
778
- arrow([])(explore)(input),
779
- undefined,
780
- [],
781
- );
782
-
783
- explore = {
784
- ...explore,
785
- source: "function",
786
- from: "array",
787
- };
788
- return ts.factory.createCallExpression(
789
- ts.factory.createIdentifier(
790
- importer.emplaceUnion(
791
- config.prefix,
792
- elements.map((e) => e.name).join(" | "),
793
- () =>
794
- arrow(
795
- FeatureProgrammer.parameterDeclarations(config)(
796
- TypeFactory.keyword("any"),
797
- )(ts.factory.createIdentifier("input")),
798
- )({
799
- ...explore,
800
- postfix: "",
801
- })(ts.factory.createIdentifier("input")),
802
- ),
803
- ),
804
- undefined,
805
- FeatureProgrammer.argumentsArray(config)(explore)(input),
806
- );
807
- };
808
-
809
- /* -----------------------------------------------------------
810
- RETURN SCRIPTS
811
- ----------------------------------------------------------- */
812
- const wrap_required = (
813
- input: ts.Expression,
814
- meta: Metadata,
815
- explore: FeatureProgrammer.IExplore,
816
- ): ((expression: ts.Expression) => ts.Expression) => {
817
- if (meta.required === true && meta.any === false)
818
- return (expression) => expression;
819
- return (expression) =>
820
- ts.factory.createConditionalExpression(
821
- ts.factory.createStrictInequality(
822
- ts.factory.createIdentifier("undefined"),
823
- input,
824
- ),
825
- undefined,
826
- expression,
827
- undefined,
828
- explore.from === "array"
829
- ? ts.factory.createStringLiteral("null")
830
- : ts.factory.createIdentifier("undefined"),
831
- );
832
- };
833
-
834
- const wrap_nullable = (
835
- input: ts.Expression,
836
- meta: Metadata,
837
- ): ((expression: ts.Expression) => ts.Expression) => {
838
- if (meta.nullable === false) return (expression) => expression;
839
- return (expression) =>
840
- ts.factory.createConditionalExpression(
841
- ts.factory.createStrictInequality(
842
- ts.factory.createNull(),
843
- input,
844
- ),
845
- undefined,
846
- expression,
847
- undefined,
848
- ts.factory.createStringLiteral("null"),
849
- );
850
- };
851
-
852
- const wrap_functional = (
853
- input: ts.Expression,
854
- meta: Metadata,
855
- explore: FeatureProgrammer.IExplore,
856
- ): ((expression: ts.Expression) => ts.Expression) => {
857
- if (meta.functional === false) return (expression) => expression;
858
- return (expression) =>
859
- ts.factory.createConditionalExpression(
860
- ts.factory.createStrictInequality(
861
- ts.factory.createStringLiteral("function"),
862
- ValueFactory.TYPEOF(input),
863
- ),
864
- undefined,
865
- expression,
866
- undefined,
867
- decode_functional(explore),
868
- );
869
- };
870
-
871
- const iterate = (
872
- importer: FunctionImporter,
873
- input: ts.Expression,
874
- unions: IUnion[],
875
- expected: string,
876
- ) =>
877
- ts.factory.createBlock(
878
- [
879
- ...unions.map((u) =>
880
- ts.factory.createIfStatement(
881
- u.is(),
882
- ts.factory.createReturnStatement(u.value()),
883
- ),
884
- ),
885
- create_throw_error(importer)(expected)(input),
886
- ],
887
- true,
888
- );
889
-
890
- /* -----------------------------------------------------------
891
- CONFIGURATIONS
892
- ----------------------------------------------------------- */
893
- const PREFIX = "$s";
894
-
895
- const configure =
896
- (project: IProject) =>
897
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
898
- const config: FeatureProgrammer.IConfig = {
899
- types: {
900
- input: (type, name) =>
901
- ts.factory.createTypeReferenceNode(
902
- name ??
903
- TypeFactory.getFullName(project.checker)(type),
904
- ),
905
- output: () => TypeFactory.keyword("string"),
906
- },
907
- prefix: PREFIX,
908
- trace: false,
909
- path: false,
910
- initializer,
911
- decoder: () => decode(project)(config)(importer),
912
- objector: {
913
- checker: () => (input, meta, explore) =>
914
- IsProgrammer.decode(project)(importer)(
915
- input,
916
- meta,
917
- explore,
918
- [],
919
- [],
920
- ),
921
- decoder: () => decode_object(importer),
922
- joiner: StringifyJoiner.object(importer),
923
- unionizer: decode_union_object(
924
- IsProgrammer.decode_object(importer),
925
- )(decode_object(importer))((exp) => exp)(
926
- (value, expected) =>
927
- create_throw_error(importer)(expected)(value),
928
- ),
929
- failure: (input, expected) =>
930
- create_throw_error(importer)(expected)(input),
931
- },
932
- generator: {
933
- arrays: () => write_array_functions(config)(importer),
934
- tuples: () =>
935
- write_tuple_functions(project)(config)(importer),
936
- },
937
- };
938
- return config;
939
- };
940
-
941
- const initializer: FeatureProgrammer.IConfig["initializer"] =
942
- ({ checker }) =>
943
- (type) => {
944
- const collection: MetadataCollection = new MetadataCollection();
945
- const meta: Metadata = MetadataFactory.analyze(checker)({
946
- resolve: true,
947
- constant: true,
948
- absorb: true,
949
- validate: (meta) => {
950
- if (meta.atomics.find((str) => str === "bigint"))
951
- throw new Error(NO_BIGINT);
952
- },
953
- })(collection)(type);
954
- return [collection, meta];
955
- };
956
-
957
- const create_throw_error =
958
- (importer: FunctionImporter) =>
959
- (expected: string) =>
960
- (value: ts.Expression) =>
961
- ts.factory.createExpressionStatement(
962
- ts.factory.createCallExpression(
963
- importer.use("throws"),
964
- [],
965
- [
966
- ts.factory.createObjectLiteralExpression(
967
- [
968
- ts.factory.createPropertyAssignment(
969
- "expected",
970
- ts.factory.createStringLiteral(expected),
971
- ),
972
- ts.factory.createPropertyAssignment(
973
- "value",
974
- value,
975
- ),
976
- ],
977
- true,
978
- ),
979
- ],
980
- ),
981
- );
982
- }
983
-
984
- interface IUnion {
985
- type: string;
986
- is: () => ts.Expression;
987
- value: () => ts.Expression;
988
- }
989
-
990
- const NO_BIGINT = "Error on typia.stringify(): does not allow bigint type.";
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../factories/MetadataCollection";
6
+ import { MetadataFactory } from "../factories/MetadataFactory";
7
+ import { StatementFactory } from "../factories/StatementFactory";
8
+ import { TypeFactory } from "../factories/TypeFactory";
9
+ import { ValueFactory } from "../factories/ValueFactory";
10
+
11
+ import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
12
+ import { IMetadataTag } from "../metadata/IMetadataTag";
13
+ import { Metadata } from "../metadata/Metadata";
14
+ import { MetadataArray } from "../metadata/MetadataArray";
15
+ import { MetadataObject } from "../metadata/MetadataObject";
16
+ import { MetadataTuple } from "../metadata/MetadataTuple";
17
+
18
+ import { IProject } from "../transformers/IProject";
19
+
20
+ import { Atomic } from "../typings/Atomic";
21
+
22
+ import { ArrayUtil } from "../utils/ArrayUtil";
23
+
24
+ import { FeatureProgrammer } from "./FeatureProgrammer";
25
+ import { IsProgrammer } from "./IsProgrammer";
26
+ import { AtomicPredicator } from "./helpers/AtomicPredicator";
27
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
28
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
29
+ import { OptionPredicator } from "./helpers/OptionPredicator";
30
+ import { StringifyJoiner } from "./helpers/StringifyJoinder";
31
+ import { StringifyPredicator } from "./helpers/StringifyPredicator";
32
+ import { UnionExplorer } from "./helpers/UnionExplorer";
33
+ import { check_native } from "./internal/check_native";
34
+ import { decode_union_object } from "./internal/decode_union_object";
35
+ import { feature_object_entries } from "./internal/feature_object_entries";
36
+ import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
37
+
38
+ export namespace StringifyProgrammer {
39
+ /* -----------------------------------------------------------
40
+ WRITER
41
+ ----------------------------------------------------------- */
42
+ /**
43
+ * @deprecated Use `write()` function instead
44
+ */
45
+ export const generate =
46
+ (project: IProject, modulo: ts.LeftHandSideExpression) =>
47
+ (type: ts.Type, name?: string) =>
48
+ write(project)(modulo)(type, name);
49
+
50
+ export const write =
51
+ (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
52
+ const importer: FunctionImporter = new FunctionImporter();
53
+ const config: FeatureProgrammer.IConfig =
54
+ configure(project)(importer);
55
+
56
+ return FeatureProgrammer.write(project)({
57
+ ...config,
58
+ addition: (collection) => [
59
+ ...IsProgrammer.write_function_statements(project)(
60
+ importer,
61
+ )(collection),
62
+ ...importer.declare(modulo),
63
+ ],
64
+ })(importer);
65
+ };
66
+
67
+ const write_array_functions =
68
+ (config: FeatureProgrammer.IConfig) =>
69
+ (importer: FunctionImporter) =>
70
+ (collection: MetadataCollection): ts.VariableStatement[] =>
71
+ collection
72
+ .arrays()
73
+ .filter((a) => a.recursive)
74
+ .map((array, i) =>
75
+ StatementFactory.constant(
76
+ `${config.prefix}a${i}`,
77
+ ts.factory.createArrowFunction(
78
+ undefined,
79
+ undefined,
80
+ FeatureProgrammer.parameterDeclarations(config)(
81
+ TypeFactory.keyword("any"),
82
+ )(ts.factory.createIdentifier("input")),
83
+ TypeFactory.keyword("any"),
84
+ undefined,
85
+ decode_array_inline(config)(importer)(
86
+ ts.factory.createIdentifier("input"),
87
+ array,
88
+ {
89
+ tracable: config.trace,
90
+ source: "function",
91
+ from: "array",
92
+ postfix: "",
93
+ },
94
+ ),
95
+ ),
96
+ ),
97
+ );
98
+
99
+ const write_tuple_functions =
100
+ (project: IProject) =>
101
+ (config: FeatureProgrammer.IConfig) =>
102
+ (importer: FunctionImporter) =>
103
+ (collection: MetadataCollection): ts.VariableStatement[] =>
104
+ collection
105
+ .tuples()
106
+ .filter((t) => t.recursive)
107
+ .map((tuple, i) =>
108
+ StatementFactory.constant(
109
+ `${config.prefix}t${i}`,
110
+ ts.factory.createArrowFunction(
111
+ undefined,
112
+ undefined,
113
+ FeatureProgrammer.parameterDeclarations(config)(
114
+ TypeFactory.keyword("any"),
115
+ )(ts.factory.createIdentifier("input")),
116
+ TypeFactory.keyword("any"),
117
+ undefined,
118
+ decode_tuple_inline(project)(config)(importer)(
119
+ ts.factory.createIdentifier("input"),
120
+ tuple,
121
+ {
122
+ tracable: config.trace,
123
+ source: "function",
124
+ from: "array",
125
+ postfix: "",
126
+ },
127
+ ),
128
+ ),
129
+ ),
130
+ );
131
+
132
+ /* -----------------------------------------------------------
133
+ DECODERS
134
+ ----------------------------------------------------------- */
135
+ const decode =
136
+ (project: IProject) =>
137
+ (config: FeatureProgrammer.IConfig) =>
138
+ (importer: FunctionImporter) =>
139
+ (
140
+ input: ts.Expression,
141
+ meta: Metadata,
142
+ explore: FeatureProgrammer.IExplore,
143
+ ): ts.Expression => {
144
+ // ANY TYPE
145
+ if (meta.any === true)
146
+ return wrap_required(
147
+ input,
148
+ meta,
149
+ explore,
150
+ )(
151
+ wrap_functional(
152
+ input,
153
+ meta,
154
+ explore,
155
+ )(
156
+ ts.factory.createCallExpression(
157
+ ts.factory.createIdentifier("JSON.stringify"),
158
+ undefined,
159
+ [input],
160
+ ),
161
+ ),
162
+ );
163
+
164
+ // ONLY NULL OR UNDEFINED
165
+ const size: number = meta.size();
166
+ if (
167
+ size === 0 &&
168
+ (meta.required === false || meta.nullable === true)
169
+ ) {
170
+ if (meta.required === false && meta.nullable === true)
171
+ return explore.from === "array"
172
+ ? ts.factory.createStringLiteral("null")
173
+ : ts.factory.createConditionalExpression(
174
+ ts.factory.createStrictEquality(
175
+ ts.factory.createNull(),
176
+ input,
177
+ ),
178
+ undefined,
179
+ ts.factory.createStringLiteral("null"),
180
+ undefined,
181
+ ts.factory.createIdentifier("undefined"),
182
+ );
183
+ else if (meta.required === false)
184
+ return explore.from === "array"
185
+ ? ts.factory.createStringLiteral("null")
186
+ : ts.factory.createIdentifier("undefined");
187
+ else return ts.factory.createStringLiteral("null");
188
+ }
189
+
190
+ //----
191
+ // LIST UP UNION TYPES
192
+ //----
193
+ const unions: IUnion[] = [];
194
+
195
+ // toJSON() METHOD
196
+ if (meta.resolved !== null)
197
+ if (size === 1)
198
+ return decode_to_json(project)(config)(importer)(
199
+ input,
200
+ meta.resolved,
201
+ explore,
202
+ );
203
+ else
204
+ unions.push({
205
+ type: "resolved",
206
+ is: () => IsProgrammer.decode_to_json(false)(input),
207
+ value: () =>
208
+ decode_to_json(project)(config)(importer)(
209
+ input,
210
+ meta.resolved!,
211
+ explore,
212
+ ),
213
+ });
214
+ else if (meta.functional === true)
215
+ unions.push({
216
+ type: "functional",
217
+ is: () => IsProgrammer.decode_functional(input),
218
+ value: () => decode_functional(explore),
219
+ });
220
+
221
+ // TEMPLATES
222
+ if (
223
+ meta.templates.length ||
224
+ ArrayUtil.has(meta.constants, (c) => c.type === "string")
225
+ )
226
+ if (AtomicPredicator.template(meta)) {
227
+ const partial = Metadata.initialize();
228
+ partial.atomics.push("string"),
229
+ unions.push({
230
+ type: "template literal",
231
+ is: () =>
232
+ IsProgrammer.decode(project)(importer)(
233
+ input,
234
+ partial,
235
+ explore,
236
+ [],
237
+ [],
238
+ ),
239
+ value: () =>
240
+ decode_atomic(project)(importer)(
241
+ input,
242
+ "string",
243
+ explore,
244
+ ),
245
+ });
246
+ }
247
+
248
+ // CONSTANTS
249
+ for (const constant of meta.constants)
250
+ if (AtomicPredicator.constant(meta)(constant.type) === false)
251
+ continue;
252
+ else if (constant.type !== "string")
253
+ unions.push({
254
+ type: "atomic",
255
+ is: () =>
256
+ IsProgrammer.decode(project)(importer)(
257
+ input,
258
+ (() => {
259
+ const partial = Metadata.initialize();
260
+ partial.atomics.push(constant.type);
261
+ return partial;
262
+ })(),
263
+ explore,
264
+ [],
265
+ [],
266
+ ),
267
+ value: () =>
268
+ decode_atomic(project)(importer)(
269
+ input,
270
+ constant.type,
271
+ explore,
272
+ ),
273
+ });
274
+ else if (meta.templates.length === 0)
275
+ unions.push({
276
+ type: "const string",
277
+ is: () =>
278
+ IsProgrammer.decode(project)(importer)(
279
+ input,
280
+ (() => {
281
+ const partial = Metadata.initialize();
282
+ partial.atomics.push("string");
283
+ return partial;
284
+ })(),
285
+ explore,
286
+ [],
287
+ [],
288
+ ),
289
+ value: () =>
290
+ decode_constant_string(project)(importer)(
291
+ input,
292
+ [...constant.values] as string[],
293
+ explore,
294
+ ),
295
+ });
296
+
297
+ /// ATOMICS
298
+ for (const type of meta.atomics)
299
+ if (AtomicPredicator.atomic(meta)(type))
300
+ unions.push({
301
+ type: "atomic",
302
+ is: () =>
303
+ IsProgrammer.decode(project)(importer)(
304
+ input,
305
+ (() => {
306
+ const partial = Metadata.initialize();
307
+ partial.atomics.push(type);
308
+ return partial;
309
+ })(),
310
+ explore,
311
+ [],
312
+ [],
313
+ ),
314
+ value: () =>
315
+ decode_atomic(project)(importer)(
316
+ input,
317
+ type,
318
+ explore,
319
+ ),
320
+ });
321
+
322
+ // TUPLES
323
+ for (const tuple of meta.tuples) {
324
+ for (const child of tuple.elements)
325
+ if (StringifyPredicator.undefindable(meta))
326
+ throw new Error(
327
+ `Error on typia.stringify(): tuple cannot contain undefined value - (${child.getName()}).`,
328
+ );
329
+ unions.push({
330
+ type: "tuple",
331
+ is: () =>
332
+ IsProgrammer.decode(project)(importer)(
333
+ input,
334
+ (() => {
335
+ const partial = Metadata.initialize();
336
+ partial.tuples.push(tuple);
337
+ return partial;
338
+ })(),
339
+ explore,
340
+ [],
341
+ [],
342
+ ),
343
+ value: () =>
344
+ decode_tuple(project)(config)(importer)(
345
+ input,
346
+ tuple,
347
+ explore,
348
+ ),
349
+ });
350
+ }
351
+
352
+ // ARRAYS
353
+ if (meta.arrays.length) {
354
+ for (const child of meta.arrays)
355
+ if (StringifyPredicator.undefindable(child.value))
356
+ throw new Error(
357
+ `Error on typia.stringify(): array cannot contain undefined value (${child.value.getName()}).`,
358
+ );
359
+ const value: () => ts.Expression =
360
+ meta.arrays.length === 1
361
+ ? () =>
362
+ decode_array(config)(importer)(
363
+ input,
364
+ meta.arrays[0]!,
365
+ {
366
+ ...explore,
367
+ from: "array",
368
+ },
369
+ )
370
+ : meta.arrays.some((elem) => elem.value.any)
371
+ ? () =>
372
+ ts.factory.createCallExpression(
373
+ ts.factory.createIdentifier("JSON.stringify"),
374
+ undefined,
375
+ [input],
376
+ )
377
+ : () =>
378
+ explore_arrays(project)(config)(importer)(
379
+ input,
380
+ meta.arrays,
381
+ {
382
+ ...explore,
383
+ from: "array",
384
+ },
385
+ );
386
+
387
+ unions.push({
388
+ type: "array",
389
+ is: () => ExpressionFactory.isArray(input),
390
+ value,
391
+ });
392
+ }
393
+
394
+ // BUILT-IN CLASSES
395
+ if (meta.natives.length)
396
+ for (const native of meta.natives)
397
+ unions.push({
398
+ type: "object",
399
+ is: () => check_native(native)(input),
400
+ value: () =>
401
+ AtomicPredicator.native(native)
402
+ ? decode_atomic(project)(importer)(
403
+ input,
404
+ native.toLowerCase() as Atomic.Literal,
405
+ explore,
406
+ )
407
+ : ts.factory.createStringLiteral("{}"),
408
+ });
409
+
410
+ // SETS
411
+ if (meta.sets.length)
412
+ unions.push({
413
+ type: "object",
414
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
415
+ value: () => ts.factory.createStringLiteral("{}"),
416
+ });
417
+
418
+ // MAPS
419
+ if (meta.maps.length)
420
+ unions.push({
421
+ type: "object",
422
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
423
+ value: () => ts.factory.createStringLiteral("{}"),
424
+ });
425
+
426
+ // OBJECTS
427
+ if (meta.objects.length)
428
+ unions.push({
429
+ type: "object",
430
+ is: () =>
431
+ ExpressionFactory.isObject({
432
+ checkNull: true,
433
+ checkArray: meta.objects.some((obj) =>
434
+ obj.properties.every(
435
+ (prop) =>
436
+ !prop.key.isSoleLiteral() ||
437
+ !prop.value.required,
438
+ ),
439
+ ),
440
+ })(input),
441
+ value: () =>
442
+ meta.isParentResolved() === false &&
443
+ meta.objects.length === 1 &&
444
+ meta.objects[0]!._Is_simple()
445
+ ? (() => {
446
+ const obj: MetadataObject = meta.objects[0]!;
447
+ const entries: IExpressionEntry<ts.Expression>[] =
448
+ feature_object_entries({
449
+ decoder: () =>
450
+ decode(project)(config)(importer),
451
+ trace: false,
452
+ path: false,
453
+ })(importer)(obj)(
454
+ ts.factory.createAsExpression(
455
+ input,
456
+ TypeFactory.keyword("any"),
457
+ ),
458
+ );
459
+ return StringifyJoiner.object(importer)(
460
+ ts.factory.createAsExpression(
461
+ input,
462
+ TypeFactory.keyword("any"),
463
+ ),
464
+ entries,
465
+ );
466
+ })()
467
+ : explore_objects(config)(importer)(input, meta, {
468
+ ...explore,
469
+ from: "object",
470
+ }),
471
+ });
472
+
473
+ //----
474
+ // RETURNS
475
+ //----
476
+ // CHECK NULL AND UNDEFINED
477
+ const wrapper = (output: ts.Expression) =>
478
+ wrap_required(
479
+ input,
480
+ meta,
481
+ explore,
482
+ )(wrap_nullable(input, meta)(output));
483
+
484
+ // DIRECT RETURN
485
+ if (unions.length === 0)
486
+ return ts.factory.createCallExpression(
487
+ ts.factory.createIdentifier("JSON.stringify"),
488
+ undefined,
489
+ [input],
490
+ );
491
+ else if (unions.length === 1) return wrapper(unions[0]!.value());
492
+
493
+ // RETURN WITH TYPE CHECKING
494
+ return wrapper(
495
+ ts.factory.createCallExpression(
496
+ ts.factory.createArrowFunction(
497
+ undefined,
498
+ undefined,
499
+ [],
500
+ undefined,
501
+ undefined,
502
+ iterate(importer, input, unions, meta.getName()),
503
+ ),
504
+ undefined,
505
+ undefined,
506
+ ),
507
+ );
508
+ };
509
+
510
+ const decode_object = (importer: FunctionImporter) =>
511
+ FeatureProgrammer.decode_object({
512
+ trace: false,
513
+ path: false,
514
+ prefix: PREFIX,
515
+ })(importer);
516
+
517
+ const decode_array =
518
+ (config: FeatureProgrammer.IConfig) =>
519
+ (importer: FunctionImporter) =>
520
+ (
521
+ input: ts.Expression,
522
+ array: MetadataArray,
523
+ explore: FeatureProgrammer.IExplore,
524
+ ) =>
525
+ array.recursive
526
+ ? ts.factory.createCallExpression(
527
+ ts.factory.createIdentifier(
528
+ importer.useLocal(`${config.prefix}a${array.index}`),
529
+ ),
530
+ undefined,
531
+ FeatureProgrammer.argumentsArray(config)({
532
+ ...explore,
533
+ source: "function",
534
+ from: "array",
535
+ })(input),
536
+ )
537
+ : decode_array_inline(config)(importer)(input, array, explore);
538
+
539
+ const decode_array_inline =
540
+ (config: FeatureProgrammer.IConfig) =>
541
+ (importer: FunctionImporter) =>
542
+ (
543
+ input: ts.Expression,
544
+ array: MetadataArray,
545
+ explore: FeatureProgrammer.IExplore,
546
+ ) =>
547
+ FeatureProgrammer.decode_array(config)(importer)(
548
+ StringifyJoiner.array,
549
+ )(input, array, explore, [], []);
550
+
551
+ const decode_tuple =
552
+ (project: IProject) =>
553
+ (config: FeatureProgrammer.IConfig) =>
554
+ (importer: FunctionImporter) =>
555
+ (
556
+ input: ts.Expression,
557
+ tuple: MetadataTuple,
558
+ explore: FeatureProgrammer.IExplore,
559
+ ): ts.Expression =>
560
+ tuple.recursive
561
+ ? ts.factory.createCallExpression(
562
+ ts.factory.createIdentifier(
563
+ importer.useLocal(`${config.prefix}t${tuple.index}`),
564
+ ),
565
+ undefined,
566
+ FeatureProgrammer.argumentsArray(config)({
567
+ ...explore,
568
+ source: "function",
569
+ })(input),
570
+ )
571
+ : decode_tuple_inline(project)(config)(importer)(
572
+ input,
573
+ tuple,
574
+ explore,
575
+ );
576
+
577
+ const decode_tuple_inline =
578
+ (project: IProject) =>
579
+ (config: FeatureProgrammer.IConfig) =>
580
+ (importer: FunctionImporter) =>
581
+ (
582
+ input: ts.Expression,
583
+ tuple: MetadataTuple,
584
+ explore: FeatureProgrammer.IExplore,
585
+ ): ts.Expression => {
586
+ const children: ts.Expression[] = tuple.elements
587
+ .filter((elem) => elem.rest === null)
588
+ .map((elem, index) =>
589
+ decode(project)(config)(importer)(
590
+ ts.factory.createElementAccessExpression(input, index),
591
+ elem,
592
+ {
593
+ ...explore,
594
+ from: "array",
595
+ postfix: explore.postfix.length
596
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
597
+ : `"[${index}]"`,
598
+ },
599
+ ),
600
+ );
601
+ const rest = (() => {
602
+ if (tuple.elements.length === 0) return null;
603
+ const last = tuple.elements.at(-1)!;
604
+ if (last.rest === null) return null;
605
+
606
+ const code = decode(project)(config)(importer)(
607
+ ts.factory.createCallExpression(
608
+ IdentifierFactory.access(input)("slice"),
609
+ undefined,
610
+ [
611
+ ts.factory.createNumericLiteral(
612
+ tuple.elements.length - 1,
613
+ ),
614
+ ],
615
+ ),
616
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
617
+ {
618
+ ...explore,
619
+ start: tuple.elements.length - 1,
620
+ },
621
+ );
622
+ return ts.factory.createCallExpression(
623
+ importer.use("rest"),
624
+ undefined,
625
+ [code],
626
+ );
627
+ })();
628
+ return StringifyJoiner.tuple(children, rest);
629
+ };
630
+
631
+ const decode_atomic =
632
+ (project: IProject) =>
633
+ (importer: FunctionImporter) =>
634
+ (
635
+ input: ts.Expression,
636
+ type: string,
637
+ explore: FeatureProgrammer.IExplore,
638
+ ) => {
639
+ if (type === "string")
640
+ return ts.factory.createCallExpression(
641
+ importer.use("string"),
642
+ undefined,
643
+ [input],
644
+ );
645
+ else if (
646
+ type === "number" &&
647
+ OptionPredicator.numeric(project.options)
648
+ )
649
+ input = ts.factory.createCallExpression(
650
+ importer.use("number"),
651
+ undefined,
652
+ [input],
653
+ );
654
+
655
+ return explore.from !== "top"
656
+ ? input
657
+ : ts.factory.createCallExpression(
658
+ IdentifierFactory.access(input)("toString"),
659
+ undefined,
660
+ undefined,
661
+ );
662
+ };
663
+
664
+ const decode_constant_string =
665
+ (project: IProject) =>
666
+ (importer: FunctionImporter) =>
667
+ (
668
+ input: ts.Expression,
669
+ values: string[],
670
+ explore: FeatureProgrammer.IExplore,
671
+ ): ts.Expression => {
672
+ if (values.every((v) => !StringifyPredicator.require_escape(v)))
673
+ return [
674
+ ts.factory.createStringLiteral('"'),
675
+ input,
676
+ ts.factory.createStringLiteral('"'),
677
+ ].reduce((x, y) => ts.factory.createAdd(x, y));
678
+ else
679
+ return decode_atomic(project)(importer)(
680
+ input,
681
+ "string",
682
+ explore,
683
+ );
684
+ };
685
+
686
+ const decode_to_json =
687
+ (project: IProject) =>
688
+ (config: FeatureProgrammer.IConfig) =>
689
+ (importer: FunctionImporter) =>
690
+ (
691
+ input: ts.Expression,
692
+ resolved: Metadata,
693
+ explore: FeatureProgrammer.IExplore,
694
+ ): ts.Expression => {
695
+ return decode(project)(config)(importer)(
696
+ ts.factory.createCallExpression(
697
+ IdentifierFactory.access(input)("toJSON"),
698
+ undefined,
699
+ [],
700
+ ),
701
+ resolved,
702
+ explore,
703
+ );
704
+ };
705
+
706
+ const decode_functional = (explore: FeatureProgrammer.IExplore) =>
707
+ explore.from === "array"
708
+ ? ts.factory.createStringLiteral("null")
709
+ : ts.factory.createIdentifier("undefined");
710
+
711
+ /* -----------------------------------------------------------
712
+ EXPLORERS
713
+ ----------------------------------------------------------- */
714
+ const explore_objects =
715
+ (config: FeatureProgrammer.IConfig) =>
716
+ (importer: FunctionImporter) =>
717
+ (
718
+ input: ts.Expression,
719
+ meta: Metadata,
720
+ explore: FeatureProgrammer.IExplore,
721
+ ) =>
722
+ meta.objects.length === 1
723
+ ? decode_object(importer)(input, meta.objects[0]!, explore)
724
+ : ts.factory.createCallExpression(
725
+ ts.factory.createIdentifier(
726
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
727
+ ),
728
+ undefined,
729
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
730
+ );
731
+
732
+ const explore_arrays =
733
+ (project: IProject) =>
734
+ (config: FeatureProgrammer.IConfig) =>
735
+ (importer: FunctionImporter) =>
736
+ (
737
+ input: ts.Expression,
738
+ elements: MetadataArray[],
739
+ explore: FeatureProgrammer.IExplore,
740
+ ): ts.Expression =>
741
+ explore_array_like_union_types(config)(importer)(
742
+ UnionExplorer.array({
743
+ checker: IsProgrammer.decode(project)(importer),
744
+ decoder: decode_array(config)(importer),
745
+ empty: ts.factory.createStringLiteral("[]"),
746
+ success: ts.factory.createTrue(),
747
+ failure: (input, expected) =>
748
+ create_throw_error(importer)(expected)(input),
749
+ }),
750
+ )(input, elements, explore);
751
+
752
+ const explore_array_like_union_types =
753
+ (config: FeatureProgrammer.IConfig) =>
754
+ (importer: FunctionImporter) =>
755
+ <T extends MetadataArray | MetadataTuple>(
756
+ factory: (
757
+ parameters: ts.ParameterDeclaration[],
758
+ ) => (
759
+ input: ts.Expression,
760
+ elements: T[],
761
+ explore: FeatureProgrammer.IExplore,
762
+ tags: IMetadataTag[],
763
+ jsDocTags: IJsDocTagInfo[],
764
+ ) => ts.ArrowFunction,
765
+ ) =>
766
+ (
767
+ input: ts.Expression,
768
+ elements: T[],
769
+ explore: FeatureProgrammer.IExplore,
770
+ ): ts.Expression => {
771
+ const arrow =
772
+ (parameters: ts.ParameterDeclaration[]) =>
773
+ (explore: FeatureProgrammer.IExplore) =>
774
+ (input: ts.Expression): ts.ArrowFunction =>
775
+ factory(parameters)(input, elements, explore, [], []);
776
+ if (elements.every((e) => e.recursive === false))
777
+ ts.factory.createCallExpression(
778
+ arrow([])(explore)(input),
779
+ undefined,
780
+ [],
781
+ );
782
+
783
+ explore = {
784
+ ...explore,
785
+ source: "function",
786
+ from: "array",
787
+ };
788
+ return ts.factory.createCallExpression(
789
+ ts.factory.createIdentifier(
790
+ importer.emplaceUnion(
791
+ config.prefix,
792
+ elements.map((e) => e.name).join(" | "),
793
+ () =>
794
+ arrow(
795
+ FeatureProgrammer.parameterDeclarations(config)(
796
+ TypeFactory.keyword("any"),
797
+ )(ts.factory.createIdentifier("input")),
798
+ )({
799
+ ...explore,
800
+ postfix: "",
801
+ })(ts.factory.createIdentifier("input")),
802
+ ),
803
+ ),
804
+ undefined,
805
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
806
+ );
807
+ };
808
+
809
+ /* -----------------------------------------------------------
810
+ RETURN SCRIPTS
811
+ ----------------------------------------------------------- */
812
+ const wrap_required = (
813
+ input: ts.Expression,
814
+ meta: Metadata,
815
+ explore: FeatureProgrammer.IExplore,
816
+ ): ((expression: ts.Expression) => ts.Expression) => {
817
+ if (meta.required === true && meta.any === false)
818
+ return (expression) => expression;
819
+ return (expression) =>
820
+ ts.factory.createConditionalExpression(
821
+ ts.factory.createStrictInequality(
822
+ ts.factory.createIdentifier("undefined"),
823
+ input,
824
+ ),
825
+ undefined,
826
+ expression,
827
+ undefined,
828
+ explore.from === "array"
829
+ ? ts.factory.createStringLiteral("null")
830
+ : ts.factory.createIdentifier("undefined"),
831
+ );
832
+ };
833
+
834
+ const wrap_nullable = (
835
+ input: ts.Expression,
836
+ meta: Metadata,
837
+ ): ((expression: ts.Expression) => ts.Expression) => {
838
+ if (meta.nullable === false) return (expression) => expression;
839
+ return (expression) =>
840
+ ts.factory.createConditionalExpression(
841
+ ts.factory.createStrictInequality(
842
+ ts.factory.createNull(),
843
+ input,
844
+ ),
845
+ undefined,
846
+ expression,
847
+ undefined,
848
+ ts.factory.createStringLiteral("null"),
849
+ );
850
+ };
851
+
852
+ const wrap_functional = (
853
+ input: ts.Expression,
854
+ meta: Metadata,
855
+ explore: FeatureProgrammer.IExplore,
856
+ ): ((expression: ts.Expression) => ts.Expression) => {
857
+ if (meta.functional === false) return (expression) => expression;
858
+ return (expression) =>
859
+ ts.factory.createConditionalExpression(
860
+ ts.factory.createStrictInequality(
861
+ ts.factory.createStringLiteral("function"),
862
+ ValueFactory.TYPEOF(input),
863
+ ),
864
+ undefined,
865
+ expression,
866
+ undefined,
867
+ decode_functional(explore),
868
+ );
869
+ };
870
+
871
+ const iterate = (
872
+ importer: FunctionImporter,
873
+ input: ts.Expression,
874
+ unions: IUnion[],
875
+ expected: string,
876
+ ) =>
877
+ ts.factory.createBlock(
878
+ [
879
+ ...unions.map((u) =>
880
+ ts.factory.createIfStatement(
881
+ u.is(),
882
+ ts.factory.createReturnStatement(u.value()),
883
+ ),
884
+ ),
885
+ create_throw_error(importer)(expected)(input),
886
+ ],
887
+ true,
888
+ );
889
+
890
+ /* -----------------------------------------------------------
891
+ CONFIGURATIONS
892
+ ----------------------------------------------------------- */
893
+ const PREFIX = "$s";
894
+
895
+ const configure =
896
+ (project: IProject) =>
897
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
898
+ const config: FeatureProgrammer.IConfig = {
899
+ types: {
900
+ input: (type, name) =>
901
+ ts.factory.createTypeReferenceNode(
902
+ name ??
903
+ TypeFactory.getFullName(project.checker)(type),
904
+ ),
905
+ output: () => TypeFactory.keyword("string"),
906
+ },
907
+ prefix: PREFIX,
908
+ trace: false,
909
+ path: false,
910
+ initializer,
911
+ decoder: () => decode(project)(config)(importer),
912
+ objector: {
913
+ checker: () => (input, meta, explore) =>
914
+ IsProgrammer.decode(project)(importer)(
915
+ input,
916
+ meta,
917
+ explore,
918
+ [],
919
+ [],
920
+ ),
921
+ decoder: () => decode_object(importer),
922
+ joiner: StringifyJoiner.object(importer),
923
+ unionizer: decode_union_object(
924
+ IsProgrammer.decode_object(importer),
925
+ )(decode_object(importer))((exp) => exp)(
926
+ (value, expected) =>
927
+ create_throw_error(importer)(expected)(value),
928
+ ),
929
+ failure: (input, expected) =>
930
+ create_throw_error(importer)(expected)(input),
931
+ },
932
+ generator: {
933
+ arrays: () => write_array_functions(config)(importer),
934
+ tuples: () =>
935
+ write_tuple_functions(project)(config)(importer),
936
+ },
937
+ };
938
+ return config;
939
+ };
940
+
941
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
942
+ ({ checker }) =>
943
+ (type) => {
944
+ const collection: MetadataCollection = new MetadataCollection();
945
+ const meta: Metadata = MetadataFactory.analyze(checker)({
946
+ resolve: true,
947
+ constant: true,
948
+ absorb: true,
949
+ validate: (meta) => {
950
+ if (meta.atomics.find((str) => str === "bigint"))
951
+ throw new Error(NO_BIGINT);
952
+ },
953
+ })(collection)(type);
954
+ return [collection, meta];
955
+ };
956
+
957
+ const create_throw_error =
958
+ (importer: FunctionImporter) =>
959
+ (expected: string) =>
960
+ (value: ts.Expression) =>
961
+ ts.factory.createExpressionStatement(
962
+ ts.factory.createCallExpression(
963
+ importer.use("throws"),
964
+ [],
965
+ [
966
+ ts.factory.createObjectLiteralExpression(
967
+ [
968
+ ts.factory.createPropertyAssignment(
969
+ "expected",
970
+ ts.factory.createStringLiteral(expected),
971
+ ),
972
+ ts.factory.createPropertyAssignment(
973
+ "value",
974
+ value,
975
+ ),
976
+ ],
977
+ true,
978
+ ),
979
+ ],
980
+ ),
981
+ );
982
+ }
983
+
984
+ interface IUnion {
985
+ type: string;
986
+ is: () => ts.Expression;
987
+ value: () => ts.Expression;
988
+ }
989
+
990
+ const NO_BIGINT = "Error on typia.stringify(): does not allow bigint type.";