ESLint extends vs plugins v2020

点点圈 提交于 2020-12-31 04:55:07

问题


There's answered question which in my opinion doesn't actually answers the question, on the difference between extends: [] vs plugins: [] in ESLint.

In my case, i just used extends section:

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

As you can see, i just used predefined config from plugin:@typescript-eslint/recommended and also overwritten @typescript-eslint/explicit-function-return-type rule in rules: {} section. But why do we need this PLUGINS section then? If everything works without it? What do i miss?


回答1:


You have done it correctly.

For your example, there are 2 ways to do add typescript-eslint...

  • 1st way:
{
  parser: "@typescript-eslint/parser",
  parserOptions: { sourceType: "module" },
  plugins: ["@typescript-eslint"],
  extends: [],
  rules: {
    "@typescript-eslint/explicit-function-return-type": [
      "error",
      {
        allowExpressions: true
      }
    ]
  }
}
  • 2nd way:
{
  plugins: [],
  extends: ["plugin:@typescript-eslint/recommended"],
  rules: {
    "@typescript-eslint/explicit-function-return-type": [
      "error",
      {
        allowExpressions: true
      }
    ]
  }
}

The difference is...

  • 1st way:
    • parser, parserOptions and plugins are manually added,
    • Only @typescript-eslint/explicit-function-return-type is enforced.
  • 2nd way:
    • plugin:@typescript-eslint/recommended has automatically added parser, parserOptions and plugins.
    • Recommended typescript rules are added and enforced.
    • @typescript-eslint/explicit-function-return-type is augmented and enforced.

This is why when you use plugin:@typescript-eslint/recommended, things work normally even if plugins is empty. A well-written plugins/configs allows that to happen.



来源:https://stackoverflow.com/questions/61528185/eslint-extends-vs-plugins-v2020

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!