Skip to content

Commit

Permalink
Allow linting specific files, and support --printConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Jan 15, 2025
1 parent 342804f commit a1f6074
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions scripts/run-eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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',
Expand All @@ -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<ESLint.LintResult[]> {
let results = await eslint.lintFiles(['.']);
let results = await eslint.lintFiles(files);
const errorResults = ESLint.getErrorResults(results);

if (errorResults.length > 0) {
Expand Down

0 comments on commit a1f6074

Please sign in to comment.