ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vite2+eslint+prettier,解决VSCode保存时候自动格式化,导致ESLint报冲突

2021-11-11 22:34:37  阅读:631  来源: 互联网

标签:禁止 false no VSCode 空格 prettier eslint true ESLint


背景描述

以前一直用破解版的Webstorm,没有用过VSCode,到新公司后不让用盗版软件了,自己捣鼓了一下VSCode。配置ESLint、Prettier代码格式化的时候,遇到了大坑!!!!保存后,莫名其妙的又被格式化成4个空格的,整整纠结了2天都没解决。直到遇到了这个文章才解决:解决VS Code保存时候自动格式化_一汀程序狗的博客-CSDN博客_vscode保存时自动格式化不知道从哪个版本起,VS code 保存会自动格式化。以前都是alt+shift+F格式化的,现在一保存就格式化,而且html什么全都乱了,react项目也没法写,悲伤的我难道要转webstorm嘛?如何解决呢,操作如下:于是我找到设置,搜索关键字:“格式化”或者“保存”,然后配置了两个参数, "editor.formatOnType": false,和"editor.formatOnSavehttps://blog.csdn.net/qq_35800306/article/details/72784164

都是JS-CSS-HTML Formatter这个垃圾插件导致的。。。VSCode给排到了第一。。。也是不懂下载了。导致后面跟项目中的ESLint、Prettier起了冲突,卸载了就好了。看评论,骂声一片!!!

 


