当前位置: 首页 > news >正文

vscode 代码保存eslint自动格式化,最新配置:Eslint+Prettier

一、安装ESlint插件,打开vscode选择“文件”–>“首选项”–>“设置”,键入“eslint”搜索,往下滑动,点击“Edit in settings.json”,打开“setttings.json”文件加入如下设置

在这里插入图片描述
在这里插入图片描述

{
  //打开文件不覆盖
  "workbench.editor.enablePreview": false,
  //关闭快速预览
  "editor.minimap.enabled": false,
  //打开自动保存
  "files.autoSave": "afterDelay",
  //使用主题
  // "workbench.colorTheme": "Darcula Theme from IntelliJ",
  // 头部注释
  "fileheader.customMade": {
    "Author": "hyy",
    "Date": "Do not edit", // 设置后默认设置文件生成时间
    "LastEditTime": "Do not edit", // 设置后,保存文件更改默认更新最后编辑时间
    "LastEditors": "hyy", // 设置后,保存文件更改默认更新最后编辑人
    "Description": ""
  },
  // 函数注释
  "fileheader.cursorMode": {
    "description": "",
    "param": "",
    "return": ""
  },
  //手机项目rem适配
  "px-to-rem.px-per-rem": 100,
  // -----------------------自动格式化配置eslint+prettier-----------------------
  // 每次保存自动格式化ctrl+s
  "editor.formatOnSave": true,
  // 每次保存的时候将代码按eslint格式进行修复
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  //配置内配置对 .vue 文件的格式化。由于eslint关于html模板和vue模板的建议存在冲突,所以只要关闭其中一个就可以解决。
  // "[vue]": {
  //   "editor.defaultFormatter": "esbenp.prettier-vscode"
  // },
  //配置内配置对 .ts 文件的格式化
  "[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  //配置内配置对 .js 文件的格式化
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  //配置内配置对 .json 文件的格式化
  "[jsonc]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  //  #去掉代码结尾的分号
  "prettier.semi": false,
  //  #使用单引号替代双引号
  "prettier.singleQuote": true,
  //  #多少字符自动换行
  "prettier.printWidth": 80,
  // 无尾逗号
  "prettier.trailingComma": "none",
  //  #让函数(名)和后面的括号之间加个空格
  "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
  // #这个按用户自身习惯选择。。由于eslint关于html模板和vue模板的建议存在冲突,所以只要关闭其中一个就可以解决。
  "vetur.format.defaultFormatter.html": "js-beautify-html",
  // #让vue中的js按编辑器自带的ts格式进行格式化
  "vetur.format.defaultFormatter.js": "vscode-typescript",
  "vetur.format.defaultFormatterOptions": {
    "js-beautify-html": {
      "wrap_attributes": "force-aligned"
      // #vue组件中html代码格式化样式
    }
  },
  // 格式化stylus, 需安装Manta's Stylus Supremacy插件
  "stylusSupremacy.insertColons": false, // 是否插入冒号
  "stylusSupremacy.insertSemicolons": false, // 是否插入分号
  "stylusSupremacy.insertBraces": false, // 是否插入大括号
  "stylusSupremacy.insertNewLineAroundImports": false, // import之后是否换行
  "stylusSupremacy.insertNewLineAroundBlocks": false // 两个选择器中是否换行
}

二、配置.eslintrc.js文件内容,网上有很多,可以根据需要自行添加

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint',
    sourceType: 'module'
  },
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  extends: ['plugin:vue/recommended', 'eslint:recommended'],

  //it is base on https://github.com/vuejs/eslint-config-vue
  rules: {
    "vue/max-attributes-per-line": [2, {
      "singleline": 10,
      "multiline": {
        "max": 1,
        "allowFirstLine": false
      }
    }],
    "vue/singleline-html-element-content-newline": "off",
    "vue/multiline-html-element-content-newline":"off",
    "vue/name-property-casing": ["error", "PascalCase"],
    "vue/no-v-html": "off",
    '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': ["error", "always", {"null": "ignore"}],
    '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']
  }
}

PS:个人安装的插件如下图所示,注意:eslint, prettier-Code formatter ,vetur 这三个插件必须安装,其他的插件根据自己的习惯,如需格式化stylus,则还需安装Manta’s Stylus Supremacy和stylus插件

在这里插入图片描述
参考原文:https://blog.csdn.net/weixin_36222137/article/details/80040758

相关文章:

  • less清除浮动clearfix代码片段
  • Git使用及配置
  • Jenkins安装部署及实现CI/CD(ubuntu20.04)
  • Apache+tomcat安装 linux
  • 数字信封+数字签名流程图
  • DBLink创建方法
  • JBPM4入门+程序下载
  • JBPM3资料
  • Mac 安装pd虚拟机,远程桌面无法使用ctrl或其他符号无法使用问题
  • Django项目开发举例之创建开发环境(1)
  • Django项目开发举例举例之创建应用模型(2)
  • Django项目开发举例之应用的管理界面(3)
  • Django项目开发举例之自定义管理界面(4)
  • Django项目开发举例之用户界面视图模版(5)
  • Django项目开发举例之用户界面表单(6)
  • 4. 路由到控制器 - Laravel从零开始教程
  • bootstrap创建登录注册页面
  • C++类中的特殊成员函数
  • CSS 专业技巧
  • Django 博客开发教程 8 - 博客文章详情页
  • Druid 在有赞的实践
  • Invalidate和postInvalidate的区别
  • Laravel 菜鸟晋级之路
  • Python socket服务器端、客户端传送信息
  • Spring Cloud中负载均衡器概览
  • 如何使用 JavaScript 解析 URL
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • #微信小程序:微信小程序常见的配置传值
  • $jQuery 重写Alert样式方法
  • (1)Android开发优化---------UI优化
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)大道至简,职场上做人做事做管理
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • **CI中自动类加载的用法总结
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .NET delegate 委托 、 Event 事件,接口回调
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .NET6 开发一个检查某些状态持续多长时间的类
  • .Net语言中的StringBuilder:入门到精通
  • /var/lib/dpkg/lock 锁定问题
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • []新浪博客如何插入代码(其他博客应该也可以)
  • [1159]adb判断手机屏幕状态并点亮屏幕
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians
  • [android]-如何在向服务器发送request时附加已保存的cookie数据
  • [C++]18:set和map的使用
  • [CDOJ 838]母仪天下 【线段树手速练习 15分钟内敲完算合格】
  • [CLR via C#]11. 事件
  • [HNOI2015]实验比较