typia 5.5.0-dev.20240302 → 5.5.0-dev.20240303
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.
- package/lib/functional.d.ts +353 -0
- package/lib/functional.js +130 -0
- package/lib/functional.js.map +1 -0
- package/lib/module.d.ts +1 -0
- package/lib/module.js +2 -1
- package/lib/module.js.map +1 -1
- package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.d.ts +6 -0
- package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.js +33 -0
- package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.js.map +1 -0
- package/lib/programmers/functional/FunctionalAssertParametersProgrammer.d.ts +11 -0
- package/lib/programmers/functional/FunctionalAssertParametersProgrammer.js +58 -0
- package/lib/programmers/functional/FunctionalAssertParametersProgrammer.js.map +1 -0
- package/lib/programmers/functional/FunctionalAssertReturnProgrammer.d.ts +14 -0
- package/lib/programmers/functional/FunctionalAssertReturnProgrammer.js +67 -0
- package/lib/programmers/functional/FunctionalAssertReturnProgrammer.js.map +1 -0
- package/lib/transformers/CallExpressionTransformer.d.ts +1 -1
- package/lib/transformers/CallExpressionTransformer.js +49 -0
- package/lib/transformers/CallExpressionTransformer.js.map +1 -1
- package/lib/transformers/features/functional/FunctionalGenericTransformer.d.ts +10 -0
- package/lib/transformers/features/functional/FunctionalGenericTransformer.js +32 -0
- package/lib/transformers/features/functional/FunctionalGenericTransformer.js.map +1 -0
- package/package.json +1 -1
- package/src/functional.ts +561 -0
- package/src/module.ts +1 -0
- package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +44 -0
- package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +100 -0
- package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +84 -0
- package/src/transformers/CallExpressionTransformer.ts +52 -5
- package/src/transformers/features/functional/FunctionalGenericTransformer.ts +41 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { IProject } from "../../transformers/IProject";
|
|
3
|
+
import { StatementFactory } from "../../factories/StatementFactory";
|
|
4
|
+
import { AssertProgrammer } from "../AssertProgrammer";
|
|
5
|
+
|
|
6
|
+
export namespace FunctionalAssertParametersProgrammer {
|
|
7
|
+
export const write =
|
|
8
|
+
(project: IProject) =>
|
|
9
|
+
(modulo: ts.LeftHandSideExpression) =>
|
|
10
|
+
(equals: boolean) =>
|
|
11
|
+
(
|
|
12
|
+
expression: ts.Expression,
|
|
13
|
+
declaration: ts.FunctionDeclaration,
|
|
14
|
+
init?: ts.Expression,
|
|
15
|
+
) => {
|
|
16
|
+
const { assert, call } = prepare(project)(modulo)(equals)(
|
|
17
|
+
declaration,
|
|
18
|
+
init,
|
|
19
|
+
);
|
|
20
|
+
const async: boolean = (() => {
|
|
21
|
+
if (declaration.type === undefined) return false;
|
|
22
|
+
const type: ts.Type = project.checker.getTypeFromTypeNode(
|
|
23
|
+
declaration.type,
|
|
24
|
+
);
|
|
25
|
+
return type.isTypeParameter() && type.symbol.name === "Promise";
|
|
26
|
+
})();
|
|
27
|
+
|
|
28
|
+
return ts.factory.createFunctionDeclaration(
|
|
29
|
+
async
|
|
30
|
+
? [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)]
|
|
31
|
+
: undefined,
|
|
32
|
+
undefined,
|
|
33
|
+
undefined,
|
|
34
|
+
undefined,
|
|
35
|
+
declaration.parameters,
|
|
36
|
+
declaration.type,
|
|
37
|
+
ts.factory.createBlock(
|
|
38
|
+
[
|
|
39
|
+
assert,
|
|
40
|
+
call,
|
|
41
|
+
ts.factory.createReturnStatement(
|
|
42
|
+
ts.factory.createCallExpression(
|
|
43
|
+
expression,
|
|
44
|
+
undefined,
|
|
45
|
+
declaration.parameters.map((p) =>
|
|
46
|
+
ts.factory.createIdentifier(p.name.getText()),
|
|
47
|
+
),
|
|
48
|
+
),
|
|
49
|
+
),
|
|
50
|
+
],
|
|
51
|
+
true,
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const prepare =
|
|
57
|
+
(project: IProject) =>
|
|
58
|
+
(modulo: ts.LeftHandSideExpression) =>
|
|
59
|
+
(equals: boolean) =>
|
|
60
|
+
(declaration: ts.FunctionDeclaration, init?: ts.Expression) => {
|
|
61
|
+
const typeNode: ts.TypeNode = ts.factory.createTypeLiteralNode([
|
|
62
|
+
ts.factory.createPropertySignature(
|
|
63
|
+
undefined,
|
|
64
|
+
"parameters",
|
|
65
|
+
undefined,
|
|
66
|
+
ts.factory.createTupleTypeNode(
|
|
67
|
+
declaration.parameters.map((p) => p.type!),
|
|
68
|
+
),
|
|
69
|
+
),
|
|
70
|
+
]);
|
|
71
|
+
const type: ts.Type = project.checker.getTypeFromTypeNode(typeNode);
|
|
72
|
+
|
|
73
|
+
const assert = StatementFactory.constant(
|
|
74
|
+
"assert",
|
|
75
|
+
AssertProgrammer.write(project)(modulo)(equals)(type, undefined, init),
|
|
76
|
+
);
|
|
77
|
+
const call = ts.factory.createExpressionStatement(
|
|
78
|
+
ts.factory.createCallExpression(
|
|
79
|
+
ts.factory.createIdentifier("assert"),
|
|
80
|
+
undefined,
|
|
81
|
+
[
|
|
82
|
+
ts.factory.createObjectLiteralExpression(
|
|
83
|
+
[
|
|
84
|
+
ts.factory.createPropertyAssignment(
|
|
85
|
+
"parameters",
|
|
86
|
+
ts.factory.createArrayLiteralExpression(
|
|
87
|
+
declaration.parameters.map((p) =>
|
|
88
|
+
ts.factory.createIdentifier(p.name.getText()),
|
|
89
|
+
),
|
|
90
|
+
),
|
|
91
|
+
),
|
|
92
|
+
],
|
|
93
|
+
true,
|
|
94
|
+
),
|
|
95
|
+
],
|
|
96
|
+
),
|
|
97
|
+
);
|
|
98
|
+
return { assert, call, type };
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { IProject } from "../../transformers/IProject";
|
|
3
|
+
import { StatementFactory } from "../../factories/StatementFactory";
|
|
4
|
+
import { AssertProgrammer } from "../AssertProgrammer";
|
|
5
|
+
|
|
6
|
+
export namespace FunctionAssertReturnProgrammer {
|
|
7
|
+
export const write =
|
|
8
|
+
(project: IProject) =>
|
|
9
|
+
(modulo: ts.LeftHandSideExpression) =>
|
|
10
|
+
(equals: boolean) =>
|
|
11
|
+
(
|
|
12
|
+
expression: ts.Expression,
|
|
13
|
+
declaration: ts.FunctionDeclaration,
|
|
14
|
+
init?: ts.Expression,
|
|
15
|
+
) => {
|
|
16
|
+
const { async, assert, variable, call, returns } = prepare(project)(
|
|
17
|
+
modulo,
|
|
18
|
+
)(equals)(expression, declaration, init);
|
|
19
|
+
return ts.factory.createFunctionDeclaration(
|
|
20
|
+
async
|
|
21
|
+
? [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)]
|
|
22
|
+
: undefined,
|
|
23
|
+
undefined,
|
|
24
|
+
undefined,
|
|
25
|
+
undefined,
|
|
26
|
+
declaration.parameters,
|
|
27
|
+
declaration.type,
|
|
28
|
+
ts.factory.createBlock([assert, variable, call, returns], true),
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const prepare =
|
|
33
|
+
(project: IProject) =>
|
|
34
|
+
(modulo: ts.LeftHandSideExpression) =>
|
|
35
|
+
(equals: boolean) =>
|
|
36
|
+
(
|
|
37
|
+
expression: ts.Expression,
|
|
38
|
+
declaration: ts.FunctionDeclaration,
|
|
39
|
+
init?: ts.Expression,
|
|
40
|
+
) => {
|
|
41
|
+
const [type, async]: [ts.Type, boolean] = (() => {
|
|
42
|
+
const type: ts.Type = project.checker.getTypeFromTypeNode(
|
|
43
|
+
declaration.type!,
|
|
44
|
+
);
|
|
45
|
+
return type.isTypeParameter() && type.symbol.name === "Promise"
|
|
46
|
+
? [type.aliasTypeArguments![0]!, true]
|
|
47
|
+
: [type, false];
|
|
48
|
+
})();
|
|
49
|
+
const assert = StatementFactory.constant(
|
|
50
|
+
"assert",
|
|
51
|
+
AssertProgrammer.write(project)(modulo)(equals)(type, undefined, init),
|
|
52
|
+
);
|
|
53
|
+
const funcCaller = ts.factory.createCallExpression(
|
|
54
|
+
expression,
|
|
55
|
+
undefined,
|
|
56
|
+
declaration.parameters.map((p) =>
|
|
57
|
+
ts.factory.createIdentifier(p.name.getText()),
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
const variable = StatementFactory.constant(
|
|
61
|
+
"output",
|
|
62
|
+
async ? ts.factory.createAwaitExpression(funcCaller) : funcCaller,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const call = ts.factory.createExpressionStatement(
|
|
66
|
+
ts.factory.createCallExpression(
|
|
67
|
+
ts.factory.createIdentifier("assert"),
|
|
68
|
+
undefined,
|
|
69
|
+
[
|
|
70
|
+
ts.factory.createObjectLiteralExpression([
|
|
71
|
+
ts.factory.createPropertyAssignment(
|
|
72
|
+
"return",
|
|
73
|
+
ts.factory.createIdentifier("output"),
|
|
74
|
+
),
|
|
75
|
+
]),
|
|
76
|
+
],
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
const returns = ts.factory.createReturnStatement(
|
|
80
|
+
ts.factory.createIdentifier("output"),
|
|
81
|
+
);
|
|
82
|
+
return { async, type, assert, variable, call, returns };
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -96,11 +96,17 @@ import { CreateHttpAssertFormDataTransformer } from "./features/http/CreateHttpA
|
|
|
96
96
|
import { CreateHttpFormDataTransformer } from "./features/http/CreateHttpFormDataTransformer";
|
|
97
97
|
import { CreateHttpIsFormDataTransformer } from "./features/http/CreateHttpIsFormDataTransformer";
|
|
98
98
|
import { CreateHttpValidateFormDataTransformer } from "./features/http/CreateHttpValidateFormDataTransformer";
|
|
99
|
+
import { FunctionalGenericTransformer } from "./features/functional/FunctionalGenericTransformer";
|
|
100
|
+
import { FunctionalAssertFunctionProgrammer } from "../programmers/functional/FunctionalAssertFunctionProgrammer";
|
|
101
|
+
import { FunctionalAssertParametersProgrammer } from "../programmers/functional/FunctionalAssertParametersProgrammer";
|
|
102
|
+
import { FunctionAssertReturnProgrammer } from "../programmers/functional/FunctionalAssertReturnProgrammer";
|
|
99
103
|
|
|
100
104
|
export namespace CallExpressionTransformer {
|
|
101
105
|
export const transform =
|
|
102
106
|
(project: IProject) =>
|
|
103
|
-
(
|
|
107
|
+
(
|
|
108
|
+
expression: ts.CallExpression,
|
|
109
|
+
): ts.Expression | ts.FunctionDeclaration | null => {
|
|
104
110
|
//----
|
|
105
111
|
// VALIDATIONS
|
|
106
112
|
//----
|
|
@@ -127,9 +133,9 @@ export namespace CallExpressionTransformer {
|
|
|
127
133
|
if (functor === undefined) return expression;
|
|
128
134
|
|
|
129
135
|
// RETURNS WITH TRANSFORMATION
|
|
130
|
-
const result: ts.Expression | null = functor()(
|
|
131
|
-
|
|
132
|
-
)(expression);
|
|
136
|
+
const result: ts.Expression | ts.FunctionDeclaration | null = functor()(
|
|
137
|
+
project,
|
|
138
|
+
)(expression.expression)(expression);
|
|
133
139
|
return result ?? expression;
|
|
134
140
|
};
|
|
135
141
|
|
|
@@ -145,7 +151,9 @@ type Task = (
|
|
|
145
151
|
project: IProject,
|
|
146
152
|
) => (
|
|
147
153
|
modulo: ts.LeftHandSideExpression,
|
|
148
|
-
) => (
|
|
154
|
+
) => (
|
|
155
|
+
expression: ts.CallExpression,
|
|
156
|
+
) => ts.Expression | ts.FunctionDeclaration | null;
|
|
149
157
|
|
|
150
158
|
const FUNCTORS: Record<string, Record<string, () => Task>> = {
|
|
151
159
|
module: {
|
|
@@ -187,6 +195,45 @@ const FUNCTORS: Record<string, Record<string, () => Task>> = {
|
|
|
187
195
|
createValidateEquals: () => CreateValidateTransformer.transform(true),
|
|
188
196
|
createRandom: () => CreateRandomTransformer.transform,
|
|
189
197
|
},
|
|
198
|
+
functional: {
|
|
199
|
+
// ASSERTIONS
|
|
200
|
+
assertFunction: () =>
|
|
201
|
+
FunctionalGenericTransformer.transform({
|
|
202
|
+
method: "assertFunction",
|
|
203
|
+
equals: false,
|
|
204
|
+
programmer: FunctionalAssertFunctionProgrammer.write,
|
|
205
|
+
}),
|
|
206
|
+
assertParameters: () =>
|
|
207
|
+
FunctionalGenericTransformer.transform({
|
|
208
|
+
method: "assertParameters",
|
|
209
|
+
equals: false,
|
|
210
|
+
programmer: FunctionalAssertParametersProgrammer.write,
|
|
211
|
+
}),
|
|
212
|
+
assertReturn: () =>
|
|
213
|
+
FunctionalGenericTransformer.transform({
|
|
214
|
+
method: "assertReturn",
|
|
215
|
+
equals: false,
|
|
216
|
+
programmer: FunctionAssertReturnProgrammer.write,
|
|
217
|
+
}),
|
|
218
|
+
assertEqualsFunction: () =>
|
|
219
|
+
FunctionalGenericTransformer.transform({
|
|
220
|
+
method: "assertEqualsFunction",
|
|
221
|
+
equals: true,
|
|
222
|
+
programmer: FunctionalAssertFunctionProgrammer.write,
|
|
223
|
+
}),
|
|
224
|
+
assertEqualsParameters: () =>
|
|
225
|
+
FunctionalGenericTransformer.transform({
|
|
226
|
+
method: "assertEqualsParameters",
|
|
227
|
+
equals: true,
|
|
228
|
+
programmer: FunctionalAssertParametersProgrammer.write,
|
|
229
|
+
}),
|
|
230
|
+
assertEqualsReturn: () =>
|
|
231
|
+
FunctionalGenericTransformer.transform({
|
|
232
|
+
method: "assertEqualsReturn",
|
|
233
|
+
equals: true,
|
|
234
|
+
programmer: FunctionAssertReturnProgrammer.write,
|
|
235
|
+
}),
|
|
236
|
+
},
|
|
190
237
|
http: {
|
|
191
238
|
// FORM-DATA
|
|
192
239
|
formData: () => HttpFormDataTransformer.transform,
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { IProject } from "../../IProject";
|
|
3
|
+
import { TransformerError } from "../../TransformerError";
|
|
4
|
+
|
|
5
|
+
export namespace FunctionalGenericTransformer {
|
|
6
|
+
export const transform =
|
|
7
|
+
(props: {
|
|
8
|
+
method: string;
|
|
9
|
+
programmer: (
|
|
10
|
+
project: IProject,
|
|
11
|
+
) => (
|
|
12
|
+
modulo: ts.LeftHandSideExpression,
|
|
13
|
+
) => (
|
|
14
|
+
equals: boolean,
|
|
15
|
+
) => (
|
|
16
|
+
expression: ts.CallExpression,
|
|
17
|
+
declaration: ts.FunctionDeclaration,
|
|
18
|
+
init?: ts.Expression,
|
|
19
|
+
) => ts.FunctionDeclaration;
|
|
20
|
+
equals: boolean;
|
|
21
|
+
}) =>
|
|
22
|
+
(project: IProject) =>
|
|
23
|
+
(modulo: ts.LeftHandSideExpression) =>
|
|
24
|
+
(expression: ts.CallExpression) => {
|
|
25
|
+
// CHECK PARAMETER
|
|
26
|
+
if (expression.arguments.length === 0)
|
|
27
|
+
throw new TransformerError({
|
|
28
|
+
code: `typia.functional.${props.method}`,
|
|
29
|
+
message: `no input value.`,
|
|
30
|
+
});
|
|
31
|
+
else if (false === ts.isFunctionDeclaration(expression.arguments[0]!))
|
|
32
|
+
throw new TransformerError({
|
|
33
|
+
code: `typia.functional.${props.method}`,
|
|
34
|
+
message: `input value is not a function.`,
|
|
35
|
+
});
|
|
36
|
+
return props.programmer(project)(modulo)(props.equals)(
|
|
37
|
+
expression,
|
|
38
|
+
expression.arguments[0]!,
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
}
|