3.1 KiB
3.1 KiB
.claude/rules/ Directory Reference
Purpose
The .claude/rules/ directory allows modular organization of instructions with optional path scoping.
Location
project/
├── .claude/
│ └── rules/
│ ├── code-style.md
│ ├── testing.md
│ └── api.md
└── CLAUDE.md
File Format
Each rule file is a markdown file with optional frontmatter:
---
paths: ["src/**/*.ts", "src/**/*.tsx"]
description: TypeScript code style rules
---
# TypeScript Rules
## Formatting
- Use 2-space indentation
- Prefer single quotes
## Types
- Always use explicit types for function parameters
- Avoid `any` type
Frontmatter Options
paths (Official) / globs (Legacy)
Array of glob patterns that scope when this rule applies.
Official field name: paths: (as per Claude Code documentation)
Legacy/alternative: globs: (also supported for backwards compatibility)
Both fields behave identically - use paths: for new rules:
---
paths: ["src/**/*.ts"] # Official - Only for TypeScript in src/
---
---
globs: ["src/**/*.ts"] # Legacy - Still works, but prefer paths:
---
---
paths: ["tests/**/*", "**/*.test.ts"] # Test files anywhere
---
If no paths/globs specified, rule applies everywhere.
Note: Config-audit normalizes both to patterns internally and tracks which field was used via pattern_source.
description
Brief description of what the rule covers:
---
description: Code formatting and style preferences
---
alwaysApply
Force rule to always be included regardless of current file:
---
alwaysApply: true
---
Loading Behavior
- Rules are loaded when entering relevant directories
- Glob patterns are matched against current file/directory
- Matching rules are included in context
- Non-matching rules are not loaded
Example Rules
code-style.md
---
paths: ["src/**/*"]
description: Source code style
---
# Code Style
- TypeScript > JavaScript
- Explicit types for public API
- Document exported functions
testing.md
---
paths: ["tests/**/*", "**/*.test.ts", "**/*.spec.ts"]
description: Testing guidelines
---
# Testing
- Use Jest for unit tests
- Descriptive test names
- Arrange-Act-Assert pattern
api.md
---
paths: ["src/api/**/*", "src/routes/**/*"]
description: API development rules
---
# API Guidelines
- RESTful conventions
- Validate all inputs
- Consistent error responses
Best Practices
- Split by concern: One rule file per topic
- Use specific globs: Avoid overly broad patterns
- Keep rules focused: 200-500 words per file
- Document purpose: Use description frontmatter
- Review periodically: Remove outdated rules
Migration from CLAUDE.md
To convert from monolithic CLAUDE.md to rules:
- Identify distinct sections in CLAUDE.md
- Create rule file for each section
- Add appropriate globs
- Remove sections from CLAUDE.md
- Test that rules load correctly
Debugging
To see which rules are loaded:
- Check Claude Code logs
- Rules appear in context when relevant files are active