-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
lintTs.mjs
133 lines (114 loc) · 3.97 KB
/
lintTs.mjs
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as os from 'os';
import * as path from 'path';
import * as url from 'url';
import chalk from 'chalk';
import {ESLint} from 'eslint';
import pLimit from 'p-limit';
import {getPackagesWithTsConfig} from './buildUtils.mjs';
// we want to limit the number of processes we spawn
const cpus = Math.max(1, os.cpus().length - 1);
const mutex = pLimit(cpus);
const fix = process.argv.slice(2).some(arg => arg === '--fix');
const monorepoRoot = path.resolve(url.fileURLToPath(import.meta.url), '../..');
// TODO: remove this list at some point and run against all packages
const packagesToTest = [
'babel-jest',
'babel-plugin-jest-hoist',
'diff-sequences',
'jest-changed-files',
'jest-console',
'jest-docblock',
'jest-environment',
'jest-globals',
'jest-resolve-dependencies',
'jest-schemas',
'jest-source-map',
'jest-test-result',
'jest-test-sequencer',
'jest-transform',
'jest-types',
'test-globals',
'test-utils',
];
const packagesWithTs = getPackagesWithTsConfig()
.map(({packageDir}) => packageDir)
.concat(path.resolve(monorepoRoot, 'e2e'))
.filter(packageDir => packagesToTest.some(pkg => packageDir.endsWith(pkg)));
const allLintResults = [];
try {
await Promise.all(
packagesWithTs.map(packageDir =>
mutex(async () => {
const eslint = new ESLint({
cache: true,
cacheLocation: path.resolve(packageDir, '.eslintcache'),
cwd: monorepoRoot,
extensions: ['.ts'],
fix,
fixTypes: ['problem', 'suggestion', 'layout'],
overrideConfig: {
extends: [
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
parser: '@typescript-eslint/parser',
parserOptions: {
project: ['./tsconfig.json', `${packageDir}/tsconfig.json`],
tsconfigRootDir: monorepoRoot,
},
plugins: ['@typescript-eslint'],
root: true,
rules: {
'@typescript-eslint/consistent-type-exports': 'error',
'@typescript-eslint/dot-notation': 'error',
'@typescript-eslint/non-nullable-type-assertion-style': 'error',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/prefer-readonly': 'error',
// TODO: activate this at some point
// '@typescript-eslint/prefer-readonly-parameter-types': 'error',
'@typescript-eslint/prefer-reduce-type-parameter': 'error',
'@typescript-eslint/return-await': 'error',
'@typescript-eslint/strict-boolean-expressions': 'error',
'@typescript-eslint/switch-exhaustiveness-check': 'error',
},
},
});
const filesToLint = packageDir.endsWith('e2e')
? `${packageDir}/__tests__/`
: `${packageDir}/src/`;
let results = await eslint.lintFiles(filesToLint);
if (fix) {
await ESLint.outputFixes(results);
// re-run after outputting fixes
results = await eslint.lintFiles(filesToLint);
}
const filteredResults = ESLint.getErrorResults(results);
allLintResults.push(...filteredResults);
}),
),
);
} catch (e) {
console.error(
chalk.inverse.red(' Unable to lint using TypeScript info files '),
);
throw e;
}
if (allLintResults.length > 0) {
const eslint = new ESLint({cwd: monorepoRoot});
const formatter = await eslint.loadFormatter('stylish');
const resultText = formatter.format(allLintResults);
console.error(resultText);
console.error(
chalk.inverse.red(' Unable to lint using TypeScript info files '),
);
process.exitCode = 1;
} else {
console.log(
chalk.inverse.green(' Successfully linted using TypeScript info files '),
);
}