-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathgenerate-lib.mts
314 lines (276 loc) · 8.63 KB
/
generate-lib.mts
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
import type {
AnalyzeOptions,
ScopeManager,
Variable,
} from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/types';
import type { TSESTreeOptions } from '@typescript-eslint/typescript-estree';
import { analyze } from '@typescript-eslint/scope-manager';
import { AST_TOKEN_TYPES } from '@typescript-eslint/types';
import { parse } from '@typescript-eslint/typescript-estree';
import { FlatESLint } from '@typescript-eslint/utils/ts-eslint';
import fs from 'node:fs';
import path from 'node:path';
import prettier from 'prettier';
import { rimraf } from 'rimraf';
import ts from 'typescript';
import {
PACKAGES_SCOPE_MANAGER,
PACKAGES_TYPES,
PRETTIER_CONFIG_PATH,
REPO_ROOT,
} from './paths.mts';
function parseAndAnalyze(
code: string,
analyzeOptions: AnalyzeOptions,
parserOptions: TSESTreeOptions,
): {
ast: ReturnType<typeof parse>;
scopeManager: ReturnType<typeof analyze>;
} {
const ast = parse(code, { ...parserOptions });
const scopeManager = analyze(ast, analyzeOptions);
return { ast, scopeManager };
}
const libMap = new Map(ts.libMap);
// add the "full" libs as well - these are used by the default config resolution system
for (const [lib] of ts.libMap) {
if (
(/^es2\d{3}$/.test(lib) || lib === 'esnext') &&
// there's no "full" lib for es2015
lib !== 'es2015'
) {
libMap.set(`${lib}.full`, `lib.${lib}.full.d.ts`);
}
}
// the base lib used when the target is unknown
libMap.set('lib', 'lib.d.ts');
function addAutoGeneratedComment(code: string[]): string {
return [
'// THIS CODE WAS AUTOMATICALLY GENERATED',
'// DO NOT EDIT THIS CODE BY HAND',
'// RUN THE FOLLOWING COMMAND FROM THE WORKSPACE ROOT TO REGENERATE:',
'// npx nx generate-lib repo',
'',
...code,
].join('\n');
}
const PRETTIER_CONFIG = prettier.resolveConfig('file.ts', {
config: PRETTIER_CONFIG_PATH,
});
const TS_LIB_FOLDER = path.join(REPO_ROOT, 'node_modules', 'typescript', 'lib');
const OUTPUT_FOLDER = path.join(PACKAGES_SCOPE_MANAGER, 'src', 'lib');
const TYPES_FILE = path.join(PACKAGES_TYPES, 'src', 'lib.ts');
const BARREL_PATH = path.join(OUTPUT_FOLDER, 'index.ts');
const BASE_CONFIG_MODULE_NAME = 'base-config';
const SHARED_CONFIG_MODULE = path.join(
OUTPUT_FOLDER,
`${BASE_CONFIG_MODULE_NAME}.ts`,
);
enum BASE_CONFIG_EXPORT_NAMES {
TYPE = 'TYPE',
VALUE = 'VALUE',
TYPE_AND_VALUE = 'TYPE_VALUE',
}
async function formatCode(code: string[]): Promise<string> {
return await prettier.format(addAutoGeneratedComment(code), {
parser: 'typescript',
...(await PRETTIER_CONFIG),
});
}
function sanitize(name: string): string {
return name.replaceAll('.', '_');
}
function getVariablesFromScope(scopeManager: ScopeManager): Variable[] {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const scope = scopeManager.globalScope!.childScopes[0];
const variables: Variable[] = [];
for (const variable of scope.variables) {
if (variable.isTypeVariable) {
variables.push(variable);
}
}
return variables;
}
const REFERENCE_REGEX = /\/ <reference lib="(.+)" \/>/;
function getReferences(
ast: { comments: TSESTree.Comment[] } & TSESTree.Program,
): Set<string> {
const comments = ast.comments.filter(
c =>
c.type === AST_TOKEN_TYPES.Line &&
c.value.startsWith('/ <reference lib="'),
);
const references = new Set<string>();
for (const comment of comments) {
const match = REFERENCE_REGEX.exec(comment.value);
if (!match) {
continue;
}
references.add(match[1]);
}
return references;
}
async function main(): Promise<void> {
try {
rimraf.sync(OUTPUT_FOLDER);
} catch {
// ignored
}
try {
fs.mkdirSync(OUTPUT_FOLDER);
} catch {
// ignored
}
const filesWritten: string[] = [
SHARED_CONFIG_MODULE,
TYPES_FILE,
BARREL_PATH,
];
// the shared
fs.writeFileSync(
SHARED_CONFIG_MODULE,
await formatCode([
`export const ${
BASE_CONFIG_EXPORT_NAMES.TYPE
} = Object.freeze(${JSON.stringify({
eslintImplicitGlobalSetting: 'readonly',
isTypeVariable: true,
isValueVariable: false,
})});`,
`export const ${
BASE_CONFIG_EXPORT_NAMES.VALUE
} = Object.freeze(${JSON.stringify({
eslintImplicitGlobalSetting: 'readonly',
isTypeVariable: false,
isValueVariable: true,
})});`,
`export const ${
BASE_CONFIG_EXPORT_NAMES.TYPE_AND_VALUE
} = Object.freeze(${JSON.stringify({
eslintImplicitGlobalSetting: 'readonly',
isTypeVariable: true,
isValueVariable: true,
})});`,
'',
]),
);
for (const [libName, filename] of libMap) {
const libPath = path.join(TS_LIB_FOLDER, filename);
const { ast, scopeManager } = parseAndAnalyze(
fs.readFileSync(libPath, 'utf8'),
{
// we don't want any libs
lib: [],
sourceType: 'module',
},
{
comment: true,
loc: true,
range: true,
},
) as {
// https://github.com/typescript-eslint/typescript-eslint/issues/8347
ast: { comments: TSESTree.Comment[] } & TSESTree.Program;
} & ReturnType<typeof parseAndAnalyze>;
const code = [`export const ${sanitize(libName)} = {`];
const references = getReferences(ast);
if (references.size > 0) {
// add a newline before the export
code.unshift('');
}
// import and spread all of the references
const imports = [
"import type { ImplicitLibVariableOptions } from '../variable';",
];
for (const reference of references) {
const name = sanitize(reference);
imports.push(`import { ${name} } from './${reference}'`);
code.push(`...${name},`);
}
const requiredBaseImports = new Set<BASE_CONFIG_EXPORT_NAMES>();
// add a declaration for each variable
const variables = getVariablesFromScope(scopeManager);
for (const variable of variables) {
const importName = ((): BASE_CONFIG_EXPORT_NAMES => {
if (variable.isTypeVariable && variable.isValueVariable) {
return BASE_CONFIG_EXPORT_NAMES.TYPE_AND_VALUE;
}
if (variable.isTypeVariable) {
return BASE_CONFIG_EXPORT_NAMES.TYPE;
}
if (variable.isValueVariable) {
return BASE_CONFIG_EXPORT_NAMES.VALUE;
}
// shouldn't happen
throw new Error(
"Unexpected variable that's is not a type or value variable",
);
})();
requiredBaseImports.add(importName);
code.push(`'${variable.name}': ${importName},`);
}
code.push('} as Record<string, ImplicitLibVariableOptions>;');
if (requiredBaseImports.size > 0) {
imports.push(
`import {${[...requiredBaseImports]
.sort()
.join(',')}} from './${BASE_CONFIG_MODULE_NAME}';`,
);
}
if (imports.length > 0) {
code.unshift(...imports, '');
}
const formattedCode = await formatCode(code);
const writePath = path.join(OUTPUT_FOLDER, `${libName}.ts`);
fs.writeFileSync(writePath, formattedCode);
filesWritten.push(writePath);
console.log(
'Wrote',
variables.length,
'variables, and',
references.size,
'references for',
libName,
);
}
// generate and write a barrel file
const barrelImports = []; // use a separate way so everything is in the same order
const barrelCode = ['', `const lib = {`];
for (const lib of libMap.keys()) {
const name = sanitize(lib);
if (name === 'lib') {
barrelImports.push(`import { lib as libBase } from './${lib}'`);
barrelCode.push(`'${lib}': libBase,`);
} else {
barrelImports.push(`import { ${name} } from './${lib}'`);
barrelCode.push(lib === name ? `${lib},` : `'${lib}': ${name},`);
}
}
barrelCode.unshift(...barrelImports);
barrelCode.push('} as const;');
barrelCode.push('', 'export { lib };');
const formattedBarrelCode = await formatCode(barrelCode);
fs.writeFileSync(BARREL_PATH, formattedBarrelCode);
console.log('Wrote barrel file');
// generate a string union type for the lib names
const libUnionCode = [
`type Lib = ${[...libMap.keys()].map(k => `'${k}'`).join(' | ')};`,
'',
'export { Lib };',
];
const formattedLibUnionCode = await formatCode(libUnionCode);
fs.writeFileSync(TYPES_FILE, formattedLibUnionCode);
console.log('Wrote Lib union type file');
const lint = new FlatESLint({
fix: true,
});
const results = await lint.lintFiles(filesWritten);
await FlatESLint.outputFixes(results);
console.log('Autofixed lint errors');
}
main().catch((e: unknown) => {
console.error(e);
// eslint-disable-next-line no-process-exit
process.exit(1);
});