typia 4.2.0 → 4.2.1

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.
@@ -1,167 +1,171 @@
1
- import ts from "typescript";
2
-
3
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
- import { TemplateFactory } from "../../factories/TemplateFactory";
5
-
6
- import { IExpressionEntry } from "../helpers/IExpressionEntry";
7
- import { metadata_to_pattern } from "./metadata_to_pattern";
8
-
9
- /**
10
- * @internal
11
- */
12
- export const stringify_dynamic_properties = (
13
- dynamic: IExpressionEntry<ts.Expression>[],
14
- regular: string[],
15
- ): ts.Expression => {
16
- // BASIC STATMEMENT, CHECK UNDEFINED
17
- const statements: ts.Statement[] = [
18
- ts.factory.createIfStatement(
19
- ts.factory.createStrictEquality(
20
- ts.factory.createIdentifier("undefined"),
21
- ts.factory.createIdentifier("value"),
22
- ),
23
- ts.factory.createReturnStatement(
24
- ts.factory.createStringLiteral(""),
25
- ),
26
- ),
27
- ];
28
-
29
- // PREPARE RETURN FUNCTION
30
- const output = () => {
31
- const mapped = ts.factory.createCallExpression(
32
- IdentifierFactory.access(
33
- ts.factory.createCallExpression(
34
- ts.factory.createIdentifier("Object.entries"),
35
- undefined,
36
- [ts.factory.createIdentifier("input")],
37
- ),
38
- )("map"),
39
- undefined,
40
- [
41
- ts.factory.createArrowFunction(
42
- undefined,
43
- undefined,
44
- [
45
- IdentifierFactory.parameter(
46
- ts.factory.createArrayBindingPattern([
47
- ts.factory.createBindingElement(
48
- undefined,
49
- undefined,
50
- "key",
51
- ),
52
- ts.factory.createBindingElement(
53
- undefined,
54
- undefined,
55
- "value",
56
- ),
57
- ]),
58
- ts.factory.createTypeReferenceNode("[string, any]"),
59
- ),
60
- ],
61
- undefined,
62
- undefined,
63
- ts.factory.createBlock(statements),
64
- ),
65
- ],
66
- );
67
- const filtered = ts.factory.createCallExpression(
68
- IdentifierFactory.access(mapped)("filter"),
69
- undefined,
70
- [
71
- ts.factory.createArrowFunction(
72
- undefined,
73
- undefined,
74
- [IdentifierFactory.parameter("str")],
75
- undefined,
76
- undefined,
77
- ts.factory.createStrictInequality(
78
- ts.factory.createStringLiteral(""),
79
- ts.factory.createIdentifier("str"),
80
- ),
81
- ),
82
- ],
83
- );
84
- return ts.factory.createCallExpression(
85
- IdentifierFactory.access(filtered)("join"),
86
- undefined,
87
- [ts.factory.createStringLiteral(",")],
88
- );
89
- };
90
-
91
- // WHEN REGULAR PROPERTY EXISTS
92
- if (regular.length)
93
- statements.push(
94
- ts.factory.createIfStatement(
95
- ts.factory.createCallExpression(
96
- IdentifierFactory.access(
97
- ts.factory.createArrayLiteralExpression(
98
- regular.map((key) =>
99
- ts.factory.createStringLiteral(key),
100
- ),
101
- ),
102
- )("some"),
103
- undefined,
104
- [
105
- ts.factory.createArrowFunction(
106
- undefined,
107
- undefined,
108
- [IdentifierFactory.parameter("regular")],
109
- undefined,
110
- undefined,
111
- ts.factory.createStrictEquality(
112
- ts.factory.createIdentifier("regular"),
113
- ts.factory.createIdentifier("key"),
114
- ),
115
- ),
116
- ],
117
- ),
118
- ts.factory.createReturnStatement(
119
- ts.factory.createStringLiteral(""),
120
- ),
121
- ),
122
- );
123
-
124
- // ONLY STRING TYPED KEY EXISTS
125
- const simple: boolean =
126
- dynamic.length === 1 &&
127
- dynamic[0]!.key.size() === 1 &&
128
- dynamic[0]!.key.atomics[0] === "string";
129
- if (simple === true) {
130
- statements.push(stringify(dynamic[0]!));
131
- return output();
132
- }
133
-
134
- // COMPOSITE TEMPLATE LITERAL TYPES
135
- for (const entry of dynamic) {
136
- const condition: ts.IfStatement = ts.factory.createIfStatement(
137
- ts.factory.createCallExpression(
138
- ts.factory.createIdentifier(
139
- `RegExp(/${metadata_to_pattern(true)(entry.key)}/).test`,
140
- ),
141
- undefined,
142
- [ts.factory.createIdentifier("key")],
143
- ),
144
- stringify(entry),
145
- );
146
- statements.push(condition);
147
- }
148
- return output();
149
- };
150
-
151
- /**
152
- * @internal
153
- */
154
- const stringify = (
155
- entry: IExpressionEntry<ts.Expression>,
156
- ): ts.ReturnStatement =>
157
- ts.factory.createReturnStatement(
158
- TemplateFactory.generate([
159
- ts.factory.createCallExpression(
160
- ts.factory.createIdentifier("JSON.stringify"),
161
- [],
162
- [ts.factory.createIdentifier("key")],
163
- ),
164
- ts.factory.createStringLiteral(":"),
165
- entry.expression,
166
- ]),
167
- );
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
+ import { TemplateFactory } from "../../factories/TemplateFactory";
5
+
6
+ import { IExpressionEntry } from "../helpers/IExpressionEntry";
7
+ import { metadata_to_pattern } from "./metadata_to_pattern";
8
+
9
+ /**
10
+ * @internal
11
+ */
12
+ export const stringify_dynamic_properties = (
13
+ dynamic: IExpressionEntry<ts.Expression>[],
14
+ regular: string[],
15
+ ): ts.Expression => {
16
+ // BASIC STATMEMENT, CHECK UNDEFINED
17
+ const statements: ts.Statement[] = [
18
+ ts.factory.createIfStatement(
19
+ ts.factory.createStrictEquality(
20
+ ts.factory.createIdentifier("undefined"),
21
+ ts.factory.createIdentifier("value"),
22
+ ),
23
+ ts.factory.createReturnStatement(
24
+ ts.factory.createStringLiteral(""),
25
+ ),
26
+ ),
27
+ ];
28
+
29
+ // PREPARE RETURN FUNCTION
30
+ const output = () => {
31
+ const mapped = ts.factory.createCallExpression(
32
+ IdentifierFactory.access(
33
+ ts.factory.createCallExpression(
34
+ ts.factory.createIdentifier("Object.entries"),
35
+ undefined,
36
+ [ts.factory.createIdentifier("input")],
37
+ ),
38
+ )("map"),
39
+ undefined,
40
+ [
41
+ ts.factory.createArrowFunction(
42
+ undefined,
43
+ undefined,
44
+ [
45
+ IdentifierFactory.parameter(
46
+ ts.factory.createArrayBindingPattern([
47
+ ts.factory.createBindingElement(
48
+ undefined,
49
+ undefined,
50
+ "key",
51
+ ),
52
+ ts.factory.createBindingElement(
53
+ undefined,
54
+ undefined,
55
+ "value",
56
+ ),
57
+ ]),
58
+ ts.factory.createTypeReferenceNode("[string, any]"),
59
+ ),
60
+ ],
61
+ undefined,
62
+ undefined,
63
+ ts.factory.createBlock(statements),
64
+ ),
65
+ ],
66
+ );
67
+ const filtered = ts.factory.createCallExpression(
68
+ IdentifierFactory.access(mapped)("filter"),
69
+ undefined,
70
+ [
71
+ ts.factory.createArrowFunction(
72
+ undefined,
73
+ undefined,
74
+ [IdentifierFactory.parameter("str")],
75
+ undefined,
76
+ undefined,
77
+ ts.factory.createStrictInequality(
78
+ ts.factory.createStringLiteral(""),
79
+ ts.factory.createIdentifier("str"),
80
+ ),
81
+ ),
82
+ ],
83
+ );
84
+ return ts.factory.createCallExpression(
85
+ IdentifierFactory.access(filtered)("join"),
86
+ undefined,
87
+ [ts.factory.createStringLiteral(",")],
88
+ );
89
+ };
90
+
91
+ // WHEN REGULAR PROPERTY EXISTS
92
+ if (regular.length)
93
+ statements.push(
94
+ ts.factory.createIfStatement(
95
+ ts.factory.createCallExpression(
96
+ IdentifierFactory.access(
97
+ ts.factory.createArrayLiteralExpression(
98
+ regular.map((key) =>
99
+ ts.factory.createStringLiteral(key),
100
+ ),
101
+ ),
102
+ )("some"),
103
+ undefined,
104
+ [
105
+ ts.factory.createArrowFunction(
106
+ undefined,
107
+ undefined,
108
+ [IdentifierFactory.parameter("regular")],
109
+ undefined,
110
+ undefined,
111
+ ts.factory.createStrictEquality(
112
+ ts.factory.createIdentifier("regular"),
113
+ ts.factory.createIdentifier("key"),
114
+ ),
115
+ ),
116
+ ],
117
+ ),
118
+ ts.factory.createReturnStatement(
119
+ ts.factory.createStringLiteral(""),
120
+ ),
121
+ ),
122
+ );
123
+
124
+ // ONLY STRING TYPED KEY EXISTS
125
+ const simple: boolean =
126
+ dynamic.length === 1 &&
127
+ dynamic[0]!.key.size() === 1 &&
128
+ dynamic[0]!.key.atomics[0] === "string";
129
+ if (simple === true) {
130
+ statements.push(stringify(dynamic[0]!));
131
+ return output();
132
+ }
133
+
134
+ // COMPOSITE TEMPLATE LITERAL TYPES
135
+ for (const entry of dynamic) {
136
+ const condition: ts.IfStatement = ts.factory.createIfStatement(
137
+ ts.factory.createCallExpression(
138
+ ts.factory.createIdentifier(
139
+ `RegExp(/${metadata_to_pattern(true)(entry.key)}/).test`,
140
+ ),
141
+ undefined,
142
+ [ts.factory.createIdentifier("key")],
143
+ ),
144
+ stringify(entry),
145
+ );
146
+ statements.push(condition);
147
+ }
148
+ statements.push(
149
+ ts.factory.createReturnStatement(ts.factory.createStringLiteral("")),
150
+ );
151
+
152
+ return output();
153
+ };
154
+
155
+ /**
156
+ * @internal
157
+ */
158
+ const stringify = (
159
+ entry: IExpressionEntry<ts.Expression>,
160
+ ): ts.ReturnStatement =>
161
+ ts.factory.createReturnStatement(
162
+ TemplateFactory.generate([
163
+ ts.factory.createCallExpression(
164
+ ts.factory.createIdentifier("JSON.stringify"),
165
+ [],
166
+ [ts.factory.createIdentifier("key")],
167
+ ),
168
+ ts.factory.createStringLiteral(":"),
169
+ entry.expression,
170
+ ]),
171
+ );
package/src/transform.ts CHANGED
@@ -1,27 +1,27 @@
1
- import ts from "typescript";
2
-
3
- import { FileTransformer } from "./transformers/FileTransformer";
4
- import { ITransformOptions } from "./transformers/ITransformOptions";
5
-
6
- export const transform = (
7
- program: ts.Program,
8
- options?: ITransformOptions,
9
- ): ts.TransformerFactory<ts.SourceFile> => {
10
- const compilerOptions: ts.CompilerOptions = program.getCompilerOptions();
11
- const strict: boolean =
12
- compilerOptions.strictNullChecks !== undefined
13
- ? !!compilerOptions.strictNullChecks
14
- : !!compilerOptions.strict;
15
- if (strict === false)
16
- throw new Error(
17
- `Error on "tsconfig.json": typia requires \`compilerOptions.strictNullChecks\` to be true.`,
18
- );
19
- return FileTransformer.transform({
20
- program,
21
- compilerOptions,
22
- checker: program.getTypeChecker(),
23
- printer: ts.createPrinter(),
24
- options: options || {},
25
- });
26
- };
27
- export default transform;
1
+ import ts from "typescript";
2
+
3
+ import { FileTransformer } from "./transformers/FileTransformer";
4
+ import { ITransformOptions } from "./transformers/ITransformOptions";
5
+
6
+ export const transform = (
7
+ program: ts.Program,
8
+ options?: ITransformOptions,
9
+ ): ts.TransformerFactory<ts.SourceFile> => {
10
+ const compilerOptions: ts.CompilerOptions = program.getCompilerOptions();
11
+ const strict: boolean =
12
+ compilerOptions.strictNullChecks !== undefined
13
+ ? !!compilerOptions.strictNullChecks
14
+ : !!compilerOptions.strict;
15
+ if (strict === false)
16
+ throw new Error(
17
+ `Error on "tsconfig.json": typia requires \`compilerOptions.strictNullChecks\` to be true.`,
18
+ );
19
+ return FileTransformer.transform({
20
+ program,
21
+ compilerOptions,
22
+ checker: program.getTypeChecker(),
23
+ printer: ts.createPrinter(),
24
+ options: options || {},
25
+ });
26
+ };
27
+ export default transform;
@@ -1,62 +1,62 @@
1
- export interface ITransformOptions {
2
- /**
3
- * Whether to validate finite number or not.
4
- *
5
- * If configured true, number typed values would be validated by Number.isNaN().
6
- *
7
- * However, whatever you configure, it would be ignored when marshaling or parsing.
8
- *
9
- * - when marshaling, always be true
10
- * - assertStringify()
11
- * - validateEncode()
12
- * - when parsing, always be false
13
- * - assertParse()
14
- * - isDecode()
15
- *
16
- * @default false
17
- */
18
- finite?: boolean;
19
-
20
- /**
21
- * Whether to validate finite number or not.
22
- *
23
- * If configured true, number typed values would be validated by Number.isFinite().
24
- *
25
- * However, whatever you configure, it can be ignored in below case.
26
- *
27
- * - when `finite` option is true, this option would be ignored
28
- * - when marshaling, always be true
29
- * - assertStringify()
30
- * - validateEncode()
31
- * - when parsing, always be false
32
- * - assertParse()
33
- * - isDecode()
34
- *
35
- * @default false
36
- */
37
- numeric?: boolean;
38
-
39
- /**
40
- * Whether to validate functional type or not.
41
- *
42
- * However, whatever you configure, it becomes false when marshaling or parsing.
43
- *
44
- * @default false
45
- */
46
- functional?: boolean;
47
-
48
- /**
49
- * Whether to check undefined value or not.
50
- *
51
- * JavaScript can assign `undefined` value to a specific property and it is an
52
- * issue when validating without allowing superfluous properties. Should undefined
53
- * value assigned superfluous property be allowed or not?
54
- *
55
- * Note that, this option only works on {@link equals} function. Other function
56
- * like {@link assertEquals} or {@link validateEquals} would ignore this option
57
- * value and always allow the `undefined` value.
58
- *
59
- * @default true
60
- */
61
- undefined?: boolean;
62
- }
1
+ export interface ITransformOptions {
2
+ /**
3
+ * Whether to validate finite number or not.
4
+ *
5
+ * If configured true, number typed values would be validated by Number.isNaN().
6
+ *
7
+ * However, whatever you configure, it would be ignored when marshaling or parsing.
8
+ *
9
+ * - when marshaling, always be true
10
+ * - assertStringify()
11
+ * - validateEncode()
12
+ * - when parsing, always be false
13
+ * - assertParse()
14
+ * - isDecode()
15
+ *
16
+ * @default false
17
+ */
18
+ finite?: boolean;
19
+
20
+ /**
21
+ * Whether to validate finite number or not.
22
+ *
23
+ * If configured true, number typed values would be validated by Number.isFinite().
24
+ *
25
+ * However, whatever you configure, it can be ignored in below case.
26
+ *
27
+ * - when `finite` option is true, this option would be ignored
28
+ * - when marshaling, always be true
29
+ * - assertStringify()
30
+ * - validateEncode()
31
+ * - when parsing, always be false
32
+ * - assertParse()
33
+ * - isDecode()
34
+ *
35
+ * @default false
36
+ */
37
+ numeric?: boolean;
38
+
39
+ /**
40
+ * Whether to validate functional type or not.
41
+ *
42
+ * However, whatever you configure, it becomes false when marshaling or parsing.
43
+ *
44
+ * @default false
45
+ */
46
+ functional?: boolean;
47
+
48
+ /**
49
+ * Whether to check undefined value or not.
50
+ *
51
+ * JavaScript can assign `undefined` value to a specific property and it is an
52
+ * issue when validating without allowing superfluous properties. Should undefined
53
+ * value assigned superfluous property be allowed or not?
54
+ *
55
+ * Note that, this option only works on {@link equals} function. Other function
56
+ * like {@link assertEquals} or {@link validateEquals} would ignore this option
57
+ * value and always allow the `undefined` value.
58
+ *
59
+ * @default true
60
+ */
61
+ undefined?: boolean;
62
+ }
@@ -1,13 +1,13 @@
1
- import ts from "typescript";
2
-
3
- import { CallExpressionTransformer } from "./CallExpressionTransformer";
4
- import { IProject } from "./IProject";
5
-
6
- export namespace NodeTransformer {
7
- export const transform =
8
- (project: IProject) =>
9
- (expression: ts.Node): ts.Node =>
10
- ts.isCallExpression(expression)
11
- ? CallExpressionTransformer.transform(project)(expression)
12
- : expression;
13
- }
1
+ import ts from "typescript";
2
+
3
+ import { CallExpressionTransformer } from "./CallExpressionTransformer";
4
+ import { IProject } from "./IProject";
5
+
6
+ export namespace NodeTransformer {
7
+ export const transform =
8
+ (project: IProject) =>
9
+ (expression: ts.Node): ts.Node =>
10
+ ts.isCallExpression(expression)
11
+ ? CallExpressionTransformer.transform(project)(expression)
12
+ : expression;
13
+ }
@@ -1,30 +1,33 @@
1
- export namespace PatternUtil {
2
- export const fix = (str: string): string => {
3
- const first: number = str.indexOf(STRING);
4
- const last: number = str.lastIndexOf(STRING);
5
- return [
6
- first === -1 || none("(")(str.slice(0, first)) ? "^" : "",
7
- str,
8
- last === -1 || none(")")(str.slice(last + STRING.length))
9
- ? "$"
10
- : "",
11
- ].join("");
12
- };
13
-
14
- export const escape = (str: string): string => {
15
- return str
16
- .replace(/[|\\/{}()[\]^$+*?.]/g, "\\$&")
17
- .replace(/-/g, "\\x2d");
18
- };
19
-
20
- export const NUMBER = "-?\\d+\\.?\\d*";
21
- export const BOOLEAN = "true|false";
22
- export const STRING = "(.*)";
23
- }
24
-
25
- const none =
26
- (parenthesis: string) =>
27
- (str: string): boolean => {
28
- for (const ch of str) if (ch !== parenthesis) return true;
29
- return false;
30
- };
1
+ export namespace PatternUtil {
2
+ export const fix = (str: string): string => {
3
+ const first: number = str.indexOf(STRING);
4
+ const last: number = str.lastIndexOf(STRING);
5
+ return [
6
+ first === -1 || none("(")(str.slice(0, first)) ? "^" : "",
7
+ str,
8
+ last === -1 || none(")")(str.slice(last + STRING.length))
9
+ ? "$"
10
+ : "",
11
+ ].join("");
12
+ };
13
+
14
+ export const escape = (str: string): string => {
15
+ return str
16
+ .replace(/[|\\/{}()[\]^$+*?.]/g, "\\$&")
17
+ .replace(/-/g, "\\x2d");
18
+ };
19
+
20
+ export const NUMBER =
21
+ "[+-]?" + // optional sign
22
+ "\\d+(?:\\.\\d+)?" + // integer or decimal
23
+ "(?:[eE][+-]?\\d+)?"; // optional exponent
24
+ export const BOOLEAN = "true|false";
25
+ export const STRING = "(.*)";
26
+ }
27
+
28
+ const none =
29
+ (parenthesis: string) =>
30
+ (str: string): boolean => {
31
+ for (const ch of str) if (ch !== parenthesis) return true;
32
+ return false;
33
+ };