Skip to content

Commit 6f3b73b

Browse files
albertoilyavolodin
authored andcommitted
Breaking: log number of fixable problems (fixes #7364) (#8324)
* Breaking: log number of fixable problems (fixes #7364) * Shorten message * Add fixable counts to codeframe * Update documentaiton and rebase * Fixing minor issues * Fixing indentation issues
1 parent 923a9cf commit 6f3b73b

File tree

2 files changed

+138
-4
lines changed

2 files changed

+138
-4
lines changed

index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ function formatMessage(message, parentResult) {
7474
* Gets the formatted output summary for a given number of errors and warnings.
7575
* @param {number} errors The number of errors.
7676
* @param {number} warnings The number of warnings.
77+
* @param {number} fixableErrors The number of fixable errors.
78+
* @param {number} fixableWarnings The number of fixable warnings.
7779
* @returns {string} The formatted output summary.
7880
*/
79-
function formatSummary(errors, warnings) {
81+
function formatSummary(errors, warnings, fixableErrors, fixableWarnings) {
8082
const summaryColor = errors > 0 ? "red" : "yellow";
8183
const summary = [];
84+
const fixablesSummary = [];
8285

8386
if (errors > 0) {
8487
summary.push(`${errors} ${pluralize("error", errors)}`);
@@ -88,7 +91,21 @@ function formatSummary(errors, warnings) {
8891
summary.push(`${warnings} ${pluralize("warning", warnings)}`);
8992
}
9093

91-
return chalk[summaryColor].bold(`${summary.join(" and ")} found.`);
94+
if (fixableErrors > 0) {
95+
fixablesSummary.push(`${fixableErrors} ${pluralize("error", fixableErrors)}`);
96+
}
97+
98+
if (fixableWarnings > 0) {
99+
fixablesSummary.push(`${fixableWarnings} ${pluralize("warning", fixableWarnings)}`);
100+
}
101+
102+
let output = chalk[summaryColor].bold(`${summary.join(" and ")} found.`);
103+
104+
if (fixableErrors || fixableWarnings) {
105+
output += chalk[summaryColor].bold(`\n${fixablesSummary.join(" and ")} potentially fixable with the \`--fix\` option.`);
106+
}
107+
108+
return output;
92109
}
93110

94111
//------------------------------------------------------------------------------
@@ -98,19 +115,24 @@ function formatSummary(errors, warnings) {
98115
module.exports = function(results) {
99116
let errors = 0;
100117
let warnings = 0;
118+
let fixableErrors = 0;
119+
let fixableWarnings = 0;
120+
101121
const resultsWithMessages = results.filter(result => result.messages.length > 0);
102122

103123
let output = resultsWithMessages.reduce((resultsOutput, result) => {
104124
const messages = result.messages.map(message => `${formatMessage(message, result)}\n\n`);
105125

106126
errors += result.errorCount;
107127
warnings += result.warningCount;
128+
fixableErrors += result.fixableErrorCount;
129+
fixableWarnings += result.fixableWarningCount;
108130

109131
return resultsOutput.concat(messages);
110132
}, []).join("\n");
111133

112134
output += "\n";
113-
output += formatSummary(errors, warnings);
135+
output += formatSummary(errors, warnings, fixableErrors, fixableWarnings);
114136

115137
return (errors + warnings) > 0 ? output : "";
116138
};

test.js

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ describe("formatter:codeframe", () => {
7878
ruleId: "foo"
7979
}],
8080
errorCount: 0,
81-
warningCount: 1
81+
warningCount: 1,
82+
fixableErrorCount: 0,
83+
fixableWarningCount: 0
8284
}];
8385

8486
it("should return a string in the correct format for warnings", () => {
@@ -104,6 +106,27 @@ describe("formatter:codeframe", () => {
104106
assert.equal(chalkStub.yellow.bold.callCount, 1);
105107
assert.equal(chalkStub.red.bold.callCount, 0);
106108
});
109+
110+
describe("when the warning is fixable", () => {
111+
beforeEach(() => {
112+
code[0].fixableWarningCount = 1;
113+
});
114+
115+
it("should return a string in the correct format", () => {
116+
const result = formatter(code);
117+
118+
assert.equal(chalk.stripColor(result), [
119+
`warning: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
120+
"> 1 | var foo = 1;",
121+
" | ^",
122+
" 2 | var bar = 2;",
123+
" 3 | ",
124+
"\n",
125+
"1 warning found.",
126+
"1 warning potentially fixable with the `--fix` option."
127+
].join("\n"));
128+
});
129+
});
107130
});
108131

109132
describe("when passed a single error message", () => {
@@ -353,4 +376,93 @@ describe("formatter:codeframe", () => {
353376
assert.equal(chalk.stripColor(result), "error: Unexpected foo (foo) at foo.js\n\n\n1 error found.");
354377
});
355378
});
379+
380+
381+
describe("fixable problems", () => {
382+
it("should not output fixable problems message when no errors or warnings are fixable", () => {
383+
const code = [{
384+
filePath: "foo.js",
385+
errorCount: 1,
386+
warningCount: 0,
387+
fixableErrorCount: 0,
388+
fixableWarningCount: 0,
389+
messages: [{
390+
message: "Unexpected foo.",
391+
severity: 2,
392+
line: 5,
393+
column: 10,
394+
ruleId: "foo"
395+
}]
396+
}];
397+
398+
const result = formatter(code);
399+
400+
assert.notInclude(result, "potentially fixable");
401+
});
402+
403+
it("should output the fixable problems message when errors are fixable", () => {
404+
const code = [{
405+
filePath: "foo.js",
406+
errorCount: 1,
407+
warningCount: 0,
408+
fixableErrorCount: 1,
409+
fixableWarningCount: 0,
410+
messages: [{
411+
message: "Unexpected foo.",
412+
severity: 2,
413+
line: 5,
414+
column: 10,
415+
ruleId: "foo"
416+
}]
417+
}];
418+
419+
const result = formatter(code);
420+
421+
assert.include(result, "1 error potentially fixable with the `--fix` option.");
422+
});
423+
424+
it("should output fixable problems message when warnings are fixable", () => {
425+
const code = [{
426+
filePath: "foo.js",
427+
errorCount: 0,
428+
warningCount: 3,
429+
fixableErrorCount: 0,
430+
fixableWarningCount: 2,
431+
messages: [{
432+
message: "Unexpected foo."
433+
}]
434+
}];
435+
436+
const result = formatter(code);
437+
438+
assert.include(result, "2 warnings potentially fixable with the `--fix` option.");
439+
});
440+
441+
it("should output the total number of fixable errors and warnings", () => {
442+
const code = [{
443+
filePath: "foo.js",
444+
errorCount: 5,
445+
warningCount: 3,
446+
fixableErrorCount: 5,
447+
fixableWarningCount: 2,
448+
messages: [{
449+
message: "Unexpected foo."
450+
}]
451+
}, {
452+
filePath: "bar.js",
453+
errorCount: 4,
454+
warningCount: 2,
455+
fixableErrorCount: 4,
456+
fixableWarningCount: 1,
457+
messages: [{
458+
message: "Unexpected bar."
459+
}]
460+
}];
461+
462+
const result = formatter(code);
463+
464+
assert.include(result, "9 errors and 3 warnings potentially fixable with the `--fix` option.");
465+
});
466+
});
467+
356468
});

0 commit comments

Comments
 (0)