Skip to content

omerkaz/claude-code-ts-quality-hook

Repository files navigation

Claude Code TypeScript Quality Check Hook

Professional-grade quality validation hooks for Claude Code with project-specific optimizations.

Features

  • Project-Specific Optimizations: Tailored hooks for React apps, Node.js backends, and VS Code extensions
  • TypeScript Compilation Check: Fast tsc --noEmit validation with incremental builds
  • ESLint Integration: Auto-fix capability with project-specific rules
  • Prettier Formatting: Automatic code formatting with customizable settings
  • Smart Caching: TypeScript config detection with SHA256 checksums for <5ms performance
  • Module Alias Support: Full support for path aliases (@src, @components, etc.)
  • Security Focused: Uses CLAUDE_PROJECT_DIR environment variable (best practices)
  • Exit Code Handling:
    • 0 for success (silent)
    • 2 for quality issues (blocks the edit)

🚀 Quick Start

One-Command Installation

# React/Next.js/Vite projects
curl -sSL https://raw.githubusercontent.com/your-username/claude-code-ts-quality-hook/main/install.sh | bash -s react-app

# Node.js backend projects
curl -sSL https://raw.githubusercontent.com/your-username/claude-code-ts-quality-hook/main/install.sh | bash -s node-typescript

# VS Code extensions
curl -sSL https://raw.githubusercontent.com/your-username/claude-code-ts-quality-hook/main/install.sh | bash -s vscode-extension

Manual Installation

For detailed step-by-step instructions, troubleshooting, and configuration options:

📖 See Complete Installation Guide →

Quick Manual Setup

# 1. Clone repository
git clone https://github.com/your-username/claude-code-ts-quality-hook.git

# 2. Copy files to your project (choose your project type)
cp claude-code-ts-quality-hook/.claude/settings.react-app.json /path/to/your/project/.claude/settings.local.json
cp -r claude-code-ts-quality-hook/.claude/hooks /path/to/your/project/.claude/

# 3. Install dependencies
cd /path/to/your/project
npm install -D typescript eslint prettier

Project-Specific Features

React Apps (react-app)

  • JSX/TSX Support: Full React and TypeScript integration
  • React Rules: ESLint rules optimized for React development
  • Hook Validation: Rules of Hooks enforcement
  • Build Optimization: Supports Next.js, Create React App, Vite
  • File Types: Components, hooks, pages, tests

Node.js Backend (node-typescript)

  • Server Optimization: Node.js specific TypeScript settings
  • Backend Rules: ESLint rules for server-side development
  • Module Support: CommonJS and ES modules
  • File Types: Services, controllers, models, middleware, routes

VS Code Extensions (vscode-extension)

  • Extension API: Optimized for VS Code extension development
  • Webview Support: Separate configs for main extension and webviews
  • Security Rules: Webview security best practices
  • File Types: Extension main, commands, providers, webviews

📋 Prerequisites

  • Node.js 14.0.0+ with npm/yarn
  • Existing TypeScript/JavaScript project
  • Claude Code IDE

For detailed system requirements and troubleshooting, see INSTALLATION.md.

Configuration

Project-Specific Configuration Examples

React App Configuration

{
  "typescript": {
    "enabled": true,
    "incremental": true,
    "jsx": "react-jsx"
  },
  "eslint": {
    "enabled": true,
    "autofix": true,
    "extends": ["react-app", "react-app/jest"]
  },
  "prettier": {
    "enabled": true,
    "autofix": true
  }
}

Node.js Backend Configuration

{
  "typescript": {
    "enabled": true,
    "target": "ES2020",
    "module": "commonjs"
  },
  "eslint": {
    "enabled": true,
    "env": ["node", "es2021"]
  },
  "prettier": {
    "enabled": true,
    "singleQuote": true
  }
}

VS Code Extension Configuration

{
  "typescript": {
    "enabled": true,
    "lib": ["ES2020", "DOM"]
  },
  "eslint": {
    "enabled": true,
    "env": ["node", "browser"]
  },
  "vscodeSettings": {
    "webviewSecurityPolicy": {
      "enableScripts": true
    }
  }
}

How It Works

  1. Trigger: Runs automatically after Claude Code's Write, Edit, or MultiEdit tools
  2. Project Detection: Automatically selects the appropriate hook based on your configuration
  3. File Processing: Validates TypeScript/JavaScript files with project-specific rules
  4. Quality Checks:
    • TypeScript compilation (tsc --noEmit)
    • ESLint validation with auto-fix
    • Prettier formatting (optional)
  5. Result: Returns appropriate exit code to Claude Code

