Skip to content

Instantly share code, notes, and snippets.

@UncleBill
Created July 3, 2025 12:33
Show Gist options
  • Select an option

  • Save UncleBill/77e36a7346b766ad1503ce982dbb0732 to your computer and use it in GitHub Desktop.

Select an option

Save UncleBill/77e36a7346b766ad1503ce982dbb0732 to your computer and use it in GitHub Desktop.
.git/hooks/pre-commit
#!/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