如何在运行时覆盖 create-react-app 项目中的默认 .eslint 规则

How can I override default .eslint rules at runtime in a create-react-app project

我刚刚尝试更新到 create-react-app 4,并且有大量的 typescript eslint 警告,尤其是 函数 @typescript-eslint/explicit-module-boundary-types

上缺少 return 类型

有什么方法可以在运行时覆盖 .eslintrc.json 文件中的这些内容吗? 当我明确执行

时,我目前已经为我的项目提供了覆盖

npm run lint

但是当我使用 npm run start 时,我收到大量我似乎无法控制的 lint 警告。

有什么办法吗?

原来有一种方法可以覆盖 .eslint 规则。

  1. 如果您没有 .env 文件,请在您的根目录中创建一个。
  2. 将以下内容作为新行添加到其中:EXTEND_ESLINT=true
  3. 以您喜欢的方式扩展 lint 规则。我使用 .eslintrc.json 但存在其他机制

现在有了它,看看来自 React 的包含规则,这些规则存在于 node_modules\eslint-config-react-app\index.js 并将您喜欢的规则复制到您的规则部分。

你可能还需要注意覆盖部分,你可以在上面的文件中看到它的示例,如果你的项目中有 js,你可能需要删除默认解析器,

parser: 'babel-eslint' 然后只覆盖打字稿文件。