1. 程式人生 > 實用技巧 >VSCode Eslint+Prettier+Vetur常用配置

VSCode Eslint+Prettier+Vetur常用配置

  • 工程裡建立檔案.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'],

  // add your custom rules here
  //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': 0,
    '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'],
    'linebreak-style': ["off", "windows"]
  }
}
  • VSCode安裝外掛ESLint Prettier Vetur

  • 開啟VSCode設定,配置settings.json

  • 附錄1:我的VSCode配置(2020-08)
    1. 儲存時自動修復
    2. prettier使用eslint格式化
    3. vetur使用prettier格式化
{
  /************** Editor *****************/
  "editor.detectIndentation": false,
  "editor.tabSize": 2,
  "editor.wordWrap": "on",
  "editor.quickSuggestions": {
    "other": true,
    "comments": true,
    "strings": true
  },
  // 儲存時自動格式化程式碼
  "editor.formatOnSave": true,
  // eslint配置項,儲存時自動修復錯誤
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  },
  // 預設使用prettier格式化支援的檔案
  // "editor.defaultFormatter": "esbenp.prettier-vscode",
  "minapp-vscode.disableAutoConfig": true,
  "window.zoomLevel": 0,
  "files.associations": {
    "*.cjson": "jsonc",
    "*.wxml": "html",
    "*.wxss": "css",
    "*.wxs": "javascript",
    // "*.vue": "html"
  },
  "emmet.includeLanguages": {
    "wxml": "html"
  },
  //在使用搜索功能時,將這些資料夾/檔案排除在外
  "search.exclude": {
    "**/node_modules": true,
    "**/bower_components": true,
    "**/target": true,
    "**/logs": true,
  },
  "svn.default.encoding": "",
  "svn.path": "D:\\Soft\\TortoiseSVN\\bin",
  "git.path": "D:\\Soft\\Git\\Git\\bin",
  "workbench.iconTheme": "material-icon-theme",
  "workbench.colorTheme": "Nebula",
  "workbench.editor.enablePreview": true, //預覽模式關閉
  // "javascript.format.semicolons": "remove",
  // "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
  "[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
  },
  "[json]": {
    "editor.defaultFormatter": "vscode.json-language-features"
  },
  "[jsonc]": {
    "editor.defaultFormatter": "vscode.json-language-features"
  },
  "[javascript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features"
  },
  // "[vue]": {
  //   "editor.defaultFormatter": "octref.vetur"
  // },
  /************** ESlint *****************/
  "eslint.enable": true,
  "eslint.format.enable": true,
  "eslint.run": "onType",
  "eslint.options": {
    "extensions": [
      ".js",
      ".vue",
      ".ts",
      ".tsx"
    ]
  },
  "eslint.nodePath": "",
  "eslint.codeAction.showDocumentation": {
    "enable": true
  },
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "html",
    "vue"
  ],
  /************** Vetur *****************/
  // 讓vetur使用vs自帶的js格式化工具,以便在函式前面加個空格
  "vetur.format.defaultFormatter.js": "prettier",
  "workbench.startupEditor": "welcomePage",
  /************** Prettier *****************/
  //  #去掉程式碼結尾的分號 
  // "prettier.semi": false,
  // //  #使用單引號替代雙引號 
  // "prettier.singleQuote": true,
}
  • 附錄2:常見名詞解釋
