Format Claude Code edits with a hook
Use a Claude Code PostToolUse hook to run Biome format and lint on the file an Edit or Write call changed.
You ask Claude Code to change a function. It edits the file, moves to the next task, and leaves the formatter for you. If the project already uses Biome, run it from a hook on the file Claude changed.
Match Edit and Write
Put this in .claude/settings.json at the repository root. If the file exists, add the matcher alongside its other PostToolUse entries.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "bash \"$CLAUDE_PROJECT_DIR/.claude/format-edit.sh\""
}
]
}
]
}
}After a successful Edit or Write tool call, Claude Code starts the script and sends a JSON event on standard input. The event contains the tool input under tool_input. The hooks reference lists the fields.
The matcher names tools. An edit made through a Bash command triggers a Bash event, so keep your usual CI check for those changes and for edits people make outside Claude Code.
Format the file from the hook input
Save this in .claude/format-edit.sh. The script expects jq, pnpm and a project that has Biome installed.
#!/usr/bin/env bash
set -euo pipefail
file=$(jq -r '.tool_input.file_path // empty')
case "$file" in
*.js|*.jsx|*.ts|*.tsx) ;;
*) exit 0 ;;
esac
cd "$CLAUDE_PROJECT_DIR"
pnpm exec biome format --write -- "$file"
pnpm exec biome lint -- "$file"A JavaScript or TypeScript edit gets formatted first, then checked for lint errors. The quotes keep a path such as src/order notes.ts in one argument. The script uses the project root where the session started. If Claude switches to a worktree during the session, use the event’s cwd to resolve that worktree’s Biome installation.
Print a test event into the script before relying on it in a session. Create example.ts in the root with export const item={id:1} on one line and run this from that directory.
jq -n --arg file "$PWD/example.ts" '{tool_input:{file_path:$file}}' |
CLAUDE_PROJECT_DIR="$PWD" bash .claude/format-edit.shBiome reports one file formatted and one checked. Reopen the file to see the spaces around the assignment and object properties. If the hook never fires, check that the matcher is under PostToolUse and that the tool in the transcript was Edit or Write.
Use the formatter's write mode and leave lint fixes for review. A lint recommendation can change the behavior of a function, and the hook runs after each matching tool call. Biome reports lint errors without rewriting the file. The Edit or Write has already happened by the time the hook runs.