typia 5.5.0-dev.20240302 → 5.5.0-dev.20240303-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 (28) hide show
  1. package/lib/functional.d.ts +353 -0
  2. package/lib/functional.js +130 -0
  3. package/lib/functional.js.map +1 -0
  4. package/lib/module.d.ts +1 -0
  5. package/lib/module.js +2 -1
  6. package/lib/module.js.map +1 -1
  7. package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.d.ts +6 -0
  8. package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.js +33 -0
  9. package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.js.map +1 -0
  10. package/lib/programmers/functional/FunctionalAssertParametersProgrammer.d.ts +11 -0
  11. package/lib/programmers/functional/FunctionalAssertParametersProgrammer.js +58 -0
  12. package/lib/programmers/functional/FunctionalAssertParametersProgrammer.js.map +1 -0
  13. package/lib/programmers/functional/FunctionalAssertReturnProgrammer.d.ts +14 -0
  14. package/lib/programmers/functional/FunctionalAssertReturnProgrammer.js +67 -0
  15. package/lib/programmers/functional/FunctionalAssertReturnProgrammer.js.map +1 -0
  16. package/lib/transformers/CallExpressionTransformer.js +49 -0
  17. package/lib/transformers/CallExpressionTransformer.js.map +1 -1
  18. package/lib/transformers/features/functional/FunctionalGenericTransformer.d.ts +10 -0
  19. package/lib/transformers/features/functional/FunctionalGenericTransformer.js +60 -0
  20. package/lib/transformers/features/functional/FunctionalGenericTransformer.js.map +1 -0
  21. package/package.json +1 -1
  22. package/src/functional.ts +561 -0
  23. package/src/module.ts +1 -0
  24. package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +43 -0
  25. package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +99 -0
  26. package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +83 -0
  27. package/src/transformers/CallExpressionTransformer.ts +43 -0
  28. package/src/transformers/features/functional/FunctionalGenericTransformer.ts +60 -0
@@ -0,0 +1,99 @@
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
+ ): ts.ArrowFunction => {
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.createArrowFunction(
29
+ async
30
+ ? [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)]
31
+ : undefined,
32
+ undefined,
33
+ declaration.parameters,
34
+ declaration.type,
35
+ undefined,
36
+ ts.factory.createBlock(
37
+ [
38
+ assert,
39
+ call,
40
+ ts.factory.createReturnStatement(
41
+ ts.factory.createCallExpression(
42
+ expression,
43
+ undefined,
44
+ declaration.parameters.map((p) =>
45
+ ts.factory.createIdentifier(p.name.getText()),
46
+ ),
47
+ ),
48
+ ),
49
+ ],
50
+ true,
51
+ ),
52
+ );
53
+ };
54
+
55
+ export const prepare =
56
+ (project: IProject) =>
57
+ (modulo: ts.LeftHandSideExpression) =>
58
+ (equals: boolean) =>
59
+ (declaration: ts.FunctionDeclaration, init?: ts.Expression) => {
60
+ const typeNode: ts.TypeNode = ts.factory.createTypeLiteralNode([
61
+ ts.factory.createPropertySignature(
62
+ undefined,
63
+ "parameters",
64
+ undefined,
65
+ ts.factory.createTupleTypeNode(
66
+ declaration.parameters.map((p) => p.type!),
67
+ ),
68
+ ),
69
+ ]);
70
+ const type: ts.Type = project.checker.getTypeFromTypeNode(typeNode);
71
+
72
+ const assert = StatementFactory.constant(
73
+ "assert",
74
+ AssertProgrammer.write(project)(modulo)(equals)(type, undefined, init),
75
+ );
76
+ const call = ts.factory.createExpressionStatement(
77
+ ts.factory.createCallExpression(
78
+ ts.factory.createIdentifier("assert"),
79
+ undefined,
80
+ [
81
+ ts.factory.createObjectLiteralExpression(
82
+ [
83
+ ts.factory.createPropertyAssignment(
84
+ "parameters",
85
+ ts.factory.createArrayLiteralExpression(
86
+ declaration.parameters.map((p) =>
87
+ ts.factory.createIdentifier(p.name.getText()),
88
+ ),
89
+ ),
90
+ ),
91
+ ],
92
+ true,
93
+ ),
94
+ ],
95
+ ),
96
+ );
97
+ return { assert, call, type };
98
+ };
99
+ }
@@ -0,0 +1,83 @@
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
+ ): ts.ArrowFunction => {
16
+ const { async, assert, variable, call, returns } = prepare(project)(
17
+ modulo,
18
+ )(equals)(expression, declaration, init);
19
+ return ts.factory.createArrowFunction(
20
+ async
21
+ ? [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)]
22
+ : undefined,
23
+ undefined,
24
+ declaration.parameters,
25
+ declaration.type,
26
+ undefined,
27
+ ts.factory.createBlock([assert, variable, call, returns], true),
28
+ );
29
+ };
30
+
31
+ export const prepare =
32
+ (project: IProject) =>
33
+ (modulo: ts.LeftHandSideExpression) =>
34
+ (equals: boolean) =>
35
+ (
36
+ expression: ts.Expression,
37
+ declaration: ts.FunctionDeclaration,
38
+ init?: ts.Expression,
39
+ ) => {
40
+ const [type, async]: [ts.Type, boolean] = (() => {
41
+ const type: ts.Type = project.checker.getTypeFromTypeNode(
42
+ declaration.type!,
43
+ );
44
+ return type.isTypeParameter() && type.symbol.name === "Promise"
45
+ ? [type.aliasTypeArguments![0]!, true]
46
+ : [type, false];
47
+ })();
48
+ const assert = StatementFactory.constant(
49
+ "assert",
50
+ AssertProgrammer.write(project)(modulo)(equals)(type, undefined, init),
51
+ );
52
+ const funcCaller = ts.factory.createCallExpression(
53
+ expression,
54
+ undefined,
55
+ declaration.parameters.map((p) =>
56
+ ts.factory.createIdentifier(p.name.getText()),
57
+ ),
58
+ );
59
+ const variable = StatementFactory.constant(
60
+ "output",
61
+ async ? ts.factory.createAwaitExpression(funcCaller) : funcCaller,
62
+ );
63
+
64
+ const call = ts.factory.createExpressionStatement(
65
+ ts.factory.createCallExpression(
66
+ ts.factory.createIdentifier("assert"),
67
+ undefined,
68
+ [
69
+ ts.factory.createObjectLiteralExpression([
70
+ ts.factory.createPropertyAssignment(
71
+ "return",
72
+ ts.factory.createIdentifier("output"),
73
+ ),
74
+ ]),
75
+ ],
76
+ ),
77
+ );
78
+ const returns = ts.factory.createReturnStatement(
79
+ ts.factory.createIdentifier("output"),
80
+ );
81
+ return { async, type, assert, variable, call, returns };
82
+ };
83
+ }
@@ -96,6 +96,10 @@ 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 =
@@ -187,6 +191,45 @@ const FUNCTORS: Record<string, Record<string, () => Task>> = {
187
191
  createValidateEquals: () => CreateValidateTransformer.transform(true),
188
192
  createRandom: () => CreateRandomTransformer.transform,
189
193
  },
