【发布时间】:2022-01-26 08:10:19
【问题描述】:
我使用 Vue CLI 创建了一个新的 Vue3 应用程序,并为我的 linter 配置选择了 Prettier。我想使用 commitlint、husky 和 lint-staged 来验证提交消息并在推送之前对代码进行 lint。
我做了什么
基于https://commitlint.js.org/#/guides-local-setup我用哈士奇设置commitlint
npm install --save-dev @commitlint/{cli,config-conventional}
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
npm install husky --save-dev
npx husky install
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit $1'
基于https://github.com/okonet/lint-staged#installation-and-setup我设置了lint-staged
npx mrm@2 lint-staged
在 package.json 中我替换
"lint-staged": {
"*.js": "eslint --cache --fix"
}
与
"lint-staged": {
"*": "npm run lint"
}
问题
将项目中的README.md文件修改为
# my-repo
---
new commit
并尝试提交我收到以下错误消息
> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *
[STARTED] npm run lint
[FAILED] npm run lint [FAILED]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...
✖ npm run lint:
> my-repo@0.1.0 lint
> vue-cli-service lint "/home/.../my-repo/README.md"
error: Parsing error: Invalid character at README.md:1:1:
> 1 | # my-repo
| ^
2 |
3 | ---
4 |
1 error found.
npm ERR! code 1
npm ERR! path /home/my-repo
npm ERR! command failed
npm ERR! command sh -c lint-staged
npm ERR! A complete log of this run can be found in:
npm ERR! /home/.../.npm/_logs/2021-12-27T10_07_27_498Z-debug.log
husky - pre-commit hook exited with code 1 (error)
应该怎么做
仅修复已修改的文件。 linter 知道它能够修复的文件(js、ts、vue、html、...)。
当有一个修改过的降价文件时,我在打开终端并运行npm run lint 时没有收到任何错误。但是在此设置中使用 lint-staged 时确实会出错"*": "npm run lint"
对于 lint-staged 仅对“lintable”文件进行 lint 的正确设置是什么?
【问题讨论】:
-
README.md 不需要 linted。忽略 .eslintignore 中的 README.md 文件
-
@AhmadKZX 我认为应该有一个更通用的解决方案,因为通常
npm run lint不会尝试修复降价文件,因此问题可能出在其他地方 -
是的,你为什么在
.md文件上使用 ESlint?将特定的 linter 应用于支持的文件。 -
@kissu 我更新了我问题的最后一部分,也许这有帮助
-
你的
npm run lint实际上是做什么的?我很确定它根本没有执行,因此你没有任何错误。
标签: javascript vue.js eslint prettier lint-staged