vscode代碼格式化和eslint的使用
簡介
今天看著寫的代碼越來越多后,發(fā)現(xiàn)自己讀起都有點(diǎn)吃力了,哈哈,自己看著眼睛痛,就準(zhǔn)備整頓一下,畢竟這個(gè)項(xiàng)目還要維護(hù)很久的,找解決方案和測試解決方案就用了一個(gè)半小時(shí),嚴(yán)重開始懷疑自己的智商了。下面的目標(biāo)讓代碼看起來很公正
代碼編輯器
vscode
version:版本 1.35.1 (1.35.1) 2019-06-12T14:19:05.197Z更新的
vscode代碼格式化
因?yàn)槟壳肮揪臀乙粋€(gè)后端,項(xiàng)目也不大,所以就采用這種方案,簡單快捷粗暴。
一.點(diǎn)擊code->preferences->settings 點(diǎn)擊右上角{}
二.用戶自定義設(shè)置(/User/settings.json)
添加代碼
"editor.formatOnType": true, "editor.formatOnSave": true
ESLint配置
ESLint不僅有代碼規(guī)范而且還有一部分語法檢查的功能,ex:命令規(guī)范(駝峰) a==b警告提示a===b...
ESLint可以有效的規(guī)范代碼,以后還是會(huì)采用,培養(yǎng)自己的規(guī)范的編碼習(xí)慣
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
項(xiàng)目目錄下將會(huì)生成一個(gè)eslintrc.json的文件
{
"extends": "airbnb-base"
}
添加自己想要的設(shè)置,我這里node環(huán)境
{"env": {
"node": true,
"es6": true
},
"parserOptions": {#解決import export eslint報(bào)錯(cuò)
"ecmaFeatures": {
"legacyDecorators": true
}
},
"extends": "airbnb-base"
}
4.關(guān)聯(lián)eslint與vscode
1.code->preferences->settings 進(jìn)入user的seetings
2.添加以下代碼
"eslint.autoFixOnSave": true,//保存自動(dòng)修復(fù)eslint錯(cuò)誤
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "vue",
"autoFix": true
}
],
"eslint.options": {//指定eslint配置文件位置i
"configFile": ".eslintrc.json" //指定項(xiàng)目根目錄中的eslint配置文件
}
這樣vscode和eslint關(guān)聯(lián)配置完成了,不出意外會(huì)報(bào)一大堆錯(cuò)。good lucky
總結(jié):
主要是卡在eslint.options上,沒看vscode的extensions的eslint的README,而去相信了百度,沒有添加eslint.options,那么一直都無法生效.學(xué)的教訓(xùn)。
到此這篇關(guān)于vscode代碼格式化和eslint的使用的文章就介紹到這了,更多相關(guān)vscode代碼格式化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Git安裝詳細(xì)圖文教程(Git?安裝過程的每一個(gè)步驟)
這篇文章主要介紹了Git安裝詳細(xì)圖文教程(Git?安裝過程的每一個(gè)步驟),本文以Git-2.35.1.2-64-bit.exe為例給大家講解安裝過程,需要的朋友可以參考下2023-02-02
Typora?1.4.8激活?2022最新Typora破解激活使用教程
Typora?for?mac?是一款簡潔的markdown編輯器、寫作軟件。它去除了預(yù)覽窗口,模式切換器,語法符號(hào)以及所有其他不必要的干擾,今天給大家分享Typora?1.4.8?最新Typora破解激活使用教程,感興趣的朋友跟隨小編一起看看吧2023-01-01
使用idea 去除 html 代碼前的行號(hào)和空行的方法詳解
這篇文章主要介紹了使用idea 去除 html 代碼前的行號(hào)和空行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
SpringSecurity框架簡介及與shiro特點(diǎn)對(duì)比
這篇文章是SpringSecurity框架的入門篇,主要為大家介紹了SpringSecurity框架簡介及與shiro優(yōu)缺點(diǎn)對(duì)比有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03

