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 authored Jul 26, 2023
1 parent 1dadb3d commit 32576a1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
53 changes: 45 additions & 8 deletions packages/linter/src/executors/eslint/lint.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,47 @@ describe('Linter Builder', () => {
mockContext
);
expect(console.error).toHaveBeenCalledWith(
'Lint errors found in the listed files.\n'
'4 Lint errors found in the listed files.\n'
);
expect(console.warn).toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
'10 Lint warnings found in the listed files.\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.error).toHaveBeenCalledWith(
'5 Lint errors found in the listed files. (3 are fixable)\n'
);
expect(console.warn).toHaveBeenCalledWith(
'10 Lint warnings found in the listed files. (6 are fixable)\n'
);
});

Expand Down Expand Up @@ -344,10 +381,10 @@ 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'
'0 Lint errors found in the listed files.\n'
);
expect(console.warn).not.toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
'0 Lint warnings found in the listed files.\n'
);
expect(console.info).toHaveBeenCalledWith('All files pass linting.\n');
});
Expand Down Expand Up @@ -455,10 +492,10 @@ 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'
'4 Lint errors found in the listed files.\n'
);
expect(console.warn).not.toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
'10 Lint warnings found in the listed files.\n'
);
});
it('should not log if the silent flag was passed', async () => {
Expand Down Expand Up @@ -487,10 +524,10 @@ 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'
'4 Lint errors found in the listed files.\n'
);
expect(console.warn).not.toHaveBeenCalledWith(
'Lint warnings found in the listed files.\n'
'10 Lint warnings found in the listed files.\n'
);
});
});
Expand Down
14 changes: 8 additions & 6 deletions packages/linter/src/executors/eslint/lint.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
const formatter = await eslint.loadFormatter(options.format);

let totalErrors = 0;
let totalFixableErrors = 0;
let totalWarnings = 0;
let totalFixableWarnings = 0;

for (const result of lintResults) {
if (result.errorCount || result.warningCount) {
totalErrors += result.errorCount;
totalWarnings += result.warningCount;
}
totalErrors += result.errorCount || 0;
totalWarnings += result.warningCount || 0;
totalFixableErrors += result.fixableErrorCount || 0;
totalFixableWarnings += result.fixableWarningCount || 0;
}

const formattedResults = await formatter.format(lintResults);
Expand All @@ -149,11 +151,11 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
}

if (totalWarnings > 0 && printInfo) {
console.warn('Lint warnings found in the listed files.\n');
console.warn(`${totalWarnings} Lint warnings found in the listed files.${totalFixableWarnings ? ` (${totalFixableWarnings} are fixable)` : ''}\n`);
}

if (totalErrors > 0 && printInfo) {
console.error('Lint errors found in the listed files.\n');
console.error(`${totalErrors} Lint errors found in the listed files.${totalFixableErrors ? ` (${totalFixableErrors} are fixable)` : ''}\n`);
}

if (totalWarnings === 0 && totalErrors === 0 && printInfo) {
Expand Down

0 comments on commit 32576a1

Please sign in to comment.