Skip to content

Commit

Permalink
feat: add optional rules meta option to lint text
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Jul 19, 2022
1 parent 9ac43fd commit fe209d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ const lintText = async (string, options) => {
}

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

let rulesMetaForResults;

if (options && options.rulesMetaForResults === true) {
rulesMetaForResults = eslint.getRulesMetaForResults(report);
}

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

const lintFiles = async (patterns, options) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const mergeReports = reports => {
return report;
};

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

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

Expand Down
10 changes: 10 additions & 0 deletions test/lint-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ test('find configurations close to linted file', async t => {
t.true(hasRule(results, 'indent'));
});

test('rulesMetaForResults is not added to the report by default', async t => {
const report = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n');
t.is(report.rulesMetaForResults, undefined);
});

test('rulesMetaForResults option adds rulesMetaForResults to the report', async t => {
const report = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n', {rulesMetaForResults: true});
t.truthy(report.rulesMetaForResults.semi);
});

test('typescript files: two spaces fails', async t => {
const twoSpacesCwd = path.resolve('fixtures', 'typescript');
const twoSpacesfilePath = path.resolve(twoSpacesCwd, 'two-spaces.tsx');
Expand Down

0 comments on commit fe209d8

Please sign in to comment.