开发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. linux 修改时间永久

    date -s "20190421 16:36:20" &&hwclock --systohc

  2. linux 缓存手动清除

    linux下怎么清理缓存 free -m 命令可以查看内存使用情况                   sysctl 命令可以临时改变某个系统参数  如:sysctl -w net.ipv4.ip_f ...

  3. Kafka设计解析(十四)Kafka producer介绍

    转载自 huxihx,原文链接 Kafka producer介绍 Kafka 0.9版本正式使用Java版本的producer替换了原Scala版本的producer.本文着重讨论新版本produce ...

  4. Uva514

    https://vjudge.net/problem/UVA-514 #include <bits/stdc++.h> using namespace std; ; int target[ ...

  5. 443 D. Teams Formation

    http://codeforces.com/contest/879/problem/D This time the Berland Team Olympiad in Informatics is he ...

  6. day 85 Vue学习之vue-cli脚手架下载安装及配置

      1. 先下载node.js,下载地址:https://nodejs.org/en/download/ 找个目录保存,解压下载的文件,然后配置环境变量,将下面的路径配置到环境变量中. 由于 Node ...

  7. 大数据入门第十天——hadoop高可用HA

    一.HA概述 1.引言 正式引入HA机制是从hadoop2.0开始,之前的版本中没有HA机制 2.运行机制 实现高可用最关键的是消除单点故障 hadoop-ha严格来说应该分成各个组件的HA机制——H ...

  8. 20155207王雪纯 Exp2 后门原理与实践

    20155207王雪纯 Exp2 后门原理与实践 实验步骤 一.windows获取Linux shell Windows:使用 ipconfig 命令查看当前机器IP地址. 进入ncat所在文件地址, ...

  9. PostgreSQL数据库表名的大小写实验

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...

  10. [arc081F]Flip and Rectangles-[黑白染色]

    Description 传送门 Solution 有一个神秘的结论..我不知道大佬是怎么场上推出来的. 一个黑白染色图,每次可以任意翻转行或列的颜色,如果每个2*2的子矩阵内黑色格子都是偶数个,则可以 ...