.claude/rules/ Create modularized, focused rules in the directory.
Quick Start
First, confirm with the user:
- Scope: Global (all files) or path-specified (specific file types)?
- Location:
.claude/rules/(for projects) or~/.claude/rules/(for personal use)? - Category: Code style, testing, security, API, other?
- Target language/framework(if applicable)?
Next, check existing rules:
# プロジェクトのルールを確認
ls -la .claude/rules/
Check personal rules
ls -la ~/.claude/rules/
重複がないことを確認してからルールを作成する。
**ルールファイルを作成**:
```markdown
---
paths: src/**/*.ts
---
# TypeScript Guidelines
- strict モードを使用する
- オブジェクト形状には type より interface を優先
- エクスポートされる関数には明示的な戻り値型を指定
Pre-creation check
Always check before creating a rule:
-
Checking existing rules
ls .claude/rules/- Does a rule on the same topic already exist?
- Should this be added to an existing rule instead?
-
Checking for duplication with CLAUDE.md
- Is there similar content in CLAUDE.md?
- Project overview → CLAUDE.md
- Specific coding conventions → rules/
-
Appropriateness of scope
- Should this be a global rule or path-specified?
- Does it conflict with existing path-specified rules?
Rule file structure
Global rules (no Frontmatter)
Applies to all files:
# Code Style
- 2スペースインデントを使用
- let より const を優先
- 未使用変数を残さない
Path-specified rules (with Frontmatter)
Applies only when Claude handles matching files:
---
paths: src/api/**/*.ts
---
# API Development Rules
- 全エンドポイントに入力バリデーションを含める
- 標準エラーレスポンス形式を使用
- OpenAPI ドキュメントコメントを含める
YAML Frontmatter
paths fields
Single pattern:
paths: src/**/*.ts
Multiple patterns via brace expansion:
paths: src/**/*.{ts,tsx}
Comma-separated patterns:
paths: {src,lib}/**/*.ts, tests/**/*.test.ts
Glob pattern reference
| Pattern | Match target |
|---|---|
**/*.ts |
All TypeScript files anywhere |
src/**/* |
src/ All files under |
*.md |
Only Markdown files directly under the project root |
src/components/*.tsx |
React components in a specific directory |
**/*.{ts,tsx} |
TypeScript and TSX files |
{src,lib}/**/*.ts |
src/ or lib/ TypeScript in |
Directory structure
Basic structure
.claude/rules/
├── code-style.md # 一般的なコーディング規約
├── testing.md # テスト規約
└── security.md # セキュリティ要件
With subdirectories
.claude/rules/
├── frontend/
│ ├── react.md
│ └── styles.md
├── backend/
│ ├── api.md
│ └── database.md
└── general.md
All .md files are detected recursively.
Sharing rules via symbolic links
# 共有ルールディレクトリをシンボリックリンク
ln -s ~/shared-claude-rules .claude/rules/shared
# 個別ファイルをシンボリックリンク
ln -s ~/company-standards/security.md .claude/rules/security.md
Best practices
MUST
- One topic per fileKeep focus
- Use descriptive file names(
api-validation.md、rules1.mdis not) - Be specific(not "format appropriately" but "2-space indent")
- Check existing rulesbefore creating new ones
ALLOWED
- Organizing by subdirectory
- Referencing shared rules via symbolic links
- Specifying multiple paths patterns
MUST NOT
-
Everything-in-one filecreation
# ✗ Bad - rules.md に全部入れる # All Rules ## Coding ## Testing ## Deployment ## Security -
Duplication with CLAUDE.md
- CLAUDE.md: Project overview, common commands, architecture
- Rules: Specific coding guidelines
-
Time-dependent information
# ✗ Bad - 2025年1月現在、React 19 の機能を使用 -
Vague descriptions
# ✗ Bad - 適切にフォーマットする - 良い命名を使う
General rule categories
For detailed examples, see examples.md .
Code style
# Code Style
- ESLint/Prettier 設定を使用
- 本番コードに console.log を残さない
- 名前付きエクスポートを優先
- @/ プレフィックスで絶対インポートを使用
Testing
---
paths: **/*.test.{ts,tsx}
---
# Testing Standards
- Arrange-Act-Assert パターン
- 外部依存関係をモック
- エッジケースを明示的にテスト
- 新規コードは 80%+ カバレッジを目指す
API development
---
paths: src/api/**/*.ts
---
# API Guidelines
- zod で全入力をバリデーション
- 一貫したエラーフォーマットを返す: { error: string, code: string }
- 全 5xx エラーをログ
- レスポンスにリクエスト ID を含める
Security
# Security Requirements
- 機密データ(パスワード、トークン、PII)をログに残さない
- データベースクエリ前にユーザー入力をサニタイズ
- パラメータ化クエリのみを使用
- ファイルアップロードを検証: タイプ、サイズ、内容
React/Frontend
---
paths: src/components/**/*.tsx
---
# React Component Guidelines
- 関数コンポーネント + hooks を優先
- 複雑なロジックはカスタム hooks に抽出
- コストの高いレンダリングには React.memo を使用
- Props インターフェース名: ComponentNameProps
Rules vs CLAUDE.md
| Item | CLAUDE.md | Rules |
|---|---|---|
| Purpose | Project overview | Specific coding conventions |
| Scope | Overall | Conditional (path specification allowed) |
| Content | Build commands, architecture | Guidelines by file type |
| Structure | Single file | Modularized multiple files |
When to use Rules:
- Guidelines apply to specific file types or paths
- Content focused on a single topic
- The team wants modularized organization
When to use CLAUDE.md:
- General project information applies to all files
- Build/test/lint commands are needed frequently
- An architecture overview is needed
Final checklist
Before creating a rule:
- Checked existing rules (no duplicates)
- Checked for duplication with CLAUDE.md
- Identified a clear and focused topic
- Selected an appropriate scope (global or path specification)
- Used a descriptive file name
- Made the content specific and actionable
- Tested the glob pattern when using path specification