typia 5.2.1 → 5.2.2-dev.20231012-2

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 (91) hide show
  1. package/README.md +1 -1
  2. package/lib/Primitive.d.ts +1 -1
  3. package/lib/executable/TypiaSetupWizard.js +8 -8
  4. package/lib/executable/TypiaSetupWizard.js.map +1 -1
  5. package/lib/factories/JsonMetadataFactory.js +31 -0
  6. package/lib/factories/JsonMetadataFactory.js.map +1 -1
  7. package/lib/factories/MetadataCollection.js +4 -3
  8. package/lib/factories/MetadataCollection.js.map +1 -1
  9. package/lib/programmers/helpers/AtomicPredicator.js +1 -1
  10. package/lib/programmers/helpers/AtomicPredicator.js.map +1 -1
  11. package/package.json +2 -2
  12. package/src/CamelCase.ts +116 -116
  13. package/src/PascalCase.ts +116 -116
  14. package/src/Primitive.ts +135 -135
  15. package/src/Resolved.ts +116 -116
  16. package/src/SnakeCase.ts +156 -156
  17. package/src/executable/TypiaSetupWizard.ts +9 -11
  18. package/src/factories/JsonMetadataFactory.ts +7 -0
  19. package/src/factories/MetadataCollection.ts +6 -1
  20. package/src/factories/internal/metadata/emplace_metadata_object.ts +150 -150
  21. package/src/functional/$clone.ts +4 -4
  22. package/src/functional/$convention.ts +40 -40
  23. package/src/functional/Namespace.ts +164 -164
  24. package/src/http.ts +1149 -1149
  25. package/src/json.ts +648 -648
  26. package/src/misc.ts +651 -651
  27. package/src/module.ts +662 -662
  28. package/src/notations.ts +855 -855
  29. package/src/programmers/IsProgrammer.ts +252 -252
  30. package/src/programmers/TypiaProgrammer.ts +169 -169
  31. package/src/programmers/helpers/AtomicPredicator.ts +1 -1
  32. package/src/programmers/helpers/NotationJoiner.ts +146 -146
  33. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  34. package/src/programmers/json/JsonStringifyProgrammer.ts +964 -964
  35. package/src/programmers/misc/MiscCloneProgrammer.ts +790 -790
  36. package/src/programmers/misc/MiscPruneProgrammer.ts +552 -552
  37. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +72 -72
  38. package/src/programmers/notations/NotationGeneralProgrammer.ts +720 -720
  39. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +79 -79
  40. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +88 -88
  41. package/src/protobuf.ts +887 -887
  42. package/src/transformers/CallExpressionTransformer.ts +380 -380
  43. package/src/transformers/ITransformOptions.ts +62 -62
  44. package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
  45. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  46. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +12 -12
  47. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  48. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  49. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +12 -12
  50. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  51. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +12 -12
  52. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  53. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  54. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
  55. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  56. package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
  57. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  58. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
  59. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
  60. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  61. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +12 -12
  62. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +12 -12
  63. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  64. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  65. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  66. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  67. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +12 -12
  68. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +12 -12
  69. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  70. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  71. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  72. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
  73. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
  74. package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +15 -15
  75. package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +15 -15
  76. package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +15 -15
  77. package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +15 -15
  78. package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +17 -17
  79. package/src/transformers/features/notations/NotationGeneralTransformer.ts +11 -11
  80. package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +15 -15
  81. package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +17 -17
  82. package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +12 -12
  83. package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +12 -12
  84. package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +9 -9
  85. package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +9 -9
  86. package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +12 -12
  87. package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +12 -12
  88. package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +12 -12
  89. package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +12 -12
  90. package/src/utils/NamingConvention.ts +91 -91
  91. package/src/utils/StringUtil.ts +4 -4
@@ -1,169 +1,169 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import ts from "typescript";
4
-
5
- import { ImportTransformer } from "../transformers/ImportTransformer";
6
-
7
- import transform from "../transform";
8
-
9
- export namespace TypiaProgrammer {
10
- export interface IProps {
11
- input: string;
12
- output: string;
13
- project: string;
14
- }
15
-
16
- export const build = async (
17
- props: TypiaProgrammer.IProps,
18
- ): Promise<void> => {
19
- props.input = path.resolve(props.input);
20
- props.output = path.resolve(props.output);
21
-
22
- if ((await is_directory(props.input)) === false)
23
- throw new URIError(
24
- "Error on TypiaGenerator.generate(): input path is not a directory.",
25
- );
26
- else if (fs.existsSync(props.output) === false)
27
- await fs.promises.mkdir(props.output, { recursive: true });
28
- else if ((await is_directory(props.output)) === false) {
29
- const parent: string = path.join(props.output, "..");
30
- if ((await is_directory(parent)) === false)
31
- throw new URIError(
32
- "Error on TypiaGenerator.generate(): output path is not a directory.",
33
- );
34
- await fs.promises.mkdir(props.output);
35
- }
36
-
37
- // CREATE PROGRAM
38
- const { options: compilerOptions } = ts.parseJsonConfigFileContent(
39
- ts.readConfigFile(props.project, ts.sys.readFile).config,
40
- {
41
- fileExists: ts.sys.fileExists,
42
- readFile: ts.sys.readFile,
43
- readDirectory: ts.sys.readDirectory,
44
- useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
45
- },
46
- path.dirname(props.project),
47
- );
48
-
49
- const program: ts.Program = ts.createProgram(
50
- await (async () => {
51
- const container: string[] = [];
52
- await gather(props)(container)(props.input)(props.output);
53
- return container;
54
- })(),
55
- compilerOptions,
56
- );
57
-
58
- // DO TRANSFORM
59
- const diagnostics: ts.Diagnostic[] = [];
60
- const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
61
- program
62
- .getSourceFiles()
63
- .filter(
64
- (file) =>
65
- !file.isDeclarationFile &&
66
- path.resolve(file.fileName).indexOf(props.input) !== -1,
67
- ),
68
- [
69
- ImportTransformer.transform(props.input)(props.output),
70
- transform(
71
- program,
72
- ((compilerOptions.plugins as any[]) ?? []).find(
73
- (p: any) =>
74
- p.transform === "typia/lib/transform" ||
75
- p.transform === "../src/transform.ts",
76
- ) ?? {},
77
- {
78
- addDiagnostic: (diag) => diagnostics.push(diag),
79
- },
80
- ),
81
- ],
82
- program.getCompilerOptions(),
83
- );
84
-
85
- // TRACE ERRORS
86
- for (const diag of diagnostics) {
87
- const file: string = diag.file
88
- ? path.relative(diag.file.fileName, process.cwd())
89
- : "(unknown file)";
90
- const category: string =
91
- diag.category === ts.DiagnosticCategory.Warning
92
- ? "warning"
93
- : diag.category === ts.DiagnosticCategory.Error
94
- ? "error"
95
- : diag.category === ts.DiagnosticCategory.Suggestion
96
- ? "suggestion"
97
- : diag.category === ts.DiagnosticCategory.Message
98
- ? "message"
99
- : "unkown";
100
- const [line, pos] = diag.file
101
- ? (() => {
102
- const lines: string[] = diag
103
- .file!.text.substring(0, diag.start)
104
- .split("\n");
105
- if (lines.length === 0) return [0, 0];
106
- return [lines.length, lines.at(-1)!.length + 1];
107
- })()
108
- : [0, 0];
109
- console.error(
110
- `${file}:${line}:${pos} - ${category} TS${diag.code}: ${diag.messageText}`,
111
- );
112
- }
113
- if (diagnostics.length) process.exit(-1);
114
-
115
- // ARCHIVE TRANSFORMED FILES
116
- const printer: ts.Printer = ts.createPrinter({
117
- newLine: ts.NewLineKind.LineFeed,
118
- });
119
- for (const file of result.transformed) {
120
- const to: string = path
121
- .resolve(file.fileName)
122
- .replace(props.input, props.output);
123
-
124
- const content: string = printer.printFile(file);
125
- await fs.promises.writeFile(to, emend(content), "utf8");
126
- }
127
- };
128
-
129
- const emend = (content: string): string => {
130
- if (
131
- content.indexOf("typia.") === -1 ||
132
- content.indexOf("import typia") !== -1 ||
133
- content.indexOf("import * as typia") !== -1
134
- )
135
- return content;
136
- return `import typia from "typia";\n\n${content}`;
137
- };
138
-
139
- const is_directory = async (current: string): Promise<boolean> => {
140
- const stat: fs.Stats = await fs.promises.stat(current);
141
- return stat.isDirectory();
142
- };
143
-
144
- const gather =
145
- (props: IProps) =>
146
- (container: string[]) =>
147
- (from: string) =>
148
- async (to: string) => {
149
- if (from === props.output) return;
150
- else if (fs.existsSync(to) === false) await fs.promises.mkdir(to);
151
-
152
- for (const file of await fs.promises.readdir(from)) {
153
- const next: string = path.join(from, file);
154
- const stat: fs.Stats = await fs.promises.stat(next);
155
-
156
- if (stat.isDirectory()) {
157
- await gather(props)(container)(next)(path.join(to, file));
158
- continue;
159
- } else if (is_supported_extension(file)) container.push(next);
160
- }
161
- };
162
-
163
- const is_supported_extension = (filename: string): boolean => {
164
- return (
165
- (filename.endsWith(".ts") && !filename.endsWith(".d.ts")) ||
166
- (filename.endsWith(".tsx") && !filename.endsWith(".d.tsx"))
167
- );
168
- };
169
- }
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import ts from "typescript";
4
+
5
+ import { ImportTransformer } from "../transformers/ImportTransformer";
6
+
7
+ import transform from "../transform";
8
+
9
+ export namespace TypiaProgrammer {
10
+ export interface IProps {
11
+ input: string;
12
+ output: string;
13
+ project: string;
14
+ }
15
+
16
+ export const build = async (
17
+ props: TypiaProgrammer.IProps,
18
+ ): Promise<void> => {
19
+ props.input = path.resolve(props.input);
20
+ props.output = path.resolve(props.output);
21
+
22
+ if ((await is_directory(props.input)) === false)
23
+ throw new URIError(
24
+ "Error on TypiaGenerator.generate(): input path is not a directory.",
25
+ );
26
+ else if (fs.existsSync(props.output) === false)
27
+ await fs.promises.mkdir(props.output, { recursive: true });
28
+ else if ((await is_directory(props.output)) === false) {
29
+ const parent: string = path.join(props.output, "..");
30
+ if ((await is_directory(parent)) === false)
31
+ throw new URIError(
32
+ "Error on TypiaGenerator.generate(): output path is not a directory.",
33
+ );
34
+ await fs.promises.mkdir(props.output);
35
+ }
36
+
37
+ // CREATE PROGRAM
38
+ const { options: compilerOptions } = ts.parseJsonConfigFileContent(
39
+ ts.readConfigFile(props.project, ts.sys.readFile).config,
40
+ {
41
+ fileExists: ts.sys.fileExists,
42
+ readFile: ts.sys.readFile,
43
+ readDirectory: ts.sys.readDirectory,
44
+ useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
45
+ },
46
+ path.dirname(props.project),
47
+ );
48
+
49
+ const program: ts.Program = ts.createProgram(
50
+ await (async () => {
51
+ const container: string[] = [];
52
+ await gather(props)(container)(props.input)(props.output);
53
+ return container;
54
+ })(),
55
+ compilerOptions,
56
+ );
57
+
58
+ // DO TRANSFORM
59
+ const diagnostics: ts.Diagnostic[] = [];
60
+ const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
61
+ program
62
+ .getSourceFiles()
63
+ .filter(
64
+ (file) =>
65
+ !file.isDeclarationFile &&
66
+ path.resolve(file.fileName).indexOf(props.input) !== -1,
67
+ ),
68
+ [
69
+ ImportTransformer.transform(props.input)(props.output),
70
+ transform(
71
+ program,
72
+ ((compilerOptions.plugins as any[]) ?? []).find(
73
+ (p: any) =>
74
+ p.transform === "typia/lib/transform" ||
75
+ p.transform === "../src/transform.ts",
76
+ ) ?? {},
77
+ {
78
+ addDiagnostic: (diag) => diagnostics.push(diag),
79
+ },
80
+ ),
81
+ ],
82
+ program.getCompilerOptions(),
83
+ );
84
+
85
+ // TRACE ERRORS
86
+ for (const diag of diagnostics) {
87
+ const file: string = diag.file
88
+ ? path.relative(diag.file.fileName, process.cwd())
89
+ : "(unknown file)";
90
+ const category: string =
91
+ diag.category === ts.DiagnosticCategory.Warning
92
+ ? "warning"
93
+ : diag.category === ts.DiagnosticCategory.Error
94
+ ? "error"
95
+ : diag.category === ts.DiagnosticCategory.Suggestion
96
+ ? "suggestion"
97
+ : diag.category === ts.DiagnosticCategory.Message
98
+ ? "message"
99
+ : "unkown";
100
+ const [line, pos] = diag.file
101
+ ? (() => {
102
+ const lines: string[] = diag
103
+ .file!.text.substring(0, diag.start)
104
+ .split("\n");
105
+ if (lines.length === 0) return [0, 0];
106
+ return [lines.length, lines.at(-1)!.length + 1];
107
+ })()
108
+ : [0, 0];
109
+ console.error(
110
+ `${file}:${line}:${pos} - ${category} TS${diag.code}: ${diag.messageText}`,
111
+ );
112
+ }
113
+ if (diagnostics.length) process.exit(-1);
114
+
115
+ // ARCHIVE TRANSFORMED FILES
116
+ const printer: ts.Printer = ts.createPrinter({
117
+ newLine: ts.NewLineKind.LineFeed,
118
+ });
119
+ for (const file of result.transformed) {
120
+ const to: string = path
121
+ .resolve(file.fileName)
122
+ .replace(props.input, props.output);
123
+
124
+ const content: string = printer.printFile(file);
125
+ await fs.promises.writeFile(to, emend(content), "utf8");
126
+ }
127
+ };
128
+
129
+ const emend = (content: string): string => {
130
+ if (
131
+ content.indexOf("typia.") === -1 ||
132
+ content.indexOf("import typia") !== -1 ||
133
+ content.indexOf("import * as typia") !== -1
134
+ )
135
+ return content;
136
+ return `import typia from "typia";\n\n${content}`;
137
+ };
138
+
139
+ const is_directory = async (current: string): Promise<boolean> => {
140
+ const stat: fs.Stats = await fs.promises.stat(current);
141
+ return stat.isDirectory();
142
+ };
143
+
144
+ const gather =
145
+ (props: IProps) =>
146
+ (container: string[]) =>
147
+ (from: string) =>
148
+ async (to: string) => {
149
+ if (from === props.output) return;
150
+ else if (fs.existsSync(to) === false) await fs.promises.mkdir(to);
151
+
152
+ for (const file of await fs.promises.readdir(from)) {
153
+ const next: string = path.join(from, file);
154
+ const stat: fs.Stats = await fs.promises.stat(next);
155
+
156
+ if (stat.isDirectory()) {
157
+ await gather(props)(container)(next)(path.join(to, file));
158
+ continue;
159
+ } else if (is_supported_extension(file)) container.push(next);
160
+ }
161
+ };
162
+
163
+ const is_supported_extension = (filename: string): boolean => {
164
+ return (
165
+ (filename.endsWith(".ts") && !filename.endsWith(".d.ts")) ||
166
+ (filename.endsWith(".tsx") && !filename.endsWith(".d.tsx"))
167
+ );
168
+ };
169
+ }
@@ -28,4 +28,4 @@ export namespace AtomicPredicator {
28
28
  !ArrayUtil.has(meta.atomics, (a) => a.type === "string");
29
29
  }
