【问题标题】:ESLint extends vs plugins v2020ESLint 扩展 vs 插件 v2020
【发布时间】:2020-08-15 02:22:31
【问题描述】:

关于 ESLint 中 extends: []plugins: [] 之间的区别,我认为已回答 question 并没有真正回答问题。

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

extends: [
  'plugin:@typescript-eslint/recommended',
],
plugins: [],
rules: {
  '@typescript-eslint/explicit-function-return-type': [
    'error',
    {
      allowExpressions: true,
    },
  ],
}

如您所见,我只是使用了来自plugin:@typescript-eslint/recommended 的预定义配置,并且还在rules: {} 部分覆盖了@typescript-eslint/explicit-function-return-type 规则。但是为什么我们需要这个 PLUGINS 部分呢?如果没有它一切正常?我错过了什么?

【问题讨论】:

    标签: javascript typescript eslint


    【解决方案1】:

    你做得对。

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

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

    区别是……

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

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

    【讨论】:

    • 这个答案似乎暗示如果我在extend: 下添加一些东西,那么在plugins: 下添加它也是不必要的。我认为这是不对的。在我正在进行的一个项目中,我刚刚从plugins 中删除了一个插件(并将其保留在extends: 中)。现在我收到eslint 错误说Definition for rule 'xxxxx' was not found.
    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2020-04-03
    • 1970-01-01
    • 2015-11-11
    • 2019-07-30
    • 2021-12-09
    • 2017-12-20
    • 2019-11-28
    相关资源
    最近更新 更多