ESLint 扩展 vs 插件 v2020

Ale*_*Kim 11 javascript typescript eslint

在我看来,有一个已回答的问题实际上并没有回答这个问题,关于ESLint 中extends: []vs之间plugins: []的区别。

就我而言,我只使用了扩展部分:

extends: [
  'plugin:@typescript-eslint/recommended',
],
plugins: [],
rules: {
  '@typescript-eslint/explicit-function-return-type': [
    'error',
    {
      allowExpressions: true,
    },
  ],
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我只是使用了预定义的配置,plugin:@typescript-eslint/recommended@typescript-eslint/explicit-function-return-typerules: {}部分中覆盖了规则。但是为什么我们需要这个插件部分呢?如果没有它一切正常?我想念什么?

Eme*_*nik 8

你做得对。

对于您的示例,有两种方法可以添加typescript-eslint...

  • 第一种方式:
{
  parser: "@typescript-eslint/parser",
  parserOptions: { sourceType: "module" },
  plugins: ["@typescript-eslint"],
  extends: [],
  rules: {
    "@typescript-eslint/explicit-function-return-type": [
      "error",
      {
        allowExpressions: true
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)
  • 方式二:
{
  plugins: [],
  extends: ["plugin:@typescript-eslint/recommended"],
  rules: {
    "@typescript-eslint/explicit-function-return-type": [
      "error",
      {
        allowExpressions: true
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

区别在于...

  • 第一种方式:
    • parserparserOptionsplugins手动添加,
    • 只有@typescript-eslint/explicit-function-return-type被强制执行。
  • 方式二:
    • plugin:@typescript-eslint/recommended已自动添加parser,parserOptionsplugins
    • 添加并强制执行推荐的打字稿规则。
    • @typescript-eslint/explicit-function-return-type 得到加强和执行。

这就是为什么当您使用 时plugin:@typescript-eslint/recommended,即使plugins为空,也能正常工作。编写良好的插件/配置允许这种情况发生。

  • @LGenzelis我认为这取决于插件以及插件作者如何实现它,这有点烦人,因为这意味着你永远无法确定插件是否可以在不将其添加到“插件”的情况下工作。 (2认同)