背景
今天在引入一个submodule的后,跑lint的时候报了这样一个错误
Oops! Something went wrong! :(
> eslint ./src
ESLint: 8.46.0
ESLint couldn't determine the plugin "react" uniquely.
- F:\project\src\submodule\node_modules\eslint-plugin-react\index.js (loaded in "src\submodule\.eslintrc.cjs » plugin:react/jsx-runtime")
- F:\project\node_modules\eslint-plugin-react\index.js (loaded in ".eslintrc » plugin:react/recommended")
Please remove the "plugins" setting from either config or remove either plugin installation.
If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team
原因
因为子模块跟项目中都有.eslintrc文件,这两个文件都引用了相同的配置,对我而言是jsx-runtime,这时就会报错
解决办法:
- 删除相同的配置,比如子模块和项目都在。extends中引用react-app,删除一个并且删除关联的规则就行了,但这显然不是一个好办法。
- 因为子模块代码是可以更改的,所以我在子模块的.eslintrc中添加 root: true。
影响:
在子模块的.eslintrc中添加了 root: true后 子模块的代码不会再收到主项目中的eslint配置影响,这可能会减少一些个性化lint,但这是可以接收的。
eslint 查找配置文件方式
项目文件在lint的时候需要知道lint规则,eslint配置文件的查找方式
- 先找同目录下的.eslintrc文件(也可以是.eslintrc.js、.eslintrc.cjs或者最新版的.eslint.config.js)或者package.json文件里的配置,如果.eslintrc和package.json同时有配置,那么package.json中的配置会失效
- 沿着项目目录往上查找,将所有找到的配置结合起来,直到根目录或者配置了root:true的文件
当然eslint规则不止在配置文件中,也可能在其他地方:
- 项目代码中设置的:
/*eslint-disable*/和/*eslint-enable*//*global*//*eslint*//*eslint-env*/
- 跑lint的命令中的选项或者CLIEngine:
--global--rule--env-c,-config
这两个的优先级均高于配置文件