我配置中的哪些 eslint 规则很慢?

Which eslint rules in my config are slow?

我的配置包含大约 100 条规则,运行 在我的项目中使用所有这些规则执行 eslint 大约需要 10 秒。我想确定最慢的规则并消除其中的一些。我该怎么做呢? eslint有没有profiler工具?

如果设置了环境变量 TIMING

eslint 会显示规则的花费时间。 例如:

$ TIMING=1 eslint lib
Rule                         | Time (ms) | Relative
:----------------------------|----------:|--------:
valid-jsdoc                  |   203.798 |     6.7%
camelcase                    |   142.146 |     4.6%
no-unmodified-loop-condition |   136.811 |     4.5%
indent                       |   127.138 |     4.2%
no-undefined                 |   124.525 |     4.1%
keyword-spacing              |    85.397 |     2.8%
space-in-parens              |    76.179 |     2.5%
no-this-before-super         |    72.317 |     2.4%
no-implied-eval              |    69.945 |     2.3%
space-infix-ops              |    57.128 |     1.9%

另请参阅 Per-rule Performance 上的官方文档。

我发现删除慢规则并没有多大帮助,因为加载 eslint 和解析文件需要一段时间。

可以使用 eslint (docs) 的 --cache 选项来显着加快速度。

在各种编辑器中使用eslint到"lint-as-you-type"时,安装eslint_d允许运行eslint作为守护进程,并保存node 加载时间。

在我目前正在进行的项目中,结合使用 eslint_d--cache 将 linting 时间从 4+ 秒减少到 0.17!

在我的例子中,我使用的是 @typescript-eslint/eslint-plugin(带有类型信息的 linting)并且我错误地配置了 tsconfig include 字段。基于 this doc,您必须包含所有文件。所以我更新了我的 eslint 配置:

module.exports = {
  overrides: [
    {
      files: ['*.ts'],
      parserOptions: {
-       project: ['tsconfig.json'],
-       createDefaultProgram: true,
+       project: ['tsconfig.json', './projects/*/tsconfig.json'],
+       createDefaultProgram: false,
      },
    }
  ]
}

如果您使用 @angular-eslint/eslint-plugin,也会发生这种情况。阅读 the performance section of their docs

我也遇到了这个问题,这是因为我启用了 createDefaultProgram: true,删除它显着提高了我的表现!!