From a1f6074c3ae9785e0340d232d8620dda7e26585c Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Wed, 15 Jan 2025 11:42:18 -0700 Subject: [PATCH] Allow linting specific files, and support --printConfig --- scripts/run-eslint.ts | 60 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/scripts/run-eslint.ts b/scripts/run-eslint.ts index 1c70fac5c3..54ff7a3e35 100644 --- a/scripts/run-eslint.ts +++ b/scripts/run-eslint.ts @@ -42,13 +42,26 @@ main().catch((error) => { * The entrypoint to this script. */ async function main() { - const { cache, fix, quiet } = parseCommandLineArguments(); + const parsedArguments = parseCommandLineArguments(); + const { cache, fix, printConfig, quiet, files: givenFiles } = parsedArguments; + const files = givenFiles.length > 0 ? givenFiles : ['.']; const eslint = new ESLint({ cache, fix }); - const results = await runESLint(eslint, { fix, quiet }); + + if (printConfig) { + if (files.length > 1) { + throw new Error('Must pass only one file to pass --printConfig'); + } + + const config = await eslint.calculateConfigForFile(files[0]); + console.log(JSON.stringify(config, null, ' ')); + return; + } + + const results = await lintFiles(eslint, files, { fix, quiet }); const hasErrors = results.some((result) => result.errorCount > 0); - if (!quiet && !hasErrors) { + if (files.length === 0 && !quiet && !hasErrors) { evaluateWarnings(results); } } @@ -59,7 +72,13 @@ async function main() { * @returns The parsed arguments. */ function parseCommandLineArguments() { - return yargs(process.argv.slice(2)) + const { + cache, + fix, + printConfig, + quiet, + _: rest, + } = yargs(process.argv.slice(2)) .option('cache', { type: 'boolean', description: 'Cache results to speed up future runs', @@ -70,30 +89,53 @@ function parseCommandLineArguments() { description: 'Automatically fix problems', default: false, }) + .option('printConfig', { + type: 'boolean', + description: + 'Print the configuration used for the given file rather than running lint rules', + default: false, + }) .option('quiet', { type: 'boolean', description: 'Only report errors, disabling the warnings quality gate in the process', default: false, }) - .help().argv; + .help() + .string('_').argv; + + // Type assertion: yargs' types incorrectly type `_`. + const files = rest as string[]; + + return { + cache, + files, + fix, + printConfig, + quiet, + }; } /** * Runs ESLint on the project files. * * @param eslint - The ESLint instance. + * @param files - The files to lint. * @param options - The options for running ESLint. - * @param options.quiet - Whether to only report errors (true) or not (false). * @param options.fix - Whether to automatically fix problems (true) or not * (false). + * @param options.quiet - Whether to only report errors (true) or not (false). * @returns A promise that resolves to the lint results. */ -async function runESLint( +async function lintFiles( eslint: ESLint, - options: { quiet: boolean; fix: boolean }, + files: string[], + options: { + fix: boolean; + quiet: boolean; + }, ): Promise { - let results = await eslint.lintFiles(['.']); + let results = await eslint.lintFiles(files); const errorResults = ESLint.getErrorResults(results); if (errorResults.length > 0) {