正文开始(VSCode中ESLint、Prettier的配置


1、安装可参考:vite2+eslint+prettier_蓝色之恋love的博客-CSDN博客

2、ESLint的配置文件:.eslintrc.js

module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
    es6: true
  },
  extends: ['plugin:vue/vue3-recommended', 'eslint:recommended', '@vue/standard', '@vue/prettier'], // "@vue/prettier"
  parserOptions: {
    parser: 'babel-eslint'
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'prettier/prettier': 'off',
    'vue/max-attributes-per-line': [2, {
      'singleline': 10,
      'multiline': {
        'max': 1
      }
    }],
    'vue/name-property-casing': ['error', 'PascalCase'],
    // 定义对象的set存取器属性时,强制定义get
    'accessor-pairs': 2,
    'arrow-spacing': [2, {
      'before': true,
      'after': true
    }],
    // 禁止或强制在单行代码块中使用空格
    'block-spacing': [2, 'always'],
    // 强制使用一致的缩进 第二个参数为 "tab" 时,会使用tab
    'brace-style': [2, '1tbs', {
      'allowSingleLine': true
    }],
    // 双峰驼命名格式
    'camelcase': [0, {
      'properties': 'always'
    }],
    // 数组和对象键值对最后一个逗号, never参数:不能带末尾的逗号, always参数:必须带末尾的逗号
    'comma-dangle': [2, 'never'],
    // 控制逗号前后的空格
    'comma-spacing': [2, {
      'before': false,
      'after': true
    }],
    // 控制逗号在行尾出现还是在行首出现 (默认行尾)
    'comma-style': [2, 'last'],
    // 强制在子类构造函数中用super()调用父类构造函数,TypeScrip的编译器也会提示
    'constructor-super': 2,
    // 强制所有控制语句使用一致的括号风格
    'curly': [2, 'multi-line'],
    // 强制object.key 中 . 的位置,参数:
    // property,'.'号应与属性在同一行
    // object, '.' 号应与对象名在同一行
    'dot-location': [2, 'property'],
    // 文件末尾不强制换行
    'eol-last': 0,
    // 使用 === 替代 == allow-null允许null和undefined==
    'eqeqeq': [2, 'allow-null'],
    // 强制 generator 函数中 * 号周围使用一致的空格
    'generator-star-spacing': [2, {
      'before': true,
      'after': true
    }],
    // 要求回调函数中有容错处理
    'handle-callback-err': [2, '^(err|error)$'],
    'indent': [2, 2, {
      'SwitchCase': 1
    }],
    // 强制在 JSX 属性中一致地使用双引号或单引号
    '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,
    // 禁止在没有类型检查操作符的情况下与 null 进行比较
    'no-eq-null': 0,
    // 禁止使用 Array 构造函数
    'no-array-constructor': 2,
    // 禁用 arguments.caller 或 arguments.callee
    'no-caller': 2,
    // 禁止修改类声明的变量
    'no-class-assign': 2,
    // 禁止条件表达式中出现赋值操作符
    'no-cond-assign': 2,
    // 禁止修改 const 声明的变量
    'no-const-assign': 2,
    // 禁止在正则表达式中使用控制字符 :new RegExp("\x1f")
    'no-control-regex': 0,
    // 禁止删除变量
    'no-delete-var': 2,
    // 禁止 function 定义中出现重名参数
    'no-dupe-args': 2,
    // 禁止类成员中出现重复的名称
    'no-dupe-class-members': 2,
    // 禁止对象字面量中出现重复的 key
    'no-dupe-keys': 2,
    // 禁止重复的 case 标签
    'no-duplicate-case': 2,
    // 禁止在正则表达式中使用空字符集 (/^abc[]/)
    'no-empty-character-class': 2,
    // 禁止使用空解构模式no-empty-pattern
    'no-empty-pattern': 2,
    // 禁用 eval(),由于多处地方使用,暂不开启
    'no-eval': 0,
    // 禁止对 catch 子句的参数重新赋值
    'no-ex-assign': 2,
    // 禁止扩展原生类型
    'no-extend-native': 2,
    // 禁止不必要的 .bind() 调用
    'no-extra-bind': 2,
    // 禁止不必要的布尔转换
    'no-extra-boolean-cast': 2,
    // 禁止不必要的括号 //(a * b) + c;//报错
    'no-extra-parens': [2, 'functions'],
    // 禁止 case 语句落空
    'no-fallthrough': 2,
    // 禁止数字字面量中使用前导和末尾小数点
    'no-floating-decimal': 2,
    // 禁止对 function 声明重新赋值
    'no-func-assign': 2,
    // 禁止使用类似 eval() 的方法
    'no-implied-eval': 2,
    // 禁止在嵌套的块中出现 function 或 var 声明
    'no-inner-declarations': [2, 'functions'],
    // 禁止 RegExp 构造函数中无效的正则表达式字符串
    'no-invalid-regexp': 2,
    // 禁止在字符串和注释之外不规则的空白
    'no-irregular-whitespace': 2,
    // 禁用 __iterator__ 属性
    'no-iterator': 2,
    // 不允许标签与变量同名
    'no-label-var': 2,
    // 禁用标签语句
    'no-labels': [2, {
      'allowLoop': false,
      'allowSwitch': false
    }],
    // 禁用不必要的嵌套块
    'no-lone-blocks': 2,
    // 不允许空格和 tab 混合缩进
    'no-mixed-spaces-and-tabs': 2,
    // 禁止使用多个空格
    'no-multi-spaces': 2,
    // 禁止使用多行字符串,在 JavaScript 中,可以在新行之前使用斜线创建多行字符串
    'no-multi-str': 2,
    // 不允许多个空行
    'no-multiple-empty-lines': [2, {
      'max': 1
    }],
    // 禁止对原生对象赋值
    'no-native-reassign': 2,
    // 禁止在 in 表达式中出现否定的左操作数
    'no-negated-in-lhs': 2,
    // 禁止使用 Object 的构造函数
    'no-new-object': 2,
    // 禁止调用 require 时使用 new 操作符
    'no-new-require': 2,
    // 禁止 Symbol 的构造函数
    'no-new-symbol': 2,
    // 禁止对 String,Number 和 Boolean 使用 new 操作符
    'no-new-wrappers': 2,
    // 禁止把全局对象 (Math 和 JSON) 作为函数调用 错误:var math = Math();
    'no-obj-calls': 2,
    // 禁用八进制字面量
    'no-octal': 2,
    // 禁止在字符串中使用八进制转义序列
    'no-octal-escape': 2,
    // 禁止对 __dirname 和 __filename进行字符串连接
    'no-path-concat': 2,
    // 禁用 __proto__ 属性
    'no-proto': 2,
    // 禁止使用 var 多次声明同一变量
    'no-redeclare': 2,
    // 禁止正则表达式字面量中出现多个空格
    'no-regex-spaces': 2,
    // 禁用指定的通过 require 加载的模块
    'no-return-assign': [2, 'except-parens'],
    // 禁止自我赋值
    'no-self-assign': 2,
    // 禁止自身比较
    'no-self-compare': 2,
    // 禁用逗号操作符
    'no-sequences': 2,
    // 禁止覆盖受限制的标识符
    'no-shadow-restricted-names': 2,
    // 禁止 function 标识符和括号之间出现空格
    'no-spaced-func': 2,
    // 禁用稀疏数组
    'no-sparse-arrays': 2,
    // 禁止在构造函数中,在调用 super() 之前使用 this 或 super
    'no-this-before-super': 2,
    // 禁止抛出非异常字面量
    'no-throw-literal': 0,
    // 禁用行尾空格
    'no-trailing-spaces': 2,
    // 禁用未声明的变量,除非它们在 /*global */ 注释中被提到
    'no-undef': 2,
    // 禁止将变量初始化为 undefined
    'no-undef-init': 2,
    // 禁止出现令人困惑的多行表达式
    'no-unexpected-multiline': 2,
    // 禁用一成不变的循环条件
    'no-unmodified-loop-condition': 2,
    // 禁止可以在有更简单的可替代的表达式时使用三元操作符
    'no-unneeded-ternary': [2, {
      'defaultAssignment': false
    }],
    // 禁止在return、throw、continue 和 break语句之后出现不可达代码
    'no-unreachable': 2,
    'no-unsafe-finally': 2,
    // 禁止出现未使用过的变量
    'no-unused-vars': [2, {
      'vars': 'all',
      'args': 'none'
    }],
    // 禁止不必要的 .call() 和 .apply()
    'no-useless-call': 2,
    // 禁止不必要的计算性能键对象的文字
    'no-useless-computed-key': 2,
    'no-useless-constructor': 2,
    // 禁用不必要的转义字符
    'no-useless-escape': 0,
    // 禁止属性前有空白
    'no-whitespace-before-property': 2,
    // 禁用 with 语句
    '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
    }],
    // 要求或禁止使用分号而不是 ASI(这个才是控制行尾部分号的,)
    'semi': [2, 'never'],
    // 强制分号之前和之后使用一致的空格
    'semi-spacing': [2, {
      'before': false,
      'after': true
    }],
    // 强制在块之前使用一致的空格
    'space-before-blocks': [2, 'always'],
    // 强制在 function的左括号之前使用一致的空格
    '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'],
    // 要求使用 isNaN() 检查 NaN
    'use-isnan': 2,
    // typeof foo === "undefimed" 错误
    'valid-typeof': 2,
    // 要求 IIFE 使用括号括起来
    'wrap-iife': [2, 'any'],
    // 强制在 yield* 表达式中 * 周围使用空格
    'yield-star-spacing': [2, 'both'],
    // 要求或禁止 “Yoda” 条件
    'yoda': [2, 'never'],
    // 要求使用 const 声明那些声明后不再被修改的变量
    'prefer-const': 2,
    // 强制在花括号中使用一致的空格
    'object-curly-spacing': [2, 'always', {
      objectsInObjects: false
    }],
    // 指定数组的元素之间要以空格隔开(, 后面), never参数:[ 之前和 ] 之后不能带空格,always参数:[ 之前和 ] 之后必须带空格
    'array-bracket-spacing': [2, 'never']
  }
}

