node.js - husky+lint-staged 在未暂存的文件上运行

标签 node.js reactjs eslint githooks

我正在尝试添加一个预提交 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/

相关文章:

angular - 升级到 EsLint : Error with angular. json(原理图:convert-tslint-to-eslint)

node.js - npm audit 在我升级包后提示漏洞

node.js - 梯形校正 : Pass mongoose connection into Keystone options

javascript - React/react-router 重定向到一组 URL?

reactjs - 在 TypeScript 中合并时如何组合两种类型?

eslint 返回错误但 Github 操作仍然成功

javascript - React JS - ESLint - 忽略具有 UNSAFE_componentWillMount、UNSAFE_componentWillUpdate 和 UNSAFE_componentWillReceiveProps 的组件的规则

node.js - Firestore 无法与 AWS Lambda 配合使用

javascript - 获取nodejs中的路径

javascript - 如何在 React.js 中渲染页面之前等待 fetch 完成