typia 4.0.9 → 4.0.10-dev.20230616

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