"no-alert": 0,//禁止使用alert confirm prompt
"no-array-constructor": 2,//禁止使用陣列構造器
"no-bitwise": 0,//禁止使用按位運算子
"no-caller": 1,//禁止使用arguments.caller或arguments.callee
"no-catch-shadow": 2,//禁止catch子句引數與外部作用域變數同名
"no-class-assign": 2,//禁止給類賦值
"no-cond-assign": 2,//禁止在條件表示式中使用賦值語句
"no-console": 2,//禁止使用console
"no-const-assign": 2,//禁止修改const宣告的變數
"no-constant-condition": 2,//禁止在條件中使用常量表達式 if(true) if(1)
"no-continue": 0,//禁止使用continue
"no-control-regex": 2,//禁止在正則表示式中使用控制字元
"no-debugger": 2,//禁止使用debugger
"no-delete-var": 2,//不能對var宣告的變數使用delete操作符
"no-div-regex": 1,//不能使用看起來像除法的正則表示式/=foo/
"no-dupe-keys": 2,//在建立物件字面量時不允許鍵重複 {a:1,a:1}
"no-dupe-args": 2,//函式引數不能重複
"no-duplicate-case": 2,//switch中的case標籤不能重複
"no-else-return": 2,//如果if語句裡面有return,後面不能跟else語句
"no-empty": 2,//塊語句中的內容不能為空
"no-empty-character-class": 2,//正則表示式中的[]內容不能為空
"no-empty-label": 2,//禁止使用空label
"no-eq-null": 2,//禁止對null使用==或!=運算子
"no-eval": 1,//禁止使用eval
"no-ex-assign": 2,//禁止給catch語句中的異常引數賦值
"no-extend-native": 2,//禁止擴充套件native物件
"no-extra-bind": 2,//禁止不必要的函式繫結
"no-extra-boolean-cast": 2,//禁止不必要的bool轉換
"no-extra-parens": 2,//禁止非必要的括號
"no-extra-semi": 2,//禁止多餘的冒號
"no-fallthrough": 1,//禁止switch穿透
"no-floating-decimal": 2,//禁止省略浮點數中的0 .5 3.
"no-func-assign": 2,//禁止重複的函式宣告
"no-implicit-coercion": 1,//禁止隱式轉換
"no-implied-eval": 2,//禁止使用隱式eval
"no-inline-comments": 0,//禁止行內備註
"no-inner-declarations": [2, "functions"],//禁止在塊語句中使用宣告(變數或函式)
"no-invalid-regexp": 2,//禁止無效的正則表示式
"no-invalid-this": 2,//禁止無效的this,只能用在構造器,類,物件字面量
"no-irregular-whitespace": 2,//不能有不規則的空格
"no-iterator": 2,//禁止使用__iterator__ 屬性
"no-label-var": 2,//label名不能與var宣告的變數名相同
"no-labels": 2,//禁止標籤宣告
"no-lone-blocks": 2,//禁止不必要的巢狀塊
"no-lonely-if": 2,//禁止else語句內只有if語句
"no-loop-func": 1,//禁止在迴圈中使用函式(如果沒有引用外部變數不形成閉包就可以)
"no-mixed-requires": [0, false],//宣告時不能混用宣告型別
"no-mixed-spaces-and-tabs": [2, false],//禁止混用tab和空格
"linebreak-style": [0, "windows"],//換行風格
"no-multi-spaces": 1,//不能用多餘的空格
"no-multi-str": 2,//字串不能用\換行
"no-multiple-empty-lines": [1, {"max": 2}],//空行最多不能超過2行
"no-native-reassign": 2,//不能重寫native物件
"no-negated-in-lhs": 2,//in 操作符的左邊不能有!
"no-nested-ternary": 0,//禁止使用巢狀的三目運算
"no-new": 1,//禁止在使用new構造一個例項後不賦值
"no-new-func": 1,//禁止使用new Function
"no-new-object": 2,//禁止使用new Object()
"no-new-require": 2,//禁止使用new require
"no-new-wrappers": 2,//禁止使用new建立包裝例項,new String new Boolean new Number
"no-obj-calls": 2,//不能呼叫內建的全域性物件,比如Math() JSON()
"no-octal": 2,//禁止使用八進位制數字
"no-octal-escape": 2,//禁止使用八進位制轉義序列
"no-param-reassign": 2,//禁止給引數重新賦值
"no-path-concat": 0,//node中不能使用__dirname或__filename做路徑拼接
"no-plusplus": 0,//禁止使用++,--
"no-process-env": 0,//禁止使用process.env
"no-process-exit": 0,//禁止使用process.exit()
"no-proto": 2,//禁止使用__proto__屬性
"no-redeclare": 2,//禁止重複宣告變數
"no-regex-spaces": 2,//禁止在正則表示式字面量中使用多個空格 /foo bar/
"no-restricted-modules": 0,//如果禁用了指定模組,使用就會報錯
"no-return-assign": 1,//return 語句中不能有賦值表示式
"no-script-url": 0,//禁止使用javascript:void(0)
"no-self-compare": 2,//不能比較自身
"no-sequences": 0,//禁止使用逗號運算子
"no-shadow": 2,//外部作用域中的變數不能與它所包含的作用域中的變數或引數同名
"no-shadow-restricted-names": 2,//嚴格模式中規定的限制識別符號不能作為宣告時的變數名使用
"no-spaced-func": 2,//函式呼叫時 函式名與()之間不能有空格
"no-sparse-arrays": 2,//禁止稀疏陣列, [1,,2]
"no-sync": 0,//nodejs 禁止同步方法
"no-ternary": 0,//禁止使用三目運算子
"no-trailing-spaces": 1,//一行結束後面不要有空格
"no-this-before-super": 0,//在呼叫super()之前不能使用this或super
"no-throw-literal": 2,//禁止丟擲字面量錯誤 throw "error";
"no-undef": 1,//不能有未定義的變數
"no-undef-init": 2,//變數初始化時不能直接給它賦值為undefined
"no-undefined": 2,//不能使用undefined
"no-unexpected-multiline": 2,//避免多行表示式
"no-underscore-dangle": 1,//識別符號不能以_開頭或結尾
"no-unneeded-ternary": 2,//禁止不必要的巢狀 var isYes = answer === 1 ? true : false;
"no-unreachable": 2,//不能有無法執行的程式碼
"no-unused-expressions": 2,//禁止無用的表示式
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],//不能有聲明後未被使用的變數或引數
"no-use-before-define": 2,//未定義前不能使用
"no-useless-call": 2,//禁止不必要的call和apply
"no-void": 2,//禁用void操作符
"no-var": 0,//禁用var,用let和const代替
"no-warning-comments": [1, { "terms": ["todo", "fixme", "xxx"], "location": "start" }],//不能有警告備註
"no-with": 2,//禁用with
"array-bracket-spacing": [2, "never"],//是否允許非空數組裡面有多餘的空格
"arrow-parens": 0,//箭頭函式用小括號括起來
"arrow-spacing": 0,//=>的前/後括號
"accessor-pairs": 0,//在物件中使用getter/setter
"block-scoped-var": 0,//塊語句中使用var
"brace-style": [1, "1tbs"],//大括號風格
"callback-return": 1,//避免多次呼叫回撥什麼的
"camelcase": 2,//強制駝峰法命名
"comma-dangle": [2, "never"],//物件字面量項尾不能有逗號
"comma-spacing": 0,//逗號前後的空格
"comma-style": [2, "last"],//逗號風格,換行時在行首還是行尾
"complexity": [0, 11],//迴圈複雜度
"computed-property-spacing": [0, "never"],//是否允許計算後的鍵名什麼的
"consistent-return": 0,//return 後面是否允許省略
"consistent-this": [2, "that"],//this別名
"constructor-super": 0,//非派生類不能呼叫super,派生類必須呼叫super
"curly": [2, "all"],//必須使用 if(){} 中的{}
"default-case": 2,//switch語句最後必須有default
"dot-location": 0,//物件訪問符的位置,換行的時候在行首還是行尾
"dot-notation": [0, { "allowKeywords": true }],//避免不必要的方括號
"eol-last": 0,//檔案以單一的換行符結束
"eqeqeq": 2,//必須使用全等
"func-names": 0,//函式表示式必須有名字
"func-style": [0, "declaration"],//函式風格,規定只能使用函式宣告/函式表示式
"generator-star-spacing": 0,//生成器函式*的前後空格
"guard-for-in": 0,//for in迴圈要用if語句過濾
"handle-callback-err": 0,//nodejs 處理錯誤
"id-length": 0,//變數名長度
"indent": [2, 4],//縮排風格
"init-declarations": 0,//宣告時必須賦初值
"key-spacing": [0, { "beforeColon": false, "afterColon": true }],//物件字面量中冒號的前後空格
"lines-around-comment": 0,//行前/行後備注
"max-depth": [0, 4],//巢狀塊深度
"max-len": [0, 80, 4],//字串最大長度
"max-nested-callbacks": [0, 2],//回撥巢狀深度
"max-params": [0, 3],//函式最多隻能有3個引數
"max-statements": [0, 10],//函式內最多有幾個宣告
"new-cap": 2,//函式名首行大寫必須使用new方式呼叫,首行小寫必須用不帶new方式呼叫
"new-parens": 2,//new時必須加小括號
"newline-after-var": 2,//變數聲明後是否需要空一行
"object-curly-spacing": [0, "never"],//大括號內是否允許不必要的空格
"object-shorthand": 0,//強制物件字面量縮寫語法
"one-var": 1,//連續宣告
"operator-assignment": [0, "always"],//賦值運算子 += -=什麼的
"operator-linebreak": [2, "after"],//換行時運算子在行尾還是行首
"padded-blocks": 0,//塊語句內行首行尾是否要空行
"prefer-const": 0,//首選const
"prefer-spread": 0,//首選展開運算
"prefer-reflect": 0,//首選Reflect的方法
"quotes": [1, "single"],//引號型別 `` "" ''
"quote-props":[2, "always"],//物件字面量中的屬性名是否強制雙引號

