[Claude Code Intro E29] PostToolUse Hook: 5 Patterns for Auto-format, Lint, & Tests

Опубликовано: 29 Май 2026
на канале: New beginning (neosarchizo)
184
1

If we skimmed the overall Hooks structure in chapter 28, from chapter 29 we look deeply at events one by one. The first is the most frequently used PostToolUse — a post-processing event that enforces Prettier, ESLint, and tests at the system level right after Edit/Write succeeds.

📌 Topics Covered
Trigger condition: Runs only *right after success*, failures are separated into PostToolUseFailure. Since it's already done, it can't prevent tool execution, but it can control the next model call.
Input JSON: Extracting tool_name, tool_input, tool_output, tool_use_id with jq. Why you should use jq -r '.tool_input.file_path' instead of $CLAUDE_FILE_PATH.
Two response paths: Blocking the next turn with decision:block + reason vs. injecting facts with hookSpecificOutput.additionalContext. The trap that exit 2 in PostToolUse is non-blocking.
Narrowing matchers in 3 steps: matcher (tool name) → if field (argument pattern) → branching in script.
5 practical patterns: Prettier auto-format / ESLint --fix and block on failure / jest --findRelatedTests for related tests only / injecting git branch and diff via additionalContext / accumulating all edits into JSONL audit logs.
Common pitfalls: 10KB output cap, no re-execution on resume, 600s default timeout, injecting context factually rather than imperatively.

📌 Environment Info
Claude Code latest version (Hooks system, PostToolUse event support)
.claude/settings.json (team shared), .claude/hooks/*.sh (script files), $CLAUDE_PROJECT_DIR environment variable
External tools: jq, Prettier, ESLint, Jest --findRelatedTests

📌 Chapters
0:00 What is PostToolUse
0:40 Trigger condition and PostToolUseFailure separation
1:30 Input JSON and extracting fields with jq
2:30 Two response paths: decision block and additionalContext
3:50 3 steps to narrow matchers
4:40 Pattern 1: Prettier + ESLint auto-format
6:00 Pattern 2: Block next turn on lint failure
7:00 Pattern 3: Auto-run related tests only
8:00 Pattern 4: Inject facts with additionalContext
8:40 Pattern 5: Audit logging (JSONL/HTTP)
9:10 Common pitfalls and summary

📌 Reference Links
Blog: https://neosarchizo.github.io/posts/c...
Hooks official docs: https://code.claude.com/docs/en/hooks
Settings official docs: https://code.claude.com/docs/en/settings
CLI reference: https://code.claude.com/docs/en/cli-r...
jq manual: https://jqlang.org/manual/
Prettier CLI: https://prettier.io/docs/en/cli
ESLint CLI: https://eslint.org/docs/latest/use/co...

#ClaudeCode #PostToolUse #Automation