Skip to content

Commit

Permalink
feat(linter): add error and warning statistics
Browse files Browse the repository at this point in the history
Outputs the error and warning count statistics including how many can be auto-fixed
  • Loading branch information
SimeonC committed Jan 31, 2024
1 parent 1327904 commit 3598aa6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 40 deletions.
66 changes: 47 additions & 19 deletions packages/eslint/src/executors/lint/lint.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,45 @@ describe('Linter Builder', () => {
}),
mockContext
);
expect(console.error).toHaveBeenCalledWith(
'Lint errors found in the listed files.\n'
expect(console.info).toHaveBeenCalledWith(
'✖ 14 problems (4 errors, 10 warnings)\n'
);
});

it('should log fixable errors or warnings', async () => {
mockReports = [
{
errorCount: 2,
warningCount: 4,
fixableErrorCount: 1,
fixableWarningCount: 2,
results: [],
usedDeprecatedRules: [],
},
{
errorCount: 3,
warningCount: 6,
fixableErrorCount: 2,
fixableWarningCount: 4,
results: [],
usedDeprecatedRules: [],
},
];
setupMocks();
await lintExecutor(
createValidRunBuilderOptions({
eslintConfig: './.eslintrc.json',
lintFilePatterns: ['includedFile1'],
format: 'json',
silent: false,
}),
mockContext
);
expect(console.info).toHaveBeenCalledWith(
'✖ 15 problems (5 errors, 10 warnings)\n'
);
expect(console.warn).toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
expect(console.info).toHaveBeenCalledWith(
' 3 errors and 6 warnings are potentially fixable with the `--fix` option.\n'
);
});

Expand Down Expand Up @@ -375,13 +409,13 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
}),
mockContext
);
expect(console.error).not.toHaveBeenCalledWith(
'Lint errors found in the listed files.\n'
expect(console.info).not.toHaveBeenCalledWith(
'✖ 0 problems (0 errors, 0 warnings)\n'
);
expect(console.warn).not.toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
expect(console.info).not.toHaveBeenCalledWith(
' 0 errors and 0 warnings are potentially fixable with the `--fix` option.\n'
);
expect(console.info).toHaveBeenCalledWith('All files pass linting.\n');
expect(console.info).toHaveBeenCalledWith('All files pass linting\n');
});

it('should not log warnings if the quiet flag was passed', async () => {
Expand Down Expand Up @@ -486,11 +520,8 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
}),
mockContext
);
expect(console.error).toHaveBeenCalledWith(
'Lint errors found in the listed files.\n'
);
expect(console.warn).not.toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
expect(console.info).toHaveBeenCalledWith(
'✖ 4 problems (4 errors, 0 warnings)\n'
);
});
it('should not log if the silent flag was passed', async () => {
Expand Down Expand Up @@ -518,11 +549,8 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
}),
mockContext
);
expect(console.error).not.toHaveBeenCalledWith(
'Lint errors found in the listed files.\n'
);
expect(console.warn).not.toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
expect(console.info).not.toHaveBeenCalledWith(
'✖ 14 problems (4 errors, 10 warnings)\n'
);
});
});
Expand Down
80 changes: 59 additions & 21 deletions packages/eslint/src/executors/lint/lint.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,6 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this

const formatter = await eslint.loadFormatter(normalizedOptions.format);

let totalErrors = 0;
let totalWarnings = 0;

for (const result of lintResults) {
if (result.errorCount || result.warningCount) {
totalErrors += result.errorCount;
totalWarnings += result.warningCount;
}
}

const formattedResults = await formatter.format(lintResults);

if (normalizedOptions.outputFile) {
Expand All @@ -197,23 +187,71 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
console.info(formattedResults);
}

if (totalWarnings > 0 && printInfo) {
console.warn('Lint warnings found in the listed files.\n');
}
const totals = getTotals(lintResults);

if (totalErrors > 0 && printInfo) {
console.error('Lint errors found in the listed files.\n');
}

if (totalWarnings === 0 && totalErrors === 0 && printInfo) {
console.info('All files pass linting.\n');
if (printInfo) {
outputPrintInfo(totals);
}

return {
success:
normalizedOptions.force ||
(totalErrors === 0 &&
(totals.errors === 0 &&
(normalizedOptions.maxWarnings === -1 ||
totalWarnings <= normalizedOptions.maxWarnings)),
totals.warnings <= normalizedOptions.maxWarnings)),
};
}

function getTotals(lintResults: ESLint.LintResult[]) {
let errors = 0;
let warnings = 0;
let fixableErrors = 0;
let fixableWarnings = 0;

for (const result of lintResults) {
errors += result.errorCount || 0;
warnings += result.warningCount || 0;
fixableErrors += result.fixableErrorCount || 0;
fixableWarnings += result.fixableWarningCount || 0;
}

return {
errors,
warnings,
fixableErrors,
fixableWarnings,
};
}

function pluralizedOutput(word: string, count: number) {
return `${count} ${word}${count === 1 ? '' : 's'}`;
}

function outputPrintInfo({
errors,
warnings,
fixableErrors,
fixableWarnings,
}: ReturnType<typeof getTotals>) {
const total = warnings + errors;
const totalFixable = fixableErrors + fixableWarnings;

if (total <= 0) {
console.info('\u2714 All files pass linting\n');
return;
}

console.info(
`\u2716 ${pluralizedOutput('problem', total)} (${pluralizedOutput(
'error',
errors
)}, ${pluralizedOutput('warning', warnings)})\n`
);
if (totalFixable <= 0) return;
console.info(
` ${pluralizedOutput('error', fixableErrors)} and ${pluralizedOutput(
'warning',
fixableWarnings
)} are potentially fixable with the \`--fix\` option.\n`
);
}

0 comments on commit 3598aa6

Please sign in to comment.