@typia/transform 12.0.1 → 12.1.0-dev.20260325

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,143 +1,143 @@
1
- import { ITypiaContext, ImportProgrammer } from "@typia/core";
2
- import { Singleton } from "@typia/utils";
3
- import ts from "typescript";
4
-
5
- import { NodeTransformer } from "./NodeTransformer";
6
- import { TransformerError } from "./TransformerError";
7
-
8
- /**
9
- * TypeScript source file transformer for typia.
10
- *
11
- * Entry point for typia's compile-time transformation. Traverses AST nodes,
12
- * transforms `typia.*` function calls into optimized runtime code, and injects
13
- * required imports. Skips declaration files.
14
- *
15
- * @author Jeongho Nam - https://github.com/samchon
16
- */
17
- export namespace FileTransformer {
18
- export const transform =
19
- (environments: Omit<ITypiaContext, "transformer" | "importer">) =>
20
- (transformer: ts.TransformationContext) =>
21
- (file: ts.SourceFile): ts.SourceFile => {
22
- if (file.isDeclarationFile) return file;
23
-
24
- const importer: ImportProgrammer = new ImportProgrammer({
25
- internalPrefix: "typia_transform_",
26
- runtime: environments.options.runtime,
27
- });
28
- const context: ITypiaContext = {
29
- ...environments,
30
- transformer,
31
- importer,
32
- };
33
- checkJsDocParsingMode.get(context, file);
34
-
35
- file = ts.visitEachChild(
36
- file,
37
- (node) =>
38
- iterate_node({
39
- context,
40
- node,
41
- }),
42
- transformer,
43
- );
44
- const index: number = find_import_injection_index(file);
45
- return ts.factory.updateSourceFile(
46
- file,
47
- [
48
- ...file.statements.slice(0, index),
49
- ...importer.toStatements(),
50
- ...file.statements.slice(index),
51
- ],
52
- false,
53
- file.referencedFiles,
54
- file.typeReferenceDirectives,
55
- file.hasNoDefaultLib,
56
- file.libReferenceDirectives,
57
- );
58
- };
59
-
60
- const iterate_node = (props: {
61
- context: ITypiaContext;
62
- node: ts.Node;
63
- }): ts.Node =>
64
- ts.visitEachChild(
65
- try_transform_node(props) ?? props.node,
66
- (node) =>
67
- iterate_node({
68
- context: props.context,
69
- node,
70
- }),
71
- props.context.transformer,
72
- );
73
-
74
- const try_transform_node = (props: {
75
- context: ITypiaContext;
76
- node: ts.Node;
77
- }): ts.Node | null => {
78
- try {
79
- return NodeTransformer.transform(props);
80
- } catch (exp) {
81
- // ONLY ACCEPT TRANSFORMER-ERROR
82
- if (!isTransformerError(exp)) throw exp;
83
-
84
- // REPORT DIAGNOSTIC
85
- const diagnostic = ts.createDiagnosticForNode(props.node, {
86
- key: exp.code,
87
- category: ts.DiagnosticCategory.Error,
88
- message: exp.message,
89
- code: `(${exp.code})` as any,
90
- });
91
- props.context.extras.addDiagnostic(diagnostic);
92
- return null;
93
- }
94
- };
95
-
96
- const find_import_injection_index = (file: ts.SourceFile): number => {
97
- let i: number = 0;
98
- for (; i < file.statements.length; ++i) {
99
- const stmt: ts.Statement = file.statements[i]!;
100
- if (
101
- ts.isExpressionStatement(stmt) &&
102
- ts.isStringLiteralLike(stmt.expression) &&
103
- stmt.expression.text.startsWith("use ")
104
- )
105
- continue;
106
- break;
107
- }
108
- return i;
109
- };
110
- }
111
-
112
- const isTransformerError = (error: any): error is TransformerError =>
113
- typeof error === "object" &&
114
- error !== null &&
115
- error.constructor.name === "TransformerError" &&
116
- typeof error.code === "string" &&
117
- typeof error.message === "string";
118
-
119
- const checkJsDocParsingMode = new Singleton(
120
- (context: ITypiaContext, file: ts.SourceFile) => {
121
- if (
122
- typeof file.jsDocParsingMode === "number" &&
123
- file.jsDocParsingMode !== 0
124
- ) {
125
- context.extras.addDiagnostic(
126
- ts.createDiagnosticForNode(file, {
127
- code: `(typia setup)` as any,
128
- key: "jsDocParsingMode",
129
- category: ts.DiagnosticCategory.Warning,
130
- message: [
131
- `Run "npx typia setup" or "npx typia patch" command again.`,
132
- ``,
133
- `Since TypeScript v5.3 update, "tsc" no more parses JSDoc comments. Therefore, "typia" also cannot utilize those JSDoc comments too, and it damages on some features of "typia" like "comment tags" or "JSON schema" generator.`,
134
- ``,
135
- `To solve this problem, run "npx typia setup" or "npx typia patch" command to hack the TypeScript compiler to revive the JSDoc parsing feature.`,
136
- ``,
137
- ` - reference: https://github.com/microsoft/TypeScript/pull/55739`,
138
- ].join("\n"),
139
- }),
140
- );
141
- }
142
- },
143
- );
1
+ import { ITypiaContext, ImportProgrammer } from "@typia/core";
2
+ import { Singleton } from "@typia/utils";
3
+ import ts from "typescript";
4
+
5
+ import { NodeTransformer } from "./NodeTransformer";
6
+ import { TransformerError } from "./TransformerError";
7
+
8
+ /**
9
+ * TypeScript source file transformer for typia.
10
+ *
11
+ * Entry point for typia's compile-time transformation. Traverses AST nodes,
12
+ * transforms `typia.*` function calls into optimized runtime code, and injects
13
+ * required imports. Skips declaration files.
14
+ *
15
+ * @author Jeongho Nam - https://github.com/samchon
16
+ */
17
+ export namespace FileTransformer {
18
+ export const transform =
19
+ (environments: Omit<ITypiaContext, "transformer" | "importer">) =>
20
+ (transformer: ts.TransformationContext) =>
21
+ (file: ts.SourceFile): ts.SourceFile => {
22
+ if (file.isDeclarationFile) return file;
23
+
24
+ const importer: ImportProgrammer = new ImportProgrammer({
25
+ internalPrefix: "typia_transform_",
26
+ runtime: environments.options.runtime,
27
+ });
28
+ const context: ITypiaContext = {
29
+ ...environments,
30
+ transformer,
31
+ importer,
32
+ };
33
+ checkJsDocParsingMode.get(context, file);
34
+
35
+ file = ts.visitEachChild(
36
+ file,
37
+ (node) =>
38
+ iterate_node({
39
+ context,
40
+ node,
41
+ }),
42
+ transformer,
43
+ );
44
+ const index: number = find_import_injection_index(file);
45
+ return ts.factory.updateSourceFile(
46
+ file,
47
+ [
48
+ ...file.statements.slice(0, index),
49
+ ...importer.toStatements(),
50
+ ...file.statements.slice(index),
51
+ ],
52
+ false,
53
+ file.referencedFiles,
54
+ file.typeReferenceDirectives,
55
+ file.hasNoDefaultLib,
56
+ file.libReferenceDirectives,
57
+ );
58
+ };
59
+
60
+ const iterate_node = (props: {
61
+ context: ITypiaContext;
62
+ node: ts.Node;
63
+ }): ts.Node =>
64
+ ts.visitEachChild(
65
+ try_transform_node(props) ?? props.node,
66
+ (node) =>
67
+ iterate_node({
68
+ context: props.context,
69
+ node,
70
+ }),
71
+ props.context.transformer,
72
+ );
73
+
74
+ const try_transform_node = (props: {
75
+ context: ITypiaContext;
76
+ node: ts.Node;
77
+ }): ts.Node | null => {
78
+ try {
79
+ return NodeTransformer.transform(props);
80
+ } catch (exp) {
81
+ // ONLY ACCEPT TRANSFORMER-ERROR
82
+ if (!isTransformerError(exp)) throw exp;
83
+
84
+ // REPORT DIAGNOSTIC
85
+ const diagnostic: ts.Diagnostic = {
86
+ file: props.node.getSourceFile(),
87
+ start: props.node.getStart(),
88
+ length: props.node.getWidth(),
89
+ messageText: exp.message,
90
+ category: ts.DiagnosticCategory.Error,
91
+ code: `(${exp.code})` as any,
92
+ };
93
+ props.context.extras.addDiagnostic(diagnostic);
94
+ return null;
95
+ }
96
+ };
97
+
98
+ const find_import_injection_index = (file: ts.SourceFile): number => {
99
+ let i: number = 0;
100
+ for (; i < file.statements.length; ++i) {
101
+ const stmt: ts.Statement = file.statements[i]!;
102
+ if (
103
+ ts.isExpressionStatement(stmt) &&
104
+ ts.isStringLiteralLike(stmt.expression) &&
105
+ stmt.expression.text.startsWith("use ")
106
+ )
107
+ continue;
108
+ break;
109
+ }
110
+ return i;
111
+ };
112
+ }
113
+
114
+ const isTransformerError = (error: any): error is TransformerError =>
115
+ typeof error === "object" &&
116
+ error !== null &&
117
+ error.constructor.name === "TransformerError" &&
118
+ typeof error.code === "string" &&
119
+ typeof error.message === "string";
120
+
121
+ const checkJsDocParsingMode = new Singleton(
122
+ (context: ITypiaContext, file: ts.SourceFile) => {
123
+ const jsDocParsingMode = (file as any).jsDocParsingMode;
124
+ if (typeof jsDocParsingMode === "number" && jsDocParsingMode !== 0) {
125
+ context.extras.addDiagnostic({
126
+ file,
127
+ start: 0,
128
+ length: 0,
129
+ messageText: [
130
+ `Run "npx typia setup" or "npx typia patch" command again.`,
131
+ ``,
132
+ `Since TypeScript v5.3 update, "tsc" no more parses JSDoc comments. Therefore, "typia" also cannot utilize those JSDoc comments too, and it damages on some features of "typia" like "comment tags" or "JSON schema" generator.`,
133
+ ``,
134
+ `To solve this problem, run "npx typia setup" or "npx typia patch" command to hack the TypeScript compiler to revive the JSDoc parsing feature.`,
135
+ ``,
136
+ ` - reference: https://github.com/microsoft/TypeScript/pull/55739`,
137
+ ].join("\n"),
138
+ category: ts.DiagnosticCategory.Warning,
139
+ code: `(typia setup)` as any,
140
+ });
141
+ }
142
+ },
143
+ );
@@ -1,89 +1,89 @@
1
- import {
2
- LlmApplicationProgrammer,
3
- LlmMetadataFactory,
4
- MetadataCollection,
5
- MetadataFactory,
6
- MetadataSchema,
7
- } from "@typia/core";
8
- import { ILlmSchema, ValidationPipe } from "@typia/interface";
9
- import ts from "typescript";
10
-
11
- import { ITransformProps } from "../../ITransformProps";
12
- import { TransformerError } from "../../TransformerError";
13
-
14
- export namespace LlmApplicationTransformer {
15
- export const transform = (props: ITransformProps): ts.Expression => {
16
- // GET GENERIC ARGUMENT
17
- if (!props.expression.typeArguments?.length)
18
- throw new TransformerError({
19
- code: "typia.llm.application",
20
- message: "no generic argument.",
21
- });
22
- const top: ts.Node = props.expression.typeArguments[0]!;
23
- if (ts.isTypeNode(top) === false) return props.expression;
24
-
25
- // GET CONFIG
26
- const config:
27
- | Partial<
28
- ILlmSchema.IConfig & {
29
- equals: boolean;
30
- }
31
- >
32
- | undefined = LlmMetadataFactory.getConfig({
33
- context: props.context,
34
- method: "application",
35
- node: props.expression.typeArguments[1],
36
- });
37
- const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
38
-
39
- // VALIDATE TYPE
40
- const analyze = (validate: boolean): MetadataSchema => {
41
- const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
42
- MetadataFactory.analyze({
43
- checker: props.context.checker,
44
- transformer: props.context.transformer,
45
- options: {
46
- absorb: validate,
47
- escape: true,
48
- constant: true,
49
- functional: true,
50
- validate:
51
- validate === true
52
- ? (next) =>
53
- LlmApplicationProgrammer.validate({
54
- config,
55
- metadata: next.metadata,
56
- explore: next.explore,
57
- top: next.top,
58
- })
59
- : undefined,
60
- },
61
- components: new MetadataCollection({
62
- replace: MetadataCollection.replace,
63
- }),
64
- type,
65
- });
66
- if (result.success === false)
67
- throw TransformerError.from({
68
- code: "typia.llm.application",
69
- errors: result.errors,
70
- });
71
- return result.data;
72
- };
73
- analyze(true);
74
-
75
- // GENERATE LLM APPLICATION
76
- return LlmApplicationProgrammer.write({
77
- context: props.context,
78
- modulo: props.modulo,
79
- metadata: analyze(false),
80
- name: top.getFullText().trim(),
81
- config,
82
- configArgument:
83
- props.expression.arguments?.[0] !== undefined
84
- ? props.expression.arguments[0]
85
- : undefined,
86
- });
87
- };
88
-
89
- }
1
+ import {
2
+ LlmApplicationProgrammer,
3
+ LlmMetadataFactory,
4
+ MetadataCollection,
5
+ MetadataFactory,
6
+ MetadataSchema,
7
+ } from "@typia/core";
8
+ import { ILlmSchema, ValidationPipe } from "@typia/interface";
9
+ import ts from "typescript";
10
+
11
+ import { ITransformProps } from "../../ITransformProps";
12
+ import { TransformerError } from "../../TransformerError";
13
+
14
+ export namespace LlmApplicationTransformer {
15
+ export const transform = (props: ITransformProps): ts.Expression => {
16
+ // GET GENERIC ARGUMENT
17
+ if (!props.expression.typeArguments?.length)
18
+ throw new TransformerError({
19
+ code: "typia.llm.application",
20
+ message: "no generic argument.",
21
+ });
22
+ const top: ts.Node = props.expression.typeArguments[0]!;
23
+ if (ts.isTypeNode(top) === false) return props.expression;
24
+
25
+ // GET CONFIG
26
+ const config:
27
+ | Partial<
28
+ ILlmSchema.IConfig & {
29
+ equals: boolean;
30
+ }
31
+ >
32
+ | undefined = LlmMetadataFactory.getConfig({
33
+ context: props.context,
34
+ method: "application",
35
+ node: props.expression.typeArguments[1],
36
+ });
37
+ const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
38
+
39
+ // VALIDATE TYPE
40
+ const analyze = (validate: boolean): MetadataSchema => {
41
+ const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
42
+ MetadataFactory.analyze({
43
+ checker: props.context.checker,
44
+ transformer: props.context.transformer,
45
+ options: {
46
+ absorb: validate,
47
+ escape: true,
48
+ constant: true,
49
+ functional: true,
50
+ validate:
51
+ validate === true
52
+ ? (next) =>
53
+ LlmApplicationProgrammer.validate({
54
+ config,
55
+ metadata: next.metadata,
56
+ explore: next.explore,
57
+ top: next.top,
58
+ })
59
+ : undefined,
60
+ },
61
+ components: new MetadataCollection({
62
+ replace: MetadataCollection.replace,
63
+ }),
64
+ type,
65
+ });
66
+ if (result.success === false)
67
+ throw TransformerError.from({
68
+ code: "typia.llm.application",
69
+ errors: result.errors,
70
+ });
71
+ return result.data;
72
+ };
73
+ analyze(true);
74
+
75
+ // GENERATE LLM APPLICATION
76
+ return LlmApplicationProgrammer.write({
77
+ context: props.context,
78
+ modulo: props.modulo,
79
+ metadata: analyze(false),
80
+ name: top.getFullText().trim(),
81
+ config,
82
+ configArgument:
83
+ props.expression.arguments?.[0] !== undefined
84
+ ? props.expression.arguments[0]
85
+ : undefined,
86
+ });
87
+ };
88
+
89
+ }
@@ -1,95 +1,95 @@
1
- import {
2
- LlmCoerceProgrammer,
3
- LlmMetadataFactory,
4
- MetadataCollection,
5
- MetadataFactory,
6
- MetadataSchema,
7
- } from "@typia/core";
8
- import { ILlmSchema, ValidationPipe } from "@typia/interface";
9
- import ts from "typescript";
10
-
11
- import { ITransformProps } from "../../ITransformProps";
12
- import { TransformerError } from "../../TransformerError";
13
-
14
- export namespace LlmCoerceTransformer {
15
- export const transform = (props: ITransformProps): ts.Expression => {
16
- // CHECK PARAMETER
17
- if (props.expression.arguments.length === 0)
18
- throw new TransformerError({
19
- code: "typia.llm.coerce",
20
- message: "no input value.",
21
- });
22
-
23
- // GET GENERIC ARGUMENT
24
- if (!props.expression.typeArguments?.length)
25
- throw new TransformerError({
26
- code: "typia.llm.coerce",
27
- message: "no generic argument.",
28
- });
29
-
30
- const top: ts.Node = props.expression.typeArguments[0]!;
31
- if (ts.isTypeNode(top) === false) return props.expression;
32
-
33
- // GET TYPE AND CONFIG
34
- const config: Partial<ILlmSchema.IConfig> = LlmMetadataFactory.getConfig({
35
- context: props.context,
36
- method: "coerce",
37
- node: props.expression.typeArguments[1],
38
- }) as Partial<ILlmSchema.IConfig>;
39
-
40
- const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
41
-
42
- if (type.isTypeParameter())
43
- throw new TransformerError({
44
- code: "typia.llm.coerce",
45
- message: "non-specified generic argument.",
46
- });
47
-
48
- // VALIDATE TYPE
49
- const analyze = (validate: boolean): MetadataSchema => {
50
- const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
51
- MetadataFactory.analyze({
52
- checker: props.context.checker,
53
- transformer: props.context.transformer,
54
- options: {
55
- absorb: validate,
56
- escape: true,
57
- constant: true,
58
- validate:
59
- validate === true
60
- ? (next) =>
61
- LlmCoerceProgrammer.validate({
62
- config,
63
- metadata: next.metadata,
64
- explore: next.explore,
65
- })
66
- : undefined,
67
- },
68
- components: new MetadataCollection({
69
- replace: MetadataCollection.replace,
70
- }),
71
- type,
72
- });
73
- if (result.success === false)
74
- throw TransformerError.from({
75
- code: "typia.llm.coerce",
76
- errors: result.errors,
77
- });
78
- return result.data;
79
- };
80
- analyze(true);
81
-
82
- // GENERATE CODE
83
- return ts.factory.createCallExpression(
84
- LlmCoerceProgrammer.write({
85
- context: props.context,
86
- modulo: props.modulo,
87
- metadata: analyze(false),
88
- name: (top as ts.TypeNode).getFullText().trim(),
89
- config,
90
- }),
91
- undefined,
92
- props.expression.arguments,
93
- );
94
- };
95
- }
1
+ import {
2
+ LlmCoerceProgrammer,
3
+ LlmMetadataFactory,
4
+ MetadataCollection,
5
+ MetadataFactory,
6
+ MetadataSchema,
7
+ } from "@typia/core";
8
+ import { ILlmSchema, ValidationPipe } from "@typia/interface";
9
+ import ts from "typescript";
10
+
11
+ import { ITransformProps } from "../../ITransformProps";
12
+ import { TransformerError } from "../../TransformerError";
13
+
14
+ export namespace LlmCoerceTransformer {
15
+ export const transform = (props: ITransformProps): ts.Expression => {
16
+ // CHECK PARAMETER
17
+ if (props.expression.arguments.length === 0)
18
+ throw new TransformerError({
19
+ code: "typia.llm.coerce",
20
+ message: "no input value.",
21
+ });
22
+
23
+ // GET GENERIC ARGUMENT
24
+ if (!props.expression.typeArguments?.length)
25
+ throw new TransformerError({
26
+ code: "typia.llm.coerce",
27
+ message: "no generic argument.",
28
+ });
29
+
30
+ const top: ts.Node = props.expression.typeArguments[0]!;
31
+ if (ts.isTypeNode(top) === false) return props.expression;
32
+
33
+ // GET TYPE AND CONFIG
34
+ const config: Partial<ILlmSchema.IConfig> = LlmMetadataFactory.getConfig({
35
+ context: props.context,
36
+ method: "coerce",
37
+ node: props.expression.typeArguments[1],
38
+ }) as Partial<ILlmSchema.IConfig>;
39
+
40
+ const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
41
+
42
+ if (type.isTypeParameter())
43
+ throw new TransformerError({
44
+ code: "typia.llm.coerce",
45
+ message: "non-specified generic argument.",
46
+ });
47
+
48
+ // VALIDATE TYPE
49
+ const analyze = (validate: boolean): MetadataSchema => {
50
+ const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
51
+ MetadataFactory.analyze({
52
+ checker: props.context.checker,
53
+ transformer: props.context.transformer,
54
+ options: {
55
+ absorb: validate,
56
+ escape: true,
57
+ constant: true,
58
+ validate:
59
+ validate === true
60
+ ? (next) =>
61
+ LlmCoerceProgrammer.validate({
62
+ config,
63
+ metadata: next.metadata,
64
+ explore: next.explore,
65
+ })
66
+ : undefined,
67
+ },
68
+ components: new MetadataCollection({
69
+ replace: MetadataCollection.replace,
70
+ }),
71
+ type,
72
+ });
73
+ if (result.success === false)
74
+ throw TransformerError.from({
75
+ code: "typia.llm.coerce",
76
+ errors: result.errors,
77
+ });
78
+ return result.data;
79
+ };
80
+ analyze(true);
81
+
82
+ // GENERATE CODE
83
+ return ts.factory.createCallExpression(
84
+ LlmCoerceProgrammer.write({
85
+ context: props.context,
86
+ modulo: props.modulo,
87
+ metadata: analyze(false),
88
+ name: (top as ts.TypeNode).getFullText().trim(),
89
+ config,
90
+ }),
91
+ undefined,
92
+ props.expression.arguments,
93
+ );
94
+ };
95
+ }