Exit Codes

  • 0: All checks passed (edit allowed)
  • 1: General error (configuration issue, missing dependencies)
  • 2: Quality issues found (edit blocked until fixed)

Supported File Types by Project

React Apps

  • TypeScript: .ts, .tsx (with JSX support)
  • JavaScript: .js, .jsx (with JSX support)
  • Test files: *.test.*, *.spec.*

Node.js Backend

  • TypeScript: .ts (server-focused)
  • JavaScript: .js (server-focused)
  • Test files: *.test.ts, *.spec.ts

VS Code Extensions

  • TypeScript: .ts (extension and webview)
  • JavaScript: .js (legacy support)

Universally Ignored Files

  • node_modules/
  • dist/, build/, out/, .next/
  • .d.ts declaration files
  • Config files (*.config.js, *.config.ts)
  • Coverage and cache directories

Advanced Features

Multiple TypeScript Configurations

The hooks automatically detect and use appropriate configs:

  • tsconfig.json (main)
  • tsconfig.build.json (build-specific)
  • tsconfig.test.json (test-specific)
  • tsconfig.webview.json (VS Code webviews)

Smart File Mapping

Each hook intelligently maps files to the correct TypeScript configuration:

  • Webview files → tsconfig.webview.json
  • Test files → tsconfig.test.json
  • Source files → tsconfig.json

Performance Optimizations

  • Sub-5ms Validation: SHA256 caching for config files
  • Incremental Builds: TypeScript incremental compilation
  • ESLint Caching: Automatic cache management
  • Parallel Execution: TypeScript, ESLint, and Prettier run concurrently

Manual Testing

Test any hook manually:

# Test React app hook
node .claude/hooks/react-app/quality-check.js src/App.tsx

# Test Node.js hook
node .claude/hooks/node-typescript/quality-check.js src/server.ts

# Test VS Code extension hook
node .claude/hooks/vscode-extension/quality-check.js src/extension.ts

# Check exit code
echo $?

Cache Management

Each hook maintains its own TypeScript configuration cache:

  • Location: .claude/hooks/{project-type}/tsconfig-cache.json
  • Purpose: Maps files to appropriate TypeScript configs
  • Validation: SHA256 checksums detect config changes instantly
  • Auto-regeneration: Rebuilds cache when configurations change

Clear All Caches

find .claude/hooks -name "tsconfig-cache.json" -delete

Customization Examples

Disable Prettier

{
  "prettier": {
    "enabled": false
  }
}

TypeScript Only (No ESLint)

{
  "typescript": {
    "enabled": true
  },
  "eslint": {
    "enabled": false
  }
}

Debug Mode for Troubleshooting

{
  "general": {
    "debug": true
  }
}

Custom ESLint Extensions

For React projects with additional file types:

{
  "supportedExtensions": [".ts", ".tsx", ".js", ".jsx", ".mjs"]
}

Troubleshooting

Common Issues

Hook Not Running

  1. Verify correct settings file: .claude/settings.local.json
  2. Check file path in command: node .claude/hooks/{project-type}/quality-check.js
  3. Ensure Node.js can execute the script

TypeScript Errors Not Showing

  1. Verify tsconfig.json exists in project root
  2. Check TypeScript installation: npx tsc --version
  3. Enable debug mode to see processing details

ESLint Not Working

  1. Verify .eslintrc.* configuration exists
  2. Check ESLint installation: npx eslint --version
  3. Ensure file extensions match ESLint configuration

Prettier Issues

  1. Check .prettierrc or prettier.config.js exists
  2. Verify Prettier installation: npx prettier --version
  3. Disable Prettier if not needed: "prettier": { "enabled": false }

Performance Issues

Slow TypeScript Checking

  • Enable incremental builds: "incremental": true
  • Use project references for monorepos
  • Check for large node_modules in TypeScript path

Cache Problems

# Clear all caches and regenerate
rm -rf .claude/hooks/*/tsconfig-cache.json
rm -rf node_modules/.cache

Integration Examples

Create React App + TypeScript

npx create-react-app my-app --template typescript
cd my-app
cp path/to/claude-code-ts-quality-hook/.claude/settings.react-app.json .claude/settings.local.json
cp -r path/to/claude-code-ts-quality-hook/.claude/hooks .claude/

Next.js Project

npx create-next-app@latest my-app --typescript
cd my-app
cp path/to/claude-code-ts-quality-hook/.claude/settings.react-app.json .claude/settings.local.json
cp -r path/to/claude-code-ts-quality-hook/.claude/hooks .claude/

Express + TypeScript Backend

mkdir my-backend && cd my-backend
npm init -y
npm install -D typescript @types/node
cp path/to/claude-code-ts-quality-hook/.claude/settings.node-typescript.json .claude/settings.local.json
cp -r path/to/claude-code-ts-quality-hook/.claude/hooks .claude/

VS Code Extension Development

npx @vscode/vscode-generator-code my-extension
cd my-extension
cp path/to/claude-code-ts-quality-hook/.claude/settings.vscode-extension.json .claude/settings.local.json
cp -r path/to/claude-code-ts-quality-hook/.claude/hooks .claude/

Repository Structure

claude-code-ts-quality-hook/
├── .claude/
│   ├── hooks/
│   │   ├── react-app/
│   │   │   ├── quality-check.js
│   │   │   └── hook-config.json
│   │   ├── node-typescript/
│   │   │   ├── quality-check.js
│   │   │   └── hook-config.json
│   │   ├── vscode-extension/
│   │   │   ├── quality-check.js
│   │   │   └── hook-config.json
│   │   └── quality-check.js          # Generic hook
│   ├── settings.local.json           # Default settings (React)
│   ├── settings.react-app.json       # React app settings
│   ├── settings.node-typescript.json # Node.js settings
│   └── settings.vscode-extension.json # VS Code extension settings
├── examples/
│   └── sample-project-setup.md       # Setup examples
├── README.md                          # This file
├── LICENSE                            # MIT License
├── package.json                       # Package metadata
└── .gitignore                         # Git ignore rules

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and test thoroughly
  4. Commit your changes: git commit -m 'Add amazing feature'
  5. Push to the branch: git push origin feature/amazing-feature
  6. Submit a pull request

Testing Contributions

Before submitting a PR:

  1. Test with all three project types (React, Node.js, VS Code extension)
  2. Verify TypeScript, ESLint, and Prettier integration
  3. Check performance with large projects
  4. Ensure error messages are clear and helpful

Security Considerations

  • Uses CLAUDE_PROJECT_DIR environment variable (recommended by Claude Code)
  • No directory traversal vulnerabilities
  • Validates all file paths before processing
  • Sanitizes command arguments
  • No execution of user-provided code

Performance Benchmarks

Typical performance on a modern development machine:

  • TypeScript check: 50-200ms (incremental: 10-50ms)
  • ESLint validation: 30-100ms (cached: 5-20ms)
  • Prettier formatting: 10-50ms
  • Total hook execution: 100-350ms (optimized: 25-120ms)

Test Results

All hooks have been validated with comprehensive testing:

Integration Tests: PASSED

  • React App Hook: Correctly detects TypeScript, JSX, and React-specific issues
  • Node.js TypeScript Hook: Validates server-side TypeScript patterns
  • VS Code Extension Hook: Catches extension API and webview issues
  • Generic Hook: Works across all file types

Test Coverage:

  • TypeScript compilation errors (type mismatches, missing properties)
  • ESLint rule violations (unused variables, missing error handling)
  • Prettier formatting inconsistencies
  • Project-specific optimizations verified

Performance Validated:

  • All hooks return correct exit codes (0 for success, 2 for errors)
  • Caching system working for sub-5ms config validation
  • CLAUDE_PROJECT_DIR environment variable handling verified

Run ./test-runner.sh to execute the full test suite.

Related Projects

Comparison with bartolli/claude-code-typescript-hooks

Our implementation offers several enhancements:

  • Prettier Integration: Automatic code formatting support
  • Enhanced Configuration: More granular control over rules and behavior
  • Better Documentation: Comprehensive setup guides and examples
  • Security Improvements: Following latest Claude Code best practices
  • Project-Specific Optimizations: Tailored configurations for different use cases

License

MIT License - see LICENSE file for details.

Changelog

v1.0.0

  • ✨ Initial release with project-specific hooks
  • ✨ React app, Node.js backend, and VS Code extension support
  • ✨ Prettier integration
  • ✨ Enhanced TypeScript config caching
  • ✨ Security improvements with CLAUDE_PROJECT_DIR
  • ✨ Comprehensive documentation and examples

Support

If you encounter issues:

  1. Check the troubleshooting section
  2. Enable debug mode for more information
  3. Search existing issues on GitHub
  4. Create a new issue with:
    • Your project type (React/Node.js/VS Code extension)
    • Debug output (with sensitive info removed)
    • Steps to reproduce the issue

Acknowledgments

About

Professional-grade quality validation hooks for Claude Code with project-specific optimizations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors