@nestia/sdk 2.6.3 → 2.6.4-dev.20240401

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,137 +1,137 @@
1
- import { HashMap, HashSet } from "tstl";
2
- import ts from "typescript";
3
-
4
- import { ITypeTuple } from "../structures/ITypeTuple";
5
- import { GenericAnalyzer } from "./GenericAnalyzer";
6
-
7
- export namespace ImportAnalyzer {
8
- export interface IOutput {
9
- features: [string, string[]][];
10
- alias: string;
11
- }
12
-
13
- export type Dictionary = HashMap<string, HashSet<string>>;
14
-
15
- export function analyze(
16
- checker: ts.TypeChecker,
17
- genericDict: GenericAnalyzer.Dictionary,
18
- importDict: Dictionary,
19
- type: ts.Type,
20
- ): ITypeTuple | null {
21
- type = get_type(checker, type);
22
- explore_escaped_name(checker, genericDict, importDict, type);
23
-
24
- try {
25
- return {
26
- type,
27
- typeName: explore_escaped_name(checker, genericDict, importDict, type),
28
- };
29
- } catch {
30
- return null;
31
- }
32
- }
33
-
34
- /* ---------------------------------------------------------
35
- TYPE
36
- --------------------------------------------------------- */
37
- function get_type(checker: ts.TypeChecker, type: ts.Type): ts.Type {
38
- const symbol: ts.Symbol | undefined = type.getSymbol();
39
- return symbol && get_name(symbol) === "Promise"
40
- ? escape_promise(checker, type)
41
- : type;
42
- }
43
-
44
- function escape_promise(checker: ts.TypeChecker, type: ts.Type): ts.Type {
45
- const generic: readonly ts.Type[] = checker.getTypeArguments(
46
- type as ts.TypeReference,
47
- );
48
- if (generic.length !== 1)
49
- throw new Error(
50
- "Error on ImportAnalyzer.analyze(): invalid promise type.",
51
- );
52
- return generic[0];
53
- }
54
-
55
- function get_name(symbol: ts.Symbol): string {
56
- return explore_name(
57
- symbol.escapedName.toString(),
58
- symbol.getDeclarations()?.[0]?.parent,
59
- );
60
- }
61
-
62
- /* ---------------------------------------------------------
63
- ESCAPED TEXT WITH IMPORT STATEMENTS
64
- --------------------------------------------------------- */
65
- function explore_escaped_name(
66
- checker: ts.TypeChecker,
67
- genericDict: GenericAnalyzer.Dictionary,
68
- importDict: Dictionary,
69
- type: ts.Type,
70
- ): string {
71
- //----
72
- // CONDITIONAL BRANCHES
73
- //----
74
- // DECOMPOSE GENERIC ARGUMENT
75
- while (genericDict.has(type) === true) type = genericDict.get(type)!;
76
-
77
- // PRIMITIVE
78
- const symbol: ts.Symbol | undefined = type.aliasSymbol ?? type.getSymbol();
79
-
80
- // UNION OR INTERSECT
81
- if (type.aliasSymbol === undefined && type.isUnionOrIntersection()) {
82
- const joiner: string = type.isIntersection() ? " & " : " | ";
83
- return type.types
84
- .map((child) =>
85
- explore_escaped_name(checker, genericDict, importDict, child),
86
- )
87
- .join(joiner);
88
- }
89
-
90
- // NO SYMBOL
91
- else if (symbol === undefined)
92
- return checker.typeToString(
93
- type,
94
- undefined,
95
- ts.TypeFormatFlags.NoTruncation,
96
- );
97
-
98
- //----
99
- // SPECIALIZATION
100
- //----
101
- const name: string = get_name(symbol);
102
- const sourceFile: ts.SourceFile | undefined =
103
- symbol.declarations?.[0]?.getSourceFile();
104
- if (sourceFile === undefined) return name;
105
-
106
- if (sourceFile.fileName.indexOf("typescript/lib") === -1) {
107
- const set: HashSet<string> = importDict.take(
108
- sourceFile.fileName,
109
- () => new HashSet(),
110
- );
111
- set.insert(name.split(".")[0]);
112
- }
113
-
114
- // CHECK GENERIC
115
- const generic: readonly ts.Type[] = type.aliasSymbol
116
- ? type.aliasTypeArguments || []
117
- : checker.getTypeArguments(type as ts.TypeReference);
118
- return generic.length
119
- ? name === "Promise"
120
- ? explore_escaped_name(checker, genericDict, importDict, generic[0])
121
- : `${name}<${generic
122
- .map((child) =>
123
- explore_escaped_name(checker, genericDict, importDict, child),
124
- )
125
- .join(", ")}>`
126
- : name;
127
- }
128
-
129
- function explore_name(name: string, decl?: ts.Node): string {
130
- return decl && ts.isModuleBlock(decl)
131
- ? explore_name(
132
- `${decl.parent.name.getFullText().trim()}.${name}`,
133
- decl.parent.parent,
134
- )
135
- : name;
136
- }
137
- }
1
+ import { HashMap, HashSet } from "tstl";
2
+ import ts from "typescript";
3
+
4
+ import { ITypeTuple } from "../structures/ITypeTuple";
5
+ import { GenericAnalyzer } from "./GenericAnalyzer";
6
+
7
+ export namespace ImportAnalyzer {
8
+ export interface IOutput {
9
+ features: [string, string[]][];
10
+ alias: string;
11
+ }
12
+
13
+ export type Dictionary = HashMap<string, HashSet<string>>;
14
+
15
+ export function analyze(
16
+ checker: ts.TypeChecker,
17
+ genericDict: GenericAnalyzer.Dictionary,
18
+ importDict: Dictionary,
19
+ type: ts.Type,
20
+ ): ITypeTuple | null {
21
+ type = get_type(checker, type);
22
+ explore_escaped_name(checker, genericDict, importDict, type);
23
+
24
+ try {
25
+ return {
26
+ type,
27
+ typeName: explore_escaped_name(checker, genericDict, importDict, type),
28
+ };
29
+ } catch {
30
+ return null;
31
+ }
32
+ }
33
+
34
+ /* ---------------------------------------------------------
35
+ TYPE
36
+ --------------------------------------------------------- */
37
+ function get_type(checker: ts.TypeChecker, type: ts.Type): ts.Type {
38
+ const symbol: ts.Symbol | undefined = type.getSymbol();
39
+ return symbol && get_name(symbol) === "Promise"
40
+ ? escape_promise(checker, type)
41
+ : type;
42
+ }
43
+
44
+ function escape_promise(checker: ts.TypeChecker, type: ts.Type): ts.Type {
45
+ const generic: readonly ts.Type[] = checker.getTypeArguments(
46
+ type as ts.TypeReference,
47
+ );
48
+ if (generic.length !== 1)
49
+ throw new Error(
50
+ "Error on ImportAnalyzer.analyze(): invalid promise type.",
51
+ );
52
+ return generic[0];
53
+ }
54
+
55
+ function get_name(symbol: ts.Symbol): string {
56
+ return explore_name(
57
+ symbol.escapedName.toString(),
58
+ symbol.getDeclarations()?.[0]?.parent,
59
+ );
60
+ }
61
+
62
+ /* ---------------------------------------------------------
63
+ ESCAPED TEXT WITH IMPORT STATEMENTS
64
+ --------------------------------------------------------- */
65
+ function explore_escaped_name(
66
+ checker: ts.TypeChecker,
67
+ genericDict: GenericAnalyzer.Dictionary,
68
+ importDict: Dictionary,
69
+ type: ts.Type,
70
+ ): string {
71
+ //----
72
+ // CONDITIONAL BRANCHES
73
+ //----
74
+ // DECOMPOSE GENERIC ARGUMENT
75
+ while (genericDict.has(type) === true) type = genericDict.get(type)!;
76
+
77
+ // PRIMITIVE
78
+ const symbol: ts.Symbol | undefined = type.aliasSymbol ?? type.getSymbol();
79
+
80
+ // UNION OR INTERSECT
81
+ if (type.aliasSymbol === undefined && type.isUnionOrIntersection()) {
82
+ const joiner: string = type.isIntersection() ? " & " : " | ";
83
+ return type.types
84
+ .map((child) =>
85
+ explore_escaped_name(checker, genericDict, importDict, child),
86
+ )
87
+ .join(joiner);
88
+ }
89
+
90
+ // NO SYMBOL
91
+ else if (symbol === undefined)
92
+ return checker.typeToString(
93
+ type,
94
+ undefined,
95
+ ts.TypeFormatFlags.NoTruncation,
96
+ );
97
+
98
+ //----
99
+ // SPECIALIZATION
100
+ //----
101
+ const name: string = get_name(symbol);
102
+ const sourceFile: ts.SourceFile | undefined =
103
+ symbol.declarations?.[0]?.getSourceFile();
104
+ if (sourceFile === undefined) return name;
105
+
106
+ if (sourceFile.fileName.indexOf("typescript/lib") === -1) {
107
+ const set: HashSet<string> = importDict.take(
108
+ sourceFile.fileName,
109
+ () => new HashSet(),
110
+ );
111
+ set.insert(name.split(".")[0]);
112
+ }
113
+
114
+ // CHECK GENERIC
115
+ const generic: readonly ts.Type[] = type.aliasSymbol
116
+ ? type.aliasTypeArguments || []
117
+ : checker.getTypeArguments(type as ts.TypeReference);
118
+ return generic.length
119
+ ? name === "Promise"
120
+ ? explore_escaped_name(checker, genericDict, importDict, generic[0])
121
+ : `${name}<${generic
122
+ .map((child) =>
123
+ explore_escaped_name(checker, genericDict, importDict, child),
124
+ )
125
+ .join(", ")}>`
126
+ : name;
127
+ }
128
+
129
+ function explore_name(name: string, decl?: ts.Node): string {
130
+ return decl && ts.isModuleBlock(decl)
131
+ ? explore_name(
132
+ `${decl.parent.name.getFullText().trim()}.${name}`,
133
+ decl.parent.parent,
134
+ )
135
+ : name;
136
+ }
137
+ }