首页 > 解决方案 > 反应原生 eslint 预提交不起作用

问题描述

我想在提交之前强制执行 eslint 规则,并且我已经在 react-native package.js 文件中配置了预提交,但它在提交时没有显示 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-nativeeslinthusky

解决方案


你跑了husky install吗?

您还可以尝试将名为“pre-commit”的文件添加到 .husky 文件夹,其内容如下:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged

推荐阅读