vscode代碼格式化和eslint

簡介

今天看着寫的代碼愈來愈多後,發現本身讀起都有點吃力了,哈哈,本身看着眼睛痛,就準備整頓一下,畢竟這個項目還要維護好久的,找解決方案和測試解決方案就用了一個半小時,嚴重開始懷疑本身的智商了。下面的目標讓代碼看起來很公正javascript

代碼編輯器

vscode
version:版本 1.35.1 (1.35.1) 2019-06-12T14:19:05.197Z更新的vue

vscode代碼格式化

由於目前公司就我一個後端,項目也不大,因此就採用這種方案,簡單快捷粗暴。java

一.點擊code->preferences->settings 點擊右上角{}
二.用戶自定義設置(/User/settings.json)
添加代碼
"editor.formatOnType": true,
"editor.formatOnSave": true

ESLint配置

ESLint不只有代碼規範並且還有一部分語法檢查的功能,ex:命令規範(駝峯) a==b警告提示a===b...
ESLint能夠有效的規範代碼,之後仍是會採用,培養本身的規範的編碼習慣
https://cn.eslint.org/node

1.vscode安裝ESLint

這裏以配置eslint-config-aribnb的例子
vscode在extensions中安裝ESLintreact

2.npm安裝

npm install -g eslint

3.建立.eslintrc文件

softwaredeMacBook-Pro:koa-pro software$ "eslint --init"
? How would you like to configure ESLint? "Use a popular style guide"
? Which style guide do you want to follow? "Airbnb" (https://github.com/airbnb/javascript)
? Do you use React? "No"
? What format do you want your config file to be in? "JSON"
Checking peerDependencies of eslint-config-airbnb-base@latest
The config that you have selected requires the following dependencies:

eslint-config-airbnb-base@latest eslint@^4.19.1 || ^5.3.0 eslint-plugin-import@^2.14.0
? Would you like to install them now with npm? "Yes"
Installing eslint-config-airbnb-base@latest, eslint@^4.19.1 || ^5.3.0, eslint-plugin-import@^2.14.0
npm WARN koa-pro@1.0.0 No repository field.

+ eslint@5.16.0
+ eslint-plugin-import@2.17.3
+ eslint-config-airbnb-base@13.1.0
updated 3 packages and audited 7469 packages in 23.559s
found 370 vulnerabilities (1 low, 367 moderate, 2 high)
  run `npm audit fix` to fix them, or `npm audit` for details
Successfully created .eslintrc.json file in /Users/software/workspace/Me/huafu/koa-pro

項目目錄下將會生成一個eslintrc.json的文件git

{
    "extends": "airbnb-base"
}
添加本身想要的設置,我這裏node環境
{"env": {
    "node": true,
    "es6": true
  },
  "parserOptions": {#解決import export eslint報錯
    "ecmaFeatures": {
      "legacyDecorators": true
    }
  },
    "extends": "airbnb-base"
}

4.關聯eslint與vscode

1.code->preferences->settings 進入user的seetings
2.添加如下代碼

"eslint.autoFixOnSave": true,//保存自動修復eslint錯誤
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    {
      "language": "vue",
      "autoFix": true
    }
  ],
  "eslint.options": {//指定eslint配置文件位置i
    "configFile": ".eslintrc.json" //指定項目根目錄中的eslint配置文件
  }

這樣vscode和eslint關聯配置完成了,不出意外會報一大堆錯。good luckyes6

總結:

主要是卡在eslint.options上,沒看vscode的extensions的eslint的README,而去相信了百度,沒有添加eslint.options,那麼一直都沒法生效.學的教訓。good lucky for megithub

相關文章
相關標籤/搜索