简体   繁体   English

扩展 eslint-config-react-app 时 no-use-before-define 无效

[英]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-appno-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.

相关问题 ESLint no-use-before-define - ESLint no-use-before-define 如何修复 eslint 错误 no-use-before-define - How to fix eslint error no-use-before-define Eslint:如何处理依赖函数的 no-use-before-define? - Eslint: how to handle no-use-before-define for dependent functions? eslint no-use-before-define 规则在 node.js 中不起作用 - eslint no-use-before-define rules does not work in node.js 如何解决这个错误:package.json » eslint-config-react-app/jest#overrides[0]: Environment key "jest/globals" is unknown - How to solve this error: package.json » eslint-config-react-app/jest#overrides[0]: Environment key "jest/globals" is unknown 无法加载在“package.json » eslint-config-react-app/jest”中声明的插件“jest”:无法读取未定义的属性(读取“meta”) - Failed to load plugin 'jest' declared in 'package.json » eslint-config-react-app/jest': Cannot read properties of undefined (reading 'meta') 出于什么目的,“未定义前使用”会警告已声明的函数? - For what purpose 'no-use-before-define' warns about declared functions? 如何避开变量定义的no-use-before-define let foo = foo || [] ;? - How can I sidestep no-use-before-define for the variable definition let foo = foo || [];? ESLint 错误 - ESLint 找不到配置“react-app” - ESLint error - ESLint couldn't find the config "react-app" eslint 无法加载要扩展的配置“react-app”。 在项目中进行更改时 - eslint Failed to load config "react-app" to extend from. when making changes in project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM