Gunakan PostToolUse Hooks untuk memicu lint otomatis setelah Claude menulis file. Exit code 2 mengirim error kembali ke Claude untuk perbaikan otomatis.
Claude Code menulis kode dengan cepat, tapi tidak selalu ingat menjalankan lint. Anda minta dia "sekalian periksa setelah menulis", kadang dilakukan, kadang terlewat. Hooks menyelesaikan masalah ini — memindahkan tanggung jawab pemeriksaan kode dari Claude ke sistem.
Artikel ini fokus pada satu hal: menggunakan PostToolUse Hooks untuk memicu pemeriksaan otomatis setelah Claude menulis kode, sehingga setiap perubahan file memenuhi standar kode Anda.
Logika Hook pemeriksaan kode sederhana:
Write atau Edit untuk menulis fileExit code 2 adalah kuncinya — membuat Claude menerima error dan memperbaiki otomatis, membentuk siklus "tulis→periksa→perbaiki".
{
"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'"
}
]
}
]
}
}
Pindahkan logika pemeriksaan ke script terpisah agar lebih mudah dikelola.
Buat .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
Referensikan script di settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [{ "type": "command", "command": "bash .claude/hooks/lint.sh" }]
}
]
}
}
Exit code 2 + stderr adalah kunci perbaikan otomatis 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
Setelah dikonfigurasi, alur kerja menjadi:
Writerubocop -A — menemukan 3 masalahAnda tidak perlu menjalankan perintah pemeriksaan secara manual.
Global (~/.claude/settings.json): Cocok saat menggunakan aturan yang sama di semua proyek.
Level proyek (.claude/settings.json di-commit ke git): Direkomendasikan untuk tim. Siapa pun yang membuka repositori langsung mendapat konfigurasi yang benar.
Keduanya bisa berdampingan dan digabungkan saat dijalankan.
Pemeriksaan kode otomatis dengan Hooks bermuara pada tiga langkah:
PostToolUse + matcher "Write|Edit" untuk menangkap penulisan fileMulai dari satu bahasa. Setelah berjalan, tambahkan yang lain. Commit script ke .claude/hooks/ untuk dibagikan ke tim.