vscode eslint自动修复_vscode保存代码,自动按照eslint规范格式化代码设置

本文介绍了如何在VSCode中设置 ESLint 自动修复和格式化代码,确保团队代码风格一致。首先确保项目已安装 ESLint 依赖,然后在项目根目录创建 .eslintrc.js 文件定义规则。接着安装并配置VSCode的eslint和vetur插件,通过设置实现保存时自动格式化。此外,还提供了关闭 ESLint 检查的方法以及其他团队的代码规范参考。
摘要由CSDN通过智能技术生成

前言

编辑器代码风格一致,是前端代码规范的一部分。同一个项目,或者同一个小组,保持代码风格一致很必要。就拿vue项目来说,之前做的几个项目,很多小伙伴代码格式化用的是vue-beautify ,这个格式化工具有个明显的缺点,就是三元不等式明明可以一行显示,非得格式化成3行,import用{}引入多个变量或者函数,非得格式化成好几行,看起来很是别扭。因此,好的格式化工具和团队代码风格一致,显得格外重要。我建议我们整个小组运用同一个编辑器,同一种代码校验,同一个格式化方式。下面我来介绍一下使用vscode+eslint 自动保存,自动格式化的一种方式!

eslint 自动格式化

先说一个前提吧,你在中安装了eslint的依赖,不然配置无用。

例如如下依赖:

"eslint": "^",
"eslint-friendly-formatter": "^",
"eslint-loader": "^",
"eslint-plugin-html": "^",

上面说的是一个前提,下面来说一下具体的配置步骤:

首先,在我们项目跟目录添加. 文件,用于校验代码,编写eslint相关规则,关于eslint的一些具体规则,请查看eslint文档

下面列一下我们项目中常用的eslint规则:

module.exports = {
  root: true,
  parser: 'babel-eslint',
  parserOptions: {
  sourceType: 'module',
  "allowImportExportEverywhere": true  //ignore eslint error: 'import' and 'export' may only appear at the top level
  },
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  extends: 'eslint:recommended',
  // required to lint *.vue files
  plugins: [
    'html',
    'vue'
  ],
  // add your custom rules here
  //it is base on 
  'rules': {
    'accessor-pairs': 2,
    'arrow-spacing': [2, {
      'before': true,
      'after': true
    }],
    'block-spacing': [2, 'always'],
    'brace-style': [2, '1tbs', {
      'allowSingleLine': true
    }],
    'camelcase': [0, {
      'properties': 'always'
    }],
    'comma-dangle': [2, 'never'],
    'comma-spacing': [2, {
      'before': false,
      'after': true
    }],
    'comma-style': [2, 'last'],
    'constructor-super': 2,
    'curly': [2, 'multi-line'],
    'dot-location': [2, 'property'],
    'eol-last': 2,
    'eqeqeq': [2, 'allow-null'],
    'generator-star-spacing': [2, {
      'before': true,
      'after': true
    }],
    'handle-callback-err': [2, '^(err|error)$'],
    'indent': [2, 2, {
      'SwitchCase': 1
    }],
    'jsx-quotes': [2, 'prefer-single'],
    'key-spacing': [2, {
      'beforeColon': false,
      'afterColon': true
    }],
    'keyword-spacing': [2, {
      'before': true,
      'after': true
    }],
    'new-cap': [2, {
      'newIsCap': true,
      'capIsNew': false
    }],
    'new-parens': 2,
    'no-array-constructor': 2,
    'no-caller': 2,
    'no-console': 'off',
    'no-class-assign': 2,
    'no-cond-assign': 2,
    'no-const-assign': 2,
    'no-control-regex': 0,
    'no-delete-var': 2,
    'no-dupe-args': 2,
    'no-dupe-class-members': 2,
    'no-dupe-keys': 2,
    'no-duplicate-case': 2,
    'no-empty-character-class': 2,
    'no-empty-pattern': 2,
    'no-eval': 2,
    'no-ex-assign': 2,
    'no-extend-native': 2,
    'no-extra-bind': 2,
    'no-extra-boolean-cast': 2,
    'no-extra-parens': [2, 'functions'],
    'no-fallthrough': 2,
    'no-floating-decimal': 2,
    'no-func-assign': 2,
    'no-implied-eval': 2,
    'no-inner-declarations': [2, 'functions'],
    'no-invalid-regexp': 2,
    'no-irregular-whitespace': 2,
    'no-iterator': 2,
    'no-label-var': 2,
    'no-labels': [2, {
      'allowLoop': false,
      'allowSwitch': false
    }],
    'no-lone-blocks': 2,
    'no-mixed-spaces-and-tabs': 2,
    'no-multi-spaces': 2,
    'no-multi-str': 2,
    'no-multiple-empty-lines': [2, {
      'max': 1
    }],
    'no-native-reassign': 2,
    'no-negated-in-lhs': 2,
    'no-new-object': 2,
    'no-new-require': 2,
    'no-new-symbol': 2,
    'no-new-wrappers': 2,
    'no-obj-calls': 2,
    'no-octal': 2,
    'no-octal-escape': 2,
    'no-path-concat': 2,
    'no-proto': 2,
    'no-redeclare': 2,
    'no-regex-spaces': 2,
    'no-return-assign': [2, 'except-parens'],
    'no-self-assign': 2,
    'no-self-compare': 2,
    'no-sequences': 2,
    'no-shadow-restricted-names': 2,
    'no-spaced-func': 2,
    'no-sparse-arrays': 2,
    'no-this-before-super': 2,
    'no-throw-literal': 2,
    'no-trailing-spaces': 2,
    'no-undef': 2,
    'no-undef-init': 2,
    'no-unexpected-multiline': 2,
    'no-unmodified-loop-condition': 2,
    'no-unneeded-ternary': [2, {
      'defaultAssignment': false
    }],
    'no-unreachable': 2,
    'no-unsafe-finally': 2,
    'no-unused-vars': [2, {
      'vars': 'all',
      'args': 'none'
    }],
    'no-useless-call': 2,
    'no-useless-computed-key': 2,
    'no-useless-constructor': 2,
    'no-useless-escape': 0,
    'no-whitespace-before-property': 2,
    'no-with': 2,
    'one-var': [2, {
      'initialized': 'never'
    }],
    'operator-linebreak': [2, 'after', {
      'overrides': {
        '?': 'before',
        ':': 'before'
      }
    }],
    'padded-blocks': [2, 'never'],
    'quotes': [2, 'single', {
      'avoidEscape': true,
      'allowTemplateLiterals': true
    }],
    'semi': [2, 'never'],
    'semi-spacing': [2, {
      'before': false,
      'after': true
    }],
    'space-before-blocks': [2, 'always'],
    'space-before-function-paren': [2, 'never'],
    'space-in-parens': [2, 'never'],
    'space-infix-ops': 2,
    'space-unary-ops': [2, {
      'words': true,
      'nonwords': false
    }],
    'spaced-comment': [2, 'always', {
      'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
    }],
    'template-curly-spacing': [2, 'never'],
    'use-isnan': 2,
    'valid-typeof': 2,
    'wrap-iife': [2, 'any'],
    'yield-star-spacing': [2, 'both'],
    'yoda': [2, 'never'],
    'prefer-const': 2,
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
    'object-curly-spacing': [2, 'always', {
      objectsInObjects: false
    }],
    'array-bracket-spacing': [2, 'never']
  }
}

