Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tally to string and verbose formatters #6153

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 25 additions & 25 deletions lib/formatters/__tests__/stringFormatter.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { stripIndent } = require('common-tags');

const prepareFormatterOutput = require('./prepareFormatterOutput');
const stringFormatter = require('../stringFormatter');

Expand Down Expand Up @@ -54,11 +56,11 @@ describe('stringFormatter', () => {

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(
`
expect(output).toBe(stripIndent`
path/to/file.css
1:1 × Unexpected foo bar`.trimStart(),
);
1:1 × Unexpected foo bar

1 problem (1 error, 0 warnings)`);
});

it('removes rule name from warning text', () => {
Expand All @@ -71,7 +73,7 @@ path/to/file.css
line: 1,
column: 1,
rule: 'rule-name',
severity: 'error',
severity: 'warning',
text: 'Unexpected foo (rule-name)',
},
],
Expand All @@ -82,11 +84,11 @@ path/to/file.css

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(
`
expect(output).toBe(stripIndent`
path/to/file.css
1:1 × Unexpected foo rule-name`.trimStart(),
);
1:1 ‼ Unexpected foo rule-name

1 problem (0 errors, 1 warning)`);
});

it('outputs warnings without stdout `TTY`', () => {
Expand All @@ -112,11 +114,11 @@ path/to/file.css

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(
`
expect(output).toBe(stripIndent`
path/to/file.css
1:1 × Unexpected foo bar`.trimStart(),
);
1:1 × Unexpected foo bar

1 problem (1 error, 0 warnings)`);
});

it('output warnings with more than 80 characters and `process.stdout.columns` equal 90 characters', () => {
Expand Down Expand Up @@ -144,12 +146,12 @@ path/to/file.css

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(
`
expect(output).toBe(stripIndent`
path/to/file.css
1:1 × Unexpected very very very very very very very bar-very-very-very-very-very-long
very very very very very very long foo`.trimStart(),
);
very very very very very very long foo

1 problem (1 error, 0 warnings)`);
});

it('condenses deprecations and invalid option warnings', () => {
Expand Down Expand Up @@ -190,12 +192,10 @@ path/to/file.css

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(
`
expect(output).toBe(stripIndent`
Invalid Option: Unexpected option for baz

Deprecation Warning: Deprecated foo See: bar`.trimStart(),
);
Deprecation Warning: Deprecated foo See: bar`);
});

it('handles ignored file', () => {
Expand Down Expand Up @@ -235,10 +235,10 @@ Deprecation Warning: Deprecated foo See: bar`.trimStart(),

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(
`
expect(output).toBe(stripIndent`
path/to/file.css
1:1 × bar`.trimStart(),
);
1:1 × bar

1 problem (1 error, 0 warnings)`);
});
});
61 changes: 35 additions & 26 deletions lib/formatters/__tests__/verboseFormatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ describe('verboseFormatter', () => {
path/to/file.css
1:2 × Unexpected foo bar

1 problem (1 error, 0 warnings)

1 source checked
path/to/file.css

1 problem found
severity level "error": 1
bar: 1`);
1 error found
bar: 1`);
});

it('outputs 0 stdout column', () => {
Expand Down Expand Up @@ -88,12 +89,13 @@ describe('verboseFormatter', () => {
path/to/file.css
1:2 × Unexpected foo bar

1 problem (1 error, 0 warnings)

1 source checked
path/to/file.css

1 problem found
severity level "error": 1
bar: 1`);
1 error found
bar: 1`);

process.stdout.columns = stdoutColumn;
});
Expand Down Expand Up @@ -127,12 +129,13 @@ describe('verboseFormatter', () => {
path/to/file.css
1:2 × Unexpected foo bar

1 problem (1 error, 0 warnings)

1 source checked
path/to/file.css

1 problem found
severity level "error": 1
bar: 1`);
1 error found
bar: 1`);

process.stdout.columns = stdoutColumn;
});
Expand Down Expand Up @@ -188,15 +191,17 @@ describe('verboseFormatter', () => {
file2.css
3:1 ‼ Expected cat baz

3 problems (2 errors, 1 warning)

2 sources checked
path/to/file.css
file2.css

3 problems found
severity level "error": 2
bar: 2
severity level "warning": 1
baz: 1`);
2 errors found
bar: 2

1 warning found
baz: 1`);
});

it('outputs lineless syntax error', () => {
Expand All @@ -222,12 +227,13 @@ describe('verboseFormatter', () => {
path/to/file.css
× Unexpected foo SyntaxError

1 problem (1 error, 0 warnings)

1 source checked
path/to/file.css

1 problem found
severity level "error": 1
SyntaxError: 1`);
1 error found
SyntaxError: 1`);
});

it('outputs one ignored file', () => {
Expand Down Expand Up @@ -275,12 +281,13 @@ describe('verboseFormatter', () => {
<input css>
1:2 × Unexpected foo bar

1 problem (1 error, 0 warnings)

1 source checked
<input css>

1 problem found
severity level "error": 1
bar: 1`);
1 error found
bar: 1`);
});

it('outputs plugin rule warnings', () => {
Expand Down Expand Up @@ -308,12 +315,13 @@ describe('verboseFormatter', () => {
file.css
1:2 × Unexpected foo plugin/bar

1 problem (1 error, 0 warnings)

1 source checked
file.css

1 problem found
severity level "error": 1
plugin/bar: 1`);
1 error found
plugin/bar: 1`);
});

it('outputs rule warnings with metadata', () => {
Expand Down Expand Up @@ -348,11 +356,12 @@ describe('verboseFormatter', () => {
file.css
1:2 × Unexpected foo no-foo

1 problem (1 error, 0 warnings)

1 source checked
file.css

1 problem found
severity level "error": 1
no-foo: 1`);
1 error found
no-foo: 1`);
});
});
32 changes: 31 additions & 1 deletion lib/formatters/stringFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const stringWidth = require('string-width');
const table = require('table');
const { yellow, dim, underline, blue, red, green } = require('picocolors');

const pluralize = require('../utils/pluralize');
const { assertNumber } = require('../utils/validateTypes');
const terminalLink = require('./terminalLink');

Expand Down Expand Up @@ -251,17 +252,22 @@ module.exports = function (results, returnValue) {

output += deprecationsFormatter(results);

let errorCount = 0;
let warningCount = 0;

output = results.reduce((accum, result) => {
// Treat parseErrors as warnings
if (result.parseErrors) {
for (const error of result.parseErrors)
for (const error of result.parseErrors) {
result.warnings.push({
line: error.line,
column: error.column,
rule: error.stylelintType,
severity: 'error',
text: `${error.text} (${error.stylelintType})`,
});
errorCount += 1;
}
}

accum += formatter(
Expand All @@ -270,6 +276,19 @@ module.exports = function (results, returnValue) {
(returnValue && returnValue.cwd) || process.cwd(),
);

for (const warning of result.warnings) {
switch (warning.severity) {
case 'error':
errorCount += 1;
break;
case 'warning':
warningCount += 1;
break;
default:
throw new Error(`Unknown severity: "${warning.severity}"`);
}
}

return accum;
}, output);

Expand All @@ -278,6 +297,17 @@ module.exports = function (results, returnValue) {

if (output !== '') {
output = `\n${output}\n\n`;

const total = errorCount + warningCount;

if (total > 0) {
const tally =
`${total} ${pluralize('problem', total)}` +
` (${errorCount} ${pluralize('error', errorCount)}` +
`, ${warningCount} ${pluralize('warning', warningCount)})`;

output += `${tally}\n\n`;
}
}

return output;
Expand Down