Skip to content

Commit

Permalink
Include rulesMeta in linting results (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Jul 21, 2022
1 parent 9ac43fd commit cd86133
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 61 deletions.
2 changes: 1 addition & 1 deletion cli.js
Expand Up @@ -149,7 +149,7 @@ if (process.env.GITHUB_ACTIONS && !options.fix && !options.reporter) {

const log = async report => {
const reporter = options.reporter || process.env.GITHUB_ACTIONS ? await xo.getFormatter(options.reporter || 'compact') : formatterPretty;
process.stdout.write(reporter(report.results));
process.stdout.write(reporter(report.results, {rulesMeta: report.rulesMeta}));
process.exitCode = report.errorCount === 0 ? 0 : 1;
};

Expand Down
9 changes: 7 additions & 2 deletions index.js
Expand Up @@ -66,7 +66,10 @@ const lintText = async (string, options) => {
}

const report = await eslint.lintText(string, {filePath, warnIgnored});
return processReport(report, {isQuiet});

const rulesMeta = eslint.getRulesMetaForResults(report);

return processReport(report, {isQuiet, rulesMeta});
};

const lintFiles = async (patterns, options) => {
Expand Down Expand Up @@ -102,7 +105,9 @@ const lintFiles = async (patterns, options) => {

const report = await eslint.lintFiles(files);

return processReport(report, {isQuiet: options.isQuiet});
const rulesMeta = eslint.getRulesMetaForResults(report);

return processReport(report, {isQuiet: options.isQuiet, rulesMeta});
}));

const report = mergeReports(reports);
Expand Down
4 changes: 3 additions & 1 deletion lib/report.js
Expand Up @@ -13,18 +13,20 @@ const mergeReports = reports => {
report.results.push(...currentReport.results);
report.errorCount += currentReport.errorCount;
report.warningCount += currentReport.warningCount;
report.rulesMeta = {...report.rulesMeta, ...currentReport.rulesMeta};
}

return report;
};

const processReport = (report, {isQuiet = false} = {}) => {
const processReport = (report, {isQuiet = false, rulesMeta} = {}) => {
if (isQuiet) {
report = ESLint.getErrorResults(report);
}

const result = {
results: report,
rulesMeta,
...getReportStatistics(report),
};

Expand Down
24 changes: 18 additions & 6 deletions test/lint-files.js
Expand Up @@ -7,9 +7,11 @@ import xo from '../index.js';
const {__dirname} = createEsmUtils(import.meta);
process.chdir(__dirname);

const hasRule = (results, filePath, ruleId) => {
const hasRule = (results, filePath, ruleId, rulesMeta) => {
const result = results.find(x => x.filePath === filePath);
return result ? result.messages.some(x => x.ruleId === ruleId) : false;
const hasRuleInResults = result ? result.messages.some(x => x.ruleId === ruleId) : false;
const hasRuleInResultsMeta = rulesMeta ? typeof rulesMeta[ruleId] === 'object' : true;
return hasRuleInResults && hasRuleInResultsMeta;
};

test('only accepts allowed extensions', async t => {
Expand Down Expand Up @@ -120,14 +122,15 @@ test('multiple negative patterns should act as positive patterns', async t => {
});

test.failing('enable rules based on nodeVersion', async t => {
const {results} = await xo.lintFiles('**/*', {cwd: 'fixtures/engines-overrides'});
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: 'fixtures/engines-overrides'});

// The transpiled file (as specified in `overrides`) should use `await`
t.true(
hasRule(
results,
path.resolve('fixtures/engines-overrides/promise-then-transpile.js'),
'promise/prefer-await-to-then',
rulesMeta,
),
);
// The non transpiled files can use `.then`
Expand All @@ -136,6 +139,7 @@ test.failing('enable rules based on nodeVersion', async t => {
results,
path.resolve('fixtures/engines-overrides/promise-then.js'),
'promise/prefer-await-to-then',
rulesMeta,
),
);
});
Expand All @@ -152,13 +156,14 @@ test('do not lint eslintignored files', async t => {
});

test('find configurations close to linted file', async t => {
const {results} = await xo.lintFiles('**/*', {cwd: 'fixtures/nested-configs'});
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: 'fixtures/nested-configs'});

t.true(
hasRule(
results,
path.resolve('fixtures/nested-configs/child/semicolon.js'),
'semi',
rulesMeta,
),
);

Expand All @@ -167,6 +172,7 @@ test('find configurations close to linted file', async t => {
results,
path.resolve('fixtures/nested-configs/child-override/child-prettier-override/semicolon.js'),
'prettier/prettier',
rulesMeta,
),
);

Expand All @@ -175,6 +181,7 @@ test('find configurations close to linted file', async t => {
results,
path.resolve('fixtures/nested-configs/no-semicolon.js'),
'semi',
rulesMeta,
),
);

Expand All @@ -183,18 +190,20 @@ test('find configurations close to linted file', async t => {
results,
path.resolve('fixtures/nested-configs/child-override/two-spaces.js'),
'indent',
rulesMeta,
),
);
});

test.serial('typescript files', async t => {
const {results} = await xo.lintFiles('**/*', {cwd: 'fixtures/typescript'});
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: 'fixtures/typescript'});

t.true(
hasRule(
results,
path.resolve('fixtures/typescript/two-spaces.tsx'),
'@typescript-eslint/indent',
rulesMeta,
),
);

Expand All @@ -203,6 +212,7 @@ test.serial('typescript files', async t => {
results,
path.resolve('fixtures/typescript/child/extra-semicolon.ts'),
'@typescript-eslint/no-extra-semi',
rulesMeta,
),
);

Expand All @@ -211,6 +221,7 @@ test.serial('typescript files', async t => {
results,
path.resolve('fixtures/typescript/child/sub-child/four-spaces.ts'),
'@typescript-eslint/indent',
rulesMeta,
),
);
});
Expand Down Expand Up @@ -287,13 +298,14 @@ test('webpack import resolver is used if {webpack: true}', async t => {
});

async function configType(t, {dir}) {
const {results} = await xo.lintFiles('**/*', {cwd: path.resolve('fixtures', 'config-files', dir)});
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: path.resolve('fixtures', 'config-files', dir)});

t.true(
hasRule(
results,
path.resolve('fixtures', 'config-files', dir, 'file.js'),
'indent',
rulesMeta,
),
);
}
Expand Down

0 comments on commit cd86133

Please sign in to comment.