[英]no-use-before-define has no effect when extending eslint-config-react-app
I am trying to configure eslint to use eslint-config-react-app
as a base and specify a few specific rules on top of that.我正在尝试将 eslint 配置为使用eslint-config-react-app
作为基础,并在此基础上指定一些特定规则。 One of those rules is no-use-before-define
, and I am trying this out on the following trivial example to check whether the rule is working as desired:这些规则之一是no-use-before-define
,我在下面的简单示例中尝试了这一点,以检查规则是否按预期工作:
const a = () => {
b();
};
const b = () => {};
a();
If I set up my.eslintrc.json like this, then the rule works as expected:如果我像这样设置 my.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
However, if I include "extends": "react-app"
, no eslint errors are found:但是,如果我包含"extends": "react-app"
,则找不到 eslint 错误:
{
"extends": "react-app",
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"no-use-before-define": "error"
}
}
If I intentionally introduce a change that would cause another eslint violation - eg remove the a();
如果我故意引入会导致另一个违反 eslint 的更改 - 例如删除a();
a the end, then that is found, but not the no-use-before-define
violation: a 结束,然后找到,但不是no-use-before-define
违规:
1:7 warning 'a' is assigned a value but never used no-unused-vars
Intuitively, I would expect any rules in.eslintrc.json to be applied on top of the config indicated in "extends"
, but it seems like that's not what's happening.直观地说,我希望 .eslintrc.json 中的任何规则都将应用于"extends"
中指示的配置之上,但似乎并非如此。
What am I misunderstanding here?我在这里有什么误解? Is there a way to extend eslint-config-react-app
and have the no-use-before-define
rule work correctly?有没有办法扩展eslint-config-react-app
并让no-use-before-define
规则正常工作?
Looks like I've figured it out.看来我已经想通了。
It seems I was overriding the severity of the rule, but not the options, which were set to { "functions": false, "variables": false, "classes": false }
.似乎我覆盖了规则的严重性,但没有覆盖设置为{ "functions": false, "variables": false, "classes": false }
的选项。 So it remained in a configuration where eslint would only find an issue in the case of multiple variables declared with the same name in the same scope .所以它保持在一个配置中,其中 eslint 只会在同一个 scope 中声明的多个同名变量的情况下发现问题。
Specifying the options explicitly yields the desired behavior:明确指定选项会产生所需的行为:
"no-use-before-define": ["error", { "functions": true, "variables": true }]
Looks like this also works for reverting back to the eslint default options for this rule:看起来这也适用于恢复到此规则的 eslint 默认选项:
"no-use-before-define": ["error", {}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.