Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint files with same xo config together #621

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 22 additions & 9 deletions index.js
@@ -1,7 +1,7 @@
import path from 'node:path';
import {ESLint} from 'eslint';
import {globby, isGitIgnoredSync} from 'globby';
import {isEqual} from 'lodash-es';
import {isEqual, groupBy} from 'lodash-es';
import micromatch from 'micromatch';
import arrify from 'arrify';
import slash from 'slash';
Expand All @@ -12,7 +12,7 @@ import {
} from './lib/options-manager.js';
import {mergeReports, processReport, getIgnoredReport} from './lib/report.js';

const runEslint = async (lint, options) => {
const runEslint = async (lint, options, eslint) => {
const {filePath, eslintOptions, isQuiet} = options;
const {cwd, baseConfig: {ignorePatterns}} = eslintOptions;

Expand All @@ -26,7 +26,7 @@ const runEslint = async (lint, options) => {
return getIgnoredReport(filePath);
}

const eslint = new ESLint(eslintOptions);
eslint = eslint || new ESLint(eslintOptions);

if (filePath && await eslint.isPathIgnored(filePath)) {
return getIgnoredReport(filePath);
Expand Down Expand Up @@ -72,16 +72,29 @@ const lintText = async (string, options) => {
);
};

const lintFile = async (filePath, options) => runEslint(
eslint => eslint.lintFiles([filePath]),
await parseOptions({...options, filePath}),
);

const lintFiles = async (patterns, options) => {
const files = await globFiles(patterns, options);

const allOptions = await Promise.all(
files.map(filePath => parseOptions({...options, filePath})),
);

// Files with same `xoConfigPath` can lint together
// https://github.com/xojs/xo/issues/599
const groups = groupBy(allOptions, 'xoConfigPath');
const reports = await Promise.all(
files.map(filePath => lintFile(filePath, options)),
Object.values(groups)
.map(filesWithOptions => {
const options = filesWithOptions[0];
const eslint = new ESLint(options.eslintOptions);
const files = filesWithOptions.map(({filePath}) => filePath);

return runEslint(
() => eslint.lintFiles(files),
options,
eslint,
);
}),
);

const report = mergeReports(reports.filter(({isIgnored}) => !isIgnored));
Expand Down
9 changes: 7 additions & 2 deletions lib/options-manager.js
Expand Up @@ -132,7 +132,7 @@ const mergeWithFileConfig = async options => {
await fs.writeFile(options.tsConfigPath, JSON.stringify(config));
}

return {options, prettierOptions};
return {options, prettierOptions, xoConfigPath};
};

/**
Expand Down Expand Up @@ -538,13 +538,18 @@ const gatherImportResolvers = options => {

const parseOptions = async options => {
options = normalizeOptions(options);
const {options: foundOptions, prettierOptions} = await mergeWithFileConfig(options);
const {
options: foundOptions,
prettierOptions,
xoConfigPath,
} = await mergeWithFileConfig(options);
const {filePath, warnIgnored, ...eslintOptions} = buildConfig(foundOptions, prettierOptions);
return {
filePath,
warnIgnored,
isQuiet: options.quiet,
eslintOptions,
xoConfigPath,
};
};

Expand Down