开发Vue或者React的项目的时候,我们经常用到ESLint进行代码的校验,当代码出现不符合规范的格式的时候,ESLint会在控制台提示相关的异常信息。

ESLint极大的提高了团队代码的一致性和规范性,接下来老马介绍一下我的VSCode的相关配置和插件实现保存代码的时候,根据ESLint的配置自动修复代码和格式化。

VSCode安装插件

配置VSCode的用户配置

{
...
"vetur.format.defaultFormatter.js": "vscode-typescript",
"vetur.format.defaultFormatter.html": "js-beautify-html",
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "html",
"autoFix": true
},
{
"language": "vue",
"autoFix": true
}
],
// 保存自动修复
"eslint.autoFixOnSave": true,
// jsx自动修复有问题,取消js的format
"editor.formatOnSave": false,
// Enable/disable default JavaScript formatter (For Prettier)
"javascript.format.enable": false,
"prettier.singleQuote": true,
// 点击保存时,根据 eslint 规则自定修复,同时集成 prettier 到 eslint 中
"prettier.eslintIntegration": true,
...
}

注意事项

如果需要启动ESLint的自动修复,需要在项目根目录下,或者package.json文件中配置ESLint的配置。

参考我的Vue项目的ESLint的校验规则:

// .eslintrc.js
// https://eslint.org/docs/user-guide/configuring
module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint'
},
env: {
browser: true
},
extends: [
// https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevent
// ion consider switching to `plugin:vue/strongly-recommended` or
// `plugin:vue/recommended` for stricter rules.
'plugin:vue/essential',
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
'standard'
],
// required to lint *.vue files
plugins: ['vue'],
globals: {
NODE_ENV: false
},
rules: {
// allow async-await
'generator-star-spacing': 'off',
// allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
// 添加,分号必须
semi: ['error', 'always'],
'no-unexpected-multiline': 'off',
'space-before-function-paren': ['error', 'never'],
// 'quotes': ["error", "double", { "avoidEscape": true }]
quotes: [
'error',
'single',
{
avoidEscape: true
}
]
}
};

参考我的React项目的ESLint的校验规则:

需要安装的插件

npm i -D eslint eslint-plugin-flowtype eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react babel-eslint prettier eslint-config-prettier eslint-config-react-app eslint-plugin-prettier
// .eslintrc.js
module.exports = {
root: true,
parserOptions: {
// 检查 ES6 语法
parser: 'babel-eslint',
},
env: {
browser: true,
},
// extending airbnb config and config derived from eslint-config-prettier
// 这里是 vue
extends: ['react-app', 'prettier'], // 选择 eslint 插件
plugins: ["react", "jsx-a11y", "import", 'prettier'], // react
// extends: ['airbnb-base', 'prettier'],
// plugins: ['prettier', 'react'], // 不需要框架
// extends: ['airbnb-base', 'prettier'],
// plugins: ['prettier'], // 自定义 eslint 检查规则
rules: {
// 自定义 prettier 规则 (实际上,可配置项非常有限)
'prettier/prettier': [
'error',
{
singleQuote: true,
trailingComma: 'all',
},
],
camelcase: 'off', // 强制驼峰法命名
'no-new': 'off', // 禁止在使用new构造一个实例后不赋值
'space-before-function-paren': 'off', // 函数定义时括号前面不要有空格
'no-plusplus': 'off', // 禁止使用 ++, ——
'max-len': 'off', // 字符串最大长度
'func-names': 'off', // 函数表达式必须有名字
'no-param-reassign': 'off', // 不准给函数入参赋值 // react 如果在项目中文件名后缀是 .js 而不是 .jsx 避免报错
// "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
// react props validation 错误
// "react/prop-types": 0,
},
};

