问题
设置了eslint(不使用Prettier的情况下)等等规则,但是自动格式化一直不起效果,按照如下步骤可以一步步检查看看是哪里出了问题
步骤
一、确认插件和解释器的安装
@typescript-eslint/parser
:这是一个ESLint解析器,它允许ESLint解析TypeScript代码。@typescript-eslint/eslint-plugin
:这是一个ESLint插件,它包含了一些针对TypeScript的特定规则。eslint-plugin-react
:这是一个ESLint插件,它包含了一些针对React和JSX的特定规则。- 全局是否安装eslint
- npm版本校验
- node版本校验
(一)@typescript-eslint/parser安装确认
执行命令:
npm list @typescript-eslint/parser
结果一:(有类似输出 说明已经安装)
your-project-name@1.0.0 /path/to/your/project
└── @typescript-eslint/parser@4.28.5
结果二:
(有类似输出说明未安装,安装命令:npm install --save-dev @typescript-eslint/parser)
npm ERR! code ELSPROBLEMS
npm ERR! extraneous: @typescript-eslint/parser@4.28.5 /path/to/your/project/node_modules/@typescript-eslint/parser
(二) @typescript-eslint/eslint-plugin安装确认
执行命令:
npm list @typescript-eslint/eslint-plugin
结果一:(类似以下 已安装)
your-project-name@1.0.0 /path/to/your/project
└── @typescript-eslint/eslint-plugin@4.28.5结果二:
(未安装,安装命令:npm install --save-dev @typescript-eslint/eslint-plugin)
npm ERR! code ELSPROBLEMS
npm ERR! extraneous: @typescript-eslint/eslint-plugin@4.28.5 /path/to/your/project/node_modules/@typescript-eslint/eslint-plugin
(三) eslint-plugin-react 安装确认
执行命令:
npm list eslint-plugin-react
结果一:(类似以下 已安装)
输出
eslint-plugin-react
的版本号结果二:
(未安装,安装命令:npm install --save-dev eslint-plugin-react)
输出
empty
(四) eslint检查
确保项目中已经安装了ESLint。在终端中运行
npm list eslint
检查。如果没有安装,运行npm install --save-dev eslint
来安装。确保全局环境中安装ESLint。运行
npm list -g eslint
检查。如果没有安装,运行npm install -g eslint
来安装。确保VS Code已经安装了ESLint插件。在VS Code的插件面板中搜索"ESLint"来检查。如果没有安装,则在插件面板中找到ESLint插件并点击安装。
(五) npm版本校验
npm版本过低。ESLint需要npm的版本至少为5.2.0。运行
npm -v
来检查npm版本。如果版本过低,运行npm install -g npm
来升级npm。
(六) node版本校验
Node.js版本过低。ESLint需要Node.js的版本至少为10.12.0。运行
node -v
来检查Node.js版本。如果版本过低,则升级Node.js。
二、settings.json的配置检查
(是否开启自动修复)
打开VS Code的设置。点击菜单栏的
文件(File)
>首选项(Preferences)
>设置(Settings)
来打开设置,或者使用快捷键Ctrl+,
。在搜索框中输入
editor.codeActionsOnSave
。点击
编辑在 settings.json 中
链接,打开settings.json
文件。在
settings.json
文件中,添加或修改以下配置:{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
保存
settings.json
文件。
三、.eslintrc 配置文件
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"react"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended"
],
"rules": {
// 你的规则
}
}
同时也检查一下项目中是否有.eslintignore
文件,如果有,确保它没有忽略TSX文件
四、eslint缓存清除 重新加载
1.清除VS Code的ESLint缓存。
Ctrl+Shift+P
快捷键来打开命令面板, 在VS Code的命令面板中运行ESLint: Reset ESLint Cache
命令2.在VS Code中,打开命令面板(
Ctrl+Shift+P
),并输入运行ESLint: Restart ESLint Server
命令
五、 PowerShell的执行策略问题
(一)问题
(执行eslint --init
命令报错)
File C:\Program Files\nodejs\eslint.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
(二)解决
1.打开一个PowerShell窗口,以管理员权限运行。
以管理员权限运行PowerShell窗口,步骤操作:
在Windows搜索栏中输入"PowerShell"。
在搜索结果中找到"Windows PowerShell",然后右键点击它。
在弹出的菜单中选择"以管理员身份运行"。
2.运行以下命令来改变执行策略:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
这个命令将允许当前用户运行未签名的脚本。
如果你不希望改变执行策略,你也可以直接使用
npx
来运行eslint --init
命令:npx eslint --init