本项目基本规范是依托于 vue 官方的 eslint 规则 eslint-config-vue 做了少许的修改。你也可以进行一些属于你自己的定制,例如:目前使用缩进是2个空格,假如你觉得4个更顺眼,你可以如下修改

   'indent': [4, 4, {
      'SwitchCase': 2 // 针对switch case的缩进
    }],

其次,vscode中添加eslint和vetur插件:

如下图

41499770-c3d8fd92-71b8-11e8-830a-bb0c2fae89d1.png

安装好了之后,会自动根据你上面配置的规则进行代码检查,不合格的会高亮显示,如下图:

41499828-d11fb97c-71b9-11e8-993c-0abb0b2a4a83.png

经过上面步骤,目前保存还不能自动格式化,下面说下如何自动格式化!

第三,自动格式化设置,

1、window电脑:

文件 > 首选项 > 设置 打开 VSCode 配置文件

2、mac电脑

code>首选项 >设置

我的设置如下:

{
  "": 2,
  "": {
      "*.vue": "vue"
  },
  "": true,
  "": {
      "extensions": [
          ".js",
          ".vue"
      ]
  },
"": [
    "javascript",{
        "language": "vue",
        "autoFix": true
    },"html",
    "vue"
],
  "": {
      "**/node_modules": true,
      "**/bower_components": true,
      "**/dist": true
  },
  "": {
      "javascript": "jsx",
      "vue": "html",
      "vue-html": "html"
  },
  "": false,
  "": 0,
  "": "boundary",
  "": "smooth",
  "": true,
  "": false,
  "": "'Droid Sans Mono', 'Courier New', monospace, 'Droid Sans Fallback'",
  "": "${dirty}${activeEditorMedium}${separator}${rootName}",
  "": true,
  "": "top",
}

这样,你就可以保存自动按照配置格式化代码了,体验如下:

41499956-0f23bd34-71bc-11e8-88b3-c2f5021c0680.gif

关闭eslint检查

关于关闭eslint,其实不是本节的内容,本节目的是开启,并自动格式化和检查!假如你实在是想关闭的话,设置方法如下:

1、vue create的项目

在中

lintOnSave: false

2、以前的项目,vue init webpack的

config/ 文件。 将

useEslint: true 

设置为

useEslint: false 

即可

其他推荐

其他团队也有自己的代码规范方式例如饿了么团队:https://www.npmjs.com/package/eslint-config-elemefe

vue团队:

关于vscode扩展插件,目前通用的,不错的推荐看这篇文章:

### 回答1: 要使用VSCode配合ESLint自动修复格式化Vue3或React代码,需要按照以下步骤操作: 1. 在VSCode中安装ESLint插件和Prettier插件。 2. 在项目根目录下安装ESLint和Prettier依赖包。 3. 在项目根目录下创建.eslintrc.js文件,并配置ESLint规则。 4. 在VSCode设置中,将"editor.formatOnSave"设置true,以便在保存文件时自动格式化代码。 5. 在VSCode设置中,将"eslint.validate"设置为"onSave",以便在保存文件时自动运行ESLint检查。 6. 在VSCode的命令面板中,选择"ESLint: Fix all auto-fixable Problems",以自动修复ESLint检查出的问题。 7. 在VSCode的命令面板中,选择"Prettier: Format Document",以自动格式化代码。 通过以上步骤,就可以使用VSCode配合ESLint自动修复格式化Vue3或React代码了。 ### 回答2: VSCode 是一款功能强大的现代化代码编辑器,它支持多种语言和框架,如 Vue3 和 React。同时,它也支持集成 ESLint 插件,以帮助我们检测并自动修复代码规范和格式问题。在该环境中配合 ESLint 自动修复格式化非常简单,下面将为大家介绍具体操作步骤。 首先,我们需要在 VSCode 中搜索并安装 ESLint 插件。安装完毕后,我们可在左侧的“Extensions”面板中看到它。 接着,在项目的根目录下初始化一个新的 eslint 配置文件,运行以下命令: ``` npm install eslint --save-dev ./node_modules/.bin/eslint --init ``` 其中eslint --init命令将会进入eslint的配置向导并根据你的需求自动生成.eslintrc.* 配置文件, 这里的 `—save-dev` 参数意味着将 eslint 安装为开发依赖。同时,我们还需要使用npm/yarn 以安装eslint插件的规则依赖,这里以使用 airbnb 的规则为例: ``` npx install-peerdeps --dev eslint-config-airbnb-base ``` 然后,我们需要在 VSCode 首选项中启用 ESLint 插件,并选择自动保存文件时执行 ESLint 格式化功能。为了实现这个目标,我们需要按照以下步骤: - 打开 VSCode 首选项(Preferences)并搜索“eslint”。 - 在搜索结果中选择“ESLint > Auto Fix On Save”选项并将其勾选上。 image 这样,当我们编写 Vue3 或 React 代码时,ESLint自动纠正一些常见的语法和格式问题。一些错误内容比如缩进、括号空格等都会自动得到修复和统一。如果确实存在无法自行修复的问题,我们仍然可以在编辑器中看到它们。在这种情况下,我们需要自行解决问题或查看 ESLint 文档以了解如何解决错误。 综上所述,通过使用 ESLint 工具和 VSCode 编辑器的自动保存功能,我们可以更轻松地维护我们的代码质量和统一性。它可以帮助我们避免格式错误和其他常见的代码问题,确保我们的代码遵循最佳实践,并更加易于阅读和维护。 ### 回答3: VS Code 是一款非常流行的代码编辑器,支持很多功能强大的扩展插件,其中一个非常实用的插件就是 ESLint 自动修复格式化插件。对于 Vue.js 3 或 React 项目的开发者来说,这个插件是非常有用的。 首先,我们要安装 VS Code 上的 ESLint 插件,然后在项目中使用 npm 或 yarn 安装 ESLint: ``` npm install eslint --save-dev ``` 或 ``` yarn add eslint --dev ``` 然后,我们可以在项目根目录下创建一个 `.eslintrc.js` 配置文件,来配置 ESLint 的规则和插件。在 Vue.js 3 项目中,我们可以使用 `@vue/eslint-config` 来设置规则,而在 React 项目中,我们可以使用 `eslint-config-react-app`。配置文件大概长成这样: ``` module.exports = { root: true, env: { browser: true, node: true, es2021: true, }, extends: [ 'plugin:vue/vue3-recommended', 'eslint:recommended', ], parserOptions: { ecmaVersion: 12, sourceType: 'module', }, plugins: [ 'vue', ], rules: { // add your custom rules here }, } ``` 我们可以根据项目需要,自定义 ESLint 的规则和插件。配置好了之后,我们就可以在 VS Code 中使用 ESLint 插件了。 在 VS Code 中打开 Vue.js 3 或 React 项目后,我们可以看到编辑器左下角会出现一个 ESLint 的标志,这表示 VS Code 已经启用了 ESLint 插件。当我们写代码时,如果发现代码出现了 ESLint 的错误或警告,我们只需要将鼠标悬停在出错的地方,就可以看到具体的提示信息。 此时,我们可以使用 VS Code 的快捷键 `Ctrl + .` 或 `Cmd + .`来打开 Quick Fix 菜单,然后选择 "Fix this" 或 "Fix all" 选项,来让 VS Code 自动修复代码中的 ESLint 错误和警告。这个操作非常方便,可以帮助我们快速修复代码中的问题并保持代码风格一致。 总的来说,VS Code 配合 ESLint 自动修复格式化的功能能够大大提高开发效率,降低出错几率,值得我们掌握和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值