@nestia/core 4.2.0-dev.20241211 → 4.2.0
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/LICENSE +21 -21
- package/README.md +87 -87
- package/package.json +3 -3
- package/src/adaptors/WebSocketAdaptor.ts +426 -426
- package/src/decorators/DynamicModule.ts +43 -43
- package/src/decorators/EncryptedBody.ts +101 -101
- package/src/decorators/EncryptedController.ts +38 -38
- package/src/decorators/EncryptedModule.ts +100 -100
- package/src/decorators/EncryptedRoute.ts +219 -219
- package/src/decorators/NoTransformConfigurationError.ts +32 -32
- package/src/decorators/PlainBody.ts +79 -79
- package/src/decorators/SwaggerCustomizer.ts +115 -115
- package/src/decorators/SwaggerExample.ts +100 -100
- package/src/decorators/TypedBody.ts +59 -59
- package/src/decorators/TypedException.ts +128 -128
- package/src/decorators/TypedFormData.ts +195 -195
- package/src/decorators/TypedHeaders.ts +64 -64
- package/src/decorators/TypedParam.ts +66 -66
- package/src/decorators/TypedQuery.ts +245 -245
- package/src/decorators/TypedRoute.ts +214 -214
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/internal/EncryptedConstant.ts +4 -4
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
- package/src/decorators/internal/get_path_and_querify.ts +108 -108
- package/src/decorators/internal/get_path_and_stringify.ts +122 -122
- package/src/decorators/internal/get_text_body.ts +20 -20
- package/src/decorators/internal/headers_to_object.ts +13 -13
- package/src/decorators/internal/is_request_body_undefined.ts +14 -14
- package/src/decorators/internal/load_controller.ts +49 -49
- package/src/decorators/internal/route_error.ts +45 -45
- package/src/decorators/internal/validate_request_body.ts +74 -74
- package/src/decorators/internal/validate_request_form_data.ts +77 -77
- package/src/decorators/internal/validate_request_headers.ts +86 -86
- package/src/decorators/internal/validate_request_query.ts +74 -74
- package/src/index.ts +5 -5
- package/src/module.ts +21 -21
- package/src/options/INestiaTransformOptions.ts +24 -24
- package/src/options/INestiaTransformProject.ts +8 -8
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestFormDataProps.ts +27 -27
- package/src/options/IRequestHeadersValidator.ts +22 -22
- package/src/options/IRequestQueryValidator.ts +20 -20
- package/src/options/IResponseBodyQuerifier.ts +25 -25
- package/src/options/IResponseBodyStringifier.ts +30 -30
- package/src/programmers/PlainBodyProgrammer.ts +70 -70
- package/src/programmers/TypedBodyProgrammer.ts +132 -132
- package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
- package/src/programmers/TypedHeadersProgrammer.ts +63 -63
- package/src/programmers/TypedParamProgrammer.ts +30 -30
- package/src/programmers/TypedQueryBodyProgrammer.ts +63 -63
- package/src/programmers/TypedQueryProgrammer.ts +65 -65
- package/src/programmers/TypedQueryRouteProgrammer.ts +56 -56
- package/src/programmers/TypedRouteProgrammer.ts +76 -76
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
- package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +110 -110
- package/src/transformers/MethodTransformer.ts +103 -103
- package/src/transformers/NodeTransformer.ts +23 -23
- package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
- package/src/transformers/ParameterTransformer.ts +57 -57
- package/src/transformers/TypedRouteTransformer.ts +85 -85
- package/src/transformers/WebSocketRouteTransformer.ts +120 -120
- package/src/typings/Creator.ts +3 -3
- package/src/typings/get-function-location.d.ts +7 -7
- package/src/utils/ArrayUtil.ts +7 -7
- package/src/utils/ExceptionManager.ts +112 -112
- package/src/utils/Singleton.ts +20 -20
- package/src/utils/SourceFinder.ts +57 -57
- package/src/utils/VersioningStrategy.ts +27 -27
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
|
|
3
|
-
import { StatementFactory } from "typia/lib/factories/StatementFactory";
|
|
4
|
-
import { ValidateProgrammer } from "typia/lib/programmers/ValidateProgrammer";
|
|
5
|
-
import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
|
|
6
|
-
|
|
7
|
-
import { HttpQuerifyProgrammer } from "./HttpQuerifyProgrammer";
|
|
8
|
-
|
|
9
|
-
export namespace HttpValidateQuerifyProgrammer {
|
|
10
|
-
export const write = (props: {
|
|
11
|
-
context: ITypiaContext;
|
|
12
|
-
modulo: ts.LeftHandSideExpression;
|
|
13
|
-
type: ts.Type;
|
|
14
|
-
}): ts.ArrowFunction =>
|
|
15
|
-
ts.factory.createArrowFunction(
|
|
16
|
-
undefined,
|
|
17
|
-
undefined,
|
|
18
|
-
[IdentifierFactory.parameter("input")],
|
|
19
|
-
undefined,
|
|
20
|
-
undefined,
|
|
21
|
-
ts.factory.createBlock([
|
|
22
|
-
StatementFactory.constant({
|
|
23
|
-
name: "validate",
|
|
24
|
-
value: ValidateProgrammer.write({
|
|
25
|
-
config: {
|
|
26
|
-
equals: false,
|
|
27
|
-
},
|
|
28
|
-
context: {
|
|
29
|
-
...props.context,
|
|
30
|
-
options: {
|
|
31
|
-
...props.context.options,
|
|
32
|
-
functional: false,
|
|
33
|
-
numeric: true,
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
modulo: props.modulo,
|
|
37
|
-
type: props.type,
|
|
38
|
-
name: undefined,
|
|
39
|
-
}),
|
|
40
|
-
}),
|
|
41
|
-
StatementFactory.constant({
|
|
42
|
-
name: "query",
|
|
43
|
-
value: HttpQuerifyProgrammer.write({
|
|
44
|
-
context: {
|
|
45
|
-
...props.context,
|
|
46
|
-
options: {
|
|
47
|
-
...props.context.options,
|
|
48
|
-
functional: false,
|
|
49
|
-
numeric: false,
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
modulo: props.modulo,
|
|
53
|
-
type: props.type,
|
|
54
|
-
}),
|
|
55
|
-
}),
|
|
56
|
-
StatementFactory.constant({
|
|
57
|
-
name: "output",
|
|
58
|
-
value: ts.factory.createCallExpression(
|
|
59
|
-
ts.factory.createIdentifier("query"),
|
|
60
|
-
undefined,
|
|
61
|
-
[ts.factory.createIdentifier("input")],
|
|
62
|
-
),
|
|
63
|
-
}),
|
|
64
|
-
ts.factory.createReturnStatement(
|
|
65
|
-
ts.factory.createAsExpression(
|
|
66
|
-
ts.factory.createCallExpression(
|
|
67
|
-
ts.factory.createIdentifier("validate"),
|
|
68
|
-
undefined,
|
|
69
|
-
[ts.factory.createIdentifier("output")],
|
|
70
|
-
),
|
|
71
|
-
ts.factory.createTypeReferenceNode("any"),
|
|
72
|
-
),
|
|
73
|
-
),
|
|
74
|
-
]),
|
|
75
|
-
);
|
|
76
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
|
|
3
|
+
import { StatementFactory } from "typia/lib/factories/StatementFactory";
|
|
4
|
+
import { ValidateProgrammer } from "typia/lib/programmers/ValidateProgrammer";
|
|
5
|
+
import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
|
|
6
|
+
|
|
7
|
+
import { HttpQuerifyProgrammer } from "./HttpQuerifyProgrammer";
|
|
8
|
+
|
|
9
|
+
export namespace HttpValidateQuerifyProgrammer {
|
|
10
|
+
export const write = (props: {
|
|
11
|
+
context: ITypiaContext;
|
|
12
|
+
modulo: ts.LeftHandSideExpression;
|
|
13
|
+
type: ts.Type;
|
|
14
|
+
}): ts.ArrowFunction =>
|
|
15
|
+
ts.factory.createArrowFunction(
|
|
16
|
+
undefined,
|
|
17
|
+
undefined,
|
|
18
|
+
[IdentifierFactory.parameter("input")],
|
|
19
|
+
undefined,
|
|
20
|
+
undefined,
|
|
21
|
+
ts.factory.createBlock([
|
|
22
|
+
StatementFactory.constant({
|
|
23
|
+
name: "validate",
|
|
24
|
+
value: ValidateProgrammer.write({
|
|
25
|
+
config: {
|
|
26
|
+
equals: false,
|
|
27
|
+
},
|
|
28
|
+
context: {
|
|
29
|
+
...props.context,
|
|
30
|
+
options: {
|
|
31
|
+
...props.context.options,
|
|
32
|
+
functional: false,
|
|
33
|
+
numeric: true,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
modulo: props.modulo,
|
|
37
|
+
type: props.type,
|
|
38
|
+
name: undefined,
|
|
39
|
+
}),
|
|
40
|
+
}),
|
|
41
|
+
StatementFactory.constant({
|
|
42
|
+
name: "query",
|
|
43
|
+
value: HttpQuerifyProgrammer.write({
|
|
44
|
+
context: {
|
|
45
|
+
...props.context,
|
|
46
|
+
options: {
|
|
47
|
+
...props.context.options,
|
|
48
|
+
functional: false,
|
|
49
|
+
numeric: false,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
modulo: props.modulo,
|
|
53
|
+
type: props.type,
|
|
54
|
+
}),
|
|
55
|
+
}),
|
|
56
|
+
StatementFactory.constant({
|
|
57
|
+
name: "output",
|
|
58
|
+
value: ts.factory.createCallExpression(
|
|
59
|
+
ts.factory.createIdentifier("query"),
|
|
60
|
+
undefined,
|
|
61
|
+
[ts.factory.createIdentifier("input")],
|
|
62
|
+
),
|
|
63
|
+
}),
|
|
64
|
+
ts.factory.createReturnStatement(
|
|
65
|
+
ts.factory.createAsExpression(
|
|
66
|
+
ts.factory.createCallExpression(
|
|
67
|
+
ts.factory.createIdentifier("validate"),
|
|
68
|
+
undefined,
|
|
69
|
+
[ts.factory.createIdentifier("output")],
|
|
70
|
+
),
|
|
71
|
+
ts.factory.createTypeReferenceNode("any"),
|
|
72
|
+
),
|
|
73
|
+
),
|
|
74
|
+
]),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { Metadata } from "typia/lib/schemas/metadata/Metadata";
|
|
2
|
-
|
|
3
|
-
export namespace CoreMetadataUtil {
|
|
4
|
-
export const atomics = (
|
|
5
|
-
meta: Metadata,
|
|
6
|
-
): Set<"boolean" | "bigint" | "number" | "string"> =>
|
|
7
|
-
new Set([
|
|
8
|
-
...meta.atomics.map((a) => a.type),
|
|
9
|
-
...meta.constants.map((c) => c.type),
|
|
10
|
-
...(meta.templates.length ? (["string"] as const) : []),
|
|
11
|
-
]);
|
|
12
|
-
|
|
13
|
-
export const isUnion = (meta: Metadata): boolean =>
|
|
14
|
-
atomics(meta).size +
|
|
15
|
-
meta.arrays.length +
|
|
16
|
-
meta.tuples.length +
|
|
17
|
-
meta.natives.length +
|
|
18
|
-
meta.maps.length +
|
|
19
|
-
meta.objects.length >
|
|
20
|
-
1;
|
|
21
|
-
}
|
|
1
|
+
import { Metadata } from "typia/lib/schemas/metadata/Metadata";
|
|
2
|
+
|
|
3
|
+
export namespace CoreMetadataUtil {
|
|
4
|
+
export const atomics = (
|
|
5
|
+
meta: Metadata,
|
|
6
|
+
): Set<"boolean" | "bigint" | "number" | "string"> =>
|
|
7
|
+
new Set([
|
|
8
|
+
...meta.atomics.map((a) => a.type),
|
|
9
|
+
...meta.constants.map((c) => c.type),
|
|
10
|
+
...(meta.templates.length ? (["string"] as const) : []),
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export const isUnion = (meta: Metadata): boolean =>
|
|
14
|
+
atomics(meta).size +
|
|
15
|
+
meta.arrays.length +
|
|
16
|
+
meta.tuples.length +
|
|
17
|
+
meta.natives.length +
|
|
18
|
+
meta.maps.length +
|
|
19
|
+
meta.objects.length >
|
|
20
|
+
1;
|
|
21
|
+
}
|
package/src/transform.ts
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
|
|
3
|
-
|
|
4
|
-
import { INestiaTransformOptions } from "./options/INestiaTransformOptions";
|
|
5
|
-
import { FileTransformer } from "./transformers/FileTransformer";
|
|
6
|
-
|
|
7
|
-
export const transform = (
|
|
8
|
-
program: ts.Program,
|
|
9
|
-
options: INestiaTransformOptions | undefined,
|
|
10
|
-
extras: ITypiaContext["extras"],
|
|
11
|
-
): ts.TransformerFactory<ts.SourceFile> => {
|
|
12
|
-
const compilerOptions: ts.CompilerOptions = program.getCompilerOptions();
|
|
13
|
-
const strict: boolean =
|
|
14
|
-
compilerOptions.strictNullChecks !== undefined
|
|
15
|
-
? !!compilerOptions.strictNullChecks
|
|
16
|
-
: !!compilerOptions.strict;
|
|
17
|
-
if (strict === false)
|
|
18
|
-
extras.addDiagnostic({
|
|
19
|
-
category: ts.DiagnosticCategory.Error,
|
|
20
|
-
code: "(@nestia/core)" as any,
|
|
21
|
-
file: undefined,
|
|
22
|
-
start: undefined,
|
|
23
|
-
length: undefined,
|
|
24
|
-
messageText: "strict mode is required.",
|
|
25
|
-
});
|
|
26
|
-
return FileTransformer.transform({
|
|
27
|
-
program,
|
|
28
|
-
compilerOptions,
|
|
29
|
-
checker: program.getTypeChecker(),
|
|
30
|
-
printer: ts.createPrinter(),
|
|
31
|
-
options: options ?? {},
|
|
32
|
-
extras,
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
export default transform;
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
|
|
3
|
+
|
|
4
|
+
import { INestiaTransformOptions } from "./options/INestiaTransformOptions";
|
|
5
|
+
import { FileTransformer } from "./transformers/FileTransformer";
|
|
6
|
+
|
|
7
|
+
export const transform = (
|
|
8
|
+
program: ts.Program,
|
|
9
|
+
options: INestiaTransformOptions | undefined,
|
|
10
|
+
extras: ITypiaContext["extras"],
|
|
11
|
+
): ts.TransformerFactory<ts.SourceFile> => {
|
|
12
|
+
const compilerOptions: ts.CompilerOptions = program.getCompilerOptions();
|
|
13
|
+
const strict: boolean =
|
|
14
|
+
compilerOptions.strictNullChecks !== undefined
|
|
15
|
+
? !!compilerOptions.strictNullChecks
|
|
16
|
+
: !!compilerOptions.strict;
|
|
17
|
+
if (strict === false)
|
|
18
|
+
extras.addDiagnostic({
|
|
19
|
+
category: ts.DiagnosticCategory.Error,
|
|
20
|
+
code: "(@nestia/core)" as any,
|
|
21
|
+
file: undefined,
|
|
22
|
+
start: undefined,
|
|
23
|
+
length: undefined,
|
|
24
|
+
messageText: "strict mode is required.",
|
|
25
|
+
});
|
|
26
|
+
return FileTransformer.transform({
|
|
27
|
+
program,
|
|
28
|
+
compilerOptions,
|
|
29
|
+
checker: program.getTypeChecker(),
|
|
30
|
+
printer: ts.createPrinter(),
|
|
31
|
+
options: options ?? {},
|
|
32
|
+
extras,
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
export default transform;
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import { ImportProgrammer } from "typia/lib/programmers/ImportProgrammer";
|
|
3
|
-
import { TransformerError } from "typia/lib/transformers/TransformerError";
|
|
4
|
-
|
|
5
|
-
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
6
|
-
import { NodeTransformer } from "./NodeTransformer";
|
|
7
|
-
|
|
8
|
-
export namespace FileTransformer {
|
|
9
|
-
export const transform =
|
|
10
|
-
(context: Omit<INestiaTransformContext, "importer" | "transformer">) =>
|
|
11
|
-
(transformer: ts.TransformationContext) =>
|
|
12
|
-
(file: ts.SourceFile): ts.SourceFile => {
|
|
13
|
-
if (file.isDeclarationFile) return file;
|
|
14
|
-
const importer = new ImportProgrammer({
|
|
15
|
-
internalPrefix: "nestia_core_transform",
|
|
16
|
-
});
|
|
17
|
-
file = ts.visitEachChild(
|
|
18
|
-
file,
|
|
19
|
-
(node) =>
|
|
20
|
-
iterate_node({
|
|
21
|
-
context: {
|
|
22
|
-
...context,
|
|
23
|
-
transformer,
|
|
24
|
-
importer,
|
|
25
|
-
},
|
|
26
|
-
file,
|
|
27
|
-
node,
|
|
28
|
-
}),
|
|
29
|
-
transformer,
|
|
30
|
-
);
|
|
31
|
-
const index: number = find_import_injection_index(file);
|
|
32
|
-
return ts.factory.updateSourceFile(
|
|
33
|
-
file,
|
|
34
|
-
[
|
|
35
|
-
...file.statements.slice(0, index),
|
|
36
|
-
...importer.toStatements(),
|
|
37
|
-
...file.statements.slice(index),
|
|
38
|
-
],
|
|
39
|
-
false,
|
|
40
|
-
file.referencedFiles,
|
|
41
|
-
file.typeReferenceDirectives,
|
|
42
|
-
file.hasNoDefaultLib,
|
|
43
|
-
file.libReferenceDirectives,
|
|
44
|
-
);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const iterate_node = (props: {
|
|
48
|
-
context: INestiaTransformContext;
|
|
49
|
-
file: ts.SourceFile;
|
|
50
|
-
node: ts.Node;
|
|
51
|
-
}): ts.Node =>
|
|
52
|
-
ts.visitEachChild(
|
|
53
|
-
try_transform_node(props) ?? props.node,
|
|
54
|
-
(child) =>
|
|
55
|
-
iterate_node({
|
|
56
|
-
context: props.context,
|
|
57
|
-
file: props.file,
|
|
58
|
-
node: child,
|
|
59
|
-
}),
|
|
60
|
-
props.context.transformer,
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
const try_transform_node = (props: {
|
|
64
|
-
context: INestiaTransformContext;
|
|
65
|
-
file: ts.SourceFile;
|
|
66
|
-
node: ts.Node;
|
|
67
|
-
}): ts.Node | null => {
|
|
68
|
-
try {
|
|
69
|
-
return NodeTransformer.transform(props);
|
|
70
|
-
} catch (exp) {
|
|
71
|
-
// ONLY ACCEPT TRANSFORMER-ERROR
|
|
72
|
-
if (!isTransformerError(exp)) throw exp;
|
|
73
|
-
|
|
74
|
-
// AVOID SPECIAL BUG OF TYPESCRIPT COMPILER API
|
|
75
|
-
(props.node as any).parent ??= props.file;
|
|
76
|
-
|
|
77
|
-
// REPORT DIAGNOSTIC
|
|
78
|
-
const diagnostic = (ts as any).createDiagnosticForNode(props.node, {
|
|
79
|
-
key: exp.code,
|
|
80
|
-
category: ts.DiagnosticCategory.Error,
|
|
81
|
-
message: exp.message,
|
|
82
|
-
code: `(${exp.code})` as any,
|
|
83
|
-
});
|
|
84
|
-
props.context.extras.addDiagnostic(diagnostic);
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const find_import_injection_index = (file: ts.SourceFile): number => {
|
|
90
|
-
let i: number = 0;
|
|
91
|
-
for (; i < file.statements.length; ++i) {
|
|
92
|
-
const stmt: ts.Statement = file.statements[i]!;
|
|
93
|
-
if (
|
|
94
|
-
ts.isExpressionStatement(stmt) &&
|
|
95
|
-
ts.isStringLiteralLike(stmt.expression) &&
|
|
96
|
-
stmt.expression.text.startsWith("use ")
|
|
97
|
-
)
|
|
98
|
-
continue;
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
return i;
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const isTransformerError = (error: any): error is TransformerError =>
|
|
106
|
-
typeof error === "object" &&
|
|
107
|
-
error !== null &&
|
|
108
|
-
error.constructor.name === "TransformerError" &&
|
|
109
|
-
typeof error.code === "string" &&
|
|
110
|
-
typeof error.message === "string";
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { ImportProgrammer } from "typia/lib/programmers/ImportProgrammer";
|
|
3
|
+
import { TransformerError } from "typia/lib/transformers/TransformerError";
|
|
4
|
+
|
|
5
|
+
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
6
|
+
import { NodeTransformer } from "./NodeTransformer";
|
|
7
|
+
|
|
8
|
+
export namespace FileTransformer {
|
|
9
|
+
export const transform =
|
|
10
|
+
(context: Omit<INestiaTransformContext, "importer" | "transformer">) =>
|
|
11
|
+
(transformer: ts.TransformationContext) =>
|
|
12
|
+
(file: ts.SourceFile): ts.SourceFile => {
|
|
13
|
+
if (file.isDeclarationFile) return file;
|
|
14
|
+
const importer = new ImportProgrammer({
|
|
15
|
+
internalPrefix: "nestia_core_transform",
|
|
16
|
+
});
|
|
17
|
+
file = ts.visitEachChild(
|
|
18
|
+
file,
|
|
19
|
+
(node) =>
|
|
20
|
+
iterate_node({
|
|
21
|
+
context: {
|
|
22
|
+
...context,
|
|
23
|
+
transformer,
|
|
24
|
+
importer,
|
|
25
|
+
},
|
|
26
|
+
file,
|
|
27
|
+
node,
|
|
28
|
+
}),
|
|
29
|
+
transformer,
|
|
30
|
+
);
|
|
31
|
+
const index: number = find_import_injection_index(file);
|
|
32
|
+
return ts.factory.updateSourceFile(
|
|
33
|
+
file,
|
|
34
|
+
[
|
|
35
|
+
...file.statements.slice(0, index),
|
|
36
|
+
...importer.toStatements(),
|
|
37
|
+
...file.statements.slice(index),
|
|
38
|
+
],
|
|
39
|
+
false,
|
|
40
|
+
file.referencedFiles,
|
|
41
|
+
file.typeReferenceDirectives,
|
|
42
|
+
file.hasNoDefaultLib,
|
|
43
|
+
file.libReferenceDirectives,
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const iterate_node = (props: {
|
|
48
|
+
context: INestiaTransformContext;
|
|
49
|
+
file: ts.SourceFile;
|
|
50
|
+
node: ts.Node;
|
|
51
|
+
}): ts.Node =>
|
|
52
|
+
ts.visitEachChild(
|
|
53
|
+
try_transform_node(props) ?? props.node,
|
|
54
|
+
(child) =>
|
|
55
|
+
iterate_node({
|
|
56
|
+
context: props.context,
|
|
57
|
+
file: props.file,
|
|
58
|
+
node: child,
|
|
59
|
+
}),
|
|
60
|
+
props.context.transformer,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const try_transform_node = (props: {
|
|
64
|
+
context: INestiaTransformContext;
|
|
65
|
+
file: ts.SourceFile;
|
|
66
|
+
node: ts.Node;
|
|
67
|
+
}): ts.Node | null => {
|
|
68
|
+
try {
|
|
69
|
+
return NodeTransformer.transform(props);
|
|
70
|
+
} catch (exp) {
|
|
71
|
+
// ONLY ACCEPT TRANSFORMER-ERROR
|
|
72
|
+
if (!isTransformerError(exp)) throw exp;
|
|
73
|
+
|
|
74
|
+
// AVOID SPECIAL BUG OF TYPESCRIPT COMPILER API
|
|
75
|
+
(props.node as any).parent ??= props.file;
|
|
76
|
+
|
|
77
|
+
// REPORT DIAGNOSTIC
|
|
78
|
+
const diagnostic = (ts as any).createDiagnosticForNode(props.node, {
|
|
79
|
+
key: exp.code,
|
|
80
|
+
category: ts.DiagnosticCategory.Error,
|
|
81
|
+
message: exp.message,
|
|
82
|
+
code: `(${exp.code})` as any,
|
|
83
|
+
});
|
|
84
|
+
props.context.extras.addDiagnostic(diagnostic);
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const find_import_injection_index = (file: ts.SourceFile): number => {
|
|
90
|
+
let i: number = 0;
|
|
91
|
+
for (; i < file.statements.length; ++i) {
|
|
92
|
+
const stmt: ts.Statement = file.statements[i]!;
|
|
93
|
+
if (
|
|
94
|
+
ts.isExpressionStatement(stmt) &&
|
|
95
|
+
ts.isStringLiteralLike(stmt.expression) &&
|
|
96
|
+
stmt.expression.text.startsWith("use ")
|
|
97
|
+
)
|
|
98
|
+
continue;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
return i;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const isTransformerError = (error: any): error is TransformerError =>
|
|
106
|
+
typeof error === "object" &&
|
|
107
|
+
error !== null &&
|
|
108
|
+
error.constructor.name === "TransformerError" &&
|
|
109
|
+
typeof error.code === "string" &&
|
|
110
|
+
typeof error.message === "string";
|