如何在终端中只打印出 eslint 错误的数量?

How to print out only the number of eslint errors in the terminal?

我正在使用的项目有一个庞大的代码库,这意味着如果我在终端中执行 eslint *.js,我会在输出中得到数千行。我只想调整此命令以打印出错误数量,而不是实际一一列出所有错误。

如何使我的结果与此类似:

96 problems

根据文档 (https://eslint.org/docs/user-guide/command-line-interface),您可以 运行 使用标志 --quiet

eslint --quiet 'src/**'

您可能会发现这很有用,它会为您提供每种错误的数量

https://www.npmjs.com/package/eslint-formatter-summary-chart

% eslint --format summary-chart src
 
==== Files ====
bar.js : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇  33.33%
foo.js : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇  66.67%
 
 
==== Rules ====
constructor-super     : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇  16.67%
no-cond-assign        : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇  16.67%
no-constant-condition : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇  16.67%
no-debugger           : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇  16.67%
no-unused-vars        : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇  33.33%

多想想,如果你真的只想要一个数字,那么创建你自己的格式化程序,看起来像这样。

const errorsInFile => (el, currentEl) => el + currentEl.errorCount

module.exports = function (results) {
  return `${results.reduce(errorsInFile, 0)} problems`
}

或者只是为了好玩,我们可以用 Ramda 在功能上做到这一点

import { map, pipe, prop, reduce, sum } from 'ramda'

const sumArgs = (...args) => sum(args)
const nProblems = n => `${n} problems`

module.exports = pipe(
  map(prop(‘errorCount’),
  reduce(sumArgs),
  nProblems,
)

在 shell 脚本(例如 bash 或 zsh)中,您可以使用 wc 和 [=14] 计算 eslint 输出的错误行数=] 它到控制台。

ERRCOUNT=$(eval "eslint | wc -l")
echo $OUTPUT errors

示例结果:

185 errors