U

Ultimate Pre-commit Code Formatter Inspector

Enterprise-ready hook that automates auto-format code before each commit. Built for Claude Code with best practices and real-world patterns.

HookCommunitycode reviewv1.0.0MIT
0 views0 copies

Ultimate Pre-commit Code Formatter Inspector

Comprehensive multi-language code formatting inspector that validates formatting compliance across your entire codebase before every commit.

When to Use This Hook

Attach this hook when you need to:

  • Enforce consistent code formatting across JavaScript, Python, Go, Rust, and other languages before commits
  • Run multiple formatters (Prettier, Black, gofmt, rustfmt) in a single pre-commit pass
  • Generate detailed formatting compliance reports with diff previews of required changes

Consider alternatives when:

  • Your team uses a single language and a simple per-file formatting hook suffices
  • You prefer post-commit formatting with auto-fix rather than pre-commit blocking

Quick Start

Configuration

name: ultimate-pre-commit-code-formatter-inspector type: hook trigger: PreToolUse category: code-review

Example Trigger

# Hook triggers before a git commit command git commit -m "feat: add user profile page" # Inspector runs formatters on all staged files

Example Output

Pre-commit Formatter Inspector
  Staged files: 12
  JavaScript/TypeScript (7 files):
    Prettier: 5 pass, 2 need formatting
  Python (3 files):
    Black: 3 pass
  Go (2 files):
    gofmt: 2 pass
Summary: 2 files need formatting
  src/components/UserProfile.tsx (Prettier)
  src/hooks/useAuth.ts (Prettier)
Run: npx prettier --write src/components/UserProfile.tsx src/hooks/useAuth.ts

Core Concepts

Language Coverage Overview

AspectDetails
JavaScript/TypeScriptPrettier with project config (.prettierrc)
PythonBlack with pyproject.toml settings
Gogofmt (built into Go toolchain)
Rustrustfmt with rustfmt.toml configuration
CSS/SCSSPrettier with CSS parser
YAML/JSONPrettier with appropriate parser selection

Inspection Workflow

Commit Initiated
       |
  Get Staged Files
       |
  Group by Language
       |
  ā”Œā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”
  |    |    |    |
 JS   Py   Go  Rust
  |    |    |    |
Pret  Blk  gfmt rfmt
  |    |    |    |
  ā””ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”˜
       |
  Aggregate Results
       |
  ā”Œā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”
  |         |
All Pass  Failures
  |         |
Allow    Block +
Commit   Show Fixes

Configuration

ParameterTypeDefaultDescription
formattersobjectauto-detectedMap of file extensions to formatter commands
auto_fixbooleanfalseAutomatically fix formatting instead of blocking
check_only_stagedbooleantrueOnly check files in the git staging area
parallelbooleantrueRun formatters for different languages in parallel
show_diffbooleanfalseShow the exact formatting diff for each violation

Best Practices

  1. Auto-Detect Available Formatters - Check which formatters are installed and only run those. A missing formatter should generate a warning, not block the entire commit process.

  2. Provide One-Command Fix - When the inspector finds formatting issues, output the exact command to fix all violations. Developers should be able to copy-paste and re-commit in seconds.

  3. Check Only Staged Files - Running formatters on the entire codebase is slow and surfaces pre-existing issues. Focus on staged files to validate only what is being committed.

  4. Support Auto-Fix Mode - For teams that prefer automatic formatting, enable auto_fix mode to format files and re-stage them rather than blocking. This trades strictness for convenience.

  5. Cache Formatter Results - If a file has not changed since the last successful format check, skip it. Caching reduces inspection time for large commits with few changes.

Common Issues

  1. Formatter Version Mismatch - Different developers with different formatter versions produce conflicting formats. Pin formatter versions in your project's package manager and document required versions.

  2. Partial Staging Conflicts - When only part of a file is staged, formatting the full file can change unstaged lines. Use git stash --keep-index before formatting to isolate staged content.

  3. Formatter Timeout on Large Files - Some formatters are slow on files with thousands of lines. Set per-file timeouts and skip formatting for files that exceed them with a logged warning.

Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates