creating-rules
.claude/rules/ディレクトリにベストプラクティスに沿ったルールファイル(.md)を作成します。コーディングルール、テスト規約、セキュリティ要件などのプロジェクト固有の指示を作成する場合に使用します。paths frontmatterによる条件付きルール、適切なファイル名、サブディレクトリ構造をサポートします。
When & Why to Use This Skill
The 'Creating Rules' skill automates the generation of project-specific instruction files within the directory. It helps developers enforce coding standards, testing protocols, and security requirements by creating structured Markdown files that guide Claude's behavior. By leveraging 'paths' frontmatter and best-practice directory structures, it ensures the AI agent applies the correct rules to the relevant parts of the codebase, enhancing consistency and reducing manual oversight.
Use Cases
- Standardizing Code Quality: Automatically generate rules for naming conventions, architectural patterns, and linting preferences to ensure the AI agent maintains a consistent code style across the project.
- Context-Aware Testing Protocols: Define specific testing requirements (e.g., Vitest for frontend, Pytest for backend) that only activate when the agent is working within designated directories using glob patterns.
- Security and Compliance Enforcement: Create mandatory security guidelines for sensitive modules, such as authentication or database handling, ensuring the agent follows best practices for data protection.
- Project-Specific Onboarding: Quickly set up a new repository with a comprehensive set of instructions that align the AI agent with the team's unique workflow and technical debt management strategies.
| name | creating-rules |
|---|---|
| description | .claude/rules/ディレクトリにベストプラクティスに沿ったルールファイル(.md)を作成します。コーディングルール、テスト規約、セキュリティ要件などのプロジェクト固有の指示を作成する場合に使用します。paths frontmatterによる条件付きルール、適切なファイル名、サブディレクトリ構造をサポートします。 |
Creating Rules
.claude/rules/ディレクトリにベストプラクティスに沿ったルールファイルを作成する。
ワークフロー
1. 要件の確認
ユーザーに以下を確認:
- ルールのトピック(例:コードスタイル、テスト、セキュリティ)
- 対象ファイル(特定のパスに限定するか)
- サブディレクトリに配置するか
2. ルールファイルの作成
ディレクトリ構造
.claude/rules/
├── code-style.md # 全体に適用
├── testing.md # 全体に適用
├── frontend/ # サブディレクトリで整理
│ ├── react.md
│ └── styles.md
└── backend/
├── api.md
└── database.md
ファイル形式
条件なしルール(全ファイルに適用):
# [トピック名]
- ルール1
- ルール2
条件付きルール(特定ファイルのみ適用):
---
paths: src/**/*.ts
---
# [トピック名]
- ルール1
- ルール2
paths globパターン
| パターン | マッチ対象 |
|---|---|
**/*.ts |
全ディレクトリのTypeScriptファイル |
src/**/* |
src/以下の全ファイル |
*.md |
ルートのMarkdownファイル |
src/**/*.{ts,tsx} |
src/以下のTS/TSXファイル |
{src,lib}/**/*.ts, tests/**/*.test.ts |
複数パターンの組み合わせ |
3. ベストプラクティス
詳細は references/best-practices.md を参照。
要点:
- 焦点を絞る: 1ファイル1トピック
- 説明的なファイル名: 内容が分かる名前
- 条件付きルールは控えめに: 本当に必要な場合のみ
pathsを使用 - サブディレクトリで整理: 関連ルールをグループ化
- 具体的に記述: 「コードを適切にフォーマット」より「2スペースインデントを使用」
4. 作成後の検証
作成したルールファイルを検証:
- 配置確認:
.claude/rules/に正しく配置されているか - 構文確認: frontmatterのYAML構文が正しいか
- パターン確認:
pathsのglobパターンが意図通りか - 内容確認: ルールが具体的で実行可能か
検証コマンド例:
# ファイル一覧を確認
ls -la .claude/rules/
# frontmatterの構文確認(pathsが正しいか)
head -10 .claude/rules/[ファイル名].md
問題があれば修正し、再度検証する。
5. 完了確認
- ルールが正しく読み込まれることを確認
/memoryコマンドで読み込み状況を確認可能