@nestia/sdk 2.4.2 → 2.4.3
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/NestiaSdkApplication.js +2 -6
- package/lib/NestiaSdkApplication.js.map +1 -1
- package/lib/analyses/AccessorAnalyzer.js.map +1 -1
- package/lib/analyses/ConfigAnalyzer.js +4 -8
- package/lib/analyses/ConfigAnalyzer.js.map +1 -1
- package/lib/analyses/ControllerAnalyzer.js +6 -8
- package/lib/analyses/ControllerAnalyzer.js.map +1 -1
- package/lib/analyses/ExceptionAnalyzer.js.map +1 -1
- package/lib/analyses/GenericAnalyzer.js +1 -2
- package/lib/analyses/GenericAnalyzer.js.map +1 -1
- package/lib/analyses/ImportAnalyzer.js +4 -4
- package/lib/analyses/ImportAnalyzer.js.map +1 -1
- package/lib/analyses/PathAnalyzer.js.map +1 -1
- package/lib/analyses/ReflectAnalyzer.js +7 -8
- package/lib/analyses/ReflectAnalyzer.js.map +1 -1
- package/lib/analyses/SecurityAnalyzer.js.map +1 -1
- package/lib/executable/internal/CommandParser.js.map +1 -1
- package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
- package/lib/executable/internal/NestiaSdkCommand.js.map +1 -1
- package/lib/executable/sdk.js +11 -11
- package/lib/executable/sdk.js.map +1 -1
- package/lib/generates/E2eGenerator.js.map +1 -1
- package/lib/generates/SdkGenerator.js.map +1 -1
- package/lib/generates/SwaggerGenerator.js +5 -11
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/generates/internal/E2eFileProgrammer.js +2 -8
- package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkDistributionComposer.js.map +1 -1
- package/lib/generates/internal/SdkDtoGenerator.js +3 -9
- package/lib/generates/internal/SdkDtoGenerator.js.map +1 -1
- package/lib/generates/internal/SdkFileProgrammer.js +4 -4
- package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkFunctionProgrammer.js +12 -20
- package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkImportWizard.js.map +1 -1
- package/lib/generates/internal/SdkRouteDirectory.js +1 -3
- package/lib/generates/internal/SdkRouteDirectory.js.map +1 -1
- package/lib/generates/internal/SdkSimulationProgrammer.js +5 -7
- package/lib/generates/internal/SdkSimulationProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkTypeDefiner.js +2 -5
- package/lib/generates/internal/SdkTypeDefiner.js.map +1 -1
- package/lib/generates/internal/SwaggerSchemaGenerator.js +29 -44
- package/lib/generates/internal/SwaggerSchemaGenerator.js.map +1 -1
- package/lib/generates/internal/SwaggerSchemaValidator.js +3 -9
- package/lib/generates/internal/SwaggerSchemaValidator.js.map +1 -1
- package/lib/structures/MethodType.js +1 -7
- package/lib/structures/MethodType.js.map +1 -1
- package/lib/structures/TypeEntry.js.map +1 -1
- package/lib/utils/ArrayUtil.js.map +1 -1
- package/lib/utils/FileRetriever.js.map +1 -1
- package/lib/utils/ImportDictionary.js +1 -4
- package/lib/utils/ImportDictionary.js.map +1 -1
- package/lib/utils/MapUtil.js.map +1 -1
- package/lib/utils/PathUtil.js.map +1 -1
- package/lib/utils/SourceFinder.js.map +1 -1
- package/package.json +4 -7
- package/src/INestiaConfig.ts +234 -234
- package/src/NestiaSdkApplication.ts +253 -268
- package/src/analyses/AccessorAnalyzer.ts +60 -60
- package/src/analyses/ConfigAnalyzer.ts +147 -164
- package/src/analyses/ControllerAnalyzer.ts +379 -399
- package/src/analyses/ExceptionAnalyzer.ts +115 -124
- package/src/analyses/GenericAnalyzer.ts +51 -57
- package/src/analyses/ImportAnalyzer.ts +138 -159
- package/src/analyses/PathAnalyzer.ts +98 -100
- package/src/analyses/ReflectAnalyzer.ts +425 -433
- package/src/analyses/SecurityAnalyzer.ts +20 -20
- package/src/executable/internal/CommandParser.ts +15 -15
- package/src/executable/internal/NestiaConfigLoader.ts +67 -68
- package/src/executable/internal/NestiaSdkCommand.ts +60 -64
- package/src/executable/sdk.ts +73 -73
- package/src/generates/E2eGenerator.ts +64 -67
- package/src/generates/SdkGenerator.ts +96 -100
- package/src/generates/SwaggerGenerator.ts +372 -410
- package/src/generates/internal/E2eFileProgrammer.ts +123 -129
- package/src/generates/internal/SdkDistributionComposer.ts +91 -91
- package/src/generates/internal/SdkDtoGenerator.ts +424 -450
- package/src/generates/internal/SdkFileProgrammer.ts +106 -111
- package/src/generates/internal/SdkFunctionProgrammer.ts +466 -501
- package/src/generates/internal/SdkImportWizard.ts +55 -55
- package/src/generates/internal/SdkRouteDirectory.ts +17 -19
- package/src/generates/internal/SdkSimulationProgrammer.ts +133 -142
- package/src/generates/internal/SdkTypeDefiner.ts +119 -124
- package/src/generates/internal/SwaggerSchemaGenerator.ts +382 -401
- package/src/generates/internal/SwaggerSchemaValidator.ts +198 -210
- package/src/index.ts +4 -4
- package/src/module.ts +2 -2
- package/src/structures/IController.ts +79 -81
- package/src/structures/IErrorReport.ts +6 -6
- package/src/structures/INestiaProject.ts +13 -13
- package/src/structures/INormalizedInput.ts +20 -20
- package/src/structures/IRoute.ts +40 -41
- package/src/structures/ISwagger.ts +91 -91
- package/src/structures/ISwaggerComponents.ts +29 -29
- package/src/structures/ISwaggerError.ts +8 -8
- package/src/structures/ISwaggerInfo.ts +80 -80
- package/src/structures/ISwaggerLazyProperty.ts +7 -7
- package/src/structures/ISwaggerLazySchema.ts +7 -7
- package/src/structures/ISwaggerRoute.ts +51 -51
- package/src/structures/ISwaggerSecurityScheme.ts +65 -65
- package/src/structures/ITypeTuple.ts +6 -6
- package/src/structures/MethodType.ts +5 -11
- package/src/structures/ParamCategory.ts +1 -1
- package/src/structures/TypeEntry.ts +22 -22
- package/src/utils/ArrayUtil.ts +26 -26
- package/src/utils/FileRetriever.ts +22 -22
- package/src/utils/ImportDictionary.ts +125 -128
- package/src/utils/MapUtil.ts +14 -14
- package/src/utils/PathUtil.ts +10 -10
- package/src/utils/SourceFinder.ts +66 -70
- package/src/utils/StripEnums.ts +5 -10
|
@@ -1,124 +1,115 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import { IController } from "../structures/IController";
|
|
5
|
-
import { INestiaProject } from "../structures/INestiaProject";
|
|
6
|
-
import { IRoute } from "../structures/IRoute";
|
|
7
|
-
import { ITypeTuple } from "../structures/ITypeTuple";
|
|
8
|
-
import { GenericAnalyzer } from "./GenericAnalyzer";
|
|
9
|
-
import { ImportAnalyzer } from "./ImportAnalyzer";
|
|
10
|
-
|
|
11
|
-
export namespace ExceptionAnalyzer {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const TYPED_EXCEPTION_PATH = path.join(
|
|
118
|
-
"node_modules",
|
|
119
|
-
"@nestia",
|
|
120
|
-
"core",
|
|
121
|
-
"lib",
|
|
122
|
-
"decorators",
|
|
123
|
-
"TypedException.d.ts",
|
|
124
|
-
);
|
|
1
|
+
import path from "path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { IController } from "../structures/IController";
|
|
5
|
+
import { INestiaProject } from "../structures/INestiaProject";
|
|
6
|
+
import { IRoute } from "../structures/IRoute";
|
|
7
|
+
import { ITypeTuple } from "../structures/ITypeTuple";
|
|
8
|
+
import { GenericAnalyzer } from "./GenericAnalyzer";
|
|
9
|
+
import { ImportAnalyzer } from "./ImportAnalyzer";
|
|
10
|
+
|
|
11
|
+
export namespace ExceptionAnalyzer {
|
|
12
|
+
export const analyze =
|
|
13
|
+
(project: INestiaProject) =>
|
|
14
|
+
(
|
|
15
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
16
|
+
importDict: ImportAnalyzer.Dictionary,
|
|
17
|
+
) =>
|
|
18
|
+
(controller: IController, func: IController.IFunction) =>
|
|
19
|
+
(
|
|
20
|
+
declaration: ts.MethodDeclaration,
|
|
21
|
+
): Record<number | "2XX" | "3XX" | "4XX" | "5XX", IRoute.IOutput> => {
|
|
22
|
+
const output: Record<
|
|
23
|
+
number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
24
|
+
IRoute.IOutput
|
|
25
|
+
> = {} as any;
|
|
26
|
+
for (const decorator of declaration.modifiers ?? [])
|
|
27
|
+
if (ts.isDecorator(decorator))
|
|
28
|
+
analyzeTyped(project)(genericDict, importDict)(controller, func)(
|
|
29
|
+
output,
|
|
30
|
+
)(decorator);
|
|
31
|
+
return output;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const analyzeTyped =
|
|
35
|
+
(project: INestiaProject) =>
|
|
36
|
+
(
|
|
37
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
38
|
+
importDict: ImportAnalyzer.Dictionary,
|
|
39
|
+
) =>
|
|
40
|
+
(controller: IController, func: IController.IFunction) =>
|
|
41
|
+
(output: Record<number | "2XX" | "3XX" | "4XX" | "5XX", IRoute.IOutput>) =>
|
|
42
|
+
(decorator: ts.Decorator): boolean => {
|
|
43
|
+
// CHECK DECORATOR
|
|
44
|
+
if (!ts.isCallExpression(decorator.expression)) return false;
|
|
45
|
+
else if ((decorator.expression.typeArguments ?? []).length !== 1)
|
|
46
|
+
return false;
|
|
47
|
+
|
|
48
|
+
// CHECK SIGNATURE
|
|
49
|
+
const signature: ts.Signature | undefined =
|
|
50
|
+
project.checker.getResolvedSignature(decorator.expression);
|
|
51
|
+
if (!signature || !signature.declaration) return false;
|
|
52
|
+
else if (
|
|
53
|
+
path
|
|
54
|
+
.resolve(signature.declaration.getSourceFile().fileName)
|
|
55
|
+
.indexOf(TYPED_EXCEPTION_PATH) === -1
|
|
56
|
+
)
|
|
57
|
+
return false;
|
|
58
|
+
|
|
59
|
+
// GET TYPE INFO
|
|
60
|
+
const node: ts.TypeNode = decorator.expression.typeArguments![0];
|
|
61
|
+
const type: ts.Type = project.checker.getTypeFromTypeNode(node);
|
|
62
|
+
if (type.isTypeParameter()) {
|
|
63
|
+
project.errors.push({
|
|
64
|
+
file: controller.file,
|
|
65
|
+
controller: controller.name,
|
|
66
|
+
function: func.name,
|
|
67
|
+
message: "TypedException() without generic argument specification.",
|
|
68
|
+
});
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const tuple: ITypeTuple | null = ImportAnalyzer.analyze(
|
|
73
|
+
project.checker,
|
|
74
|
+
genericDict,
|
|
75
|
+
importDict,
|
|
76
|
+
type,
|
|
77
|
+
);
|
|
78
|
+
if (tuple === null || tuple.typeName === "__type") {
|
|
79
|
+
project.errors.push({
|
|
80
|
+
file: controller.file,
|
|
81
|
+
controller: controller.name,
|
|
82
|
+
function: func.name,
|
|
83
|
+
message: "TypeException() with implicit (unnamed) type.",
|
|
84
|
+
});
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// DO ASSIGN
|
|
89
|
+
const matched: IController.IException[] = Object.entries(func.exceptions)
|
|
90
|
+
.filter(([_key, value]) =>
|
|
91
|
+
value.type.includes(" | ") && tuple.typeName.includes(" | ")
|
|
92
|
+
? value.type.split(" | ").sort().join(" | ") ===
|
|
93
|
+
tuple.typeName.split(" | ").sort().join(" | ")
|
|
94
|
+
: value.type === tuple.typeName,
|
|
95
|
+
)
|
|
96
|
+
.map(([_key, value]) => value);
|
|
97
|
+
for (const m of matched)
|
|
98
|
+
output[m.status] = {
|
|
99
|
+
type: tuple.type,
|
|
100
|
+
typeName: tuple.typeName,
|
|
101
|
+
contentType: "application/json",
|
|
102
|
+
description: m.description,
|
|
103
|
+
};
|
|
104
|
+
return true;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const TYPED_EXCEPTION_PATH = path.join(
|
|
109
|
+
"node_modules",
|
|
110
|
+
"@nestia",
|
|
111
|
+
"core",
|
|
112
|
+
"lib",
|
|
113
|
+
"decorators",
|
|
114
|
+
"TypedException.d.ts",
|
|
115
|
+
);
|
|
@@ -1,57 +1,51 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
export namespace GenericAnalyzer {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// RECUSRIVE EXPLORATION
|
|
54
|
-
explore(checker, dict, superNode);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export namespace GenericAnalyzer {
|
|
4
|
+
export type Dictionary = WeakMap<ts.Type, ts.Type>;
|
|
5
|
+
|
|
6
|
+
export function analyze(
|
|
7
|
+
checker: ts.TypeChecker,
|
|
8
|
+
classNode: ts.ClassDeclaration,
|
|
9
|
+
): Dictionary {
|
|
10
|
+
const dict: Dictionary = new WeakMap();
|
|
11
|
+
explore(checker, dict, classNode);
|
|
12
|
+
return dict;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function explore(
|
|
16
|
+
checker: ts.TypeChecker,
|
|
17
|
+
dict: Dictionary,
|
|
18
|
+
classNode: ts.ClassDeclaration,
|
|
19
|
+
): void {
|
|
20
|
+
if (classNode.heritageClauses === undefined) return;
|
|
21
|
+
|
|
22
|
+
for (const heritage of classNode.heritageClauses)
|
|
23
|
+
for (const hType of heritage.types) {
|
|
24
|
+
// MUST BE CLASS
|
|
25
|
+
const expression: ts.Type = checker.getTypeAtLocation(hType.expression);
|
|
26
|
+
const superNode: ts.Declaration | undefined =
|
|
27
|
+
expression.symbol.getDeclarations()?.[0];
|
|
28
|
+
|
|
29
|
+
if (superNode === undefined || !ts.isClassDeclaration(superNode))
|
|
30
|
+
continue;
|
|
31
|
+
|
|
32
|
+
// SPECIFY GENERICS
|
|
33
|
+
const usages: ReadonlyArray<ts.TypeNode> = hType.typeArguments || [];
|
|
34
|
+
const parameters: ReadonlyArray<ts.TypeParameterDeclaration> =
|
|
35
|
+
superNode.typeParameters || [];
|
|
36
|
+
|
|
37
|
+
parameters.forEach((param, index) => {
|
|
38
|
+
const paramType: ts.Type = checker.getTypeAtLocation(param);
|
|
39
|
+
const usageType: ts.Type =
|
|
40
|
+
usages[index] !== undefined
|
|
41
|
+
? checker.getTypeAtLocation(usages[index])
|
|
42
|
+
: checker.getTypeAtLocation(param.default!);
|
|
43
|
+
|
|
44
|
+
dict.set(paramType, usageType);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// RECUSRIVE EXPLORATION
|
|
48
|
+
explore(checker, dict, superNode);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -1,159 +1,138 @@
|
|
|
1
|
-
import { HashMap } from "tstl/container/HashMap";
|
|
2
|
-
import { HashSet } from "tstl/container/HashSet";
|
|
3
|
-
import ts from "typescript";
|
|
4
|
-
|
|
5
|
-
import { ITypeTuple } from "../structures/ITypeTuple";
|
|
6
|
-
import { GenericAnalyzer } from "./GenericAnalyzer";
|
|
7
|
-
|
|
8
|
-
export namespace ImportAnalyzer {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
.map((child) =>
|
|
140
|
-
explore_escaped_name(
|
|
141
|
-
checker,
|
|
142
|
-
genericDict,
|
|
143
|
-
importDict,
|
|
144
|
-
child,
|
|
145
|
-
),
|
|
146
|
-
)
|
|
147
|
-
.join(", ")}>`
|
|
148
|
-
: name;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function explore_name(name: string, decl?: ts.Node): string {
|
|
152
|
-
return decl && ts.isModuleBlock(decl)
|
|
153
|
-
? explore_name(
|
|
154
|
-
`${decl.parent.name.getFullText().trim()}.${name}`,
|
|
155
|
-
decl.parent.parent,
|
|
156
|
-
)
|
|
157
|
-
: name;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
1
|
+
import { HashMap } from "tstl/container/HashMap";
|
|
2
|
+
import { HashSet } from "tstl/container/HashSet";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
|
|
5
|
+
import { ITypeTuple } from "../structures/ITypeTuple";
|
|
6
|
+
import { GenericAnalyzer } from "./GenericAnalyzer";
|
|
7
|
+
|
|
8
|
+
export namespace ImportAnalyzer {
|
|
9
|
+
export interface IOutput {
|
|
10
|
+
features: [string, string[]][];
|
|
11
|
+
alias: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type Dictionary = HashMap<string, HashSet<string>>;
|
|
15
|
+
|
|
16
|
+
export function analyze(
|
|
17
|
+
checker: ts.TypeChecker,
|
|
18
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
19
|
+
importDict: Dictionary,
|
|
20
|
+
type: ts.Type,
|
|
21
|
+
): ITypeTuple | null {
|
|
22
|
+
type = get_type(checker, type);
|
|
23
|
+
explore_escaped_name(checker, genericDict, importDict, type);
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
return {
|
|
27
|
+
type,
|
|
28
|
+
typeName: explore_escaped_name(checker, genericDict, importDict, type),
|
|
29
|
+
};
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* ---------------------------------------------------------
|
|
36
|
+
TYPE
|
|
37
|
+
--------------------------------------------------------- */
|
|
38
|
+
function get_type(checker: ts.TypeChecker, type: ts.Type): ts.Type {
|
|
39
|
+
const symbol: ts.Symbol | undefined = type.getSymbol();
|
|
40
|
+
return symbol && get_name(symbol) === "Promise"
|
|
41
|
+
? escape_promise(checker, type)
|
|
42
|
+
: type;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function escape_promise(checker: ts.TypeChecker, type: ts.Type): ts.Type {
|
|
46
|
+
const generic: readonly ts.Type[] = checker.getTypeArguments(
|
|
47
|
+
type as ts.TypeReference,
|
|
48
|
+
);
|
|
49
|
+
if (generic.length !== 1)
|
|
50
|
+
throw new Error(
|
|
51
|
+
"Error on ImportAnalyzer.analyze(): invalid promise type.",
|
|
52
|
+
);
|
|
53
|
+
return generic[0];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function get_name(symbol: ts.Symbol): string {
|
|
57
|
+
return explore_name(
|
|
58
|
+
symbol.escapedName.toString(),
|
|
59
|
+
symbol.getDeclarations()?.[0]?.parent,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* ---------------------------------------------------------
|
|
64
|
+
ESCAPED TEXT WITH IMPORT STATEMENTS
|
|
65
|
+
--------------------------------------------------------- */
|
|
66
|
+
function explore_escaped_name(
|
|
67
|
+
checker: ts.TypeChecker,
|
|
68
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
69
|
+
importDict: Dictionary,
|
|
70
|
+
type: ts.Type,
|
|
71
|
+
): string {
|
|
72
|
+
//----
|
|
73
|
+
// CONDITIONAL BRANCHES
|
|
74
|
+
//----
|
|
75
|
+
// DECOMPOSE GENERIC ARGUMENT
|
|
76
|
+
while (genericDict.has(type) === true) type = genericDict.get(type)!;
|
|
77
|
+
|
|
78
|
+
// PRIMITIVE
|
|
79
|
+
const symbol: ts.Symbol | undefined = type.aliasSymbol ?? type.getSymbol();
|
|
80
|
+
|
|
81
|
+
// UNION OR INTERSECT
|
|
82
|
+
if (type.aliasSymbol === undefined && type.isUnionOrIntersection()) {
|
|
83
|
+
const joiner: string = type.isIntersection() ? " & " : " | ";
|
|
84
|
+
return type.types
|
|
85
|
+
.map((child) =>
|
|
86
|
+
explore_escaped_name(checker, genericDict, importDict, child),
|
|
87
|
+
)
|
|
88
|
+
.join(joiner);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// NO SYMBOL
|
|
92
|
+
else if (symbol === undefined)
|
|
93
|
+
return checker.typeToString(
|
|
94
|
+
type,
|
|
95
|
+
undefined,
|
|
96
|
+
ts.TypeFormatFlags.NoTruncation,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
//----
|
|
100
|
+
// SPECIALIZATION
|
|
101
|
+
//----
|
|
102
|
+
const name: string = get_name(symbol);
|
|
103
|
+
const sourceFile: ts.SourceFile | undefined =
|
|
104
|
+
symbol.declarations?.[0]?.getSourceFile();
|
|
105
|
+
if (sourceFile === undefined) return name;
|
|
106
|
+
|
|
107
|
+
if (sourceFile.fileName.indexOf("typescript/lib") === -1) {
|
|
108
|
+
const set: HashSet<string> = importDict.take(
|
|
109
|
+
sourceFile.fileName,
|
|
110
|
+
() => new HashSet(),
|
|
111
|
+
);
|
|
112
|
+
set.insert(name.split(".")[0]);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// CHECK GENERIC
|
|
116
|
+
const generic: readonly ts.Type[] = type.aliasSymbol
|
|
117
|
+
? type.aliasTypeArguments || []
|
|
118
|
+
: checker.getTypeArguments(type as ts.TypeReference);
|
|
119
|
+
return generic.length
|
|
120
|
+
? name === "Promise"
|
|
121
|
+
? explore_escaped_name(checker, genericDict, importDict, generic[0])
|
|
122
|
+
: `${name}<${generic
|
|
123
|
+
.map((child) =>
|
|
124
|
+
explore_escaped_name(checker, genericDict, importDict, child),
|
|
125
|
+
)
|
|
126
|
+
.join(", ")}>`
|
|
127
|
+
: name;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function explore_name(name: string, decl?: ts.Node): string {
|
|
131
|
+
return decl && ts.isModuleBlock(decl)
|
|
132
|
+
? explore_name(
|
|
133
|
+
`${decl.parent.name.getFullText().trim()}.${name}`,
|
|
134
|
+
decl.parent.parent,
|
|
135
|
+
)
|
|
136
|
+
: name;
|
|
137
|
+
}
|
|
138
|
+
}
|