Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Claude PostToolUse hook for code formatting#3461

Merged
ewels merged 1 commit into
mainfrom
claude/add-posttooluse-formatting-hook-4e4Ns
Jan 8, 2026
Merged

Add Claude PostToolUse hook for code formatting#3461
ewels merged 1 commit into
mainfrom
claude/add-posttooluse-formatting-hook-4e4Ns

Conversation

@ewels

@ewels ewels commented Jan 8, 2026

Copy link
Copy Markdown
Member

Adds a Claude Code hook that runs ruff-check, ruff-format, and prettier via pre-commit after Write or Edit operations to ensure consistent code formatting across Python, JS, JSON, YAML, and Jinja templates.

Based on this now-famous X thread.

@claude

claude Bot commented Jan 8, 2026

Copy link
Copy Markdown

Review Summary

Good addition for automating code formatting! The hook successfully integrates with pre-commit to ensure consistent code style. However, there are a few potential improvements to consider for robustness and user experience.

Detailed Review

Code Quality & Best Practices ✅

Positive:

  • Clean JSON structure following Claude Code settings format
  • Appropriate use of PostToolUse hook for automatic formatting
  • Correctly targets Write and Edit operations where formatting is needed
  • Uses existing pre-commit infrastructure rather than duplicating logic

Suggestions:

  1. Quoted file path handling: The command uses "$CLAUDE_FILE_PATH" which is good for handling spaces, but consider that file paths might contain single quotes or other special characters. The current approach should work for most cases.

  2. Hook matcher specificity: The matcher "Write|Edit" will trigger for all Write/Edit operations. Consider if you want to exclude certain file types (e.g., binary files, images) though pre-commit should handle this gracefully.

Potential Issues 🔍

Minor concerns:

  1. Silent failures with || true: While this prevents blocking Claude Code on formatting failures, it means users won't be notified if:

    • pre-commit is not installed
    • The hooks are not set up (pre-commit install)
    • There are actual syntax errors that ruff cannot fix

    Recommendation: Consider adding a notification mechanism or at least logging when formatting fails.

  2. Multiple sequential commands: Running three separate pre-commit commands could be slow for large files. Each pre-commit invocation has startup overhead.

    Alternative approach:

    pre-commit run --files "$CLAUDE_FILE_PATH" || true

    This would run all applicable hooks for the file in one go.

  3. Missing documentation: Consider adding a comment in the settings file explaining what the hook does and how to disable it if needed:

    {
      "_comment": "Auto-format code on Write/Edit operations using pre-commit hooks",
      "hooks": { ... }
    }

Performance Considerations ⚡

  • Overhead: Running pre-commit after every Write/Edit will add latency. For single-file operations this should be acceptable (< 1-2 seconds), but it could accumulate during refactoring sessions.
  • Pre-commit caching: Pre-commit caches its environments, so subsequent runs should be faster.
  • Recommendation: Monitor user feedback on whether the formatting delay becomes noticeable during interactive sessions.

Security Concerns 🔒

No significant security issues identified. The hook:

  • Uses environment variables provided by Claude Code ($CLAUDE_FILE_PATH)
  • Runs local tools (pre-commit) that users should already have installed
  • Uses || true to prevent command injection from failing commands

Test Coverage 🧪

Limitations:

  • This is a configuration file, so traditional unit tests don't apply
  • Manual testing would involve:
    1. Creating/editing Python files and verifying ruff runs
    2. Creating/editing JSON/YAML files and verifying prettier runs
    3. Confirming || true prevents blocking on errors
    4. Testing with files containing special characters in paths

Recommendation: Add a note in CLAUDE.md or README about the existence of this hook and how developers can verify it's working.

Additional Suggestions 💡

  1. Consider adding a user preference: Some users might want to disable auto-formatting. Could add a comment showing how to disable:

    // To disable auto-formatting, remove this file or set "hooks": {}
  2. File type filtering: You could add file extension checks to avoid running formatters on files they can't handle:

    case "$CLAUDE_FILE_PATH" in *.py) pre-commit run ruff-check --files "$CLAUDE_FILE_PATH" || true; pre-commit run ruff-format --files "$CLAUDE_FILE_PATH" || true;; *.json|*.yaml|*.yml) pre-commit run prettier --files "$CLAUDE_FILE_PATH" || true;; esac

    Though pre-commit already handles this internally, so this may be redundant.

  3. Alignment with CLAUDE.md: The CLAUDE.md file mentions using ruff and pre-commit hooks. This PR aligns perfectly with that guidance by automating the process.

Verdict ✅

Approve with minor suggestions. This is a useful quality-of-life improvement that will help maintain code consistency when working with Claude Code. The implementation is sound, though the suggestions above could enhance robustness and user experience.

The referenced X thread context about AI code generation and formatting is very relevant - automating formatting ensures Claude's outputs consistently match project standards without manual intervention.

@ewels ewels force-pushed the claude/add-posttooluse-formatting-hook-4e4Ns branch 4 times, most recently from a67d357 to b3f4ae9 Compare January 8, 2026 20:00
Adds a Claude Code hook that runs pre-commit after Write or Edit
operations to ensure consistent code formatting.
@ewels ewels force-pushed the claude/add-posttooluse-formatting-hook-4e4Ns branch from b3f4ae9 to 021b91e Compare January 8, 2026 20:01
@ewels ewels merged commit c933090 into main Jan 8, 2026
12 checks passed
@ewels ewels deleted the claude/add-posttooluse-formatting-hook-4e4Ns branch January 8, 2026 20:05
@ewels ewels added this to the v1.34 milestone Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants