Created
July 3, 2025 12:33
-
-
Save UncleBill/77e36a7346b766ad1503ce982dbb0732 to your computer and use it in GitHub Desktop.
.git/hooks/pre-commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # .git/hooks/pre-commit | |
| forbidJsx() | |
| { | |
| # check the input diff text with ripgrep, if it contains `.jsx` or `.tsx`, finish the script | |
| # otherwise, the script will continue to the next step. | |
| local diff_text="$1" | |
| if echo "$diff_text" | rg -q "^\+.*from '.*\w\.(j|t)sx'"; then | |
| echo "Disallowed imports found:" >&2 | |
| echo "$diff_text" | rg "^\+.*from '.*\w\.(j|t)sx'" >&2 | |
| echo -e "\n\033[41mError: importing with .jsx and .tsx extensions are not allowed.\033[0m" >&2 | |
| echo "Please remove the file extensions from the import statements." >&2 | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| # Get the diff of staged files for relevant file types | |
| STAGED_DIFF=$(git diff --cached -- '*.js' '*.jsx' '*.ts' '*.tsx') | |
| # If there are no relevant staged changes, exit successfully | |
| if [ -z "$STAGED_DIFF" ]; then | |
| exit 0 | |
| fi | |
| # Run the check | |
| forbidJsx "$STAGED_DIFF" | |
| exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment