-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathprocessor.js
More file actions
414 lines (378 loc) · 13.8 KB
/
processor.js
File metadata and controls
414 lines (378 loc) · 13.8 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* TypeScript type processing functionality
* @module processor
*/
// @ts-check
import ts from 'typescript';
/** @typedef {import('./index').ImportSkeleton} ImportSkeleton */
/** @typedef {import('./index').ImportFunctionSkeleton} ImportFunctionSkeleton */
/** @typedef {import('./index').ImportTypeSkeleton} ImportTypeSkeleton */
/** @typedef {import('./index').ImportPropertySkeleton} ImportPropertySkeleton */
/** @typedef {import('./index').ImportConstructorSkeleton} ImportConstructorSkeleton */
/** @typedef {import('./index').Parameter} Parameter */
/** @typedef {import('./index').BridgeType} BridgeType */
/**
* @typedef {{
* print: (level: "warning" | "error", message: string, node?: ts.Node) => void,
* }} DiagnosticEngine
*/
/**
* TypeScript type processor class
*/
export class TypeProcessor {
/**
* Create a TypeScript program from a d.ts file
* @param {string} filePath - Path to the d.ts file
* @param {ts.CompilerOptions} options - Compiler options
* @returns {ts.Program} TypeScript program object
*/
static createProgram(filePath, options) {
const host = ts.createCompilerHost(options);
return ts.createProgram([filePath], options, host);
}
/**
* @param {ts.TypeChecker} checker - TypeScript type checker
* @param {DiagnosticEngine} diagnosticEngine - Diagnostic engine
*/
constructor(checker, diagnosticEngine, options = {
inheritIterable: true,
inheritArraylike: true,
inheritPromiselike: true,
addAllParentMembersToClass: true,
replaceAliasToFunction: true,
replaceRankNFunction: true,
replaceNewableFunction: true,
noExtendsInTyprm: false,
}) {
this.checker = checker;
this.diagnosticEngine = diagnosticEngine;
this.options = options;
/** @type {Map<ts.Type, BridgeType>} */
this.processedTypes = new Map();
/** @type {Map<ts.Type, ts.Node>} Seen position by type */
this.seenTypes = new Map();
/** @type {ImportFunctionSkeleton[]} */
this.functions = [];
/** @type {ImportTypeSkeleton[]} */
this.types = [];
}
/**
* Process type declarations from a TypeScript program
* @param {ts.Program} program - TypeScript program
* @param {string} inputFilePath - Path to the input file
* @returns {ImportSkeleton} Processed type declarations
*/
processTypeDeclarations(program, inputFilePath) {
const sourceFiles = program.getSourceFiles().filter(
sf => !sf.isDeclarationFile || sf.fileName === inputFilePath
);
for (const sourceFile of sourceFiles) {
if (sourceFile.fileName.includes('node_modules/typescript/lib')) continue;
Error.stackTraceLimit = 100;
try {
sourceFile.forEachChild(node => {
this.visitNode(node);
for (const [type, node] of this.seenTypes) {
this.seenTypes.delete(type);
const typeString = this.checker.typeToString(type);
const members = type.getProperties();
if (members) {
const type = this.visitStructuredType(typeString, members);
this.types.push(type);
} else {
this.types.push(this.createUnknownType(typeString));
}
}
});
} catch (error) {
this.diagnosticEngine.print("error", `Error processing ${sourceFile.fileName}: ${error.message}`);
}
}
return { functions: this.functions, types: this.types };
}
/**
* Create an unknown type
* @param {string} typeString - Type string
* @returns {ImportTypeSkeleton} Unknown type
*/
createUnknownType(typeString) {
return {
name: typeString,
documentation: undefined,
properties: [],
methods: [],
constructor: undefined,
};
}
/**
* Visit a node and process it
* @param {ts.Node} node - The node to visit
*/
visitNode(node) {
if (ts.isFunctionDeclaration(node)) {
const func = this.visitFunctionLikeDecl(node);
if (func && node.name) {
this.functions.push({ ...func, name: node.name.getText() });
}
} else if (ts.isClassDeclaration(node)) {
const cls = this.visitClassDecl(node);
if (cls) this.types.push(cls);
}
}
/**
* Process a function declaration into ImportFunctionSkeleton format
* @param {ts.SignatureDeclaration} node - The function node
* @returns {ImportFunctionSkeleton | null} Processed function
* @private
*/
visitFunctionLikeDecl(node) {
if (!node.name) return null;
const signature = this.checker.getSignatureFromDeclaration(node);
if (!signature) return null;
/** @type {Parameter[]} */
const parameters = [];
for (const p of signature.getParameters()) {
const bridgeType = this.visitSignatureParameter(p, node);
parameters.push(bridgeType);
}
const returnType = signature.getReturnType();
const bridgeReturnType = this.visitType(returnType, node);
const documentation = this.getFullJSDocText(node);
return {
name: node.name.getText(),
parameters,
returnType: bridgeReturnType,
documentation,
};
}
/**
* Get the full JSDoc text from a node
* @param {ts.Node} node - The node to get the JSDoc text from
* @returns {string | undefined} The full JSDoc text
*/
getFullJSDocText(node) {
const docs = ts.getJSDocCommentsAndTags(node);
const parts = [];
for (const doc of docs) {
if (ts.isJSDoc(doc)) {
parts.push(doc.comment ?? "");
}
}
if (parts.length === 0) return undefined;
return parts.join("\n");
}
/**
* @param {ts.ConstructorDeclaration} node
* @returns {ImportConstructorSkeleton | null}
*/
visitConstructorDecl(node) {
const signature = this.checker.getSignatureFromDeclaration(node);
if (!signature) return null;
const parameters = [];
for (const p of signature.getParameters()) {
const bridgeType = this.visitSignatureParameter(p, node);
parameters.push(bridgeType);
}
return { parameters };
}
/**
* @param {ts.PropertyDeclaration | ts.PropertySignature} node
* @returns {ImportPropertySkeleton | null}
*/
visitPropertyDecl(node) {
if (!node.name) return null;
const type = this.checker.getTypeAtLocation(node)
const bridgeType = this.visitType(type, node);
const isReadonly = node.modifiers?.some(m => m.kind === ts.SyntaxKind.ReadonlyKeyword) ?? false;
const documentation = this.getFullJSDocText(node);
return { name: node.name.getText(), type: bridgeType, isReadonly, documentation };
}
/**
* @param {ts.Symbol} symbol
* @param {ts.Node} node
* @returns {Parameter}
*/
visitSignatureParameter(symbol, node) {
const type = this.checker.getTypeOfSymbolAtLocation(symbol, node);
const bridgeType = this.visitType(type, node);
return { name: symbol.name, type: bridgeType };
}
/**
* @param {ts.ClassDeclaration} node
* @returns {ImportTypeSkeleton | null}
*/
visitClassDecl(node) {
if (!node.name) return null;
const name = node.name.text;
const properties = [];
const methods = [];
/** @type {ImportConstructorSkeleton | undefined} */
let constructor = undefined;
for (const member of node.members) {
if (ts.isPropertyDeclaration(member)) {
const property = this.visitPropertyDecl(member);
if (property) properties.push(property);
} else if (ts.isMethodDeclaration(member)) {
const decl = this.visitFunctionLikeDecl(member);
if (decl) methods.push(decl);
} else if (ts.isConstructorDeclaration(member)) {
const decl = this.visitConstructorDecl(member);
if (decl) constructor = decl;
}
}
const documentation = this.getFullJSDocText(node);
return {
name,
constructor,
properties,
methods,
documentation,
};
}
/**
* @param {ts.SymbolFlags} flags
* @returns {string[]}
*/
debugSymbolFlags(flags) {
const result = [];
for (const key in ts.SymbolFlags) {
const val = (ts.SymbolFlags)[key];
if (typeof val === "number" && (flags & val) !== 0) {
result.push(key);
}
}
return result;
}
/**
* @param {ts.TypeFlags} flags
* @returns {string[]}
*/
debugTypeFlags(flags) {
const result = [];
for (const key in ts.TypeFlags) {
const val = (ts.TypeFlags)[key];
if (typeof val === "number" && (flags & val) !== 0) {
result.push(key);
}
}
return result;
}
/**
* @param {string} name
* @param {ts.Symbol[]} members
* @returns {ImportTypeSkeleton}
*/
visitStructuredType(name, members) {
/** @type {ImportPropertySkeleton[]} */
const properties = [];
/** @type {ImportFunctionSkeleton[]} */
const methods = [];
/** @type {ImportConstructorSkeleton | undefined} */
let constructor = undefined;
for (const symbol of members) {
if (symbol.flags & ts.SymbolFlags.Property) {
for (const decl of symbol.getDeclarations() ?? []) {
if (ts.isPropertyDeclaration(decl) || ts.isPropertySignature(decl)) {
const property = this.visitPropertyDecl(decl);
if (property) properties.push(property);
} else if (ts.isMethodSignature(decl)) {
const method = this.visitFunctionLikeDecl(decl);
if (method) methods.push(method);
}
}
} else if (symbol.flags & ts.SymbolFlags.Method) {
for (const decl of symbol.getDeclarations() ?? []) {
if (!ts.isMethodSignature(decl)) {
continue;
}
const method = this.visitFunctionLikeDecl(decl);
if (method) methods.push(method);
}
} else if (symbol.flags & ts.SymbolFlags.Constructor) {
for (const decl of symbol.getDeclarations() ?? []) {
if (!ts.isConstructorDeclaration(decl)) {
continue;
}
const ctor = this.visitConstructorDecl(decl);
if (ctor) constructor = ctor;
}
}
}
return { name, properties, methods, constructor, documentation: undefined };
}
/**
* Convert TypeScript type string to BridgeType
* @param {ts.Type} type - TypeScript type string
* @param {ts.Node} node - Node
* @returns {BridgeType} Bridge type
* @private
*/
visitType(type, node) {
const maybeProcessed = this.processedTypes.get(type);
if (maybeProcessed) {
return maybeProcessed;
}
/**
* @param {ts.Type} type
* @returns {BridgeType}
*/
const convert = (type) => {
/** @type {Record<string, BridgeType>} */
const typeMap = {
"number": { "double": {} },
"string": { "string": {} },
"boolean": { "bool": {} },
"void": { "void": {} },
"any": { "jsObject": {} },
"unknown": { "jsObject": {} },
"null": { "void": {} },
"undefined": { "void": {} },
"bigint": { "int": {} },
"object": { "jsObject": {} },
"symbol": { "jsObject": {} },
"never": { "void": {} },
};
const typeString = this.checker.typeToString(type);
if (typeMap[typeString]) {
return typeMap[typeString];
}
if (this.checker.isArrayType(type) || this.checker.isTupleType(type) || type.getCallSignatures().length > 0) {
return { "jsObject": {} };
}
// "a" | "b" -> string
if (this.checker.isTypeAssignableTo(type, this.checker.getStringType())) {
return { "string": {} };
}
if (type.getFlags() & ts.TypeFlags.TypeParameter) {
return { "jsObject": {} };
}
const typeName = this.deriveTypeName(type);
if (!typeName) {
this.diagnosticEngine.print("warning", `Unknown non-nominal type: ${typeString}`, node);
return { "jsObject": {} };
}
this.seenTypes.set(type, node);
return { "jsObject": { "_0": typeName } };
}
const bridgeType = convert(type);
this.processedTypes.set(type, bridgeType);
return bridgeType;
}
/**
* Derive the type name from a type
* @param {ts.Type} type - TypeScript type
* @returns {string | undefined} Type name
* @private
*/
deriveTypeName(type) {
const aliasSymbol = type.aliasSymbol;
if (aliasSymbol) {
return aliasSymbol.name;
}
const typeSymbol = type.getSymbol();
if (typeSymbol) {
return typeSymbol.name;
}
return undefined;
}
}