@typia/transform 12.0.0 → 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,226 +1,89 @@
1
- import {
2
- ITypiaContext,
3
- LlmApplicationProgrammer,
4
- LlmMetadataFactory,
5
- MetadataCollection,
6
- MetadataFactory,
7
- MetadataSchema,
8
- } from "@typia/core";
9
- import { ILlmApplication, ILlmSchema, ValidationPipe } from "@typia/interface";
10
- import ts from "typescript";
11
-
12
- import { ITransformProps } from "../../ITransformProps";
13
- import { TransformerError } from "../../TransformerError";
14
-
15
- export namespace LlmApplicationTransformer {
16
- export const transform = (props: ITransformProps): ts.Expression => {
17
- // GET GENERIC ARGUMENT
18
- if (!props.expression.typeArguments?.length)
19
- throw new TransformerError({
20
- code: "typia.llm.application",
21
- message: "no generic argument.",
22
- });
23
- const top: ts.Node = props.expression.typeArguments[0]!;
24
- if (ts.isTypeNode(top) === false) return props.expression;
25
-
26
- // GET CONFIG
27
- const config:
28
- | Partial<
29
- ILlmSchema.IConfig & {
30
- equals: boolean;
31
- }
32
- >
33
- | undefined = LlmMetadataFactory.getConfig({
34
- context: props.context,
35
- method: "application",
36
- node: props.expression.typeArguments[1],
37
- });
38
- const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
39
-
40
- // VALIDATE TYPE
41
- const analyze = (validate: boolean): MetadataSchema => {
42
- const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
43
- MetadataFactory.analyze({
44
- checker: props.context.checker,
45
- transformer: props.context.transformer,
46
- options: {
47
- absorb: validate,
48
- escape: true,
49
- constant: true,
50
- functional: true,
51
- validate:
52
- validate === true
53
- ? (next) =>
54
- LlmApplicationProgrammer.validate({
55
- config,
56
- metadata: next.metadata,
57
- explore: next.explore,
58
- top: next.top,
59
- })
60
- : undefined,
61
- },
62
- components: new MetadataCollection({
63
- replace: MetadataCollection.replace,
64
- }),
65
- type,
66
- });
67
- if (result.success === false)
68
- throw TransformerError.from({
69
- code: "typia.llm.application",
70
- errors: result.errors,
71
- });
72
- return result.data;
73
- };
74
- analyze(true);
75
-
76
- // GENERATE LLM APPLICATION
77
- return LlmApplicationProgrammer.write({
78
- context: props.context,
79
- modulo: props.modulo,
80
- metadata: analyze(false),
81
- name: top.getFullText().trim(),
82
- config,
83
- });
84
- };
85
-
86
- /** @internal */
87
- export const decompose = (
88
- method: string,
89
- props: ITransformProps,
90
- ): {
91
- application: ILlmApplication.__IPrimitive;
92
- type: ts.Type;
93
- node: ts.TypeNode;
94
- config:
95
- | Partial<
96
- ILlmSchema.IConfig & {
97
- equals: boolean;
98
- }
99
- >
100
- | undefined;
101
- } | null => {
102
- // GET GENERIC ARGUMENT
103
- if (!props.expression.typeArguments?.length)
104
- throw new TransformerError({
105
- code: `typia.llm.${method}`,
106
- message: "no generic argument.",
107
- });
108
- const top: ts.Node = props.expression.typeArguments[0]!;
109
- if (ts.isTypeNode(top) === false) return null;
110
-
111
- // GET TYPE
112
- const config:
113
- | Partial<
114
- ILlmSchema.IConfig & {
115
- equals: boolean;
116
- }
117
- >
118
- | undefined = LlmMetadataFactory.getConfig({
119
- context: props.context,
120
- method,
121
- node: props.expression.typeArguments[1],
122
- });
123
- const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
124
-
125
- // VALIDATE TYPE
126
- const analyze = (validate: boolean): MetadataSchema => {
127
- const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
128
- MetadataFactory.analyze({
129
- checker: props.context.checker,
130
- transformer: props.context.transformer,
131
- options: {
132
- absorb: validate,
133
- escape: true,
134
- constant: true,
135
- functional: true,
136
- validate:
137
- validate === true
138
- ? (next) =>
139
- LlmApplicationProgrammer.validate({
140
- config,
141
- metadata: next.metadata,
142
- explore: next.explore,
143
- top: next.top,
144
- })
145
- : undefined,
146
- },
147
- components: new MetadataCollection({
148
- replace: MetadataCollection.replace,
149
- }),
150
- type,
151
- });
152
- if (result.success === false)
153
- throw TransformerError.from({
154
- code: `typia.llm.${method}`,
155
- errors: result.errors,
156
- });
157
- return result.data;
158
- };
159
- analyze(true);
160
-
161
- // GENERATE LLM APPLICATION
162
- return {
163
- application: LlmApplicationProgrammer.writeApplication({
164
- context: props.context,
165
- modulo: props.modulo,
166
- metadata: analyze(false),
167
- config,
168
- name: top.getFullText().trim(),
169
- }),
170
- node: top,
171
- type,
172
- config,
173
- };
174
- };
175
-
176
- export const getConfigArgument = (props: {
177
- context: ITypiaContext;
178
- argument: ts.Expression;
179
- equals?: boolean | undefined;
180
- }) => {
181
- const satisfiesTypeNode: ts.TypeNode = ts.factory.createTypeReferenceNode(
182
- ts.factory.createIdentifier("Partial"),
183
- [
184
- ts.factory.createTypeReferenceNode(
185
- ts.factory.createIdentifier("Pick"),
186
- [
187
- ts.factory.createImportTypeNode(
188
- ts.factory.createLiteralTypeNode(
189
- ts.factory.createStringLiteral("typia"),
190
- ),
191
- undefined,
192
- ts.factory.createQualifiedName(
193
- ts.factory.createIdentifier("ILlmApplication"),
194
- ts.factory.createIdentifier("IConfig"),
195
- ),
196
- undefined,
197
- false,
198
- ),
199
- ts.factory.createUnionTypeNode([
200
- ts.factory.createLiteralTypeNode(
201
- ts.factory.createStringLiteral("validate"),
202
- ),
203
- ]),
204
- ],
205
- ),
206
- ],
207
- );
208
- return ts.factory.createObjectLiteralExpression(
209
- [
210
- ts.factory.createSpreadAssignment(
211
- ts.factory.createSatisfiesExpression(
212
- props.argument,
213
- satisfiesTypeNode,
214
- ),
215
- ),
216
- ts.factory.createPropertyAssignment(
217
- "equals",
218
- props.equals === true
219
- ? ts.factory.createTrue()
220
- : ts.factory.createFalse(),
221
- ),
222
- ],
223
- true,
224
- );
225
- };
226
- }
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
+ }