Stop AI from breaking your React/Next.js code. Start shipping with confidence.
Quick Start β’ Documentation β’ Examples β’ Contributing
CodeKeeper is a battle-tested (yes, I'm using it in production) collection of validation scripts that prevent AI assistants from introducing bugs, security issues, and bad practices into your React and Next.js projects. Works with any development workflow you're already using.
β ESLint β’ β Husky β’ β Lefthook β’ β pre-commit β’ β lint-staged β’ β GitHub Actions β’ β GitLab CI β’ β Jenkins β’ β Vercel β’ β Netlify
AI coding assistants are powerful but can introduce subtle bugs in React/Next.js projects:
- Type assertions that hide runtime errors (
as any
everywhere) - Barrel files that destroy tree-shaking and slow builds
- Component mega-files with 500+ lines that become unmaintainable
- Missing JSDoc that leaves your team confused about props and hooks
- Security vulnerabilities from unsafe patterns and exposed secrets
CodeKeeper provides pre-built validation scripts that catch these issues before they hit production:
Guardrail | Prevents | Impact |
---|---|---|
Type Safety | as any , unsafe casts |
-90% runtime errors |
Import Quality | Barrel file chaos | 3x faster builds |
Complexity Limits | 1000+ line files | 50% easier maintenance |
Documentation | Missing JSDoc | 2x faster onboarding |
Architecture | Messy file structure | Clean, scalable codebase |
# Install lefthook
npm install -g lefthook
# Copy CodeKeeper scripts and config
curl -fsSL https://raw.githubusercontent.com/thedaviddias/codekeeper/main/install.sh | bash
# Auto-configured! lefthook.yml is ready to use
lefthook install
# Now code freely - validation happens on commit! π
Pick your favorite:
# Install husky and lint-staged
npm install --save-dev husky lint-staged
npx husky-init
# Copy CodeKeeper scripts
curl -fsSL https://raw.githubusercontent.com/thedaviddias/codekeeper/main/install.sh | bash
# Add to package.json
{
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix",
"node scripts/validate-all.js"
]
}
}
# Update .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged
# Install lefthook
npm install -g lefthook
# Copy CodeKeeper scripts and config
curl -fsSL https://raw.githubusercontent.com/thedaviddias/codekeeper/main/install.sh | bash
# Auto-configured! lefthook.yml is ready to use
lefthook install
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: codekeeper-validation
name: CodeKeeper Validation
entry: node scripts/validate-all.js
language: node
files: \.(ts|tsx)$
// AI often generates this in React components
const UserProfile = ({ data }: any) => {
const user = data as any; // Runtime bomb!
return <div>{user.naem.toUpperCase()}</div>; // Typo crashes in production
}
// Guardrails force proper typing
interface UserProfileProps {
data: User;
}
const UserProfile = ({ data }: UserProfileProps) => {
return <div>{data.name.toUpperCase()}</div>; // TypeScript catches typos
}
- Try interactive examples β - Working projects you can test immediately
- See all React/Next.js examples β
no-unsafe-as-casts
- Blocksas any
and unsafe type assertionsno-barrel-files
- Prevents slow barrel file exportsmax-file-complexity
- Limits file size and complexityrequire-jsdoc
- Enforces documentation standards
check-as-casts.js
- TypeScript type safety validationcheck-barrel-files.js
- Import performance checkscheck-directory-structure.js
- Architecture enforcementcheck-file-complexity.js
- Complexity limitscheck-jsdoc.js
- Documentation requirementscheck-relative-imports.js
- Import consistency
lefthook.yml
- Git hooks for automatic validation.eslintrc.js
- ESLint integration examples
CodeKeeper is designed for developers who want to code in flow state without constant interruptions:
- π Code freely - Focus on building features, don't worry about perfect code
- β‘ Quick validation - Let Lefthook catch issues with specific, educational messages on commit
- π€ AI-assisted fixes - Use LLMs (like Claude, ChatGPT) to fix the issues CodeKeeper found
- β Ship with confidence - Your code is clean and follows best practices
This approach lets you stay in creative flow while ensuring code quality.
When CodeKeeper finds issues, copy them to your AI assistant:
git commit -m "add user profile feature"
# π Checking for unsafe 'any' types...
# β COMMIT BLOCKED: Found 'any' types in UserProfile.tsx!
# π‘ Replace 'any' with specific types for better type safety
# π Checking React component complexity...
# β COMMIT BLOCKED: Components exceed complexity limits!
# π‘ Split large components into smaller, focused ones
Prompt to AI: "Fix these CodeKeeper issues: [paste output]"
AI Response: Provides proper TypeScript interfaces, splits complex components, and removes barrel files.
Result: Clean, maintainable code without breaking your flow state!
CodeKeeper works with any CI/CD pipeline:
# .github/workflows/validation.yml
name: CodeKeeper Validation
on: [push, pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
- run: npm install
- run: npm run lint # ESLint + CodeKeeper plugin
- run: node scripts/validate-all.js # Standalone validation
# .gitlab-ci.yml
stages:
- validate
code-validation:
stage: validate
script:
- npm install
- npm run lint
- node scripts/validate-all.js
only:
- merge_requests
- main
// Jenkinsfile
pipeline {
agent any
stages {
stage('Install') {
steps {
sh 'npm install'
}
}
stage('Validate') {
steps {
sh 'npm run lint'
sh 'node scripts/validate-all.js'
}
}
}
}
// package.json
{
"scripts": {
"build": "npm run validate && next build",
"validate": "eslint . && node scripts/validate-all.js"
}
}
Tool | ESLint Plugin | Standalone Scripts | Setup Difficulty | Performance |
---|---|---|---|---|
ESLint | β Native | β Via npm scripts | π’ Easy | π’ Fast |
Husky + lint-staged | β Perfect fit | β Perfect fit | π’ Easy | π’ Fast |
Lefthook | β Works great | β Built-in support | π’ Easy | π’ Fast |
pre-commit | β Via ESLint hook | β Custom hook | π‘ Moderate | π’ Fast |
GitHub Actions | β Standard workflow | β Custom step | π’ Easy | π’ Fast |
GitLab CI | β Standard job | β Custom job | π’ Easy | π’ Fast |
VS Code | β Real-time errors | β Terminal only | π’ Easy | π’ Instant |
WebStorm/IntelliJ | β Built-in support | π’ Easy | π‘ Good |
π― Quick Recommendations:
- Love flow state coding? β Lefthook + CodeKeeper scripts (our favorite!)
- Already use Husky? β Add CodeKeeper to your lint-staged config
- Team uses ESLint heavily? β ESLint plugin for real-time feedback
- Want maximum speed? β Lefthook's parallel execution wins
- Node.js: Version 18.0.0 or higher (check with
node --version
) - npm: Version 8.0.0 or higher
- Use the recommended version from
.nvmrc
for best compatibility:nvm use # If you have nvm installed # or check the recommended version cat .nvmrc
-
Verify Node.js compatibility:
npm run check:node
-
Install lefthook (if not already installed):
npm install -g lefthook # or yarn global add lefthook
-
Copy the files:
# Copy validation scripts mkdir -p scripts/validation cp scripts/validation/*.js scripts/validation/ # Copy lefthook config cp lefthook.yml ./
-
Install lefthook in your repo:
lefthook install
- Blocks:
as any
,as unknown
, unsafe type assertions - Allows:
as const
, test files, migration scripts - Why: Prevents runtime type errors and maintains type safety
- Blocks: Barrel files (
index.ts
that re-exports everything) - Enforces: Explicit imports from specific files
- Why: Better tree-shaking, faster builds, clearer dependencies
- Enforces: Clean, domain-driven directory structure
- Prevents: Messy file organization and anti-patterns
- Why: Maintainable codebase and clear separation of concerns
- Limits: File size, function count, nesting depth
- Suggests: Refactoring when limits are exceeded
- Why: Prevents unmaintainable code and cognitive overload
- Requires: JSDoc comments on functions and components
- Enforces: Parameter and return type documentation
- Why: Better code understanding and AI assistance
- Enforces: Consistent import aliases (
@/
instead of../
) - Prevents: Deep relative imports that break on refactoring
- Why: Maintainable import paths and easier refactoring
For detailed guides and examples, check out the documentation:
- Quick Start Guide - Get up and running in 5 minutes
- Why Guardrails Matter - The reasoning behind each guardrail
- Real Examples - Before/after scenarios with AI-generated code
Edit the constants in each validation script:
// In check-file-complexity.js
const COMPLEXITY_LIMITS = {
lines: 500, // Max lines per file
functions: 15, // Max functions per file
dependencies: 20, // Max import statements
nestingDepth: 10, // Max nesting depth
}
Modify the exclusion patterns in each script:
// In check-as-casts.js
const ALLOWED_PATTERNS = [
/\bas\s+const\b/g,
/\.test\.(ts|tsx)$/,
/\.spec\.(ts|tsx)$/,
// Add your own patterns here
]
Use environment variables to skip checks during development:
NODE_ENV=development git commit -m "WIP"
- Start Strict: Begin with all guardrails enabled, this will avoid you nightmarish debugging sessions.
- Customize Gradually: Adjust limits based on your team's needs, you can start with the defaults and then adjust them to your needs.
- Document Exceptions: Add comments when you need to bypass rules, this will help you and your team to understand the reasons behind the exceptions.
- Review Regularly: Update guardrails as your project evolves, this will help you to keep your codebase clean and maintainable.
- Team Alignment: Ensure everyone understands the rules and their benefits, this will help you to have a more consistent codebase.
- Temporarily increase limits in the scripts
- Fix violations incrementally
- Use
NODE_ENV=development
to bypass checks temporarily
- Add specific patterns to the
ALLOWED_PATTERNS
arrays - Use file-specific exclusions in the validation logic
- Document why exceptions are needed
- Optimize the validation scripts
- Add more specific file filters
- Consider running some checks only on pre-push
Your Company? |
Ensure your CodeKeeper rules work correctly:
# Test all validation scripts
npm test
# Quick development check
node tests/quick-test.js
# Test individual validators
npm run test:as-casts
npm run test:barrel-files
npm run test:relative-imports
The test suite includes fixtures with known violations to verify each guardrail catches what it should. See tests/README.md
for details.
We welcome contributions! Whether it's:
- π Bug fixes
- β¨ New guardrail types
- π Documentation improvements
- π Translations
- π‘ Feature ideas
Before submitting changes:
- Run tests to ensure everything works:
npm test
- Add test fixtures for new validation rules
- Update documentation if needed
- Contributing Guide - General contribution guidelines
- Adding New Validation Rules - Comprehensive step-by-step guide for adding new rules
- Examples - Usage examples and patterns
- π 0+ GitHub Stars - Star us!
- π₯ 0+ Contributors - Join us!
- π’ 0+ Companies using in production
- π¬ Discussions - Ask questions, share ideas
- π¦ Follow updates - Latest features and tips
MIT License - feel free to use these guardrails in any project!
Created by David Dias, author of Front-End Checklist (75k+ stars).
Inspired by the need to maintain code quality when working with AI assistants. These guardrails help ensure that AI-generated code meets the same standards as human-written code.
β Star this repo if it helped you ship better code with AI!
Report Bug β’ Request Feature β’ Join Discord