vscode eslint配置

20,888 阅读1分钟

大神误入!大神误入!大神误入!

1. vscode安装插件eslint

2. 查看eslint插件默认配置信息

cmd + shift + p 调出命令面板,搜索default选择首选项打开默认设置,即defaultSettings.json

可以看到全部的eslint默认配置

3. 自定义vscode eslint配置项

几个比较重要的eslint配置

(1)eslint.validate

"eslint.validate": [
    "javascriptreact",
    "html",
    {"language": "javascript","autoFix": true},
    {"language": "vue","autoFix": true}
 ]

老版本的eslint插件默认只对js文件进行校验,如果需要对vue,html文件中的js进行校验,需要配置eslint.validate,添加html,vue,但是在2.0.4版本之后,内置了对typescript,vue,html的校验,所以不需要再额外配置eslint.validate了

(2)eslint错误保存自动fix

第一种配置方式:

"eslint.autoFixOnSave": false, // 已废弃,被第二种方式替代

第二种配置方式:

"editor.codeActionsOnSave": {
    "source.fixAll": true,
    "source.fixAll.eslint": true
}

第三种配置方式:

"eslint.format.enable": true,
"editor.formatOnSave": true

但是官方推荐第二种方式,详细解释参考 github.com/microsoft/v…

(3)状态栏中显示eslint,方便查看eslint服务是否正常运行

"eslint.alwaysShowStatus": true

(4)不显示eslint校验的警告信息

"eslint.quiet": true

所以,总结下,目前我的vscode的涉及的eslint配置如下:

"eslint.alwaysShowStatus": true,
"eslint.quiet": true,
"editor.codeActionsOnSave": {
    "source.fixAll": true,
    "source.fixAll.eslint": true
}