Skip to content

Commit 923a9cf

Browse files
authored
Chore: use precalculated counts in codeframe formatter (#8296)
1 parent 9f65b7d commit 923a9cf

File tree

2 files changed

+65
-38
lines changed

2 files changed

+65
-38
lines changed

index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,10 @@ module.exports = function(results) {
101101
const resultsWithMessages = results.filter(result => result.messages.length > 0);
102102

103103
let output = resultsWithMessages.reduce((resultsOutput, result) => {
104-
const messages = result.messages.map(message => {
105-
if (message.fatal || message.severity === 2) {
106-
errors++;
107-
} else {
108-
warnings++;
109-
}
110-
111-
return `${formatMessage(message, result)}\n\n`;
112-
});
104+
const messages = result.messages.map(message => `${formatMessage(message, result)}\n\n`);
105+
106+
errors += result.errorCount;
107+
warnings += result.warningCount;
113108

114109
return resultsOutput.concat(messages);
115110
}, []).join("\n");

test.js

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ describe("formatter:codeframe", () => {
5454
describe("when passed no messages", () => {
5555
const code = [{
5656
filePath: "foo.js",
57-
messages: []
57+
messages: [],
58+
errorCount: 0,
59+
warningCount: 0
5860
}];
5961

6062
it("should return nothing", () => {
@@ -64,70 +66,84 @@ describe("formatter:codeframe", () => {
6466
});
6567
});
6668

67-
describe("when passed a single message", () => {
69+
describe("when passed a single warning message", () => {
6870
const code = [{
6971
filePath: path.join(process.cwd(), "lib", "foo.js"),
7072
source: "var foo = 1;\n var bar = 2;\n",
7173
messages: [{
7274
message: "Unexpected foo.",
73-
severity: 2,
75+
severity: 1,
7476
line: 1,
7577
column: 5,
7678
ruleId: "foo"
77-
}]
79+
}],
80+
errorCount: 0,
81+
warningCount: 1
7882
}];
7983

80-
it("should return a string in the correct format for errors", () => {
84+
it("should return a string in the correct format for warnings", () => {
8185
const result = formatter(code);
8286

8387
assert.equal(chalk.stripColor(result), [
84-
`error: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
88+
`warning: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
8589
"> 1 | var foo = 1;",
8690
" | ^",
8791
" 2 | var bar = 2;",
8892
" 3 | ",
8993
"\n",
90-
"1 error found."
94+
"1 warning found."
9195
].join("\n"));
9296
});
9397

94-
it("should return a string in the correct format for warnings", () => {
95-
code[0].messages[0].severity = 1;
98+
it("should return bold yellow summary when there are only warnings", () => {
99+
sandbox.spy(chalkStub.yellow, "bold");
100+
sandbox.spy(chalkStub.red, "bold");
101+
102+
formatter(code);
103+
104+
assert.equal(chalkStub.yellow.bold.callCount, 1);
105+
assert.equal(chalkStub.red.bold.callCount, 0);
106+
});
107+
});
108+
109+
describe("when passed a single error message", () => {
110+
const code = [{
111+
filePath: path.join(process.cwd(), "lib", "foo.js"),
112+
source: "var foo = 1;\n var bar = 2;\n",
113+
messages: [{
114+
message: "Unexpected foo.",
115+
severity: 2,
116+
line: 1,
117+
column: 5,
118+
ruleId: "foo"
119+
}],
120+
errorCount: 1,
121+
warningCount: 0
122+
}];
96123

124+
it("should return a string in the correct format for errors", () => {
97125
const result = formatter(code);
98126

99127
assert.equal(chalk.stripColor(result), [
100-
`warning: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
128+
`error: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
101129
"> 1 | var foo = 1;",
102130
" | ^",
103131
" 2 | var bar = 2;",
104132
" 3 | ",
105133
"\n",
106-
"1 warning found."
134+
"1 error found."
107135
].join("\n"));
108136
});
109137

110138
it("should return bold red summary when there are errors", () => {
111139
sandbox.spy(chalkStub.yellow, "bold");
112140
sandbox.spy(chalkStub.red, "bold");
113-
code[0].messages[0].severity = 2;
114141

115142
formatter(code);
116143

117144
assert.equal(chalkStub.yellow.bold.callCount, 0);
118145
assert.equal(chalkStub.red.bold.callCount, 1);
119146
});
120-
121-
it("should return bold yellow summary when there are only warnings", () => {
122-
sandbox.spy(chalkStub.yellow, "bold");
123-
sandbox.spy(chalkStub.red, "bold");
124-
code[0].messages[0].severity = 1;
125-
126-
formatter(code);
127-
128-
assert.equal(chalkStub.yellow.bold.callCount, 1);
129-
assert.equal(chalkStub.red.bold.callCount, 0);
130-
});
131147
});
132148

133149
describe("when passed multiple messages", () => {
@@ -146,7 +162,9 @@ describe("formatter:codeframe", () => {
146162
line: 1,
147163
column: 7,
148164
ruleId: "no-unused-vars"
149-
}]
165+
}],
166+
errorCount: 2,
167+
warningCount: 0
150168
}];
151169

152170
it("should return a string with multiple entries", () => {
@@ -171,6 +189,8 @@ describe("formatter:codeframe", () => {
171189
sandbox.spy(chalkStub.yellow, "bold");
172190
sandbox.spy(chalkStub.red, "bold");
173191
code[0].messages[0].severity = 1;
192+
code[0].warningCount = 1;
193+
code[0].errorCount = 1;
174194

175195
formatter(code);
176196

@@ -190,6 +210,8 @@ describe("formatter:codeframe", () => {
190210
column: 11,
191211
source: " const foo = 1;"
192212
}],
213+
errorCount: 1,
214+
warningCount: 0,
193215
output: "function foo() {\n\n // a comment\n const foo = 1;\n}\n\n"
194216
}];
195217

@@ -221,7 +243,9 @@ describe("formatter:codeframe", () => {
221243
line: 1,
222244
column: 14,
223245
ruleId: "semi"
224-
}]
246+
}],
247+
errorCount: 1,
248+
warningCount: 0
225249
}, {
226250
filePath: "bar.js",
227251
source: "const bar = 2\n",
@@ -231,7 +255,9 @@ describe("formatter:codeframe", () => {
231255
line: 1,
232256
column: 14,
233257
ruleId: "semi"
234-
}]
258+
}],
259+
errorCount: 1,
260+
warningCount: 0
235261
}];
236262

237263
it("should return a string with multiple entries", () => {
@@ -265,7 +291,9 @@ describe("formatter:codeframe", () => {
265291
message: "Parsing error: Unexpected token {",
266292
line: 1,
267293
column: 2
268-
}]
294+
}],
295+
errorCount: 1,
296+
warningCount: 0
269297
}];
270298

271299
it("should return a string in the correct format", () => {
@@ -288,7 +316,9 @@ describe("formatter:codeframe", () => {
288316
messages: [{
289317
fatal: true,
290318
message: "Couldn't find foo.js."
291-
}]
319+
}],
320+
errorCount: 1,
321+
warningCount: 0
292322
}];
293323

294324
it("should return a string without code preview (codeframe)", () => {
@@ -306,7 +336,9 @@ describe("formatter:codeframe", () => {
306336
message: "Unexpected foo.",
307337
severity: 2,
308338
source: "foo"
309-
}]
339+
}],
340+
errorCount: 1,
341+
warningCount: 0
310342
}];
311343

312344
it("should return a string without code preview (codeframe)", () => {

0 commit comments

Comments
 (0)