从Create React App扩展TypeScript ESLint配置

时间:2020-10-01 09:18:20

标签: javascript reactjs typescript create-react-app eslint

我已按照本指南扩展了我的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=true文件和package.json

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

添加此文件后,即使使用package.json中的默认值,我也停止从命令npm run build发出警告:

enter image description here

"eslintConfig": {
  "extends": "react-app"
},

如果我将.env文件设置为EXTEND_ESLINT=false,则警告会再次显示。

enter image description here

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

"eslintConfig": {
  "extends": [ "react-app" ],
  "rules": {
    "no-multiple-empty-lines": [
      "error",
      {
        "max": 1,
        "maxEOF": 0,
        "maxBOF": 0
      }
    ]
  }
},

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

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

npx eslint . --ext .js,.jsx,.ts,.tsx

更改配置之前:

enter image description here

更改配置后:

enter image description here

在读取.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

我也已经阅读了该主题,但看不到错误之处:

How is ESLint integrated into Create React App?

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

enter image description here

0 个答案:

没有答案