VSCode配合ESLint自动修复格式化的更多相关文章

  1. 工作中遇到的vscode配合eslint完成保存为eslint格式

    vscode个人设置 // vscode的个人设置配置 { "workbench.iconTheme": "vscode-icons", "workb ...

  2. vscode vue eslint 快捷键格式化代码

    添加vetur , eslint插件   在工作区添加以下代码   "workbench.startupEditor": "welcomePage", &quo ...

  3. vscode保存代码,自动按照eslint规范格式化代码设置

    # vscode保存代码,自动按照eslint规范格式化代码设置 编辑器代码风格一致,是前端代码规范的一部分.同一个项目,或者同一个小组,保持代码风格一致很必要.就拿vue项目来说,之前做的几个项目, ...

  4. vscode 使用ESLint 自动检查,保存时自动格式化

    1:全局安装eslint `npm install -g eslint`2: 打开vscode 点击 “文件”----->“首选项”---->“设置”,在右侧“用户设置/settings. ...

  5. vscode中eslint插件的配置-prettier

    用vue-cli构建vue项目,会有个eslint代码检测的安装 可vscode自带代码格式化是prettier格式(右键有格式化文件或alt+shift+f) 这时候要在vscode上装一个esli ...

  6. vscode 中 eslint prettier 和 eslint -loader 配置关系

    前置 本文将探究 vscode prettier 插件 和 eslint 插件在 vscode 中的配置以及这两者对应的在项目中的配置文件的关系,最后提及 vscode eslint 插件配置与 es ...

  7. VSCode配置eslint

    在Vue.js项目中,使用的是eslint检查. 而在我写完代码后,cnpm run dev运行命令...然后悲剧了,一大堆报错!╮(╯▽╰)╭ 安装插件:Vetur:这是vscode上一个vue.j ...

  8. VSCode 使用 ESLint + Prettier 来统一 JS 代码

    环境: VSCode 1.33.1 Node.js 8.9.1 一.ESLint 1.介绍 ESLint是最流行的JavaScript Linter. Linter 是检查代码风格/错误的小工具.其他 ...

  9. 在使用 vscode 时 eslint 检测 .vue 文件中的less 部分内容

    问题: 在使用 vscode 以及 eslint 来检测 基于 webpack 的 vue-cli 的项目中,eslint 无法检测到 .vue 文件中的less 部分内容. 解答: 1.通过 下载 ...

随机推荐

  1. 多线程之并发容器ConcurrentHashMap

    这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为同步 ...

  2. 调试大叔V2.1.0(2018.12.17)|http/s接口调试、数据分析程序员辅助开发神器

    2018.12.17 - 调试大叔 V2.1.0*升级http通讯协议版本,完美解决Set-Cookie引起的系列问题:*新增Content-Type编码格式参数,支持保存(解决模拟不同网站或手机请求 ...

  3. Hadoop系列-zookeeper基础

    目前是刚刚初学完zookeeper,这篇文章主要是简单的对一些基本的概念进行梳理强化. zookeeper基础概念的理解 有时候计算机领域很多名词都是从一长串英文提取首字母缩写而来,但很不幸zooke ...

  4. hisi3559的usb无线网卡驱动(rtl8192cu)(一条龙服务:内核编译、驱动编译、iw等工具编译)

    usb无线网卡驱动(rtl8192cu) 内核编译.驱动编译.iw等工具编译  (哈哈,如果有其他问题,麻烦留言:) 环境 板卡:hi3559av100(arm64) 交叉编译链:aarch64-hi ...

  5. set_new_handler

    转自:http://www.cnblogs.com/hbt19860104/archive/2012/10/10/2717873.html 以及 http://zhaoweizhuanshuo.blo ...

  6. 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(3)-- 画线

    [概览] 本文实现如下的程序:(在窗体中绘画出各种不同风格的线条) 主要步骤如下: 1.在eric6中新建项目,新建窗体 2.(自动打开)进入PyQt5 Desinger,编辑图形界面,保存 3.回到 ...

  7. Velocity学习2

    Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...

  8. day1 UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 2490: illegal multibyte sequence 错误提示

    get方式得到网页的信息 #coding=utf-8 #pip install requests #直接get到网页的信息 import requests from bs4 import Beauti ...

  9. CF 258 D. Little Elephant and Broken Sorting

    D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...

  10. 什么是REST,RESTful?

    转载自https://www.zhihu.com/question/28557115 https://blog.csdn.net/hjc1984117/article/details/77334616 ...