我正在尝试配置 eslint 以使用 eslint-config-react-app
作为基础,并在此基础上指定一些具体规则。这些规则之一是 no-use-before-define
,我正在以下简单示例中尝试此操作,以检查规则是否按预期工作:
const a = () => {
b();
};
const b = () => {};
a();
如果我像这样设置 .eslintrc.json,那么该规则将按预期工作:
{
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"no-use-before-define": "error"
}
}
2:5 error 'b' was used before it was defined no-use-before-define
但是,如果我包括 "extends": "react-app"
,没有发现 eslint 错误:
{
"extends": "react-app",
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"no-use-before-define": "error"
}
}
如果我故意引入一个会导致另一次 eslint 违规的更改 - 例如删除 a();
a 结尾,然后找到 that,但找不到 no-use-before-define
违规:
1:7 warning 'a' is assigned a value but never used no-unused-vars
直观上,我希望 .eslintrc.json 中的任何规则都应用于 "extends"
中指示的配置之上 ,但情况似乎并非如此。
我在这里误解了什么?有没有办法扩展eslint-config-react-app
和有 no-use-before-define
规则工作正常吗?
最佳答案
看来我已经明白了。
看来我覆盖了规则的严重性,但没有覆盖选项,它们被设置为 { "functions": false, "variables": false, "classes":假}
。因此,它保留在这样的配置中,eslint 仅在声明后使用变量的情况下才会发现问题在同一范围内。
明确指定选项会产生所需的行为:
"no-use-before-define": ["error", { "functions": true, "variables": true }]
看起来这也适用于恢复到此规则的 eslint 默认选项:
"no-use-before-define": ["error", {}]
关于javascript - no-use-before-define 在扩展 eslint-config-react-app 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65975554/