VSCode eslint 配置格式自动校正

2,484 阅读1分钟
  1. VSCode中安装 ESLint
  2. VSCode的settings.json中添加代码
{
    ...
    //以下是要添加的代码
    "editor.codeActionsOnSave": { //  启用保存时自动修复,默认只支持.js文件
            "source.fixAll": true
    },
}

安装设置之后,代码中格式错误的地方会波浪线标出,点开文件保存时会自动修复。多个文件出现错误需一个文件一个文件保存。

以下是我的,基于webpack模板的vue项目.eslintrc.js 文件

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint'
  },
  env: {
    browser: true,
  },
  extends: [
    // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
    // 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'
  ],
  // add your custom rules here
  rules: {
    //生成器函数*的前后空格
    'generator-star-spacing': 'off',
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    // 配置首行缩进
    'indent':0,
    //不检查分号
    'semi': 0,
    ////函数定义时括号前面要不要有空格
    'space-before-function-paren': 0
  }
}