-
Notifications
You must be signed in to change notification settings - Fork 25
/
generate_annotations.js
180 lines (157 loc) Β· 4.17 KB
/
generate_annotations.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
'use strict';
const fs = require('fs');
const Module = require('module');
const ts = require('typescript');
const domain = require('domain');
const d = domain.create();
d.on('error', () => {});
const functions = [];
const scanned = new Set([
process.mainModule,
]);
const forbiddenPaths = new Set([
// duplicate with buffer module
'globalThis?.Buffer',
// prints experimental warning
'globalThis?.fetch',
// prints experimental warning
'globalThis?.FormData',
// prints experimental warning
'globalThis?.Headers',
// prints experimental warning
'globalThis?.Request',
// prints experimental warning
'globalThis?.Response',
]);
const scan = (ns, path) => {
if (scanned.has(ns)) {
return;
}
if (forbiddenPaths.has(path)) {
return;
}
scanned.add(ns);
if (typeof ns === 'function') {
functions.push(path);
}
if (typeof ns !== 'function' && (typeof ns !== 'object' || ns === null)) {
return;
}
Reflect.ownKeys(ns).forEach((name) => {
if (typeof name === 'string' && name.startsWith('_')) {
return;
}
try {
d.run(() => {
ns[name];
});
} catch {
return;
}
if (typeof name === 'symbol') {
if (name.description.startsWith('Symbol')) {
scan(ns[name], `${path}?.[${name.description}]`);
}
} else {
scan(ns[name], `${path}?.${name}`);
}
});
};
scan(globalThis, 'globalThis');
const required = new Set();
const forbidden = new Set(['repl', 'domain', 'sys', 'module']);
Module.builtinModules.forEach((m) => {
if (m.startsWith('_') || m.includes('/') || forbidden.has(m)) {
return;
}
required.add(m);
scan(require(m), m);
});
const compilerOptions = {
lib: ['lib.esnext.d.ts', 'lib.dom.d.ts'],
types: ['node'],
target: ts.ScriptTarget.Latest,
strict: true,
};
const host = ts.createCompilerHost(compilerOptions);
const originalReadFile = host.readFile;
host.readFile = (name) => {
if (name === 'index.ts') {
return `
${[...required].map((r) => `import * as ${r} from '${r}';`).join('\n')}
${functions.join('\n').replaceAll('?', '')}
`;
}
return originalReadFile.call(host, name);
};
host.writeFile = () => {
throw new Error();
};
const program = ts.createProgram(['index.ts'], compilerOptions, host);
const checker = program.getTypeChecker();
function convertSignature(signature) {
return signature.parameters.map((symbol) => {
const param = symbol.valueDeclaration;
if (param.questionToken) {
return `?${symbol.name}`;
}
if (param.dotDotDotToken) {
return `...${symbol.name}`;
}
return symbol.name;
});
}
function arrayEqual(a1, a2) {
if (a1.length !== a2.length) {
return false;
}
for (let i = 0; i < a1.length; i += 1) {
if (a1[i] !== a2[i]) {
return false;
}
}
return true;
}
const out = [];
program.getSourceFile('index.ts').statements.forEach((stmt, i) => {
const path = functions[i - required.size];
if (!path) {
return;
}
const type = checker.getTypeAtLocation(stmt.expression);
if (checker.typeToString(type) === 'any') {
console.error(path);
}
const data = {
call: [],
construct: [],
};
const C = (signatures, a) => {
signatures
.filter((s) => s.parameters.length > 0)
.forEach((signature) => {
const s = convertSignature(signature);
if (!a.some((e) => arrayEqual(s, e))) {
a.push(s);
}
});
};
C(type.getCallSignatures(), data.call);
C(type.getConstructSignatures(), data.construct);
data.call.sort((a, b) => a.length - b.length);
data.construct.sort((a, b) => a.length - b.length);
if (data.call.length > 0 || data.construct.length > 0) {
out.push(` [${path}, ${JSON.stringify(data)}]`);
}
});
fs.writeFileSync('./src/annotation_map.js', `'use strict';
/* eslint-disable */
// Generated by generate_annotations.js
// This file maps native methods to their signatures for completion
// in the repl. if a method isn't listed here, it is either unknown
// to the generator script, or it doesn't take any arguments.
${[...required].map((r) => `const ${r} = require('${r}');`).join('\n')}
module.exports = new WeakMap([
${out.join(',\n')},
].filter(([key]) => key !== undefined));
`);