194
+ functional: {
195
+ // ASSERTIONS
196
+ assertFunction: () =>
197
+ FunctionalGenericTransformer.transform({
198
+ method: "assertFunction",
199
+ equals: false,
200
+ programmer: FunctionalAssertFunctionProgrammer.write,
201
+ }),
202
+ assertParameters: () =>
203
+ FunctionalGenericTransformer.transform({
204
+ method: "assertParameters",
205
+ equals: false,
206
+ programmer: FunctionalAssertParametersProgrammer.write,
207
+ }),
208
+ assertReturn: () =>
209
+ FunctionalGenericTransformer.transform({
210
+ method: "assertReturn",
211
+ equals: false,
212
+ programmer: FunctionAssertReturnProgrammer.write,
213
+ }),
214
+ assertEqualsFunction: () =>
215
+ FunctionalGenericTransformer.transform({
216
+ method: "assertEqualsFunction",
217
+ equals: true,
218
+ programmer: FunctionalAssertFunctionProgrammer.write,
219
+ }),
220
+ assertEqualsParameters: () =>
221
+ FunctionalGenericTransformer.transform({
222
+ method: "assertEqualsParameters",
223
+ equals: true,
224
+ programmer: FunctionalAssertParametersProgrammer.write,
225
+ }),
226
+ assertEqualsReturn: () =>
227
+ FunctionalGenericTransformer.transform({
228
+ method: "assertEqualsReturn",
229
+ equals: true,
230
+ programmer: FunctionAssertReturnProgrammer.write,
231
+ }),
232
+ },
190
233
  http: {
191
234
  // FORM-DATA
192
235
  formData: () => HttpFormDataTransformer.transform,
@@ -0,0 +1,60 @@
1
+ import ts from "typescript";
2
+ import { IProject } from "../../IProject";
3
+ import { TransformerError } from "../../TransformerError";
4
+ import { TypeFactory } from "../../../factories/TypeFactory";
5
+
6
+ export namespace FunctionalGenericTransformer {
7
+ export const transform =
8
+ (props: {
9
+ method: string;
10
+ programmer: (
11
+ project: IProject,
12
+ ) => (
13
+ modulo: ts.LeftHandSideExpression,
14
+ ) => (
15
+ equals: boolean,
16
+ ) => (
17
+ expression: ts.CallExpression,
18
+ declaration: ts.FunctionDeclaration,
19
+ init?: ts.Expression,
20
+ ) => ts.Expression;
21
+ equals: boolean;
22
+ }) =>
23
+ (project: IProject) =>
24
+ (modulo: ts.LeftHandSideExpression) =>
25
+ (expression: ts.CallExpression) => {
26
+ // CHECK PARAMETER
27
+ if (expression.arguments.length === 0)
28
+ throw new TransformerError({
29
+ code: `typia.functional.${props.method}`,
30
+ message: `no input value.`,
31
+ });
32
+
33
+ // GET TYPE INFO
34
+ const [type, generic]: [ts.Type, boolean] =
35
+ expression.typeArguments && expression.typeArguments[0]
36
+ ? [
37
+ project.checker.getTypeFromTypeNode(expression.typeArguments[0]),
38
+ true,
39
+ ]
40
+ : [
41
+ project.checker.getTypeAtLocation(expression.arguments[0]!),
42
+ false,
43
+ ];
44
+ if (generic === true)
45
+ throw new TransformerError({
46
+ code: `typia.functional.${props.method}`,
47
+ message: `non-specified generic argument.`,
48
+ });
49
+ else if (TypeFactory.isFunction(type) === false)
50
+ throw new TransformerError({
51
+ code: `typia.functional.${props.method}`,
52
+ message: `input value is not a function.`,
53
+ });
54
+ return props.programmer(project)(modulo)(props.equals)(
55
+ expression,
56
+ type.symbol!.declarations![0] as ts.FunctionDeclaration,
57
+ expression.arguments[1],
58
+ );
59
+ };
60
+ }