|
1 |
| -import type { Linter } from 'eslint'; |
2 |
| -import { distinct, toArray } from '@code-pushup/utils'; |
3 |
| -import { type ESLintTarget } from '../config'; |
| 1 | +import type { ESLint, Linter } from 'eslint'; |
| 2 | +import { rm, writeFile } from 'node:fs/promises'; |
| 3 | +import { platform } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { distinct, executeProcess, toArray } from '@code-pushup/utils'; |
| 6 | +import type { ESLintTarget } from '../config'; |
4 | 7 | import { setupESLint } from '../setup';
|
5 | 8 | import type { LinterOutput, RuleOptionsPerFile } from './types';
|
6 | 9 |
|
7 | 10 | export async function lint({
|
8 | 11 | eslintrc,
|
9 | 12 | patterns,
|
10 | 13 | }: ESLintTarget): Promise<LinterOutput> {
|
| 14 | + const results = await executeLint({ eslintrc, patterns }); |
| 15 | + const ruleOptionsPerFile = await loadRuleOptionsPerFile(eslintrc, results); |
| 16 | + return { results, ruleOptionsPerFile }; |
| 17 | +} |
| 18 | + |
| 19 | +function executeLint({ |
| 20 | + eslintrc, |
| 21 | + patterns, |
| 22 | +}: ESLintTarget): Promise<ESLint.LintResult[]> { |
| 23 | + return withConfig(eslintrc, async configPath => { |
| 24 | + // running as CLI because ESLint#lintFiles() runs out of memory |
| 25 | + const { stdout } = await executeProcess({ |
| 26 | + command: 'npx', |
| 27 | + args: [ |
| 28 | + 'eslint', |
| 29 | + `--config=${configPath}`, |
| 30 | + '--no-eslintrc', |
| 31 | + '--no-error-on-unmatched-pattern', |
| 32 | + '--format=json', |
| 33 | + ...toArray(patterns).map(pattern => |
| 34 | + // globs need to be escaped on Unix |
| 35 | + platform() === 'win32' ? pattern : `'${pattern}'`, |
| 36 | + ), |
| 37 | + ], |
| 38 | + ignoreExitCode: true, |
| 39 | + cwd: process.cwd(), |
| 40 | + }); |
| 41 | + |
| 42 | + return JSON.parse(stdout) as ESLint.LintResult[]; |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +function loadRuleOptionsPerFile( |
| 47 | + eslintrc: ESLintTarget['eslintrc'], |
| 48 | + results: ESLint.LintResult[], |
| 49 | +): Promise<RuleOptionsPerFile> { |
11 | 50 | const eslint = setupESLint(eslintrc);
|
12 | 51 |
|
13 |
| - const results = await eslint.lintFiles(patterns); |
14 |
| - |
15 |
| - const ruleOptionsPerFile = await results.reduce( |
16 |
| - async (acc, { filePath, messages }) => { |
17 |
| - const filesMap = await acc; |
18 |
| - const config = (await eslint.calculateConfigForFile( |
19 |
| - filePath, |
20 |
| - )) as Linter.Config; |
21 |
| - const ruleIds = distinct( |
22 |
| - messages |
23 |
| - .map(({ ruleId }) => ruleId) |
24 |
| - .filter((ruleId): ruleId is string => ruleId != null), |
25 |
| - ); |
26 |
| - const rulesMap = Object.fromEntries( |
27 |
| - ruleIds.map(ruleId => [ |
28 |
| - ruleId, |
29 |
| - toArray(config.rules?.[ruleId] ?? []).slice(1), |
30 |
| - ]), |
31 |
| - ); |
32 |
| - return { |
33 |
| - ...filesMap, |
34 |
| - [filePath]: { |
35 |
| - ...filesMap[filePath], |
36 |
| - ...rulesMap, |
37 |
| - }, |
38 |
| - }; |
39 |
| - }, |
40 |
| - Promise.resolve<RuleOptionsPerFile>({}), |
41 |
| - ); |
| 52 | + return results.reduce(async (acc, { filePath, messages }) => { |
| 53 | + const filesMap = await acc; |
| 54 | + const config = (await eslint.calculateConfigForFile( |
| 55 | + filePath, |
| 56 | + )) as Linter.Config; |
| 57 | + const ruleIds = distinct( |
| 58 | + messages |
| 59 | + .map(({ ruleId }) => ruleId) |
| 60 | + .filter((ruleId): ruleId is string => ruleId != null), |
| 61 | + ); |
| 62 | + const rulesMap = Object.fromEntries( |
| 63 | + ruleIds.map(ruleId => [ |
| 64 | + ruleId, |
| 65 | + toArray(config.rules?.[ruleId] ?? []).slice(1), |
| 66 | + ]), |
| 67 | + ); |
| 68 | + return { |
| 69 | + ...filesMap, |
| 70 | + [filePath]: { |
| 71 | + ...filesMap[filePath], |
| 72 | + ...rulesMap, |
| 73 | + }, |
| 74 | + }; |
| 75 | + }, Promise.resolve<RuleOptionsPerFile>({})); |
| 76 | +} |
42 | 77 |
|
43 |
| - return { results, ruleOptionsPerFile }; |
| 78 | +async function withConfig<T>( |
| 79 | + eslintrc: ESLintTarget['eslintrc'], |
| 80 | + fn: (configPath: string) => Promise<T>, |
| 81 | +): Promise<T> { |
| 82 | + if (typeof eslintrc === 'string') { |
| 83 | + return fn(eslintrc); |
| 84 | + } |
| 85 | + |
| 86 | + const configPath = generateTempConfigPath(); |
| 87 | + await writeFile(configPath, JSON.stringify(eslintrc)); |
| 88 | + |
| 89 | + try { |
| 90 | + return await fn(configPath); |
| 91 | + } finally { |
| 92 | + await rm(configPath); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function generateTempConfigPath(): string { |
| 97 | + return join( |
| 98 | + process.cwd(), |
| 99 | + `.eslintrc.${Math.random().toString().slice(2)}.json`, |
| 100 | + ); |
44 | 101 | }
|
0 commit comments