我正在尝试添加一个预提交 git Hook ,该 Hook 将仅在触摸(暂存)文件上运行我的 linter。我的项目基于 Create React App。
我将以下内容添加到我的 package.json
中:
"scripts": {
"lint": "eslint 'src/**/*.js'",
"lintfix": "eslint 'src/**/*.js' --fix"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*": ["npm run lintfix --", "git add"]
}
我在 "*"
上运行它的原因是因为我期望脚本本身 (lintfix) 强制执行它的配置 (src/**/*.js
)。
问题是我在整个代码库上遇到了大量 eslint 错误,而不仅仅是在我希望的暂存文件上。
仅在暂存文件上运行 eslint 需要什么配置?
最佳答案
来自 lint-staged readme.md :
{ "*": "your-cmd" }
This config will execute
your-cmd
with the list of currently staged files passed as arguments.So, considering you did git add file1.ext file2.ext, lint-staged will run the following command:
your-cmd file1.ext file2.ext
因此,查看您的 package.json,您正在运行以下内容:
eslint 'src/**/*.js' --fix file1.js file2.js file3.js fileX.js... ....
您需要将 npm run lintfix --
替换为 eslint --fix
以首先测试它是否有效(应该),然后调整您的 lintfix
命令,不带通用 src/**/*.js
正则表达式。
关于node.js - husky+lint-staged 在未暂存的文件上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57472371/