From 32576a16bf5b57ad2e1276cbc7c7bc7681c4410e Mon Sep 17 00:00:00 2001 From: Simeon Cheeseman <1085899+SimeonC@users.noreply.github.com> Date: Wed, 26 Jul 2023 16:04:52 +0900 Subject: [PATCH] feat(linter): add error and warning statistics Outputs the error and warning count statistics including how many can be auto-fixed --- .../src/executors/eslint/lint.impl.spec.ts | 53 ++++++++++++++++--- .../linter/src/executors/eslint/lint.impl.ts | 14 ++--- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/packages/linter/src/executors/eslint/lint.impl.spec.ts b/packages/linter/src/executors/eslint/lint.impl.spec.ts index cbbb185a763645..bb7f8b4e016746 100644 --- a/packages/linter/src/executors/eslint/lint.impl.spec.ts +++ b/packages/linter/src/executors/eslint/lint.impl.spec.ts @@ -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' ); }); @@ -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'); }); @@ -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 () => { @@ -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' ); }); }); diff --git a/packages/linter/src/executors/eslint/lint.impl.ts b/packages/linter/src/executors/eslint/lint.impl.ts index e0ad4736a7414c..b59cd48b2d4d88 100644 --- a/packages/linter/src/executors/eslint/lint.impl.ts +++ b/packages/linter/src/executors/eslint/lint.impl.ts @@ -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); @@ -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) {