30
30
 
31
- const LIKE = new Set(["boolean", "number", "string"]);
31
+ const LIKE = new Set(["boolean", "bigint", "number", "string"]);
@@ -1,146 +1,146 @@
1
- import ts from "typescript";
2
-
3
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
- import { StatementFactory } from "../../factories/StatementFactory";
5
- import { TypeFactory } from "../../factories/TypeFactory";
6
-
7
- import { Escaper } from "../../utils/Escaper";
8
-
9
- import { metadata_to_pattern } from "../internal/metadata_to_pattern";
10
- import { IExpressionEntry } from "./IExpressionEntry";
11
-
12
- export namespace NotationJoiner {
13
- export const object =
14
- (rename: (str: string) => string) =>
15
- (
16
- input: ts.Expression,
17
- entries: IExpressionEntry<ts.Expression>[],
18
- ): ts.ConciseBody => {
19
- if (entries.length === 0) return ts.factory.createIdentifier("{}");
20
-
21
- const regular = entries.filter((e) => e.key.isSoleLiteral());
22
- const dynamic = entries.filter((e) => !e.key.isSoleLiteral());
23
- const literal = ts.factory.createObjectLiteralExpression(
24
- regular.map((entry) => {
25
- const str: string = rename(entry.key.getSoleLiteral()!);
26
- return ts.factory.createPropertyAssignment(
27
- Escaper.variable(str)
28
- ? str
29
- : ts.factory.createStringLiteral(str),
30
- entry.expression,
31
- );
32
- }),
33
- true,
34
- );
35
- if (dynamic.length === 0) return literal;
36
-
37
- const key = ts.factory.createIdentifier("key");
38
- const output = ts.factory.createIdentifier("output");
39
-
40
- const statements: ts.Statement[] = [];
41
- if (regular.length !== 0)
42
- statements.push(
43
- ts.factory.createIfStatement(
44
- ts.factory.createCallExpression(
45
- IdentifierFactory.access(
46
- ts.factory.createArrayLiteralExpression(
47
- regular.map((r) =>
48
- ts.factory.createStringLiteral(
49
- r.key.getSoleLiteral()!,
50
- ),
51
- ),
52
- ),
53
- )("some"),
54
- undefined,
55
- [
56
- ts.factory.createArrowFunction(
57
- undefined,
58
- undefined,
59
- [IdentifierFactory.parameter("regular")],
60
- undefined,
61
- undefined,
62
- ts.factory.createStrictEquality(
63
- ts.factory.createIdentifier("regular"),
64
- ts.factory.createIdentifier("key"),
65
- ),
66
- ),
67
- ],
68
- ),
69
- ts.factory.createContinueStatement(),
70
- ),
71
- );
72
- statements.push(
73
- ...dynamic.map((entry) =>
74
- ts.factory.createIfStatement(
75
- ts.factory.createCallExpression(
76
- ts.factory.createIdentifier(
77
- `RegExp(/${metadata_to_pattern(true)(
78
- entry.key,
79
- )}/).test`,
80
- ),
81
- undefined,
82
- [key],
83
- ),
84
- ts.factory.createBlock([
85
- ts.factory.createExpressionStatement(
86
- ts.factory.createBinaryExpression(
87
- ts.factory.createElementAccessExpression(
88
- output,
89
- key,
90
- ),
91
- ts.factory.createToken(
92
- ts.SyntaxKind.EqualsToken,
93
- ),
94
- entry.expression,
95
- ),
96
- ),
97
- ts.factory.createContinueStatement(),
98
- ]),
99
- ),
100
- ),
101
- );
102
-
103
- return ts.factory.createBlock([
104
- StatementFactory.constant(
105
- "output",
106
- ts.factory.createAsExpression(
107
- literal,
108
- TypeFactory.keyword("any"),
109
- ),
110
- ),
111
- ts.factory.createForOfStatement(
112
- undefined,
113
- StatementFactory.entry("key")("value"),
114
- ts.factory.createCallExpression(
115
- ts.factory.createIdentifier("Object.entries"),
116
- undefined,
117
- [input],
118
- ),
119
- ts.factory.createBlock(statements),
120
- ),
121
- ts.factory.createReturnStatement(output),
122
- ]);
123
- };
124
-
125
- export const tuple = (
126
- children: ts.Expression[],
127
- rest: ts.Expression | null,
128
- ): ts.Expression => {
129
- return ts.factory.createAsExpression(
130
- ts.factory.createArrayLiteralExpression(
131
- rest === null
132
- ? children
133
- : [...children, ts.factory.createSpreadElement(rest)],
134
- true,
135
- ),
136
- TypeFactory.keyword("any"),
137
- );
138
- };
139
-
140
- export const array = (input: ts.Expression, arrow: ts.Expression) =>
141
- ts.factory.createCallExpression(
142
- ts.factory.createPropertyAccessExpression(input, "map"),
143
- undefined,
144
- [arrow],
145
- );
146
- }
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
+ import { StatementFactory } from "../../factories/StatementFactory";
5
+ import { TypeFactory } from "../../factories/TypeFactory";
6
+
7
+ import { Escaper } from "../../utils/Escaper";
8
+
9
+ import { metadata_to_pattern } from "../internal/metadata_to_pattern";
10
+ import { IExpressionEntry } from "./IExpressionEntry";
11
+
12
+ export namespace NotationJoiner {
13
+ export const object =
14
+ (rename: (str: string) => string) =>
15
+ (
16
+ input: ts.Expression,
17
+ entries: IExpressionEntry<ts.Expression>[],
18
+ ): ts.ConciseBody => {
19
+ if (entries.length === 0) return ts.factory.createIdentifier("{}");
20
+
21
+ const regular = entries.filter((e) => e.key.isSoleLiteral());
22
+ const dynamic = entries.filter((e) => !e.key.isSoleLiteral());
23
+ const literal = ts.factory.createObjectLiteralExpression(
24
+ regular.map((entry) => {
25
+ const str: string = rename(entry.key.getSoleLiteral()!);
26
+ return ts.factory.createPropertyAssignment(
27
+ Escaper.variable(str)
28
+ ? str
29
+ : ts.factory.createStringLiteral(str),
30
+ entry.expression,
31
+ );
32
+ }),
33
+ true,
34
+ );
35
+ if (dynamic.length === 0) return literal;
36
+
37
+ const key = ts.factory.createIdentifier("key");
38
+ const output = ts.factory.createIdentifier("output");
39
+
40
+ const statements: ts.Statement[] = [];
41
+ if (regular.length !== 0)
42
+ statements.push(
43
+ ts.factory.createIfStatement(
44
+ ts.factory.createCallExpression(
45
+ IdentifierFactory.access(
46
+ ts.factory.createArrayLiteralExpression(
47
+ regular.map((r) =>
48
+ ts.factory.createStringLiteral(
49
+ r.key.getSoleLiteral()!,
50
+ ),
51
+ ),
52
+ ),
53
+ )("some"),
54
+ undefined,
55
+ [
56
+ ts.factory.createArrowFunction(
57
+ undefined,
58
+ undefined,
59
+ [IdentifierFactory.parameter("regular")],
60
+ undefined,
61
+ undefined,
62
+ ts.factory.createStrictEquality(
63
+ ts.factory.createIdentifier("regular"),
64
+ ts.factory.createIdentifier("key"),
65
+ ),
66
+ ),
67
+ ],
68
+ ),
69
+ ts.factory.createContinueStatement(),
70
+ ),
71
+ );
72
+ statements.push(
73
+ ...dynamic.map((entry) =>
74
+ ts.factory.createIfStatement(
75
+ ts.factory.createCallExpression(
76
+ ts.factory.createIdentifier(
77
+ `RegExp(/${metadata_to_pattern(true)(
78
+ entry.key,
79
+ )}/).test`,
80
+ ),
81
+ undefined,
82
+ [key],
83
+ ),
84
+ ts.factory.createBlock([
85
+ ts.factory.createExpressionStatement(
86
+ ts.factory.createBinaryExpression(
87
+ ts.factory.createElementAccessExpression(
88
+ output,
89
+ key,
90
+ ),
91
+ ts.factory.createToken(
92
+ ts.SyntaxKind.EqualsToken,
93
+ ),
94
+ entry.expression,
95
+ ),
96
+ ),
97
+ ts.factory.createContinueStatement(),
98
+ ]),
99
+ ),
100
+ ),
101
+ );
102
+
103
+ return ts.factory.createBlock([
104
+ StatementFactory.constant(
105
+ "output",
106
+ ts.factory.createAsExpression(
107
+ literal,
108
+ TypeFactory.keyword("any"),
109
+ ),
110
+ ),
111
+ ts.factory.createForOfStatement(
112
+ undefined,
113
+ StatementFactory.entry("key")("value"),
114
+ ts.factory.createCallExpression(
115
+ ts.factory.createIdentifier("Object.entries"),
116
+ undefined,
117
+ [input],
118
+ ),
119
+ ts.factory.createBlock(statements),
120
+ ),
121
+ ts.factory.createReturnStatement(output),
122
+ ]);
123
+ };
124
+
125
+ export const tuple = (
126
+ children: ts.Expression[],
127
+ rest: ts.Expression | null,
128
+ ): ts.Expression => {
129
+ return ts.factory.createAsExpression(
130
+ ts.factory.createArrayLiteralExpression(
131
+ rest === null
132
+ ? children
133
+ : [...children, ts.factory.createSpreadElement(rest)],
134
+ true,
135
+ ),
136
+ TypeFactory.keyword("any"),
137
+ );
138
+ };
139
+
140
+ export const array = (input: ts.Expression, arrow: ts.Expression) =>
141
+ ts.factory.createCallExpression(
142
+ ts.factory.createPropertyAccessExpression(input, "map"),
143
+ undefined,
144
+ [arrow],
145
+ );
146
+ }