Files
KomediruzeckiandGitHub bfe886e7b4 fix(hooks): lint-fix staged frontend files with the frontend ESLint config (#561)
The pre-commit hook ran `pnpm exec eslint --fix` from the repo root on every
staged JS/TS file. ESLint 10 looks up the config from each file's directory,
so a frontend-only batch was linted, but one worker/, shared/, packages/ or
scripts/ path in the batch has no eslint.config.* above it: ESLint aborts the
whole run ("couldn't find an eslint.config.* file", exit 2) before fixing
anything, frontend files included. `|| true` hid that and the hook printed ok.

Run ESLint from inside frontend/ on the frontend paths only, prefix stripped,
the same run `pnpm lint` does in pre-push and CI. Files outside frontend/ are
still formatted by prettier, never handed to ESLint. --no-warn-ignored keeps
config-ignored files (vite.config.ts, public/**) quiet. A non-zero ESLint exit
is now reported instead of swallowed; it still does not block the commit.

Regressed in ff428deb (pnpm migration), which replaced the per-package run
added in 928befec with a root-level one.
2026-09-27 01:03:41 +02:00

52 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
# Pre-commit auto-fixer - formats and lint-fixes the STAGED files so every commit
# lands already clean, matching the static checks the pre-push / CI enforce.
#
# Only staged files are touched and re-staged; unstaged work and unrelated files
# are never modified. Formatting failures block the commit; residual (unfixable)
# lint errors do NOT - the pre-push hook is the hard gate, so commits stay quick.
#
# Installed automatically via the root package.json "prepare" script
# (git config core.hooksPath .githooks). Bypass in a pinch with:
# git commit --no-verify
#
# Caveat: a partially-staged file (only some hunks staged) is re-staged whole, so
# its unstaged hunks get folded into the commit. Use --no-verify for those.
set -e
# Staged, still-present files (Added/Copied/Modified/Renamed).
staged="$(git diff --cached --name-only --diff-filter=ACMR)"
[ -z "$staged" ] && exit 0
# Prettier handles these; ESLint only the JS/TS family. (No svg — Prettier has
# no SVG parser and errors when handed an explicit .svg path.)
fmt_files="$(printf '%s\n' "$staged" | grep -Ei '\.(ts|tsx|js|jsx|cjs|mjs|css|json|jsonc|md|html)$' || true)"
lint_files="$(printf '%s\n' "$staged" | grep -Ei '\.(ts|tsx|js|jsx|cjs|mjs)$' || true)"
if [ -n "$fmt_files" ]; then
echo "pre-commit: prettier --write"
printf '%s\n' "$fmt_files" | xargs pnpm exec prettier --write --log-level warn
fi
# ESLint runs from inside frontend/ on its own paths, so frontend/eslint.config.js
# applies - the same run as `pnpm lint` in pre-push and CI. No other package has an
# ESLint config: a worker/, shared/ or packages/ path in the list made ESLint abort
# with "couldn't find an eslint.config.* file" before fixing anything, frontend
# files included. Those files are still formatted above, just never linted.
frontend_lint="$(printf '%s\n' "$lint_files" | sed -n 's|^frontend/||p')"
if [ -n "$frontend_lint" ]; then
echo "pre-commit: eslint --fix (frontend)"
# Don't abort the commit on unfixable lint - pre-push enforces it before CI.
# --no-warn-ignored: a staged file the config ignores (vite.config.ts, public/**)
# is skipped silently, as `pnpm lint` skips it.
printf '%s\n' "$frontend_lint" | xargs pnpm -C frontend exec eslint --fix --no-warn-ignored ||
echo "pre-commit: eslint exited non-zero (output above) - not blocking; pre-push runs pnpm lint"
fi
# Re-stage only the files we just touched (-r: skip git add when none matched,
# e.g. an extension-less file like a hook was the only thing staged).
printf '%s\n' "$fmt_files" "$lint_files" | grep -v '^$' | sort -u | xargs -r git add
echo "pre-commit: ok"