-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathgenerateTypeScriptDefinitions.js
More file actions
422 lines (374 loc) Β· 12.2 KB
/
Copy pathgenerateTypeScriptDefinitions.js
File metadata and controls
422 lines (374 loc) Β· 12.2 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
415
416
417
418
419
420
421
422
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
// $FlowFixMe[untyped-import] in OSS only
import {ESLint} from 'eslint';
import {
translateFlowDefToTSDef,
translateFlowToFlowDef,
} from 'flow-api-translator';
import fs from 'fs';
import nullthrows from 'nullthrows';
import path from 'path';
import * as prettier from 'prettier';
// $FlowFixMe[untyped-import] in OSS only
import SignedSource from 'signedsource';
const WORKSPACE_ROOT = path.resolve(__dirname, '..');
const TYPES_DIR = 'types';
const SRC_DIR = 'src';
type LintMessage = {
readonly ruleId: ?string,
readonly message: string,
readonly line: number,
...
};
export const AUTO_GENERATED_PATTERNS: ReadonlyArray<string> = ['packages/**'];
// Globs of paths for which we do not generate TypeScript definitions,
// matched during glob traversal. A directory match ignores all contents.
const IGNORED_PATTERNS = [
'**/__tests__',
'**/__flowtests__',
'**/__mocks__',
'**/__fixtures__',
'**/node_modules',
'packages/metro-babel-register',
'packages/*/build',
'packages/metro/src/cli.js',
'packages/**/third-party',
'packages/metro/src/integration_tests',
'packages/metro-runtime/**/!(types*).js',
];
function isSourceTSDeclaration(filePath: string): boolean {
const parts = filePath.split(path.sep);
return filePath.endsWith('.d.ts') && parts[2] === SRC_DIR;
}
function isExistingTSDeclaration(filePath: string): boolean {
const parts = filePath.split(path.sep);
return filePath.endsWith('.d.ts') && parts[2] === TYPES_DIR;
}
export async function generateTsDefsForJsGlobs(
globPattern: string | ReadonlyArray<string>,
opts: Readonly<{
verifyOnly: boolean,
}> = {verifyOnly: false},
) {
const linter = new ESLint({
fix: true,
cwd: WORKSPACE_ROOT,
});
const prettierConfig = await resolvePrettierConfig();
const globPatterns = Array.isArray(globPattern) ? globPattern : [globPattern];
const existingDefs = new Set<string>();
const sourceDefs = new Set<string>();
const filesToProcess: Array<[jsFile: string, flowSourceFile: string]> =
Array.from(
globPatterns
.flatMap(pattern =>
fs
.globSync(pattern, {
exclude: dirent =>
IGNORED_PATTERNS.some(ignorePattern =>
path.matchesGlob(
path.relative(
WORKSPACE_ROOT,
path.resolve(dirent.parentPath, dirent.name.toString()),
),
ignorePattern,
),
),
cwd: WORKSPACE_ROOT,
withFileTypes: true as true,
})
.filter(dirent => dirent.isFile())
.map(dirent =>
path.relative(
WORKSPACE_ROOT,
path.resolve(dirent.parentPath, dirent.name.toString()),
),
),
)
.reduce((toProcess, filePath) => {
if (filePath.endsWith('.flow.js')) {
// For .flow.js files, record the `.flow.js` as the source for the
// corresponding `.js` file, which is enforced to be a transparent
// entry file that only registers Babel and re-exports the module.
toProcess.set(filePath.replace(/\.flow\.js$/, '.js'), filePath);
} else if (filePath.endsWith('.js') && !toProcess.has(filePath)) {
toProcess.set(filePath, filePath);
} else if (isSourceTSDeclaration(filePath)) {
sourceDefs.add(path.resolve(WORKSPACE_ROOT, filePath));
} else if (isExistingTSDeclaration(filePath)) {
existingDefs.add(path.resolve(WORKSPACE_ROOT, filePath));
}
return toProcess;
}, new Map<string, string>())
.entries(),
);
const errors = [];
async function writeOutputFile(
sourceContent: string,
absoluteTsFile: string,
sourceFile: string,
) {
// Lint and fix the generated output
let [lintResult] = await linter.lintText(sourceContent, {
filePath: absoluteTsFile,
});
let lintedOutput = lintResult.output ?? sourceContent;
const withoutUnusedGeneratedDeclarations =
removeUnusedGeneratedDeclarations(lintedOutput, lintResult.messages);
if (withoutUnusedGeneratedDeclarations !== lintedOutput) {
[lintResult] = await linter.lintText(withoutUnusedGeneratedDeclarations, {
filePath: absoluteTsFile,
});
lintedOutput = lintResult.output ?? withoutUnusedGeneratedDeclarations;
}
if (lintResult.messages.length > 0) {
console.warn(sourceFile, lintResult.messages);
}
const formattedOutput = await prettier.format(lintedOutput, prettierConfig);
// Add signedsource (generated) token to the header
const withToken = formattedOutput
.replace(
'\n */\n',
`\n * ${SignedSource.getSigningToken()}\n *` +
`\n * This file was translated from Flow by ${path.relative(WORKSPACE_ROOT, __filename).replaceAll(path.sep, '/')}` +
`\n * Original file: ${sourceFile.replaceAll(path.sep, '/')}` +
'\n * To regenerate, run:' +
'\n * js1 build metro-ts-defs (internal) OR' +
'\n * yarn run build-ts-defs (OSS) ' +
'\n */\n',
)
// format -> noformat
.replace(`\n * ${'@'}format\n`, `\n * ${'@'}noformat\n`);
// Sign the file
const finalOutput = SignedSource.signFile(withToken);
existingDefs.delete(absoluteTsFile);
if (opts.verifyOnly) {
let existingFile = null;
try {
existingFile = await fs.promises.readFile(absoluteTsFile, 'utf-8');
if (finalOutput !== existingFile) {
errors.push({
sourceFile,
error: new Error('.d.ts file is out of sync'),
});
}
} catch {
errors.push({sourceFile, error: new Error('.d.ts file missing')});
}
} else {
await fs.promises.mkdir(path.dirname(absoluteTsFile), {
recursive: true,
});
await fs.promises.writeFile(absoluteTsFile, finalOutput);
}
}
await Promise.all(
filesToProcess.map(async ([jsFile, sourceFile]) => {
const absoluteTsFile = getTSDeclAbsolutePath(jsFile);
const sourceTSDeclationPath = absoluteTsFile.replace(TYPES_DIR, SRC_DIR);
const absoluteSourceFile = path.resolve(WORKSPACE_ROOT, sourceFile);
// If a source .d.ts file exists, copy it directly.
if (sourceDefs.has(sourceTSDeclationPath)) {
const source = await fs.promises.readFile(
sourceTSDeclationPath,
'utf-8',
);
await writeOutputFile(source, absoluteTsFile, sourceFile);
return;
}
const source = await fs.promises.readFile(absoluteSourceFile, 'utf-8');
if (!source.includes('@flow')) {
errors.push({
sourceFile,
error: new Error('Expected @flow directive'),
});
return;
}
try {
const flowDef = await translateFlowToFlowDef(source);
if (flowDef.includes('declare module.exports')) {
errors.push({
sourceFile,
error: new Error(
'module.exports is not supported by TypeScript auto-generation',
),
});
} else {
const tsDef = await translateFlowDefToTSDef(flowDef);
const beforeLint = tsDef
// Fix up gap left in license header by removal of atflow
.replace('\n *\n *\n', '\n *\n')
// TypeScript has no analogue for __proto__: null
.replace(/__proto__: null[,;]?/g, '');
await writeOutputFile(beforeLint, absoluteTsFile, sourceFile);
}
} catch (error) {
errors.push({sourceFile, error});
}
}),
);
if (existingDefs.size > 0) {
const orphanedDefs = Array.from(existingDefs);
if (opts.verifyOnly) {
orphanedDefs.forEach(sourceFile => {
errors.push({
error: new Error('.d.ts appears to be orphaned'),
sourceFile,
});
});
} else {
// Delete .d.ts files under a generated location that were not generated.
await Promise.all(
orphanedDefs.map(sourceFile => fs.promises.unlink(sourceFile)),
);
}
}
if (errors.length > 0) {
errors.sort((a, b) => a.sourceFile.localeCompare(b.sourceFile));
throw new AggregateError(
errors,
'Errors encountered while generating TypeScript definitions',
);
}
}
function removeUnusedGeneratedDeclarations(
sourceContent: string,
messages: ReadonlyArray<LintMessage>,
): string {
const lines = sourceContent.split('\n');
for (const message of messages) {
if (message.ruleId !== '@typescript-eslint/no-unused-vars') {
continue;
}
const name = message.message.match(
/^'([^']+)' is defined but never used/,
)?.[1];
if (name == null || message.line == null) {
continue;
}
const lineIndex = message.line - 1;
if (
removeSingleBindingImportAtLine(lines, lineIndex, name) ||
removeDeclareConstAtLine(lines, lineIndex, name)
) {
continue;
}
}
return lines.join('\n');
}
function removeSingleBindingImportAtLine(
lines: Array<string>,
lineIndex: number,
name: string,
): boolean {
const start = findBlockStart(lines, lineIndex, line =>
/^\s*import\s/.test(line),
);
if (start == null) {
return false;
}
const end = findBlockEnd(lines, start);
if (end == null) {
return false;
}
const statement = lines.slice(start, end + 1).join('\n');
if (!isSingleBindingImport(statement, name)) {
return false;
}
lines.splice(start, end - start + 1);
return true;
}
function removeDeclareConstAtLine(
lines: Array<string>,
lineIndex: number,
name: string,
): boolean {
const declarationPattern = new RegExp(
`^\\s*declare const ${escapeRegExp(name)}\\b`,
);
const start = findBlockStart(lines, lineIndex, line =>
declarationPattern.test(line),
);
if (start == null) {
return false;
}
const end = findBlockEnd(lines, start);
if (end == null) {
return false;
}
lines.splice(start, end - start + 1);
return true;
}
function findBlockStart(
lines: ReadonlyArray<string>,
lineIndex: number,
predicate: string => boolean,
): ?number {
for (let i = lineIndex; i >= 0; i--) {
if (predicate(lines[i])) {
return i;
}
}
return null;
}
function findBlockEnd(lines: ReadonlyArray<string>, start: number): ?number {
for (let i = start; i < lines.length; i++) {
if (/;\s*$/.test(lines[i])) {
return i;
}
}
return null;
}
function isSingleBindingImport(statement: string, name: string): boolean {
const escapedName = escapeRegExp(name);
const compact = statement.replace(/\s+/g, ' ').trim();
return (
new RegExp(`^import ${escapedName} from `).test(compact) ||
new RegExp(`^import \\* as ${escapedName} from `).test(compact) ||
new RegExp(`^import \\{ ${escapedName} \\} from `).test(compact) ||
new RegExp(`^import \\{${escapedName}\\} from `).test(compact)
);
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function getTSDeclAbsolutePath(jsRelativePath: string) {
const parts = jsRelativePath.split(path.sep);
if (parts[2] !== 'src') {
throw new Error(
'Expected relative path of the form packages/<pkg>/src/...',
);
}
parts[2] = TYPES_DIR;
const basename = nullthrows(parts.pop());
parts.push(basename.slice(0, -3) + '.d.ts');
return path.resolve(WORKSPACE_ROOT, parts.join(path.sep));
}
async function resolvePrettierConfig() {
const fakeTsDecl = path.resolve(__dirname, './dummy.d.ts');
return {
...(await prettier.resolveConfig(fakeTsDecl)),
filepath: fakeTsDecl,
printWidth: 200,
};
}
// When run as a script, execute pattern from argv
if (process.mainModule === module) {
// Usage: node scripts/generateTypeScriptDefinitions.js [glob...]
// Omit globs to use hardcoded defaults.
generateTsDefsForJsGlobs(
process.argv.length >= 3 ? process.argv.slice(2) : AUTO_GENERATED_PATTERNS,
).catch(error => {
process.exitCode = 1;
console.error(error);
});
}