-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(plugin-eslint): run eslint as separate process to prevent exceed…
…ing memory
- Loading branch information
1 parent
10dd3c6
commit c25b367
Showing
6 changed files
with
156 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,101 @@ | ||
import type { Linter } from 'eslint'; | ||
import { distinct, toArray } from '@code-pushup/utils'; | ||
import { type ESLintTarget } from '../config'; | ||
import type { ESLint, Linter } from 'eslint'; | ||
import { rm, writeFile } from 'node:fs/promises'; | ||
import { platform } from 'node:os'; | ||
import { join } from 'node:path'; | ||
import { distinct, executeProcess, toArray } from '@code-pushup/utils'; | ||
import type { ESLintTarget } from '../config'; | ||
import { setupESLint } from '../setup'; | ||
import type { LinterOutput, RuleOptionsPerFile } from './types'; | ||
|
||
export async function lint({ | ||
eslintrc, | ||
patterns, | ||
}: ESLintTarget): Promise<LinterOutput> { | ||
const results = await executeLint({ eslintrc, patterns }); | ||
const ruleOptionsPerFile = await loadRuleOptionsPerFile(eslintrc, results); | ||
return { results, ruleOptionsPerFile }; | ||
} | ||
|
||
function executeLint({ | ||
eslintrc, | ||
patterns, | ||
}: ESLintTarget): Promise<ESLint.LintResult[]> { | ||
return withConfig(eslintrc, async configPath => { | ||
// running as CLI because ESLint#lintFiles() runs out of memory | ||
const { stdout } = await executeProcess({ | ||
command: 'npx', | ||
args: [ | ||
'eslint', | ||
`--config=${configPath}`, | ||
'--no-eslintrc', | ||
'--no-error-on-unmatched-pattern', | ||
'--format=json', | ||
...toArray(patterns).map(pattern => | ||
// globs need to be escaped on Unix | ||
platform() === 'win32' ? pattern : `'${pattern}'`, | ||
), | ||
], | ||
ignoreExitCode: true, | ||
cwd: process.cwd(), | ||
}); | ||
|
||
return JSON.parse(stdout) as ESLint.LintResult[]; | ||
}); | ||
} | ||
|
||
function loadRuleOptionsPerFile( | ||
eslintrc: ESLintTarget['eslintrc'], | ||
results: ESLint.LintResult[], | ||
): Promise<RuleOptionsPerFile> { | ||
const eslint = setupESLint(eslintrc); | ||
|
||
const results = await eslint.lintFiles(patterns); | ||
|
||
const ruleOptionsPerFile = await results.reduce( | ||
async (acc, { filePath, messages }) => { | ||
const filesMap = await acc; | ||
const config = (await eslint.calculateConfigForFile( | ||
filePath, | ||
)) as Linter.Config; | ||
const ruleIds = distinct( | ||
messages | ||
.map(({ ruleId }) => ruleId) | ||
.filter((ruleId): ruleId is string => ruleId != null), | ||
); | ||
const rulesMap = Object.fromEntries( | ||
ruleIds.map(ruleId => [ | ||
ruleId, | ||
toArray(config.rules?.[ruleId] ?? []).slice(1), | ||
]), | ||
); | ||
return { | ||
...filesMap, | ||
[filePath]: { | ||
...filesMap[filePath], | ||
...rulesMap, | ||
}, | ||
}; | ||
}, | ||
Promise.resolve<RuleOptionsPerFile>({}), | ||
); | ||
return results.reduce(async (acc, { filePath, messages }) => { | ||
const filesMap = await acc; | ||
const config = (await eslint.calculateConfigForFile( | ||
filePath, | ||
)) as Linter.Config; | ||
const ruleIds = distinct( | ||
messages | ||
.map(({ ruleId }) => ruleId) | ||
.filter((ruleId): ruleId is string => ruleId != null), | ||
); | ||
const rulesMap = Object.fromEntries( | ||
ruleIds.map(ruleId => [ | ||
ruleId, | ||
toArray(config.rules?.[ruleId] ?? []).slice(1), | ||
]), | ||
); | ||
return { | ||
...filesMap, | ||
[filePath]: { | ||
...filesMap[filePath], | ||
...rulesMap, | ||
}, | ||
}; | ||
}, Promise.resolve<RuleOptionsPerFile>({})); | ||
} | ||
|
||
return { results, ruleOptionsPerFile }; | ||
async function withConfig<T>( | ||
eslintrc: ESLintTarget['eslintrc'], | ||
fn: (configPath: string) => Promise<T>, | ||
): Promise<T> { | ||
if (typeof eslintrc === 'string') { | ||
return fn(eslintrc); | ||
} | ||
|
||
const configPath = generateTempConfigPath(); | ||
await writeFile(configPath, JSON.stringify(eslintrc)); | ||
|
||
try { | ||
return await fn(configPath); | ||
} finally { | ||
await rm(configPath); | ||
} | ||
} | ||
|
||
function generateTempConfigPath(): string { | ||
return join( | ||
process.cwd(), | ||
`.eslintrc.${Math.random().toString().slice(2)}.json`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters