在NodeJS中引入EsLint,并在提交时进行语法检查

1. 安装NPM

1.1 哈士奇

在提交时触发某些操作

 npm uninstall ghooks --save-dev && npm install husky --save-dev

1.2 eslint 可进行中文本地化。

文法检查

 npm install -g eslint --save-dev
 eslint init
    >? How would you like to configure ESLint? Inspect your JavaScript file(s)
    >? Which file(s), path(s), or glob(s) should be examined? ./src/assets/js/
    >? What format do you want your config file to be in? JavaScript
    >? Are you using ECMAScript 6 features? No
    >? Where will your code run? Browser
    >? Do you use CommonJS? Yes
    >? Do you use JSX? No

在package.json中进行脚本调用的设置

  "scripts": {
    "precommit": "npm run test.sh -q || eslint ./src/assets/js/"
  }

3. 确认

eslint ./src/assets/js/
npm run precommit
npm run eslint

4. 在Git提交时检查错误。

git commit
>......
>✖ 1733 problems (1733 errors, 0 warnings)
>
>
> husky - pre-commit hook failed (add --no-verify to bypass)
> husky - to debug, use 'npm run precommit'

5. Rails + yarn + VueJS 版

yarn add eslint
yarn add eslint-config-vue
yarn add ghooks
yarn add husky
 "scripts": {
    "precommit": "npm run test.sh -q || eslint ./src/assets/js/"
  }
yarn run precommit

确认package.json中写的precommit功能在运行中。

广告
将在 10 秒后关闭
bannerAds