create-react-app eslint-config-react-app@7.0.0: @typescript-eslint/eslint-plugin 应该作为对等依赖,

wfauudbj  于 3个月前  发布在  React
关注(0)|答案(4)|浏览(43)

描述bug

当前版本的 eslint-config-react-app@typescript-eslint/eslint-plugin 等定义为依赖项。这导致在依赖树中未唯一解析 @typescript-eslint/eslint-plugin 时,eslint 失败。以下是一个示例:

ESLint couldn't determine the plugin "@typescript-eslint" uniquely.

- ...\node_modules\@typescript-eslint\eslint-plugin\dist\index.js (loaded in ".eslintrc.json")
- ...\node_modules\eslint-config-react-app\node_modules\@typescript-eslint\eslint-plugin\dist\index.js (loaded in ".eslintrc.json » eslint-config-react-app#overrides[0]")

它应该被声明为对等依赖项,并由消费仓库自行安装。在插件架构中,当一个插件使用另一个插件时,它应该是对等依赖项。这将是一个破坏性更改,必须作为新的主要版本发布。

你尝试恢复依赖关系了吗?

删除锁文件和 node_modules 在这种情况下不会有所帮助。我也曾在使用 yarn@3node_modules 时遇到过这个问题,我试图消除重复项并无法解决问题。即使我尝试让所有各方使用相同版本的 @typescript-eslint/eslint-plugin (5.23.0), 它仍然有两个链接,一个在 ./node_modules 下面,一个在 ./eslint-config-react-app/node_modules 下面。我认为这是 yarn@3 上的错误,但问题也应由 eslint-config-react-app 解决。

你在用户指南中搜索了哪些术语?

  • @typescript-eslint
  • ESLint couldn't determine the plugin "@typescript-eslint" uniquely.
  • peer dependency
  • eslint-cofnig-react-app

环境

这种情况发生在所有平台上,使用 npm、yarn@1-3

重现步骤

创建一个带有此 package.json 的虚拟仓库:

{
  "name": "typescript-eslint-plugin-peer-issue",
  "private": true,
  "scripts": {
    "lint": "eslint --ext=ts ."
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "5.4",
    "eslint": "^8.15.0",
    "eslint-config-react-app": "^7.0.1"
  }
}

以及此 .eslintrc.json :

{
  "extends": [
    "react-app"
  ],
  "plugins": [
    "@typescript-eslint"
  ]
}

创建一个虚拟的 src/index.ts :

export const dummy = {}

运行:

yarn
yarn lint

预期行为

ESLint 将正常工作

实际行为

ESLint 以以下方式失败:

ESLint couldn't determine the plugin "@typescript-eslint" uniquely.

- ...\node_modules\@typescript-eslint\eslint-plugin\dist\index.js (loaded in ".eslintrc.json")
- ...\node_modules\eslint-config-react-app\node_modules\@typescript-eslint\eslint-plugin\dist\index.js (loaded in ".eslintrc.json » eslint-config-react-app#overrides[0]")

可复现的演示

https://github.com/unional/typescript-guidelines/tree/main/examples/eslint-plugin-peer
在这里添加了一些更多信息: https://unional.github.io/typescript-guidelines/blog/2022-eslint-plugin-peer-deps

nkcskrwz

nkcskrwz2#

"@typescript-eslint" be declared as a peer dependency maybe is not a mistake.
Because eslint-config-react-app@7.0.0 use @rushstack/eslint-patch/modern-module-resolution . This patch allows a shared ESLint config to bring along its own plugins, rather than imposing peer dependencies on every consumer of the config. For more detail see @rushstack/eslint-patch
create-react-app/packages/eslint-config-react-app/base.js
Line 11 in f34d88e
| | require('@rushstack/eslint-patch/modern-module-resolution'); |

flseospp

flseospp3#

eslint-config-react-app 已经声明了带有 "@typescript-eslint" 的插件,因此在 .eslintrc.json 中扩展 eslint-config-react-app 时不需要再次声明。
从你的 .eslintrc.json 中删除重复的 "@typescript-eslint" 可以解决 ESLint couldn't determine the plugin "@typescript-eslint" uniquely. 的问题。对我来说是有效的。

uwopmtnx

uwopmtnx4#

如何实际操作"从'@typescript-eslint'中移除重复项"以解决这个问题?对我来说还不清楚。

相关问题