设置vue项目eslint prettier vetur

vscode工作区设置:

{
  "editor.tabSize": 2,
  // 开启保存后即触发格式化  不开开了会根据"vetur.format.defaultFormatter.js": "vscode-typescript",
  // "editor.formatOnSave": true,
  //关闭右侧快速预览
  "editor.minimap.enabled": true,
  "editor.fontSize": 14, //设置文字大小
  "editor.lineHeight": 0, //设置文字行高
  "editor.lineNumbers": "on", //开启行数提示
  "editor.quickSuggestions": {
    //开启自动显示建议
    "other": true,
    "comments": true,
    "strings": true
  },

  "window.zoomLevel": 0, // 调整窗口的缩放级别

  // 文件自动保存
  // "files.autoSave": "afterDelay",
  // "files.autoSaveDelay": 30000,
  // eslint.autoFixOnSave - enables auto fix on save.
  // Please note auto fix on save is only available if VS Code's files.autoSave is either off, onFocusChange or onWindowChange
  // . It will not work with afterDelay.
  //https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
  // #每次保存的时候将代码按eslint格式进行修复
  // "eslint.autoFixOnSave": true,废除
  // 用该属性触发eslint修复
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },

  //根据文件后缀名定义vue文件类型
  "files.associations": {
    "*.vue": "vue"
  },
  // 添加 vue 支持
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    {
      "language": "vue",
      "autoFix": true
    }
  ],

  // 该属性相当于配置js文件的操作
  // "[javascript]": {
  //   "editor.formatOnSave": true,
  //   "editor.defaultFormatter": "esbenp.prettier-vscode"
  // },

  //  #让prettier使用eslint的代码格式进行校验,prettier默认覆盖vscode格式化快捷键
  // 该属性已废除
  // "prettier.eslintIntegration": true,
  //  #声明结尾使用分号(默认true)
  "prettier.semi": false,
  //  #使用带引号替代双引号
  "prettier.singleQuote": true,
  // #只有一个参数的箭头函数的参数是否带圆括号(默认avoid)
  "prettier.arrowParens": "avoid",
  // #多行JSX中的>放置在最后一行的结尾,而不是另起一行(默认false)
  "prettier.jsxBracketSameLine": false,

  //关闭rg.exe进程 用cnpm导致会出现rg.exe占用内存很高
  "search.followSymlinks": false,

  // 默认格式化vue中的<script> region的方式,按编辑器自带的ts格式进行格式化
  "vetur.format.defaultFormatter.js": "vscode-typescript",
  // 默认格式化vue中的<template> region的方式
  //由于prettier不能格式化vue文件template  所以使用js-beautify-html格式化
  // https://vuejs.github.io/vetur/formatting.html#formatters
  "vetur.format.defaultFormatter.html": "js-beautify-html",
  // 给js-beautify-html设置属性隔断
  "vetur.format.defaultFormatterOptions": {
    "js-beautify-html": {
      "wrap_attributes": "force-aligned",
    // 对属性进行换行。
    // - auto: 仅在超出行长度时才对属性进行换行。
    // - force: 对除第一个属性外的其他每个属性进行换行。
    // - force-aligned: 对除第一个属性外的其他每个属性进行换行,并保持对齐。
    // - force-expand-multiline: 对每个属性进行换行。
    // - aligned-multiple: 当超出折行长度时,将属性进行垂直对齐。
// Maximum number of line breaks to be preserved in one chunk (0 disables) "max_preserve_newlines": 0 } }, // js默认偏移一个indent "vetur.format.scriptInitialIndent": true, // style默认偏移一个indent "vetur.format.styleInitialIndent": true, // 函数名后增加空格 "javascript.format.insertSpaceBeforeFunctionParenthesis": true, "javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true, // TS "typescript.format.insertSpaceBeforeFunctionParenthesis": true }

 

需要安装vscode插件 vetur eslint prettier

1.首先要让eslint识别vue文件 安装插件eslint-plugin-vue(识别react eslint-plugin-react)

然后在eslintrc里添加插件 

并且在工作区内配置

2.让每次保存都格式化 

  "editor.formatOnSave": true,
3.每次保存都按eslint去修复
  "eslint.autoFixOnSave": true,
4.但是由于prettier还不识别vue,可以通过设置 
  // #让prettier使用eslint的代码格式进行校验,prettier默认覆盖vscode格式化快捷键
  "prettier.eslintIntegration": true,
5.再通过vetur的格式化,vetur的默认代码风格化使用的就是prettier,会自动检索项目根目录下的prettier配置文件进行格式化,只有template用js-beautify-html



参考:

eslint规则配置详解:https://segmentfault.com/a/1190000008742240

Airbnb JavaScript 编码规范   https://github.com/yuche/javascript

vutur+eslint+prettier :https://segmentfault.com/a/1190000014785115?utm_source=index-hottest

          https://www.jb51.net/article/135191.htm

         https://blog.csdn.net/ljt123456765/article/details/81356792

eslint+prettier格式化代码(非vetur,通用版本):https://www.jb51.net/article/145970.htm

 

posted @ 2018-08-23 09:16  little_ab  阅读(8930)  评论(0编辑  收藏  举报