eslint+prettier 統一代碼風格


有時git clone下來的項目會出現vscode自動保存和eslint格式沖突的情況,煩躁得很,此處列出配置統一風格。

 

1.安裝VsCode代碼插件

  • Vetur
  • Eslint
  • Prettier - Code formatter

2.配置VsCode

設置-打開設置(json)

{
    // 控制工作台中活動欄的可見性。
    "workbench.activityBar.visible": true,
    //主題設置
    // "workbench.colorTheme": "Monokai",
    // 默認編輯器字號
    "editor.fontSize": 14,
    //是否自動換行
    "editor.wordWrap": "on",
    "workbench.editor.enablePreview": false, //打開文件不覆蓋
    "search.followSymlinks": false, //關閉rg.exe進程
    "editor.minimap.enabled": false, //關閉迷你圖快速預覽
    "files.autoSave": "onWindowChange", // 切換窗口時自動保存。
    "editor.lineNumbers": "on", //開啟行數提示
    "editor.quickSuggestions": {
      //開啟自動顯示建議
      "other": true,
      "comments": true,
      "strings": true
    },
    "editor.tabSize": 2, //制表符符號eslint
  
    //.vue文件template格式化支持,並使用js-beautify-html插件
    "vetur.format.defaultFormatter.html": "js-beautify-html",
    //js-beautify-html格式化配置,屬性強制換行
    "vetur.format.defaultFormatterOptions": {
      "js-beautify-html": {
        "wrap_attributes": "force-aligned"
      }
    },
    //根據文件后綴名定義vue文件類型
    "files.associations": {
      "*.vue": "vue"
    },
    //配置 ESLint 檢查的文件類型
    "eslint.validate": [
      "javascript",
      "javascriptreact",
      {
        "language": "vue",
        "autoFix": true
      }
    ],
    //保存時eslint自動修復錯誤
    "eslint.autoFixOnSave": true,
    //保存自動格式化
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    }
  }

3.配置項目中得.eslintrc.js

 1 module.exports = {
 2   root: true,
 3   parserOptions: {
 4     parser: 'babel-eslint'
 5   },
 6   env: {
 7     browser: true
 8   },
 9   extends: [
10     // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
11     // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
12     'plugin:vue/essential',
13     // https://github.com/standard/standard/blob/master/docs/RULES-en.md
14     'standard'
15   ],
16   // required to lint *.vue files
17   plugins: [
18     'vue'
19   ],
20   rules: {
21     'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
22     'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
23     //強制使用單引號
24     quotes: ['error', 'single'],
25     //強制不使用分號結尾
26     semi: ['error', 'never']
27   },
28 }

4.配置項目中得.prettierrc

{
  "eslintIntegration": true, 
  "singleQuote": true,
  "semi": false
}

 


免責聲明!

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



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