-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand-types.js
221 lines (174 loc) · 5.81 KB
/
expand-types.js
1
2
3
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// @ts-check
import typeScript from "typescript";
// import * as fs from "fs"
import { globSync } from "glob";
/**
* @typedef {Array<string | TypeArray>} TypeArray
*/
/** @typedef {{
name?: string;
fileName?: string;
documentation?: string;
type?: string;
constructors?: DocEntry[];
parameters?: DocEntry[];
returnType?: string;
}} DocEntry */
class ExpandTypes {
/**
* @param {Object} options
* @param {string | string[]} options.globs
* @param {import("typescript") | null | undefined} [options.ts]
* @param {import("typescript").Program | null | undefined} [options.program]
*/
constructor({ globs, ts = null, program = null }) {
this.name = "expand-types-plugin";
this.ts = ts || typeScript;
this.program = program;
this.globs = globSync(globs);
/**
* @type {import("typescript").TypeChecker | null | undefined}
*/
this.typeChecker = this.program?.getTypeChecker();
this.sourceFiles = new Map();
}
createProgram = () => {
if (!this.ts) throw Error("no TS instance Found");
const program = this.ts.createProgram(this.globs, {
target: this.ts.ScriptTarget.ESNext,
module: this.ts.ModuleKind.ESNext,
allowJs: true,
checkJs: true,
});
return program;
};
collectPhase = ({ ts, node, context }) => {
if (this.checked) return;
if (!this.ts) {
this.ts = ts;
}
if (!this.program) {
this.program = this.createProgram();
}
if (!this.program) {
throw Error("Could not find program");
}
if (!this.ts) {
throw Error("Could not find TS Instance");
}
const program = this.program;
if (!this.typeChecker) {
this.typeChecker = program.getTypeChecker();
}
this.checked = true;
const sourceFilesMap = this.sourceFiles;
const sourceFiles = this.program.getSourceFiles().filter((sf) =>
this.globs.find((glob) => {
return sf.fileName.includes(glob);
}),
);
for (const sourceFile of sourceFiles) {
if (!sourceFile) return;
const types = new Map();
// Walk the tree to search for classes
this.ts.forEachChild(sourceFile, (node) =>
this.__processNode(node, types),
);
sourceFilesMap.set(sourceFile.fileName, types);
}
// console.log([...[sourceFilesMap.entries()].values()])
};
/**
* @param {import("typescript").Node} node
* @param {Map<string, string>} types
*/
__processNode = (node, types) => {
if (!this.typeChecker) return;
if (!this.ts) return;
const ts = this.ts;
if (ts.isTypeAliasDeclaration(node)) {
const type = this.typeChecker.getTypeAtLocation(node.name);
const originalProperty = this.typeChecker.typeToString(type);
const processedStr = this.__processProperty(type, node);
types.set(originalProperty, processedStr);
}
ts.forEachChild(node, (node) => this.__processNode(node, types));
};
/**
* Typescript can help us to spot types from outside of our local source files
* which we don't want to process like literals string (think of trim(), length etc) or entire classes like Date.
* @param {import("typescript").Symbol} symbol
*/
__isTypeLocal = (symbol) => {
if (!this.program) return false;
const sourceFile = symbol?.valueDeclaration?.getSourceFile();
const isStandardLibrary =
!!sourceFile && this.program.isSourceFileDefaultLibrary(sourceFile);
const isExternal =
!!sourceFile && this.program.isSourceFileFromExternalLibrary(sourceFile);
const hasDeclaration = !!symbol?.declarations?.[0];
return !(isStandardLibrary || isExternal) && hasDeclaration;
};
/**
* @param {import("typescript").Type} type
* @param {import("typescript").Node} node
* @param {number} [level=0]
* @return {string}
*/
__processProperty = (type, node, level = 0) => {
if (!this.ts) return "";
if (!this.typeChecker) return "";
const ts = this.ts;
const checker = this.typeChecker;
if (!ts.isTypeAliasDeclaration(node)) return "";
const group = [];
if (ts.isIntersectionTypeNode(node.type) || ts.isUnionTypeNode(node.type)) {
const type = checker.getTypeAtLocation(node.type);
if (type.isLiteral()) {
return checker.typeToString(type);
}
let separator = "";
if (type.isUnion() || type.isIntersection()) {
if (type.isUnion()) separator = "|";
if (type.isIntersection()) separator = "&";
for (const t of type.types) {
group.push(checker.typeToString(t));
}
}
return group.filter(Boolean).join(` ${separator} `);
}
// return checker.typeToString(type)
// Flattens objects
const obj = {};
for (const property of type.getProperties()) {
const propertyType = checker.getTypeOfSymbolAtLocation(property, node);
const propertySymbol = propertyType.getSymbol();
const propertyTypeName = checker.typeToString(propertyType);
/**
* If it's a local type belonging to our sources we are interested in
* further analysis, so we process all properties again like we did for the current given property.
*/
if (propertySymbol && this.__isTypeLocal(propertySymbol)) {
const properties = this.__processProperty(
propertyType,
node,
level + 1,
);
obj[property.name] = properties;
} else {
group.push(property.name);
obj[property.name] = propertyTypeName;
}
}
return JSON.stringify(obj, null, 2);
};
}
/**
* @param {Object} options
* @param {string[]} options.globs
* @param {import("typescript") | null | undefined} [options.ts]
* @param {import("typescript").Program | null | undefined} [options.program]
*/
export function expandTypesPlugin({ globs, ts = null, program = null }) {
return new ExpandTypes({ globs, ts, program });
}