"radix": 2,//parseInt必須指定第二個引數
"id-match": 0,//命名檢測
"require-yield": 0,//生成器函式必須有yield
"semi": [2, "always"],//語句強制分號結尾
"semi-spacing": [0, {"before": false, "after": true}],//分號前後空格
"sort-vars": 0,//變數宣告時排序
"space-after-keywords": [0, "always"],//關鍵字後面是否要空一格
"space-before-blocks": [0, "always"],//不以新行開始的塊{前面要不要有空格
"space-before-function-paren": [0, "always"],//函式定義時括號前面要不要有空格
"space-in-parens": [0, "never"],//小括號裡面要不要有空格
"space-infix-ops": 0,//中綴操作符周圍要不要有空格
"space-return-throw-case": 2,//return throw case後面要不要加空格
"space-unary-ops": [0, { "words": true, "nonwords": false }],//一元運算子的前/後要不要加空格
"spaced-comment": 0,//註釋風格不要有空格什麼的
"strict": 2,//使用嚴格模式
"use-isnan": 2,//禁止比較時使用NaN,只能用isNaN()
"valid-jsdoc": 0,//jsdoc規則
"valid-typeof": 2,//必須使用合法的typeof的值
"vars-on-top": 2,//var必須放在作用域頂部
"wrap-iife": [2, "inside"],//立即執行函式表示式的小括號風格
"wrap-regex": 0,//正則表示式字面量用小括號包起來
"yoda": [2, "never"]//禁止尤達條件