@typia/transform 12.0.0-dev.20260307-2 → 12.0.0-dev.20260310
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/CallExpressionTransformer.js +8 -0
- package/lib/CallExpressionTransformer.js.map +1 -1
- package/lib/CallExpressionTransformer.mjs +8 -0
- package/lib/CallExpressionTransformer.mjs.map +1 -1
- package/lib/features/json/JsonApplicationTransformer.js +3 -4
- package/lib/features/json/JsonApplicationTransformer.js.map +1 -1
- package/lib/features/json/JsonApplicationTransformer.mjs +4 -5
- package/lib/features/json/JsonApplicationTransformer.mjs.map +1 -1
- package/lib/features/json/JsonSchemaTransformer.js +2 -8
- package/lib/features/json/JsonSchemaTransformer.js.map +1 -1
- package/lib/features/json/JsonSchemaTransformer.mjs +3 -9
- package/lib/features/json/JsonSchemaTransformer.mjs.map +1 -1
- package/lib/features/json/JsonSchemasTransformer.js +2 -8
- package/lib/features/json/JsonSchemasTransformer.js.map +1 -1
- package/lib/features/json/JsonSchemasTransformer.mjs +3 -9
- package/lib/features/json/JsonSchemasTransformer.mjs.map +1 -1
- package/lib/features/llm/LlmApplicationTransformer.js +56 -20
- package/lib/features/llm/LlmApplicationTransformer.js.map +1 -1
- package/lib/features/llm/LlmApplicationTransformer.mjs +56 -20
- package/lib/features/llm/LlmApplicationTransformer.mjs.map +1 -1
- package/lib/features/llm/LlmCoerceTransformer.d.ts +5 -0
- package/lib/features/llm/LlmCoerceTransformer.js +81 -0
- package/lib/features/llm/LlmCoerceTransformer.js.map +1 -0
- package/lib/features/llm/LlmCoerceTransformer.mjs +77 -0
- package/lib/features/llm/LlmCoerceTransformer.mjs.map +1 -0
- package/lib/features/llm/LlmControllerTransformer.js +12 -10
- package/lib/features/llm/LlmControllerTransformer.js.map +1 -1
- package/lib/features/llm/LlmControllerTransformer.mjs +12 -10
- package/lib/features/llm/LlmControllerTransformer.mjs.map +1 -1
- package/lib/features/llm/LlmCreateCoerceTransformer.d.ts +5 -0
- package/lib/features/llm/LlmCreateCoerceTransformer.js +75 -0
- package/lib/features/llm/LlmCreateCoerceTransformer.js.map +1 -0
- package/lib/features/llm/LlmCreateCoerceTransformer.mjs +71 -0
- package/lib/features/llm/LlmCreateCoerceTransformer.mjs.map +1 -0
- package/lib/features/llm/LlmCreateParseTransformer.d.ts +5 -0
- package/lib/features/llm/LlmCreateParseTransformer.js +75 -0
- package/lib/features/llm/LlmCreateParseTransformer.js.map +1 -0
- package/lib/features/llm/LlmCreateParseTransformer.mjs +71 -0
- package/lib/features/llm/LlmCreateParseTransformer.mjs.map +1 -0
- package/lib/features/llm/LlmParametersTransformer.d.ts +1 -1
- package/lib/features/llm/LlmParametersTransformer.js +3 -6
- package/lib/features/llm/LlmParametersTransformer.js.map +1 -1
- package/lib/features/llm/LlmParametersTransformer.mjs +4 -7
- package/lib/features/llm/LlmParametersTransformer.mjs.map +1 -1
- package/lib/features/llm/LlmParseTransformer.d.ts +5 -0
- package/lib/features/llm/LlmParseTransformer.js +81 -0
- package/lib/features/llm/LlmParseTransformer.js.map +1 -0
- package/lib/features/llm/LlmParseTransformer.mjs +77 -0
- package/lib/features/llm/LlmParseTransformer.mjs.map +1 -0
- package/lib/features/llm/LlmSchemaTransformer.d.ts +1 -1
- package/lib/features/llm/LlmSchemaTransformer.js +10 -25
- package/lib/features/llm/LlmSchemaTransformer.js.map +1 -1
- package/lib/features/llm/LlmSchemaTransformer.mjs +11 -26
- package/lib/features/llm/LlmSchemaTransformer.mjs.map +1 -1
- package/package.json +4 -4
- package/src/CallExpressionTransformer.ts +579 -571
- package/src/FileTransformer.ts +143 -143
- package/src/ITransformProps.ts +20 -20
- package/src/ImportTransformer.ts +262 -262
- package/src/NodeTransformer.ts +25 -25
- package/src/TransformerError.ts +85 -85
- package/src/TypiaGenerator.ts +172 -172
- package/src/features/AssertTransformer.ts +24 -24
- package/src/features/CreateAssertTransformer.ts +24 -24
- package/src/features/CreateIsTransformer.ts +18 -18
- package/src/features/CreateRandomTransformer.ts +42 -42
- package/src/features/CreateValidateTransformer.ts +18 -18
- package/src/features/IsTransformer.ts +18 -18
- package/src/features/RandomTransformer.ts +40 -40
- package/src/features/ValidateTransformer.ts +18 -18
- package/src/features/functional/FunctionalGenericTransformer.ts +55 -55
- package/src/features/http/CreateHttpAssertFormDataTransformer.ts +13 -13
- package/src/features/http/CreateHttpAssertHeadersTransformer.ts +13 -13
- package/src/features/http/CreateHttpAssertQueryTransformer.ts +13 -13
- package/src/features/http/CreateHttpFormDataTransformer.ts +13 -13
- package/src/features/http/CreateHttpHeadersTransformer.ts +13 -13
- package/src/features/http/CreateHttpIsFormDataTransformer.ts +13 -13
- package/src/features/http/CreateHttpIsHeadersTransformer.ts +13 -13
- package/src/features/http/CreateHttpIsQueryTransformer.ts +13 -13
- package/src/features/http/CreateHttpParameterTransformer.ts +13 -13
- package/src/features/http/CreateHttpQueryTransformer.ts +13 -13
- package/src/features/http/CreateHttpValidateFormDataTransformer.ts +13 -13
- package/src/features/http/CreateHttpValidateHeadersTransformer.ts +13 -13
- package/src/features/http/CreateHttpValidateQueryTransformer.ts +13 -13
- package/src/features/http/HttpAssertFormDataTransformer.ts +13 -13
- package/src/features/http/HttpAssertHeadersTransformer.ts +13 -13
- package/src/features/http/HttpAssertQueryTransformer.ts +13 -13
- package/src/features/http/HttpFormDataTransformer.ts +13 -13
- package/src/features/http/HttpHeadersTransformer.ts +13 -13
- package/src/features/http/HttpIsFormDataTransformer.ts +13 -13
- package/src/features/http/HttpIsHeadersTransformer.ts +13 -13
- package/src/features/http/HttpIsQueryTransformer.ts +13 -13
- package/src/features/http/HttpParameterTransformer.ts +13 -13
- package/src/features/http/HttpQueryTransformer.ts +13 -13
- package/src/features/http/HttpValidateFormDataTransformer.ts +13 -13
- package/src/features/http/HttpValidateHeadersTransformer.ts +13 -13
- package/src/features/http/HttpValidateQueryTransformer.ts +13 -13
- package/src/features/json/JsonApplicationTransformer.ts +99 -102
- package/src/features/json/JsonAssertParseTransformer.ts +13 -13
- package/src/features/json/JsonAssertStringifyTransformer.ts +13 -13
- package/src/features/json/JsonCreateAssertParseTransformer.ts +13 -13
- package/src/features/json/JsonCreateAssertStringifyTransformer.ts +13 -13
- package/src/features/json/JsonCreateIsParseTransformer.ts +13 -13
- package/src/features/json/JsonCreateIsStringifyTransformer.ts +13 -13
- package/src/features/json/JsonCreateStringifyTransformer.ts +13 -13
- package/src/features/json/JsonCreateValidateParseTransformer.ts +13 -13
- package/src/features/json/JsonCreateValidateStringifyProgrammer.ts +13 -13
- package/src/features/json/JsonIsParseTransformer.ts +13 -13
- package/src/features/json/JsonIsStringifyTransformer.ts +13 -13
- package/src/features/json/JsonSchemaTransformer.ts +120 -132
- package/src/features/json/JsonSchemasTransformer.ts +130 -142
- package/src/features/json/JsonStringifyTransformer.ts +13 -13
- package/src/features/json/JsonValidateParseTransformer.ts +13 -13
- package/src/features/json/JsonValidateStringifyTransformer.ts +13 -13
- package/src/features/llm/LlmApplicationTransformer.ts +224 -184
- package/src/features/llm/LlmCoerceTransformer.ts +95 -0
- package/src/features/llm/LlmControllerTransformer.ts +81 -76
- package/src/features/llm/LlmCreateCoerceTransformer.ts +84 -0
- package/src/features/llm/LlmCreateParseTransformer.ts +84 -0
- package/src/features/llm/LlmParametersTransformer.ts +76 -88
- package/src/features/llm/LlmParseTransformer.ts +95 -0
- package/src/features/llm/LlmSchemaTransformer.ts +87 -133
- package/src/features/misc/MiscAssertCloneTransformer.ts +13 -13
- package/src/features/misc/MiscAssertPruneTransformer.ts +13 -13
- package/src/features/misc/MiscCloneTransformer.ts +13 -13
- package/src/features/misc/MiscCreateAssertCloneTransformer.ts +13 -13
- package/src/features/misc/MiscCreateAssertPruneTransformer.ts +13 -13
- package/src/features/misc/MiscCreateCloneTransformer.ts +13 -13
- package/src/features/misc/MiscCreateIsCloneTransformer.ts +13 -13
- package/src/features/misc/MiscCreateIsPruneTransformer.ts +13 -13
- package/src/features/misc/MiscCreatePruneTransformer.ts +13 -13
- package/src/features/misc/MiscCreateValidateCloneTransformer.ts +13 -13
- package/src/features/misc/MiscCreateValidatePruneTransformer.ts +13 -13
- package/src/features/misc/MiscIsCloneTransformer.ts +13 -13
- package/src/features/misc/MiscIsPruneTransformer.ts +13 -13
- package/src/features/misc/MiscLiteralsTransformer.ts +34 -34
- package/src/features/misc/MiscPruneTransformer.ts +13 -13
- package/src/features/misc/MiscValidateCloneTransformer.ts +13 -13
- package/src/features/misc/MiscValidatePruneTransformer.ts +13 -13
- package/src/features/notations/NotationAssertGeneralTransformer.ts +19 -19
- package/src/features/notations/NotationCreateAssertGeneralTransformer.ts +19 -19
- package/src/features/notations/NotationCreateGeneralTransformer.ts +19 -19
- package/src/features/notations/NotationCreateIsGeneralTransformer.ts +19 -19
- package/src/features/notations/NotationCreateValidateGeneralTransformer.ts +19 -19
- package/src/features/notations/NotationGeneralTransformer.ts +18 -18
- package/src/features/notations/NotationIsGeneralTransformer.ts +19 -19
- package/src/features/notations/NotationValidateGeneralTransformer.ts +19 -19
- package/src/features/protobuf/ProtobufAssertDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufAssertEncodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateEncodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufEncodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufIsDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufIsEncodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufMessageTransformer.ts +34 -34
- package/src/features/protobuf/ProtobufValidateDecodeTransformer.ts +13 -13
- package/src/features/protobuf/ProtobufValidateEncodeTransformer.ts +13 -13
- package/src/features/reflect/ReflectMetadataTransformer.ts +69 -69
- package/src/features/reflect/ReflectNameTransformer.ts +81 -81
- package/src/features/reflect/ReflectSchemaTransformer.ts +65 -65
- package/src/features/reflect/ReflectSchemasTransformer.ts +69 -69
- package/src/index.ts +7 -7
- package/src/internal/GenericTransformer.ts +101 -101
- package/src/transform.ts +68 -68
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LiteralFactory,
|
|
3
|
-
MetadataCollection,
|
|
4
|
-
MetadataFactory,
|
|
5
|
-
MetadataSchema,
|
|
6
|
-
} from "@typia/core";
|
|
7
|
-
import { IMetadataSchemaCollection } from "@typia/interface";
|
|
8
|
-
import ts from "typescript";
|
|
9
|
-
|
|
10
|
-
import { ITransformProps } from "../../ITransformProps";
|
|
11
|
-
import { TransformerError } from "../../TransformerError";
|
|
12
|
-
|
|
13
|
-
export namespace ReflectSchemasTransformer {
|
|
14
|
-
export const transform = (
|
|
15
|
-
props: Pick<ITransformProps, "context" | "expression">,
|
|
16
|
-
): ts.Expression => {
|
|
17
|
-
if (!props.expression.typeArguments?.length)
|
|
18
|
-
throw new TransformerError({
|
|
19
|
-
code: "typia.reflect.schemas",
|
|
20
|
-
message: "no generic argument.",
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// VALIDATE TUPLE ARGUMENTS
|
|
24
|
-
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
25
|
-
if (!ts.isTupleTypeNode(top)) return props.expression;
|
|
26
|
-
else if (top.elements.some((child) => !ts.isTypeNode(child)))
|
|
27
|
-
return props.expression;
|
|
28
|
-
|
|
29
|
-
// GET TYPES
|
|
30
|
-
const types: ts.Type[] = top.elements.map((child) =>
|
|
31
|
-
props.context.checker.getTypeFromTypeNode(child as ts.TypeNode),
|
|
32
|
-
);
|
|
33
|
-
if (types.some((t) => t.isTypeParameter()))
|
|
34
|
-
throw new TransformerError({
|
|
35
|
-
code: "typia.reflect.schemas",
|
|
36
|
-
message: "non-specified generic argument(s).",
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// METADATA
|
|
40
|
-
const components: MetadataCollection = new MetadataCollection();
|
|
41
|
-
const schemas: Array<MetadataSchema> = types.map((type) => {
|
|
42
|
-
const result = MetadataFactory.analyze({
|
|
43
|
-
checker: props.context.checker,
|
|
44
|
-
transformer: props.context.transformer,
|
|
45
|
-
options: {
|
|
46
|
-
escape: true,
|
|
47
|
-
constant: true,
|
|
48
|
-
absorb: true,
|
|
49
|
-
functional: true,
|
|
50
|
-
},
|
|
51
|
-
components: components,
|
|
52
|
-
type,
|
|
53
|
-
});
|
|
54
|
-
if (result.success === false)
|
|
55
|
-
throw TransformerError.from({
|
|
56
|
-
code: "typia.reflect.schemas",
|
|
57
|
-
errors: result.errors,
|
|
58
|
-
});
|
|
59
|
-
return result.data;
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// CONVERT TO PRIMITIVE TYPE
|
|
63
|
-
const collection: IMetadataSchemaCollection = {
|
|
64
|
-
schemas: schemas.map((s) => s.toJSON()),
|
|
65
|
-
components: components.toJSON(),
|
|
66
|
-
};
|
|
67
|
-
return LiteralFactory.write(collection);
|
|
68
|
-
};
|
|
69
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
LiteralFactory,
|
|
3
|
+
MetadataCollection,
|
|
4
|
+
MetadataFactory,
|
|
5
|
+
MetadataSchema,
|
|
6
|
+
} from "@typia/core";
|
|
7
|
+
import { IMetadataSchemaCollection } from "@typia/interface";
|
|
8
|
+
import ts from "typescript";
|
|
9
|
+
|
|
10
|
+
import { ITransformProps } from "../../ITransformProps";
|
|
11
|
+
import { TransformerError } from "../../TransformerError";
|
|
12
|
+
|
|
13
|
+
export namespace ReflectSchemasTransformer {
|
|
14
|
+
export const transform = (
|
|
15
|
+
props: Pick<ITransformProps, "context" | "expression">,
|
|
16
|
+
): ts.Expression => {
|
|
17
|
+
if (!props.expression.typeArguments?.length)
|
|
18
|
+
throw new TransformerError({
|
|
19
|
+
code: "typia.reflect.schemas",
|
|
20
|
+
message: "no generic argument.",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// VALIDATE TUPLE ARGUMENTS
|
|
24
|
+
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
25
|
+
if (!ts.isTupleTypeNode(top)) return props.expression;
|
|
26
|
+
else if (top.elements.some((child) => !ts.isTypeNode(child)))
|
|
27
|
+
return props.expression;
|
|
28
|
+
|
|
29
|
+
// GET TYPES
|
|
30
|
+
const types: ts.Type[] = top.elements.map((child) =>
|
|
31
|
+
props.context.checker.getTypeFromTypeNode(child as ts.TypeNode),
|
|
32
|
+
);
|
|
33
|
+
if (types.some((t) => t.isTypeParameter()))
|
|
34
|
+
throw new TransformerError({
|
|
35
|
+
code: "typia.reflect.schemas",
|
|
36
|
+
message: "non-specified generic argument(s).",
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// METADATA
|
|
40
|
+
const components: MetadataCollection = new MetadataCollection();
|
|
41
|
+
const schemas: Array<MetadataSchema> = types.map((type) => {
|
|
42
|
+
const result = MetadataFactory.analyze({
|
|
43
|
+
checker: props.context.checker,
|
|
44
|
+
transformer: props.context.transformer,
|
|
45
|
+
options: {
|
|
46
|
+
escape: true,
|
|
47
|
+
constant: true,
|
|
48
|
+
absorb: true,
|
|
49
|
+
functional: true,
|
|
50
|
+
},
|
|
51
|
+
components: components,
|
|
52
|
+
type,
|
|
53
|
+
});
|
|
54
|
+
if (result.success === false)
|
|
55
|
+
throw TransformerError.from({
|
|
56
|
+
code: "typia.reflect.schemas",
|
|
57
|
+
errors: result.errors,
|
|
58
|
+
});
|
|
59
|
+
return result.data;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// CONVERT TO PRIMITIVE TYPE
|
|
63
|
+
const collection: IMetadataSchemaCollection = {
|
|
64
|
+
schemas: schemas.map((s) => s.toJSON()),
|
|
65
|
+
components: components.toJSON(),
|
|
66
|
+
};
|
|
67
|
+
return LiteralFactory.write(collection);
|
|
68
|
+
};
|
|
69
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { transform } from "./transform";
|
|
2
|
-
|
|
3
|
-
export default transform;
|
|
4
|
-
|
|
5
|
-
export * from "./ImportTransformer";
|
|
6
|
-
export * from "./TypiaGenerator";
|
|
7
|
-
export * from "./transform";
|
|
1
|
+
import { transform } from "./transform";
|
|
2
|
+
|
|
3
|
+
export default transform;
|
|
4
|
+
|
|
5
|
+
export * from "./ImportTransformer";
|
|
6
|
+
export * from "./TypiaGenerator";
|
|
7
|
+
export * from "./transform";
|
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import { IProgrammerProps } from "@typia/core";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import { ITransformProps } from "../ITransformProps";
|
|
5
|
-
import { TransformerError } from "../TransformerError";
|
|
6
|
-
|
|
7
|
-
export namespace GenericTransformer {
|
|
8
|
-
export interface IProps extends ITransformProps {
|
|
9
|
-
method: string;
|
|
10
|
-
write: (props: IProgrammerProps) => ts.Expression | ts.ArrowFunction;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const scalar = (props: IProps) => {
|
|
14
|
-
// CHECK PARAMETER
|
|
15
|
-
if (props.expression.arguments.length === 0)
|
|
16
|
-
throw new TransformerError({
|
|
17
|
-
code: `typia.${props.method}`,
|
|
18
|
-
message: `no input value.`,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
// GET TYPE INFO
|
|
22
|
-
const [type, node, generic]: [ts.Type, ts.Node, boolean] =
|
|
23
|
-
props.expression.typeArguments && props.expression.typeArguments[0]
|
|
24
|
-
? [
|
|
25
|
-
props.context.checker.getTypeFromTypeNode(
|
|
26
|
-
props.expression.typeArguments[0],
|
|
27
|
-
),
|
|
28
|
-
props.expression.typeArguments[0],
|
|
29
|
-
true,
|
|
30
|
-
]
|
|
31
|
-
: [
|
|
32
|
-
props.context.checker.getTypeAtLocation(
|
|
33
|
-
props.expression.arguments[0]!,
|
|
34
|
-
),
|
|
35
|
-
props.expression.arguments[0]!,
|
|
36
|
-
false,
|
|
37
|
-
];
|
|
38
|
-
if (type.isTypeParameter())
|
|
39
|
-
throw new TransformerError({
|
|
40
|
-
code: `typia.${props.method}`,
|
|
41
|
-
message: `non-specified generic argument.`,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// DO TRANSFORM
|
|
45
|
-
return ts.factory.createCallExpression(
|
|
46
|
-
props.write({
|
|
47
|
-
context: props.context,
|
|
48
|
-
modulo: props.modulo,
|
|
49
|
-
type,
|
|
50
|
-
name: generic
|
|
51
|
-
? node.getFullText().trim()
|
|
52
|
-
: getTypeName({
|
|
53
|
-
checker: props.context.checker,
|
|
54
|
-
type,
|
|
55
|
-
node,
|
|
56
|
-
}),
|
|
57
|
-
}),
|
|
58
|
-
undefined,
|
|
59
|
-
props.expression.arguments,
|
|
60
|
-
);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export const factory = (props: IProps) => {
|
|
64
|
-
// CHECK GENERIC ARGUMENT EXISTENCE
|
|
65
|
-
if (!props.expression.typeArguments?.[0])
|
|
66
|
-
throw new TransformerError({
|
|
67
|
-
code: `typia.${props.method}`,
|
|
68
|
-
message: `generic argument is not specified.`,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// GET TYPE INFO
|
|
72
|
-
const node: ts.TypeNode = props.expression.typeArguments[0];
|
|
73
|
-
const type: ts.Type = props.context.checker.getTypeFromTypeNode(node);
|
|
74
|
-
|
|
75
|
-
if (type.isTypeParameter())
|
|
76
|
-
throw new TransformerError({
|
|
77
|
-
code: `typia.${props.method}`,
|
|
78
|
-
message: `non-specified generic argument.`,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// DO TRANSFORM
|
|
82
|
-
return props.write({
|
|
83
|
-
context: props.context,
|
|
84
|
-
modulo: props.modulo,
|
|
85
|
-
type,
|
|
86
|
-
name: node.getFullText().trim(),
|
|
87
|
-
init: props.expression.arguments[0],
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const getTypeName = (props: {
|
|
92
|
-
checker: ts.TypeChecker;
|
|
93
|
-
type: ts.Type;
|
|
94
|
-
node: ts.Node;
|
|
95
|
-
}): string =>
|
|
96
|
-
props.checker.typeToString(
|
|
97
|
-
props.type,
|
|
98
|
-
props.node,
|
|
99
|
-
ts.TypeFormatFlags.NodeBuilderFlagsMask,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
1
|
+
import { IProgrammerProps } from "@typia/core";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { ITransformProps } from "../ITransformProps";
|
|
5
|
+
import { TransformerError } from "../TransformerError";
|
|
6
|
+
|
|
7
|
+
export namespace GenericTransformer {
|
|
8
|
+
export interface IProps extends ITransformProps {
|
|
9
|
+
method: string;
|
|
10
|
+
write: (props: IProgrammerProps) => ts.Expression | ts.ArrowFunction;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const scalar = (props: IProps) => {
|
|
14
|
+
// CHECK PARAMETER
|
|
15
|
+
if (props.expression.arguments.length === 0)
|
|
16
|
+
throw new TransformerError({
|
|
17
|
+
code: `typia.${props.method}`,
|
|
18
|
+
message: `no input value.`,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// GET TYPE INFO
|
|
22
|
+
const [type, node, generic]: [ts.Type, ts.Node, boolean] =
|
|
23
|
+
props.expression.typeArguments && props.expression.typeArguments[0]
|
|
24
|
+
? [
|
|
25
|
+
props.context.checker.getTypeFromTypeNode(
|
|
26
|
+
props.expression.typeArguments[0],
|
|
27
|
+
),
|
|
28
|
+
props.expression.typeArguments[0],
|
|
29
|
+
true,
|
|
30
|
+
]
|
|
31
|
+
: [
|
|
32
|
+
props.context.checker.getTypeAtLocation(
|
|
33
|
+
props.expression.arguments[0]!,
|
|
34
|
+
),
|
|
35
|
+
props.expression.arguments[0]!,
|
|
36
|
+
false,
|
|
37
|
+
];
|
|
38
|
+
if (type.isTypeParameter())
|
|
39
|
+
throw new TransformerError({
|
|
40
|
+
code: `typia.${props.method}`,
|
|
41
|
+
message: `non-specified generic argument.`,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// DO TRANSFORM
|
|
45
|
+
return ts.factory.createCallExpression(
|
|
46
|
+
props.write({
|
|
47
|
+
context: props.context,
|
|
48
|
+
modulo: props.modulo,
|
|
49
|
+
type,
|
|
50
|
+
name: generic
|
|
51
|
+
? node.getFullText().trim()
|
|
52
|
+
: getTypeName({
|
|
53
|
+
checker: props.context.checker,
|
|
54
|
+
type,
|
|
55
|
+
node,
|
|
56
|
+
}),
|
|
57
|
+
}),
|
|
58
|
+
undefined,
|
|
59
|
+
props.expression.arguments,
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const factory = (props: IProps) => {
|
|
64
|
+
// CHECK GENERIC ARGUMENT EXISTENCE
|
|
65
|
+
if (!props.expression.typeArguments?.[0])
|
|
66
|
+
throw new TransformerError({
|
|
67
|
+
code: `typia.${props.method}`,
|
|
68
|
+
message: `generic argument is not specified.`,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// GET TYPE INFO
|
|
72
|
+
const node: ts.TypeNode = props.expression.typeArguments[0];
|
|
73
|
+
const type: ts.Type = props.context.checker.getTypeFromTypeNode(node);
|
|
74
|
+
|
|
75
|
+
if (type.isTypeParameter())
|
|
76
|
+
throw new TransformerError({
|
|
77
|
+
code: `typia.${props.method}`,
|
|
78
|
+
message: `non-specified generic argument.`,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// DO TRANSFORM
|
|
82
|
+
return props.write({
|
|
83
|
+
context: props.context,
|
|
84
|
+
modulo: props.modulo,
|
|
85
|
+
type,
|
|
86
|
+
name: node.getFullText().trim(),
|
|
87
|
+
init: props.expression.arguments[0],
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const getTypeName = (props: {
|
|
92
|
+
checker: ts.TypeChecker;
|
|
93
|
+
type: ts.Type;
|
|
94
|
+
node: ts.Node;
|
|
95
|
+
}): string =>
|
|
96
|
+
props.checker.typeToString(
|
|
97
|
+
props.type,
|
|
98
|
+
props.node,
|
|
99
|
+
ts.TypeFormatFlags.NodeBuilderFlagsMask,
|
|
100
|
+
);
|
|
101
|
+
}
|
package/src/transform.ts
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { ITransformOptions, ITypiaContext } from "@typia/core";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import { FileTransformer } from "./FileTransformer";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* TypeScript transformer for typia runtime code generation.
|
|
8
|
-
*
|
|
9
|
-
* `transform` is the entry point for typia's compile-time transformation. It
|
|
10
|
-
* converts `typia.*<T>()` function calls (e.g., `typia.is<User>(data)`) into
|
|
11
|
-
* optimized runtime validation, serialization, or transformation code.
|
|
12
|
-
*
|
|
13
|
-
* The transformer analyzes the generic type parameter `T` at compile time and
|
|
14
|
-
* generates specialized code that performs type checking without runtime
|
|
15
|
-
* reflection. This approach provides both type safety and high performance.
|
|
16
|
-
*
|
|
17
|
-
* **Requirements:**
|
|
18
|
-
*
|
|
19
|
-
* - TypeScript's `strictNullChecks` or `strict` compiler option must be enabled
|
|
20
|
-
* - The transformer must be configured in `tsconfig.json` via ts-patch or
|
|
21
|
-
* ttypescript
|
|
22
|
-
*
|
|
23
|
-
* **Configuration example (tsconfig.json):**
|
|
24
|
-
*
|
|
25
|
-
* ```json
|
|
26
|
-
* {
|
|
27
|
-
* "compilerOptions": {
|
|
28
|
-
* "strict": true,
|
|
29
|
-
* "plugins": [{ "transform": "typia/lib/transform" }]
|
|
30
|
-
* }
|
|
31
|
-
* }
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
35
|
-
* @param program TypeScript program instance from the compiler
|
|
36
|
-
* @param options Optional transformer configuration (finite, numeric,
|
|
37
|
-
* functional, undefined)
|
|
38
|
-
* @param extras Diagnostic utilities for error reporting during transformation
|
|
39
|
-
* @returns Transformer factory that processes source files
|
|
40
|
-
*/
|
|
41
|
-
export const transform = (
|
|
42
|
-
program: ts.Program,
|
|
43
|
-
options: ITransformOptions | undefined,
|
|
44
|
-
extras: ITypiaContext["extras"],
|
|
45
|
-
): ts.TransformerFactory<ts.SourceFile> => {
|
|
46
|
-
const compilerOptions: ts.CompilerOptions = program.getCompilerOptions();
|
|
47
|
-
const strict: boolean =
|
|
48
|
-
compilerOptions.strictNullChecks !== undefined
|
|
49
|
-
? !!compilerOptions.strictNullChecks
|
|
50
|
-
: !!compilerOptions.strict;
|
|
51
|
-
if (strict === false)
|
|
52
|
-
extras.addDiagnostic({
|
|
53
|
-
category: ts.DiagnosticCategory.Error,
|
|
54
|
-
code: "(typia)" as any,
|
|
55
|
-
file: undefined,
|
|
56
|
-
start: undefined,
|
|
57
|
-
length: undefined,
|
|
58
|
-
messageText: "strict mode is required.",
|
|
59
|
-
});
|
|
60
|
-
return FileTransformer.transform({
|
|
61
|
-
program,
|
|
62
|
-
compilerOptions,
|
|
63
|
-
checker: program.getTypeChecker(),
|
|
64
|
-
printer: ts.createPrinter(),
|
|
65
|
-
options: options ?? {},
|
|
66
|
-
extras,
|
|
67
|
-
});
|
|
68
|
-
};
|
|
1
|
+
import { ITransformOptions, ITypiaContext } from "@typia/core";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { FileTransformer } from "./FileTransformer";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* TypeScript transformer for typia runtime code generation.
|
|
8
|
+
*
|
|
9
|
+
* `transform` is the entry point for typia's compile-time transformation. It
|
|
10
|
+
* converts `typia.*<T>()` function calls (e.g., `typia.is<User>(data)`) into
|
|
11
|
+
* optimized runtime validation, serialization, or transformation code.
|
|
12
|
+
*
|
|
13
|
+
* The transformer analyzes the generic type parameter `T` at compile time and
|
|
14
|
+
* generates specialized code that performs type checking without runtime
|
|
15
|
+
* reflection. This approach provides both type safety and high performance.
|
|
16
|
+
*
|
|
17
|
+
* **Requirements:**
|
|
18
|
+
*
|
|
19
|
+
* - TypeScript's `strictNullChecks` or `strict` compiler option must be enabled
|
|
20
|
+
* - The transformer must be configured in `tsconfig.json` via ts-patch or
|
|
21
|
+
* ttypescript
|
|
22
|
+
*
|
|
23
|
+
* **Configuration example (tsconfig.json):**
|
|
24
|
+
*
|
|
25
|
+
* ```json
|
|
26
|
+
* {
|
|
27
|
+
* "compilerOptions": {
|
|
28
|
+
* "strict": true,
|
|
29
|
+
* "plugins": [{ "transform": "typia/lib/transform" }]
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
35
|
+
* @param program TypeScript program instance from the compiler
|
|
36
|
+
* @param options Optional transformer configuration (finite, numeric,
|
|
37
|
+
* functional, undefined)
|
|
38
|
+
* @param extras Diagnostic utilities for error reporting during transformation
|
|
39
|
+
* @returns Transformer factory that processes source files
|
|
40
|
+
*/
|
|
41
|
+
export const transform = (
|
|
42
|
+
program: ts.Program,
|
|
43
|
+
options: ITransformOptions | undefined,
|
|
44
|
+
extras: ITypiaContext["extras"],
|
|
45
|
+
): ts.TransformerFactory<ts.SourceFile> => {
|
|
46
|
+
const compilerOptions: ts.CompilerOptions = program.getCompilerOptions();
|
|
47
|
+
const strict: boolean =
|
|
48
|
+
compilerOptions.strictNullChecks !== undefined
|
|
49
|
+
? !!compilerOptions.strictNullChecks
|
|
50
|
+
: !!compilerOptions.strict;
|
|
51
|
+
if (strict === false)
|
|
52
|
+
extras.addDiagnostic({
|
|
53
|
+
category: ts.DiagnosticCategory.Error,
|
|
54
|
+
code: "(typia)" as any,
|
|
55
|
+
file: undefined,
|
|
56
|
+
start: undefined,
|
|
57
|
+
length: undefined,
|
|
58
|
+
messageText: "strict mode is required.",
|
|
59
|
+
});
|
|
60
|
+
return FileTransformer.transform({
|
|
61
|
+
program,
|
|
62
|
+
compilerOptions,
|
|
63
|
+
checker: program.getTypeChecker(),
|
|
64
|
+
printer: ts.createPrinter(),
|
|
65
|
+
options: options ?? {},
|
|
66
|
+
extras,
|
|
67
|
+
});
|
|
68
|
+
};
|