Claude Code Hook Writer: Auto-Run Lint, Typecheck or Tests After Edits

Produces a PostToolUse hook entry for settings.json, the script it calls and a concrete procedure to test that the check runs after file edits.

You get: Working code with an explanation

More like this

Ready to use: no blanks to fill in.

The prompt

claude-code-hook-writer-auto-run-lint-typecheck-or-tests-after-edits.md
You are a Claude Code configuration engineer. First ask me two short questions: which check I want to run automatically (lint, typecheck or tests), and whether it should run on the edited file or the whole project. Inspect the repo for the existing command, package manager and file types. Then write the hook. Rules: 1) Use a PostToolUse hook in .claude/settings.json with a matcher for Edit|Write|MultiEdit, and call the script through $CLAUDE_PROJECT_DIR. 2) The script reads the hook JSON from stdin, extracts the file path, skips files the check does not apply to, runs the repo's real command, prints failures to stderr and exits with code 2 so Claude sees them; it exits 0 on success. 3) Keep it fast, with a timeout, and never block on unrelated files. 4) Do not overwrite existing hooks; merge into them. Output: assumptions, the settings.json snippet, the script in a code block with its path and chmod command, a test procedure with one passing and one deliberately failing edit and the expected output, then risks and next actions.

Example output

AI-written example
# ASSUMPTIONS (from repo inspection)
# - Check: ESLint, run on the edited file only
# - Package manager: pnpm (pnpm-lock.yaml); command: pnpm exec eslint
# - File types: .ts .tsx .js .jsx .mjs .cjs; everything else is skipped
# - jq is installed; the existing PostToolUse Prettier hook is kept
// .claude/settings.json (merged; the existing Prettier hook is untouched)
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write|MultiEdit",
        "hooks": [
          {
            "type": "command",
            "command": "pnpm exec prettier --write \"$(jq -r '.tool_input.file_path')\""
          },
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/lint-edited-file.sh",
            "timeout": 45
          }
        ]
      }
    ]
  }
}
# .claude/hooks/lint-edited-file.sh
#!/usr/bin/env bash
set -uo pipefail

input=$(cat)
file=$(printf '%s' "$input" | jq -r '.tool_input.file_path // empty')

# Nothing to check
[ -z "$file" ] && exit 0
[ -f "$file" ] || exit 0

# Skip files ESLint does not apply to
case "$file" in
  */node_modules/*|*/dist/*|*/.next/*|*/coverage/*) exit 0 ;;
esac
case "$file" in
  *.ts|*.tsx|*.js|*.jsx|*.mjs|*.cjs) ;;
  *) exit 0 ;;
esac

cd "${CLAUDE_PROJECT_DIR:-.}" || exit 0

# GNU timeout, or gtimeout on macOS (brew install coreutils)
T=$(command -v timeout || command -v gtimeout || true)
run() { if [ -n "$T" ]; then "$T" 30 "$@"; else "$@"; fi; }

output
Generated from this prompt by AI. Your results will vary.

Tips and common edits

Use it when you keep asking Claude Code to run the linter or tests after edits. Answer the two questions and let it merge into your existing .claude/settings.json.

Questions about this prompt

More in Claude Code

Agent Run Post-Mortem: Trace Root Cause and Prompt or Tool Fixes

Analyzes a pasted agent trace to find the first real failure and proposes exact prompt rewrites or tool changes, each with a test.

Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.

Checkpoint and Rollback Manager with Compensation Steps and Failure Injection Tests

Guides Claude Code to implement checkpoint snapshots, rollback modes, and compensating actions for side-effecting agent phases, then prove them with failure-injection tests. Helps you decide the rollback strategy per phase.

Claude Code Behavior-Preserving Refactor with Characterization Tests and Staged Commits

Guides Claude Code to refactor a module safely by first pinning current behavior with characterization tests, then applying small reversible steps with verification after each. Delivers a diff, risk register, and rollback plan.

Claude Code Feature Implementation Plan and Build from a Ticket with Repo Convention Discovery

Has Claude Code investigate how similar features are built in the repository, produce an implementation plan with acceptance tests, then implement it in verified increments. Surfaces risks, open questions, and next actions before and after coding.

Claude Code Plan-Then-Implement a Feature With Approval Checkpoints

Makes Claude Code explore the repo, write a step-by-step plan for your approval, then build the feature in small verified steps, stopping at each checkpoint for your go-ahead.

Claude Code Root-Cause Bug Investigation with Failing Test and Minimal Patch

Directs Claude Code to reproduce a bug, trace it to a root cause using repository evidence, write a failing regression test, and apply the smallest safe fix. Ends with a verified diff, risks, and next actions.

Codebase Tour for a Specific Task

Explores a repo through the lens of your task and explains the architecture, key files and data flow, then says where to start.

Coding Agent Task Brief with Guardrails, Test Plan, and Rollback Steps

Creates a scoped brief for an AI coding agent that limits file changes, defines tests, and includes a rollback plan and review checklist.

Custom Slash Command Builder for Repeated Claude Code Tasks

Turns a task you repeat into a custom slash command file with frontmatter, arguments, restricted allowed tools and a fixed output format.

DAG Scheduler with Fan-Out/Fan-In Barriers and Resumable Run Ledger for Agent Pipelines

Guides Claude Code to build or refactor a dependency-aware DAG scheduler for agent phases, with cycle validation, bounded parallelism, join policies, retries, and an append-only run ledger so interrupted runs resume without redoing finished nodes.

Dependency Graph Planner for a Multi-Agent Software Release Workflow

Turns a list of release tasks into a dependency graph with parallel batches, critical path, shared-resource conflicts, and agent assignments. Supports the decision on what to run now versus wait on.