3、Prettier的配置文件:prettier.config.js

module.exports = {
  printWidth: 120, // 一行的字符数,如果超过会进行换行,默认为80,官方建议设100-120其中一个数
  tabWidth: 2, // 一个tab代表几个空格数,默认就是2
  useTabs: false, // 启用tab取代空格符缩进,默认为false
  semi: false, // 行尾是否使用分号,默认为true
  singleQuote: true, // 字符串是否使用单引号,默认为false,即使用双引号,建议设true,即单引号
  quoteProps: 'as-needed', // 给对象里的属性名是否要加上引号,默认为as-needed,即根据需要决定,如果不加引号会报错则加,否则不加
  trailingComma: 'es5', // 是否使用尾逗号,有三个可选值"<none|es5|all>"
  jsxSingleQuote: false, // 在jsx里是否使用单引号,你看着办
  bracketSpacing: true // 对象大括号直接是否有空格,默认为true,效果:{ foo: bar }
}

4、VSCode的设置文件:setting.json

{
  "files.exclude": {
    "**/.classpath": true,
    "**/.project": true,
    "**/.settings": true,
    "**/.factorypath": true
  },
  "workbench.startupEditor": "none",
  "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
  "redhat.telemetry.enabled": true,
  "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx1G -Xms100m",
  "tabnine.experimentalAutoImports": true,
  "kite.showWelcomeNotificationOnStartup": false,
  "[vue]": {
    "editor.defaultFormatter": "octref.vetur"
  },
  "window.zoomLevel": 1,
  "workbench.iconTheme": "vscode-icons",
  "workbench.colorTheme": "One Monokai",
  "editor.cursorBlinking": "smooth",
  "editor.cursorSmoothCaretAnimation": true,
  "diffEditor.codeLens": true,
  "editor.minimap.maxColumn": 120,
  "editor.fontSize": 14,
  "workbench.editor.autoLockGroups": {
    "imagePreview.previewEditor": true,
    "mainThreadWebview-markdown.preview": true
  },
  "eslint.alwaysShowStatus": true,
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "scssFormatter.printWidth": 200,
  "scssFormatter.singleQuote": true,
  "editor.tabSize": 2,
  "files.autoSave": "afterDelay",
  "diffEditor.wordWrap": "off",
    // 强制单引号
    "prettier.singleQuote": true,
    // 尽可能控制尾随逗号的打印
    "prettier.trailingComma": "all",
    // 让prettier使用eslint的代码格式进行校验
    "prettier.eslintIntegration": true,
	  //autoFix默认开启,只需输入字符串数组即可
    "eslint.validate": [
      "html",
      "vue",
      "vue-html",
      "javascript",
      "javascriptreact"
    ],
    "eslint.options": { //指定vscode的eslint所处理的文件的后缀
      "extensions": [
        ".js",
        ".vue",
        ".ts",
        ".tsx"
      ]
    },
    //  #让函数(名)和后面的括号之间加个空格
    "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
    // 使用插件格式化 html
    "vetur.format.defaultFormatter.html": "js-beautify-html",
    "vetur.format.defaultFormatter.js": "vscode-typescript", //让vue中的js按编辑器自带的ts格式进行格式化
    // 格式化插件的配置
    "vetur.format.defaultFormatterOptions": {
      "prettier": {
        "semi": false, // 格式化不加分号
        "singleQuote": true, // 格式化以单引号为主
      },
     "js-beautify-html": {
      // 属性强制折行对齐
      "wrap_attributes": "force-aligned"
     },
      "prettyhtml": {
        "printWidth": 100,
        "singleQuote": false,
        "wrapAttributes": false,
        "sortAttributes": true
      },
    },
    "vetur.format.defaultFormatter": {
        "html": "prettier",
        "css": "prettier",
        "postcss": "prettier",
        "scss": "prettier",
        "less": "prettier",
        "js": "prettier",
        "ts": "prettier",
        "stylus": "stylus-supremacy"
    },
    // html颜色高亮
    "files.associations": {
      ".eslintrc": "json",
      "*.vue": "vue"
    },
    "emmet.syntaxProfiles": {
        "javascript": "jsx",
        "vue": "html",
        "vue-html": "html"
    },
    // 自动修复
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    },
    "[html]": {
      "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "eslint.run": "onSave",
    "javascript.format.enable": false
}

标签:禁止,false,no,VSCode,空格,prettier,eslint,true,ESLint
来源: https://blog.csdn.net/u013915263/article/details/121277231

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有