从 Create React App 扩展 TypeScript ESLint 配置

Ogg*_*las 5 javascript typescript reactjs eslint create-react-app

我已按照本指南扩展我的 ESLint 配置。

https://create-react-app.dev/docs/setting-up-your-editor/#experimental-extending-the-eslint-config

该文档指出Note that any rules set to "error" will stop the project from building。我已在与我的文件相同的文件夹中添加了一个.env文件。EXTEND_ESLINT=truepackage.json

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

添加此文件后,npm run build即使使用 package.json 中的默认值,我也不再收到来自命令的警告:

在此输入图像描述

"eslintConfig": {
  "extends": "react-app"
},
Run Code Online (Sandbox Code Playgroud)

如果我将.env文件设置为EXTEND_ESLINT=false警告,警告就会再次出现。

在此输入图像描述

首先这样做的原因是因为我想添加no-multiple-empty-lines这样的规则:

"eslintConfig": {
  "extends": [ "react-app" ],
  "rules": {
    "no-multiple-empty-lines": [
      "error",
      {
        "max": 1,
        "maxEOF": 0,
        "maxBOF": 0
      }
    ]
  }
},
Run Code Online (Sandbox Code Playgroud)

https://eslint.org/docs/rules/no-multiple-empty-lines

我认为配置是正确的,因为当我添加此规则时,以下命令将显示预期结果:

npx eslint . --ext .js,.jsx,.ts,.tsx
Run Code Online (Sandbox Code Playgroud)

配置更改前:

在此输入图像描述

配置更改后:

在此输入图像描述

读取.env文件时,它表示npm run build将按以下顺序读取文件:.env.production.local, .env.production, .env.local, .env。因为我只有.env它,所以应该把它捡起来。文档还在上面说明了这一点EXTEND_ESLINT-You do not need to declare REACT_APP_ before the below variables as you would with custom environment variables.

https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env

我也读过这个帖子,但我看不出我哪里错了:

ESLint 如何集成到 Create React App 中?

如果我编辑该部分scripts,我将收到正常警告,但我添加的规则无论如何都不会执行。package.json"build": "set EXTEND_ESLINT=true && react-scripts build",

在此输入图像描述