vscode代碼格式化和eslint的使用
簡介
今天看著寫的代碼越來越多后,發(fā)現(xiàn)自己讀起都有點吃力了,哈哈,自己看著眼睛痛,就準備整頓一下,畢竟這個項目還要維護很久的,找解決方案和測試解決方案就用了一個半小時,嚴重開始懷疑自己的智商了。下面的目標讓代碼看起來很公正
代碼編輯器
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不僅有代碼規(guī)范而且還有一部分語法檢查的功能,ex:命令規(guī)范(駝峰) a==b警告提示a===b...
ESLint可以有效的規(guī)范代碼,以后還是會采用,培養(yǎng)自己的規(guī)范的編碼習慣
https://cn.eslint.org/
1.vscode安裝ESLint
這里以配置eslint-config-aribnb的例子
vscode在extensions中安裝ESLint
2.npm安裝
npm install -g eslint
3.創(chuàng)建.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環(huán)境
{"env": {
"node": true,
"es6": true
},
"parserOptions": {#解決import export eslint報錯
"ecmaFeatures": {
"legacyDecorators": true
}
},
"extends": "airbnb-base"
}
4.關聯(lián)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關聯(lián)配置完成了,不出意外會報一大堆錯。good lucky
總結:
主要是卡在eslint.options上,沒看vscode的extensions的eslint的README,而去相信了百度,沒有添加eslint.options,那么一直都無法生效.學的教訓。
到此這篇關于vscode代碼格式化和eslint的使用的文章就介紹到這了,更多相關vscode代碼格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Typora?1.4.8激活?2022最新Typora破解激活使用教程
Typora?for?mac?是一款簡潔的markdown編輯器、寫作軟件。它去除了預覽窗口,模式切換器,語法符號以及所有其他不必要的干擾,今天給大家分享Typora?1.4.8?最新Typora破解激活使用教程,感興趣的朋友跟隨小編一起看看吧2023-01-01

