sourcegraph/eslint-relative-formatter.js
Valery Bugakov 760db946dd
bazel: implement custom ESLint Bazel rule (#52062)
- Upgraded `aspect_bazel_lib`, `aspect_rules_js` and `aspect_rules_ts`
to the latest versions.
- Ran [bazel run
//.aspect/bazelrc:update_aspect_bazelrc_presets](40a7422385)
- Added `eslint_config` macro for client package eslint configuration
`js_library` targets.
- Implemented the custom ESLint rule, which copies `srcs` with
dependencies and **declarations** to the Bazel to lint them. This way,
we maintain the ability to do type-aware linting in Bazel.
- Added a custom ESLint formatter used in Bazel to print out relative
paths in ESLint reports.

In the follow-up PR, I will look into improvements suggested by
@alexeagle that should allow us to convert ESLint build targets into
test targets and gracefully manage linting failures.

## Test plan

1. CI
2. `bazel build $(bazel query 'kind("_eslint_test_with_types",
//client/...)')`
2023-05-22 04:05:45 -07:00

104 lines
2.6 KiB
JavaScript

// The default ESLint formatter with relative paths.
// Forked from https://github.com/eslint/eslint/blob/main/lib/cli-engine/formatters/stylish.js
const path = require('path')
const chalk = require('chalk')
const stripAnsi = require('strip-ansi')
const table = require('text-table')
function pluralize(word, count) {
return count === 1 ? word : `${word}s`
}
module.exports = function (results) {
let output = '\n',
errorCount = 0,
warningCount = 0,
fixableErrorCount = 0,
fixableWarningCount = 0,
summaryColor = 'yellow'
results.forEach(result => {
const messages = result.messages
if (messages.length === 0) {
return
}
errorCount += result.errorCount
warningCount += result.warningCount
fixableErrorCount += result.fixableErrorCount
fixableWarningCount += result.fixableWarningCount
// This is the only line that is different from the original stylish formatter.
output += `${chalk.underline(path.relative(process.cwd(), result.filePath))}\n`
output += `${table(
messages.map(message => {
let messageType
if (message.fatal || message.severity === 2) {
messageType = chalk.red('error')
summaryColor = 'red'
} else {
messageType = chalk.yellow('warning')
}
return [
'',
message.line || 0,
message.column || 0,
messageType,
message.message.replace(/([^ ])\.$/u, '$1'),
chalk.dim(message.ruleId || ''),
]
}),
{
align: ['', 'r', 'l'],
stringLength(str) {
return stripAnsi(str).length
},
}
)
.split('\n')
.map(el => el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => chalk.dim(`${p1}:${p2}`)))
.join('\n')}\n\n`
})
const total = errorCount + warningCount
if (total > 0) {
output += chalk[summaryColor].bold(
[
'\u2716 ',
total,
pluralize(' problem', total),
' (',
errorCount,
pluralize(' error', errorCount),
', ',
warningCount,
pluralize(' warning', warningCount),
')\n',
].join('')
)
if (fixableErrorCount > 0 || fixableWarningCount > 0) {
output += chalk[summaryColor].bold(
[
' ',
fixableErrorCount,
pluralize(' error', fixableErrorCount),
' and ',
fixableWarningCount,
pluralize(' warning', fixableWarningCount),
' potentially fixable with the `--fix` option.\n',
].join('')
)
}
}
// Resets output color, for prevent change on top level
return total > 0 ? chalk.reset(output) : ''
}