Skip to content

Commit ebe8678

Browse files
authored
fix(eslint): switch to compact report (#140)
The 'stylish' report is kinda hard to maintain a fork of it. I broke the errorformat parser by making our fork of stylish not quite the same.
1 parent d34b229 commit ebe8678

File tree

2 files changed

+23
-56
lines changed

2 files changed

+23
-56
lines changed

example/test/lint_test.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ EOF
2323
assert_output --partial 'src/Foo.java:9: FinalizeOverloaded: Finalize methods should not be overloaded'
2424

2525
# ESLint
26-
assert_output --partial 'src/file.ts:2:7: Type string trivially inferred from a string literal, remove type annotation [error from @typescript-eslint/no-inferrable-types]'
26+
assert_output --partial 'src/file.ts: line 2, col 7, Error - Type string trivially inferred from a string literal, remove type annotation. (@typescript-eslint/no-inferrable-types)'
2727

2828
# Buf
2929
assert_output --partial 'src/file.proto:1:1:Import "src/unused.proto" is unused.'

lint/eslint.bazel-formatter.js

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,45 @@
1-
// Fork of 'stylish' plugin that prints relative paths.
1+
// Fork of 'compact' plugin that prints relative paths.
22
// This allows an editor to navigate to the location of the lint warning even though we present
33
// eslint with paths underneath a bazel sandbox folder.
4-
// from https://github.com/eslint/eslint/blob/331cf62024b6c7ad4067c14c593f116576c3c861/lib/cli-engine/formatters/stylish.js
4+
// from https://github.com/eslint/eslint/blob/331cf62024b6c7ad4067c14c593f116576c3c861/lib/cli-engine/formatters/compact.js
55
const path = require("node:path");
66

77
/**
8-
* Given a word and a count, append an s if count is not one.
9-
* @param {string} word A word in its singular form.
10-
* @param {int} count A number controlling whether word should be pluralized.
11-
* @returns {string} The original word with an s on the end if count is not one.
8+
* Returns the severity of warning or error
9+
* @param {Object} message message object to examine
10+
* @returns {string} severity level
11+
* @private
1212
*/
13-
function pluralize(word, count) {
14-
return count === 1 ? word : `${word}s`;
13+
function getMessageType(message) {
14+
if (message.fatal || message.severity === 2) {
15+
return "Error";
16+
}
17+
return "Warning";
1518
}
1619

1720
module.exports = function (results, context) {
1821
let output = "",
19-
errorCount = 0,
20-
warningCount = 0,
21-
fixableErrorCount = 0,
22-
fixableWarningCount = 0;
22+
total = 0;
2323

2424
results.forEach((result) => {
2525
const messages = result.messages;
2626

27-
if (messages.length === 0) {
28-
return;
29-
}
30-
31-
errorCount += result.errorCount;
32-
warningCount += result.warningCount;
33-
fixableErrorCount += result.fixableErrorCount;
34-
fixableWarningCount += result.fixableWarningCount;
35-
36-
const relpath = path.relative(context.cwd, result.filePath);
27+
total += messages.length;
3728

3829
messages.forEach((message) => {
39-
const msgtext = message.message.replace(/([^ ])\.$/u, "$1");
40-
const severity =
41-
message.fatal || message.severity === 2 ? "error" : "warning";
42-
const location = [relpath, message.line, message.column].join(":");
43-
output += `${location}: ${msgtext} [${severity} from ${
44-
message.ruleId || ""
45-
}]\n`;
30+
output += `${path.relative(context.cwd, result.filePath)}: `;
31+
output += `line ${message.line || 0}`;
32+
output += `, col ${message.column || 0}`;
33+
output += `, ${getMessageType(message)}`;
34+
output += ` - ${message.message}`;
35+
output += message.ruleId ? ` (${message.ruleId})` : "";
36+
output += "\n";
4637
});
4738
});
4839

49-
const total = errorCount + warningCount;
5040
if (total > 0) {
51-
output += [
52-
"\n",
53-
"\u2716 ",
54-
total,
55-
pluralize(" problem", total),
56-
" (",
57-
errorCount,
58-
pluralize(" error", errorCount),
59-
", ",
60-
warningCount,
61-
pluralize(" warning", warningCount),
62-
")\n",
63-
].join("");
64-
65-
if (fixableErrorCount > 0 || fixableWarningCount > 0) {
66-
output += [
67-
" ",
68-
fixableErrorCount,
69-
pluralize(" error", fixableErrorCount),
70-
" and ",
71-
fixableWarningCount,
72-
pluralize(" warning", fixableWarningCount),
73-
" potentially fixable with the `--fix` option.\n",
74-
].join("");
75-
}
41+
output += `\n${total} problem${total !== 1 ? "s" : ""}`;
7642
}
43+
7744
return output;
7845
};

0 commit comments

Comments
 (0)