vscode編輯如何保存時自動校准eslint規范


  在日常開發中,一個大點的項目會有多人參與,那么可能就會出現大家的代碼風格不一,各顯神通,這個時候就要祭出我們的eslint

在這之前磨刀不誤砍柴工,我們先來配置一下我們的代碼編輯工具,如何在vscode下保存代碼時讓代碼自動以eslint標准來保存呢。

首先下載vscode就不用說了吧,那么在打開編輯器后我們在擴展商店先下載一個eslint插件

ok,下載完了讓我們打開vscode的設置項,方式:左下角圖標——>>設置

然后在這個json文件中放入下面代碼配置就完成了

 1 {
 2     "workbench.colorTheme": "Monokai",
 3     "editor.tabSize": 2,
 4     "files.associations": {
 5         "*.vue": "vue"
 6     },
 7     "eslint.autoFixOnSave": true,
 8     "eslint.options": {
 9         "extensions": [
10             ".js",
11             ".vue"
12         ]
13     },
14     "eslint.validate": [
15         "javascript",
16         {
17             "language": "vue",
18             "autoFix": true
19         },
20         "html",
21         "vue"
22     ],
23     "search.exclude": {
24         "**/node_modules": true,
25         "**/bower_components": true,
26         "**/dist": true
27     },
28     "emmet.syntaxProfiles": {
29         "javascript": "jsx",
30         "vue": "html",
31         "vue-html": "html"
32     },
33     "git.confirmSync": false,
34     "window.zoomLevel": 2,
35     "editor.renderWhitespace": "boundary",
36     "editor.cursorBlinking": "smooth",
37     "editor.minimap.enabled": true,
38     "editor.minimap.renderCharacters": false,
39     "window.title": "${dirty}${activeEditorMedium}${separator}${rootName}",
40     "editor.codeLens": true,
41     "editor.snippetSuggestions": "top",
42     "editor.multiCursorModifier": "ctrlCmd",
43     "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
44 }

在編輯器中輸入后出現不規范的地方編輯器會有標紅提示,按ctrl+s保存時也會根據這個規范保存。不過前提是公司的eslint規范與你這個規范相同,如果不同你就要根據配置來修改里面的內容了。下面是個人配置的eslint

// https://eslint.org/docs/user-guide/configuring

module.exports = {

  //此項是用來告訴eslint找當前配置文件不能往父級查找
  root: true,

  //此項是用來指定eslint解析器的,解析器必須符合規則,babel-eslint解析器是對babel解析器的包裝使其與ESLint解析
  parser: 'babel-eslint',

  //此項是用來指定javaScript語言類型和風格,sourceType用來指定js導入的方式,默認是script,此處設置為module,指某塊導入方式
  parserOptions: {
    // 設置 script(默認) 或 module,如果代碼是在ECMASCRIPT中的模塊
    sourceType: 'module',
    "ecmaVersion": 6,
    "ecmaFeatures": {
      "jsx": true
    }
  },

  // 此項指定環境的全局變量,下面的配置指定為瀏覽器環境
  env: {
    "browser": true,
    "node": true,
    "commonjs": true,
    "es6": true,
    "amd": true
  },
  // https://github.com/standard/standard/blob/master/docs/RULES-en.md
  // 此項是用來配置標准的js風格,就是說寫代碼的時候要規范的寫,如果你使用vs-code我覺得應該可以避免出錯
  extends: 'vue',
  // 此項是用來提供插件的,插件名稱省略了eslint-plugin-,下面這個配置是用來規范html的
  plugins: [
    'html',
    "flow-vars", 
    "react"
  ],
  /* 
   下面這些rules是用來設置從插件來的規范代碼的規則,使用必須去掉前綴eslint-plugin-
    主要有如下的設置規則,可以設置字符串也可以設置數字,兩者效果一致
    "off" -> 0 關閉規則
    "warn" -> 1 開啟警告規則
    "error" -> 2 開啟錯誤規則
  */
  rules: {
    // 不需要
    "space-before-function-paren": 0,  // 函數定義時括號前面要不要有空格
    "eol-last": 0,  // 文件以單一的換行符結束
    "no-extra-semi": 0, // 可以多余的冒號
    "semi": 0,  // 語句可以不需要分號結尾
    "eqeqeq": 0, // 必須使用全等
    "one-var": 0, // 連續聲明
    "no-undef": 1, // 可以 有未定義的變量

    // 警告
    "no-extra-boolean-cast": 1, // 不必要的bool轉換
    "no-extra-parens": 1, // 非必要的括號
    "no-empty": 1, // 塊語句中的內容不能為空
    "no-use-before-define": [1, "nofunc"], // 未定義前不能使用
    "complexity": [1, 10], // 循環復雜度
    "no-unused-vars": 1, // 不能有聲明后未被使用的變量或參數
    // vue
    "flow-vars/define-flow-type": 1,
    "flow-vars/use-flow-type": 1,

    // react
    "react/jsx-uses-react": 2,
    "react/jsx-uses-vars": 2,

    // 錯誤
    "comma-dangle": [2, "never"], // 對象字面量項尾不能有逗號
    "no-debugger": 2, // 禁止使用debugger
    "no-constant-condition": 2, // 禁止在條件中使用常量表達式 if(true) if(1)
    "no-dupe-args": 2, // 函數參數不能重復
    "no-dupe-keys": 2, // 在創建對象字面量時不允許鍵重復 {a:1,a:1}
    "no-duplicate-case": 2, // switch中的case標簽不能重復
    "no-empty-character-class": 2, // 正則表達式中的[]內容不能為空
    "no-invalid-regexp": 2, // 禁止無效的正則表達式
    "no-func-assign": 2, // 禁止重復的函數聲明
    "valid-typeof": 2,  // 必須使用合法的typeof的值
    "no-unreachable": 2, // 不能有無法執行的代碼
    "no-unexpected-multiline": 2, // 避免多行表達式
    "no-sparse-arrays": 2, // 禁止稀疏數組, [1,,2]
    "no-shadow-restricted-names": 2, // 嚴格模式中規定的限制標識符不能作為聲明時的變量名使用
    "no-cond-assign": 2, // 禁止在條件表達式中使用賦值語句
    "no-native-reassign": 2, // 不能重寫native對象

    // 代碼風格
    "no-else-return": 1, // 如果if語句里面有return,后面不能跟else語句
    "no-multi-spaces": 1, // 不能用多余的空格
    "key-spacing": [1, {  // 對象字面量中冒號的前后空格
      "beforeColon": false,
      "afterColon": true
    }],
    "block-scoped-var": 2, // 塊語句中使用var
    "consistent-return": 2, // return 后面是否允許省略
    "accessor-pairs": 2, // 在對象中使用getter/setter
    "dot-location": [2, "property"], // 對象訪問符的位置,換行的時候在行首還是行尾
    "no-lone-blocks": 2, // 禁止不必要的嵌套塊
    "no-labels": 2, // 禁止標簽聲明
    "no-extend-native": 2, // 禁止擴展native對象
    "no-floating-decimal": 2, // 禁止省略浮點數中的0 .5 3.
    "no-loop-func": 2, // 禁止在循環中使用函數(如果沒有引用外部變量不形成閉包就可以)
    "no-new-func": 2,  // 禁止使用new Function
    "no-self-compare": 2, // 不能比較自身
    "no-sequences": 2, // 禁止使用逗號運算符
    "no-throw-literal": 2, // 禁止拋出字面量錯誤 throw "error";
    "no-return-assign": [2, "always"], // return 語句中不能有賦值表達式
    "no-redeclare": [2, {   // 禁止重復聲明變量
      "builtinGlobals": true
    }],
    "no-unused-expressions": [2, {  // 禁止無用的表達式
      "allowShortCircuit": true,
      "allowTernary": true
    }],
    "no-useless-call": 2, // 禁止不必要的call和apply
    "no-useless-concat": 2,
    "no-void": 2, // 禁用void操作符
    "no-with": 2, // 禁用with
    "space-infix-ops": 2, // 中綴操作符周圍要不要有空格
    "valid-jsdoc": [2, { // jsdoc規則
      "requireParamDescription": true,
      "requireReturnDescription": true
    }],
    "no-warning-comments": [2, {  // 不能有警告備注
      "terms": ["todo", "fixme", "any other term"],
      "location": "anywhere"
    }],
    "curly": 1, // 必須使用 if(){} 中的{}

    // common js
    "no-duplicate-imports": 1
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM