ESLint學習(四)如何在提交時檢查代碼


前言

項目中在提交時如果有eslint相關的問題會報錯,那么是如何使用eslint在提交時檢查代碼的呢?

解決

假設已經安裝好了eslint並且完成了配置文件的配置,萬事俱備只欠東風了

1、在項目中新建eslint.sh腳本(shell腳本) 

filesCheckedByEslint=`git diff-index --cached HEAD --name-only --diff-filter ACMR | grep -v mockData | grep -v dep | egrep '(.js|.vue|.jsx|.ts)$'`
filesCheckedByStylelint=`git diff-index --cached HEAD --name-only --diff-filter ACMR | grep -v mockData | grep -v dep | egrep '(.vue|.less)$'`

if [ "$filesCheckedByEslint" ];then
    ./node_modules/eslint/bin/eslint.js $filesCheckedByEslint
else
    echo 'there is no js files to eslint check!'
fi


if [ "$filesCheckedByStylelint" ];then
    ./node_modules/stylelint/bin/stylelint.js $filesCheckedByStylelint
else
    echo 'there is no less files to eslint check!'
fi

大概意思就是在提交的時候只會檢查我們更改的文件,並對文件篩選,選出 js vue jsx這些文件,然后如果有這些文件就會執行 eslint檢查命令

2、在script中定義eslint命令

3、在package.json文件中添加如圖所示的字段

commit前會做eslint檢查,如報錯可按以下操作

eslint --fix ./src/union_monthly_list/App.vue//修復報錯文件,建議

或

git commit -anm update //忽略eslint,不建議

 


免責聲明!

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



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