反应原生 eslint 预提交不起作用

Posted

技术标签:

【中文标题】反应原生 eslint 预提交不起作用【英文标题】:react native eslint pre-commit does not work 【发布时间】: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"
    
  

【问题讨论】:

【参考方案1】:

你跑husky install了吗?

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

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

npx lint-staged

【讨论】:

是的,我做到了,缺少的是预提交文件中的命令

以上是关于反应原生 eslint 预提交不起作用的主要内容,如果未能解决你的问题,请参考以下文章

为啥安装反应原生导航模块后应用程序不起作用?

onFocus 和 onBlur 不起作用,反应原生

反应原生嵌入视频不起作用

TouchableOpacity 和按钮在反应原生模态中不起作用?

反应原生 textDecoration 属性在 Android 上不起作用

反应原生儿童视图 justifyContent 属性不起作用