Skip to content

task: Add Claude Code auto-format hook#311

Merged
dcalhoun merged 3 commits intotrunkfrom
task/claude-config
Feb 6, 2026
Merged

task: Add Claude Code auto-format hook#311
dcalhoun merged 3 commits intotrunkfrom
task/claude-config

Conversation

@dcalhoun
Copy link
Copy Markdown
Member

@dcalhoun dcalhoun commented Feb 6, 2026

What?

Adds a .claude/settings.json file with a PostToolUse hook that automatically runs Prettier on files after Claude Code edits them. Also simplifies the CLAUDE.md pre-commit checklist since formatting is now handled automatically.

Why?

The project's CLAUDE.md pre-commit checklist requires running make format (Prettier) and make lint-js-fix (ESLint) before every commit. Without automation, formatting drift can accumulate during a Claude Code session, leading to noisy diffs and extra manual steps. This hook ensures every file Claude edits is immediately formatted, keeping code consistent without manual intervention.

How?

  • .claude/settings.json: Adds a PostToolUse hook that triggers after every Edit or Write tool call. The hook extracts the edited file path from the tool input via jq and runs ./node_modules/.bin/prettier --write on that single file. Based on Claude examples.
  • CLAUDE.md: Removes make format and make lint-js from the pre-commit checklist since formatting is now automatic. Only make lint-js-fix remains (ESLint is too slow for per-edit hooks and can make semantic changes).

Design decisions

  • Local binary (./node_modules/.bin/prettier) instead of npx — avoids any risk of downloading packages from the internet.
  • Prettier only, not ESLint — Prettier on a single file is near-instant (<100 ms). ESLint with the WordPress plugin takes 1–3 s per file and can make semantic changes (e.g., removing imports).
  • Graceful degradation — PostToolUse hooks are non-blocking. If jq is missing or node_modules isn't installed, Prettier fails silently and Claude continues. Prettier also respects .prettierignore.

Testing Instructions

Not important that we test these scripting changes, but here are some ideas:

  1. Start a new Claude Code session in the project.
  2. Ask Claude to edit a JS/JSX file — confirm the file is auto-formatted after the edit.
  3. Edit a file in an ignored directory (e.g., ios/) — confirm Prettier skips it gracefully.
  4. Run make lint-js to confirm no formatting regressions.

Based on https://code.claude.com/docs/en/hooks-guide#auto-format-code-after-edits

Opted to avoid the example's use of `npx` to avoid internet usage. The
formatting fails silently if the `prettier` package is not installed.
This is now completed by Claude hooks.
The lint fix script also verifies lint rules. There is no need to run
both scripts.
@dcalhoun dcalhoun added the [Type] Build Tooling Issues or PRs related to build tooling label Feb 6, 2026
Comment thread CLAUDE.md
Comment on lines -204 to -205
# Format JavaScript code
make format
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed as redundant of new formatting hook.

Comment thread CLAUDE.md
Comment on lines -210 to -211
# Verify linting passes
make lint-js
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed as redundant of make lint-js-fix above.

@dcalhoun dcalhoun requested a review from nbradbury February 6, 2026 15:28
@dcalhoun dcalhoun marked this pull request as ready for review February 6, 2026 15:28
@dcalhoun dcalhoun enabled auto-merge (squash) February 6, 2026 15:29
@nbradbury nbradbury self-assigned this Feb 6, 2026
Copy link
Copy Markdown
Contributor

@nbradbury nbradbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! :shipit:

@dcalhoun dcalhoun merged commit 5b66f8e into trunk Feb 6, 2026
12 checks passed
@dcalhoun dcalhoun deleted the task/claude-config branch February 6, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Build Tooling Issues or PRs related to build tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants