【问题标题】:react native eslint pre-commit does not work反应原生 eslint 预提交不起作用
【发布时间】:2021-11-10 17:33:29
【问题描述】:

我想在提交之前强制执行 eslint 规则,并且我已经在 react-native package.js 文件中配置了 pre-commit,但它没有在提交时显示 eslint 错误。如果我运行 yar run lint 工作正常。

下面是我的 Package.js 文件

    {
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "react": "17.0.2",
    "react-native": "0.65.1",
    "storyblok-js-client": "^4.0.9",
    "react-query": "^3.19.1",
    "react-redux": "^7.2.4",
    "graphql": "^15.5.0",
    "graphql-request": "^3.5.0",
    "react-native-typography": "^1.4.1",
    "rn-fetch-blob": "^0.12.0",
    "react-native-webview": "^11.13.0",
    "base-64": "^1.0.0"
  },
  "devDependencies": {
    "@babel/core": "^7.15.5",
    "@babel/runtime": "^7.15.4",
    "@react-native-community/eslint-config": "^3.0.1",
    "@typescript-eslint/eslint-plugin": "^4.31.1",
    "@typescript-eslint/parser": "^4.31.1",
    "babel-jest": "^27.2.0",
    "eslint": "^7.32.0",
    "eslint-plugin-react": "^7.25.1",
    "jest": "^27.2.0",
    "metro-react-native-babel-preset": "^0.66.2",
    "react-native-codegen": "^0.0.7",
    "react-test-renderer": "17.0.2",
    "@testing-library/react-native": "^7.2.0",
    "eslint-plugin-jest": "^24.4.0",
    "fetch-mock": "^9.11.0",
    "husky": "^7.0.2",
    "lint-staged": "^11.1.2",
    "typescript-styled-plugin": "^0.18.1"
  },
  "lint-staged": {
    "src/**/*.{ts,tsx, js, jsx}": [
      "eslint --ext .tsx --ext .ts src/ --fix"
    ],
    "./src/**": [
      "prettier --write ."
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged && pretty-quick --staged"
    },
    "jest": {
      "preset": "react-native"
    }
  }
}

【问题讨论】:

    标签: react-native eslint husky


    【解决方案1】:

    你跑husky install了吗?

    您也可以尝试在 .husky 文件夹中添加一个名为“pre-commit”的文件,内容如下:

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    npx lint-staged
    

    【讨论】:

    • 是的,我做到了,缺少的是预提交文件中的命令
    猜你喜欢
    • 2018-01-02
    • 2019-09-13
    • 1970-01-01
    • 2021-06-23
    • 2021-03-12
    • 2021-01-26
    • 2017-08-09
    • 2017-02-16
    • 1970-01-01
    相关资源
    最近更新 更多