简体   繁体   English

创建 React App - 控制台 lint 警告与 eslintrc 不匹配

[英]Create React App - console lint warnings don't match eslintrc

I've got a sort of weird problem, that I don't see any mention of elsewhere.我有一个奇怪的问题,我在其他地方没有看到任何提及。

Create React App by default seems to log lint warning in the terminal when running yarn start and also in the Chrome console.默认情况下,在运行yarn start和 Chrome 控制台时,创建 React App 似乎会在终端中记录 lint 警告。 All good.都好。 Makes sense, and I've seen discussions of whether this functionality should exist, and how it should work.有道理,我已经看到有关此功能是否应该存在以及它应该如何工作的讨论。

My issue is that those warnings seem not to match my .eslintrc settings at all!我的问题是这些警告似乎根本不符合我的.eslintrc设置!

As in, if I run yarn lint , which runs eslint using my settings, I see none of the warnings that show up in the console and terminal when I start my app.就像,如果我运行yarn lint ,它使用我的设置运行eslint ,当我启动我的应用程序时,我看不到控制台和终端中显示的任何警告。

For example, I have this rule turned off in my .eslintrc file:例如,我在.eslintrc文件中关闭了此规则:

"radix": 0,

And, consequently, there's no radix warning when I run yarn lint .因此,当我运行yarn lint时没有radix警告。

(Just in response to the answer below. I've tried a variety of options for this particular rule, including "as-needed". But I wanted to turn the rule off entirely, which I've evidently accomplished, because running yarn lint shows no errors related to radix at all). (只是为了回应下面的答案。我已经为这个特定规则尝试了多种选择,包括“按需”。但我想完全关闭规则,我显然已经完成了,因为运行yarn lint根本没有显示与radix相关的错误)。

But when I start the app, I see this in yellow warning boxes in the console:但是当我启动应用程序时,我在控制台的黄色警告框中看到了这一点:

在此处输入图片说明

Anybody know why my .eslintrc file is being ignored here, and how to get these warnings to represent the rules I've set?有人知道为什么我的.eslintrc文件在这里被忽略,以及如何让这些警告代表我设置的规则?

According to the docs , you should pass either "always" or "as-needed" .根据文档,您应该通过"always""as-needed" The latter should disable those warnings.后者应该禁用这些警告。

...
"radix": "as-needed",
...

EDIT: According to this source , you will have to eject your create-react-app to edit the ESLint settings.编辑:根据此来源,您必须弹出create-react-app才能编辑 ESLint 设置。

It is possible to add EXTEND_ESLINT flag to the .env file.可以将EXTEND_ESLINT标志添加到 .env 文件中。

When set to true, ESLint configs that extend eslint-config-react-app will be used by eslint-loader.当设置为 true 时,eslint-loader 将使用扩展 eslint-config-react-app 的 ESLint 配置。 Any rules that are set to "error" will stop the application from building.任何设置为“错误”的规则都将阻止应用程序构建。

Source: https://create-react-app.dev/docs/advanced-configuration来源: https : //create-react-app.dev/docs/advanced-configuration

Your eslint config may be named .eslintrc.json and put in the root folder of your project.您的 eslint 配置可以命名为.eslintrc.json并放在项目的根文件夹中。 Its contents should look like the following (note the extends field):其内容应如下所示(注意 extends 字段):

{
  "extends": "react-app", //this extends statement is required
  "rules": {
    "radix": "as-needed"
  }
}

And the .env file placed in the root should have the following line:放置在根目录中的.env文件应该有以下几行:

EXTEND_ESLINT=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM