PostToolUse Hooks รัน lint อัตโนมัติหลัง Claude เขียนไฟล์ exit code 2 ส่ง error กลับให้ Claude แก้ไขอัตโนมัติ
Claude Code เขียนโค้ดได้รวดเร็ว แต่ไม่ได้จำเสมอไปว่าต้องรัน lint คุณขอให้มัน "ตรวจสอบระหว่างเขียนด้วย" บางครั้งก็ทำ บางครั้งก็ข้ามไป Hooks แก้ปัญหานี้ด้วยการย้ายความรับผิดชอบการตรวจสอบโค้ดจาก Claude ไปยังระบบ
บทความนี้โฟกัสที่สิ่งเดียว: ใช้ PostToolUse Hooks เพื่อเรียกใช้การตรวจสอบโดยอัตโนมัติหลังจาก Claude เขียนโค้ด เพื่อให้ทุกการเปลี่ยนแปลงไฟล์ตรงตามมาตรฐานของคุณ
ลอจิกของ Hook ตรวจสอบโค้ดนั้นง่าย:
Write หรือ Edit เพื่อเขียนไฟล์exit code 2 คือกุญแจสำคัญ — ทำให้ Claude รับ error และแก้ไขอัตโนมัติ สร้างวงจร "เขียน→ตรวจสอบ→แก้ไข"
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "bash -c 'file=$(echo \"$CLAUDE_TOOL_INPUT\" | jq -r .file_path 2>/dev/null); [[ \"$file\" == *.rb ]] && rubocop -A \"$file\" --no-color -q || true'"
}
]
}
]
}
}
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "bash -c 'file=$(echo \"$CLAUDE_TOOL_INPUT\" | jq -r .file_path 2>/dev/null); [[ \"$file\" =~ \\.(js|ts|jsx|tsx)$ ]] && npx eslint --fix \"$file\" || true'"
}
]
}
]
}
}
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "bash -c 'file=$(echo \"$CLAUDE_TOOL_INPUT\" | jq -r .file_path 2>/dev/null); [[ \"$file\" == *.py ]] && ruff check --fix \"$file\" && ruff format \"$file\" || true'"
}
]
}
]
}
}
สร้าง .claude/hooks/lint.sh:
#!/bin/bash
set -e
input=$(cat)
file=$(echo "$input" | jq -r '.tool_input.file_path // empty')
[[ -z "$file" ]] && exit 0
[[ ! -f "$file" ]] && exit 0
ext="${file##*.}"
case "$ext" in
rb) rubocop -A "$file" --no-color -q ;;
js|ts|jsx|tsx) npx eslint --fix "$file" --quiet ;;
py) ruff check --fix "$file"; ruff format "$file" ;;
go) gofmt -w "$file"; golangci-lint run "$file" 2>&1 ;;
*) exit 0 ;;
esac
อ้างอิง script ใน settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [{ "type": "command", "command": "bash .claude/hooks/lint.sh" }]
}
]
}
}
exit code 2 + stderr คือกุญแจการแก้ไขอัตโนมัติของ Claude:
run_lint() {
local output exit_code
output=$(rubocop "$file" --no-color 2>&1)
exit_code=$?
if [[ $exit_code -ne 0 ]]; then
echo "$output" >&2
exit 2
fi
rubocop -A "$file" --no-color -q
}
skip_patterns=("vendor/" "node_modules/" "db/schema.rb" ".min.js" "_test.go")
for pattern in "${skip_patterns[@]}"; do
[[ "$file" == *"$pattern"* ]] && exit 0
done
หลังตั้งค่า workflow จะเป็น:
Writerubocop -A อัตโนมัติ — พบ 3 ปัญหาคุณไม่ต้องรันคำสั่งตรวจสอบด้วยตนเองเลย
Global (~/.claude/settings.json): เหมาะเมื่อใช้กฎเดียวกันในทุกโปรเจ็กต์
ระดับโปรเจ็กต์ (.claude/settings.json commit ลง git): แนะนำสำหรับทีม ใครก็ตามที่เปิด repo จะได้รับการตั้งค่าที่ถูกต้องโดยอัตโนมัติ
ทั้งสองอยู่ร่วมกันได้ hooks ระดับโปรเจ็กต์จะรวมกับ global และทำงานทั้งคู่
การตรวจสอบโค้ดอัตโนมัติด้วย Hooks สรุปได้เป็น 3 ขั้นตอน:
PostToolUse + matcher "Write|Edit" เพื่อดักจับการเขียนไฟล์เริ่มจากภาษาเดียว เมื่อทำงานได้แล้วค่อยเพิ่มภาษาอื่น commit script ลง .claude/hooks/ เพื่อแชร์กับทีม