歡迎您光臨本站 註冊首頁

vscode代碼格式化和eslint的使用

←手機掃碼閱讀     limiyoyo @ 2020-05-07 , reply:0

簡介
今天看著寫的代碼越來越多後,發現自己讀起都有點吃力了,哈哈,自己看著眼睛痛,就準備整頓一下,畢竟這個項目還要維護很久的,找解決方案和測試解決方案就用了一個半小時,嚴重開始懷疑自己的智商了。下面的目標讓代碼看起來很公正
代碼編輯器
vscode
version:版本 1.35.1 (1.35.1) 2019-06-12T14:19:05.197Z更新的
vscode代碼格式化
因為目前公司就我一個後端,項目也不大,所以就採用這種方案,簡單快捷粗暴。
一.點擊code->preferences->settings 點擊右上角{}
二.用戶自定義設置(/User/settings.json)
添加代碼
"editor.formatOnType": true, "editor.formatOnSave": true
ESLint配置
ESLint不僅有代碼規範而且還有一部分語法檢查的功能,ex:命令規範(駝峰) a==b警告提示a===b...
ESLint可以有效的規範代碼,以後還是會採用,培養自己的規範的編碼習慣
https://cn.eslint.org/
1.vscode安裝ESLint
這裡以配置eslint-config-aribnb的例子
vscode在extensions中安裝ESLint
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的文件
{ "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 lucky
總結:
主要是卡在eslint.options上,沒看vscode的extensions的eslint的README,而去相信了搜索結果,沒有添加eslint.options,那麼一直都無法生效.學的教訓。


[limiyoyo ] vscode代碼格式化和eslint的使用已經有265次圍觀

http://coctec.com/docs/vscode/show-post-233206.html