@nestia/sdk 2.5.0-dev.20240131 → 2.5.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/lib/executable/sdk.js +11 -11
- package/lib/generates/SdkGenerator.js +0 -8
- package/lib/generates/SdkGenerator.js.map +1 -1
- package/lib/generates/internal/SdkAliasCollection.js +5 -4
- package/lib/generates/internal/SdkAliasCollection.js.map +1 -1
- package/lib/generates/internal/SdkFileProgrammer.js +0 -7
- package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkFunctionProgrammer.d.ts +1 -1
- package/lib/generates/internal/SdkFunctionProgrammer.js +3 -3
- package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkNamespaceProgrammer.js +1 -1
- package/lib/generates/internal/SdkNamespaceProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkRouteProgrammer.js +2 -2
- package/lib/generates/internal/SdkRouteProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkSimulationProgrammer.js +3 -3
- package/lib/generates/internal/SdkSimulationProgrammer.js.map +1 -1
- package/package.json +3 -3
- package/src/analyses/AccessorAnalyzer.ts +60 -60
- package/src/analyses/ConfigAnalyzer.ts +147 -147
- package/src/analyses/GenericAnalyzer.ts +51 -51
- package/src/analyses/ImportAnalyzer.ts +138 -138
- package/src/analyses/PathAnalyzer.ts +110 -110
- package/src/analyses/ReflectAnalyzer.ts +464 -464
- package/src/analyses/SecurityAnalyzer.ts +20 -20
- package/src/executable/internal/CommandParser.ts +15 -15
- package/src/executable/internal/NestiaConfigLoader.ts +67 -67
- package/src/executable/internal/NestiaSdkCommand.ts +60 -60
- package/src/executable/sdk.ts +73 -73
- package/src/generates/SdkGenerator.ts +0 -12
- package/src/generates/internal/SdkAliasCollection.ts +4 -3
- package/src/generates/internal/SdkDistributionComposer.ts +91 -91
- package/src/generates/internal/SdkFileProgrammer.ts +0 -9
- package/src/generates/internal/SdkFunctionProgrammer.ts +3 -3
- package/src/generates/internal/SdkNamespaceProgrammer.ts +1 -1
- package/src/generates/internal/SdkRouteDirectory.ts +17 -17
- package/src/generates/internal/SdkRouteProgrammer.ts +2 -2
- package/src/generates/internal/SdkSimulationProgrammer.ts +3 -3
- package/src/generates/internal/SwaggerSchemaGenerator.ts +444 -444
- package/src/generates/internal/SwaggerSchemaValidator.ts +198 -198
- package/src/index.ts +4 -4
- package/src/module.ts +2 -2
- package/src/structures/IController.ts +91 -91
- 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 +52 -52
- 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 -5
- 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/MapUtil.ts +14 -14
- package/src/utils/PathUtil.ts +10 -10
- package/src/utils/SourceFinder.ts +66 -66
- package/src/utils/StripEnums.ts +5 -5
- package/assets/bundle/api/utils/NestiaSimulator.ts +0 -70
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
import { INestApplication, VersioningType } from "@nestjs/common";
|
|
2
|
-
import { MODULE_PATH } from "@nestjs/common/constants";
|
|
3
|
-
import { NestContainer } from "@nestjs/core";
|
|
4
|
-
import { Module } from "@nestjs/core/injector/module";
|
|
5
|
-
import fs from "fs";
|
|
6
|
-
import path from "path";
|
|
7
|
-
import { HashMap, Pair, Singleton } from "tstl";
|
|
8
|
-
|
|
9
|
-
import { INestiaConfig } from "../INestiaConfig";
|
|
10
|
-
import { SdkGenerator } from "../generates/SdkGenerator";
|
|
11
|
-
import { INormalizedInput } from "../structures/INormalizedInput";
|
|
12
|
-
import { ArrayUtil } from "../utils/ArrayUtil";
|
|
13
|
-
import { MapUtil } from "../utils/MapUtil";
|
|
14
|
-
import { SourceFinder } from "../utils/SourceFinder";
|
|
15
|
-
|
|
16
|
-
export namespace ConfigAnalyzer {
|
|
17
|
-
export const input = (config: INestiaConfig): Promise<INormalizedInput> =>
|
|
18
|
-
MapUtil.take(memory, config, async () => {
|
|
19
|
-
const input = config.input;
|
|
20
|
-
if (Array.isArray(input)) return transform_input(config)(input);
|
|
21
|
-
else if (typeof input === "function")
|
|
22
|
-
return analyze_application(await input());
|
|
23
|
-
else if (typeof input === "object")
|
|
24
|
-
if (input === null)
|
|
25
|
-
throw new Error("Invalid input config. It can't be null.");
|
|
26
|
-
else return transform_input(config)(input.include, input.exclude);
|
|
27
|
-
else if (typeof input === "string")
|
|
28
|
-
return transform_input(config)([input]);
|
|
29
|
-
else throw new Error("Invalid input config.");
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
const analyze_application = async (
|
|
33
|
-
app: INestApplication,
|
|
34
|
-
): Promise<INormalizedInput> => {
|
|
35
|
-
const files: HashMap<Pair<Function, string>, Set<string>> = new HashMap();
|
|
36
|
-
const container: NestContainer = (app as any).container as NestContainer;
|
|
37
|
-
const modules: Module[] = [...container.getModules().values()].filter(
|
|
38
|
-
(m) => !!m.controllers.size,
|
|
39
|
-
);
|
|
40
|
-
for (const m of modules) {
|
|
41
|
-
const path: string =
|
|
42
|
-
Reflect.getMetadata(
|
|
43
|
-
MODULE_PATH + container.getModules().applicationId,
|
|
44
|
-
m.metatype,
|
|
45
|
-
) ??
|
|
46
|
-
Reflect.getMetadata(MODULE_PATH, m.metatype) ??
|
|
47
|
-
"";
|
|
48
|
-
for (const controller of [...m.controllers.keys()]) {
|
|
49
|
-
const file: string | null =
|
|
50
|
-
(await require("get-function-location")(controller))?.source ?? null;
|
|
51
|
-
if (file === null) continue;
|
|
52
|
-
|
|
53
|
-
const location: string = normalize_file(file);
|
|
54
|
-
if (location.length === 0) continue;
|
|
55
|
-
|
|
56
|
-
const key: Pair<Function, string> = new Pair(
|
|
57
|
-
controller as Function,
|
|
58
|
-
location,
|
|
59
|
-
);
|
|
60
|
-
files.take(key, () => new Set([])).add(path);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const versioning = (app as any).config?.versioningOptions;
|
|
65
|
-
return {
|
|
66
|
-
include: files.toJSON().map((pair) => ({
|
|
67
|
-
controller: pair.first.first,
|
|
68
|
-
file: decodeURIComponent(pair.first.second),
|
|
69
|
-
paths: [...pair.second.values()],
|
|
70
|
-
})),
|
|
71
|
-
globalPrefix:
|
|
72
|
-
typeof (app as any).config?.globalPrefix === "string"
|
|
73
|
-
? {
|
|
74
|
-
prefix: (app as any).config.globalPrefix,
|
|
75
|
-
exclude: (app as any).config.globalPrefixOptions?.exclude ?? {},
|
|
76
|
-
}
|
|
77
|
-
: undefined,
|
|
78
|
-
versioning:
|
|
79
|
-
versioning === undefined || versioning.type !== VersioningType.URI
|
|
80
|
-
? undefined
|
|
81
|
-
: {
|
|
82
|
-
prefix:
|
|
83
|
-
versioning.prefix === undefined || versioning.prefix === false
|
|
84
|
-
? "v"
|
|
85
|
-
: versioning.prefix,
|
|
86
|
-
defaultVersion: versioning.defaultVersion,
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const normalize_file = (str: string) =>
|
|
92
|
-
str.substring(
|
|
93
|
-
str.startsWith("file:///")
|
|
94
|
-
? process.cwd()[0] === "/"
|
|
95
|
-
? 7
|
|
96
|
-
: 8
|
|
97
|
-
: str.startsWith("file://")
|
|
98
|
-
? 7
|
|
99
|
-
: 0,
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
const transform_input =
|
|
103
|
-
(config: INestiaConfig) =>
|
|
104
|
-
async (include: string[], exclude?: string[]) => ({
|
|
105
|
-
include: (
|
|
106
|
-
await SourceFinder.find({
|
|
107
|
-
include,
|
|
108
|
-
exclude,
|
|
109
|
-
filter: filter(config),
|
|
110
|
-
})
|
|
111
|
-
).map((file) => ({
|
|
112
|
-
paths: [""],
|
|
113
|
-
file,
|
|
114
|
-
})),
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
const filter =
|
|
118
|
-
(config: INestiaConfig) =>
|
|
119
|
-
async (location: string): Promise<boolean> =>
|
|
120
|
-
location.endsWith(".ts") &&
|
|
121
|
-
!location.endsWith(".d.ts") &&
|
|
122
|
-
(config.output === undefined ||
|
|
123
|
-
(location.indexOf(path.join(config.output, "functional")) === -1 &&
|
|
124
|
-
(await (
|
|
125
|
-
await bundler.get(config.output)
|
|
126
|
-
)(location))) === false);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const memory = new Map<INestiaConfig, Promise<INormalizedInput>>();
|
|
130
|
-
const bundler = new Singleton(async (output: string) => {
|
|
131
|
-
const assets: string[] = await fs.promises.readdir(SdkGenerator.BUNDLE_PATH);
|
|
132
|
-
const tuples: Pair<string, boolean>[] = await ArrayUtil.asyncMap(
|
|
133
|
-
assets,
|
|
134
|
-
async (file) => {
|
|
135
|
-
const relative: string = path.join(output, file);
|
|
136
|
-
const location: string = path.join(SdkGenerator.BUNDLE_PATH, file);
|
|
137
|
-
const stats: fs.Stats = await fs.promises.stat(location);
|
|
138
|
-
return new Pair(relative, stats.isDirectory());
|
|
139
|
-
},
|
|
140
|
-
);
|
|
141
|
-
return async (file: string): Promise<boolean> => {
|
|
142
|
-
for (const it of tuples)
|
|
143
|
-
if (it.second === false && file === it.first) return true;
|
|
144
|
-
else if (it.second === true && file.indexOf(it.first) === 0) return true;
|
|
145
|
-
return false;
|
|
146
|
-
};
|
|
147
|
-
});
|
|
1
|
+
import { INestApplication, VersioningType } from "@nestjs/common";
|
|
2
|
+
import { MODULE_PATH } from "@nestjs/common/constants";
|
|
3
|
+
import { NestContainer } from "@nestjs/core";
|
|
4
|
+
import { Module } from "@nestjs/core/injector/module";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { HashMap, Pair, Singleton } from "tstl";
|
|
8
|
+
|
|
9
|
+
import { INestiaConfig } from "../INestiaConfig";
|
|
10
|
+
import { SdkGenerator } from "../generates/SdkGenerator";
|
|
11
|
+
import { INormalizedInput } from "../structures/INormalizedInput";
|
|
12
|
+
import { ArrayUtil } from "../utils/ArrayUtil";
|
|
13
|
+
import { MapUtil } from "../utils/MapUtil";
|
|
14
|
+
import { SourceFinder } from "../utils/SourceFinder";
|
|
15
|
+
|
|
16
|
+
export namespace ConfigAnalyzer {
|
|
17
|
+
export const input = (config: INestiaConfig): Promise<INormalizedInput> =>
|
|
18
|
+
MapUtil.take(memory, config, async () => {
|
|
19
|
+
const input = config.input;
|
|
20
|
+
if (Array.isArray(input)) return transform_input(config)(input);
|
|
21
|
+
else if (typeof input === "function")
|
|
22
|
+
return analyze_application(await input());
|
|
23
|
+
else if (typeof input === "object")
|
|
24
|
+
if (input === null)
|
|
25
|
+
throw new Error("Invalid input config. It can't be null.");
|
|
26
|
+
else return transform_input(config)(input.include, input.exclude);
|
|
27
|
+
else if (typeof input === "string")
|
|
28
|
+
return transform_input(config)([input]);
|
|
29
|
+
else throw new Error("Invalid input config.");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const analyze_application = async (
|
|
33
|
+
app: INestApplication,
|
|
34
|
+
): Promise<INormalizedInput> => {
|
|
35
|
+
const files: HashMap<Pair<Function, string>, Set<string>> = new HashMap();
|
|
36
|
+
const container: NestContainer = (app as any).container as NestContainer;
|
|
37
|
+
const modules: Module[] = [...container.getModules().values()].filter(
|
|
38
|
+
(m) => !!m.controllers.size,
|
|
39
|
+
);
|
|
40
|
+
for (const m of modules) {
|
|
41
|
+
const path: string =
|
|
42
|
+
Reflect.getMetadata(
|
|
43
|
+
MODULE_PATH + container.getModules().applicationId,
|
|
44
|
+
m.metatype,
|
|
45
|
+
) ??
|
|
46
|
+
Reflect.getMetadata(MODULE_PATH, m.metatype) ??
|
|
47
|
+
"";
|
|
48
|
+
for (const controller of [...m.controllers.keys()]) {
|
|
49
|
+
const file: string | null =
|
|
50
|
+
(await require("get-function-location")(controller))?.source ?? null;
|
|
51
|
+
if (file === null) continue;
|
|
52
|
+
|
|
53
|
+
const location: string = normalize_file(file);
|
|
54
|
+
if (location.length === 0) continue;
|
|
55
|
+
|
|
56
|
+
const key: Pair<Function, string> = new Pair(
|
|
57
|
+
controller as Function,
|
|
58
|
+
location,
|
|
59
|
+
);
|
|
60
|
+
files.take(key, () => new Set([])).add(path);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const versioning = (app as any).config?.versioningOptions;
|
|
65
|
+
return {
|
|
66
|
+
include: files.toJSON().map((pair) => ({
|
|
67
|
+
controller: pair.first.first,
|
|
68
|
+
file: decodeURIComponent(pair.first.second),
|
|
69
|
+
paths: [...pair.second.values()],
|
|
70
|
+
})),
|
|
71
|
+
globalPrefix:
|
|
72
|
+
typeof (app as any).config?.globalPrefix === "string"
|
|
73
|
+
? {
|
|
74
|
+
prefix: (app as any).config.globalPrefix,
|
|
75
|
+
exclude: (app as any).config.globalPrefixOptions?.exclude ?? {},
|
|
76
|
+
}
|
|
77
|
+
: undefined,
|
|
78
|
+
versioning:
|
|
79
|
+
versioning === undefined || versioning.type !== VersioningType.URI
|
|
80
|
+
? undefined
|
|
81
|
+
: {
|
|
82
|
+
prefix:
|
|
83
|
+
versioning.prefix === undefined || versioning.prefix === false
|
|
84
|
+
? "v"
|
|
85
|
+
: versioning.prefix,
|
|
86
|
+
defaultVersion: versioning.defaultVersion,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const normalize_file = (str: string) =>
|
|
92
|
+
str.substring(
|
|
93
|
+
str.startsWith("file:///")
|
|
94
|
+
? process.cwd()[0] === "/"
|
|
95
|
+
? 7
|
|
96
|
+
: 8
|
|
97
|
+
: str.startsWith("file://")
|
|
98
|
+
? 7
|
|
99
|
+
: 0,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const transform_input =
|
|
103
|
+
(config: INestiaConfig) =>
|
|
104
|
+
async (include: string[], exclude?: string[]) => ({
|
|
105
|
+
include: (
|
|
106
|
+
await SourceFinder.find({
|
|
107
|
+
include,
|
|
108
|
+
exclude,
|
|
109
|
+
filter: filter(config),
|
|
110
|
+
})
|
|
111
|
+
).map((file) => ({
|
|
112
|
+
paths: [""],
|
|
113
|
+
file,
|
|
114
|
+
})),
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const filter =
|
|
118
|
+
(config: INestiaConfig) =>
|
|
119
|
+
async (location: string): Promise<boolean> =>
|
|
120
|
+
location.endsWith(".ts") &&
|
|
121
|
+
!location.endsWith(".d.ts") &&
|
|
122
|
+
(config.output === undefined ||
|
|
123
|
+
(location.indexOf(path.join(config.output, "functional")) === -1 &&
|
|
124
|
+
(await (
|
|
125
|
+
await bundler.get(config.output)
|
|
126
|
+
)(location))) === false);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const memory = new Map<INestiaConfig, Promise<INormalizedInput>>();
|
|
130
|
+
const bundler = new Singleton(async (output: string) => {
|
|
131
|
+
const assets: string[] = await fs.promises.readdir(SdkGenerator.BUNDLE_PATH);
|
|
132
|
+
const tuples: Pair<string, boolean>[] = await ArrayUtil.asyncMap(
|
|
133
|
+
assets,
|
|
134
|
+
async (file) => {
|
|
135
|
+
const relative: string = path.join(output, file);
|
|
136
|
+
const location: string = path.join(SdkGenerator.BUNDLE_PATH, file);
|
|
137
|
+
const stats: fs.Stats = await fs.promises.stat(location);
|
|
138
|
+
return new Pair(relative, stats.isDirectory());
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
return async (file: string): Promise<boolean> => {
|
|
142
|
+
for (const it of tuples)
|
|
143
|
+
if (it.second === false && file === it.first) return true;
|
|
144
|
+
else if (it.second === true && file.indexOf(it.first) === 0) return true;
|
|
145
|
+
return false;
|
|
146
|
+
};
|
|
147
|
+
});
|
|
@@ -1,51 +1,51 @@
|
|
|
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
|
+
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,138 +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
|
-
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
|
-
}
|
|
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
|
+
}
|