问题
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
andplugins
are manually added,- Only
@typescript-eslint/explicit-function-return-type
is enforced.
- 2nd way:
plugin:@typescript-eslint/recommended
has automatically addedparser
,parserOptions
andplugins
.- 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