Enforce Code Style on Claude Code Using Hooks

abemedia · reddit · 2026-08-29

The author introduces a method to enforce code style in Claude Code using hooks instead of relying on CLAUDE.md advice. By using a Stop hook that runs linters/formatters and feeding back errors via stderr (exit code 2), Claude automatically fixes its own code before showing the diff.

The author uses stagelint, a file-routing tool, to map file globs to specific commands (Prettier, ESLint, Ruff, etc.). Configuration involves setting up .stagelint.yml for rules and adding a hook command in .claude/settings. targeting unstaged changes.

While PostToolUse hooks offer faster feedback, the Stop hook is preferred to avoid stale states and redundant full-project checks like tsc.

Original post →

More from coding & agent

coding & agent channel →