📜 Rules 规则系统

34+ 规则文件,强制执行的编码规范和最佳实践

🟡 进阶级

什么是 Rules?

Rules 是始终遵循的准则,AI 在工作时必须遵守这些规则。与 Skills(指导性)不同,Rules 是强制性的。

⚠️ 规则的强制性质

Rules 不会被绕过。它们确保 AI 输出符合项目规范,无论用户如何请求。

Rule 文件示例

# Testing Rules

## Coverage Requirement
- Minimum 80% test coverage required
- All new code must have tests
- Critical paths require 100% coverage

## Test Naming
- Test files: `*.test.ts` or `*_test.py`
- Describe blocks: describe the unit being tested
- It blocks: describe the expected behavior

## What to Test
- Happy path
- Edge cases
- Error handling
- Boundary conditions

目录结构

rules/
├── README.md              # 结构说明和安装指南
├── common/                # 通用规则(所有项目必装)
│   ├── coding-style.md    # 编码风格
│   ├── git-workflow.md    # Git 工作流
│   ├── testing.md         # 测试要求
│   ├── performance.md     # 性能指南
│   ├── patterns.md        # 设计模式
│   ├── hooks.md           # Hook 架构
│   ├── agents.md          # 代理委派规则
│   └── security.md        # 安全规范
├── typescript/            # TypeScript 专属
│   ├── coding-style.md
│   ├── patterns.md
│   ├── security.md
│   ├── testing.md
│   └── hooks.md
├── python/                # Python 专属
│   ├── coding-style.md
│   ├── patterns.md
│   ├── security.md
│   ├── testing.md
│   └── hooks.md
├── golang/                # Go 专属
├── swift/                 # Swift 专属
├── php/                   # PHP 专属
├── java/                  # Java 专属
├── kotlin/                # Kotlin 专属
├── rust/                  # Rust 专属
└── cpp/                   # C++ 专属

通用规则 (common/)

这些规则适用于所有项目,必须安装:

📐
coding-style
编码风格:命名约定、文件组织、不可变性原则
🔀
git-workflow
Git 工作流:提交格式、分支策略、PR 流程
🧪
testing
测试要求:TDD、80% 覆盖率、边界用例

重要

🔒
security
安全规范:SQL 注入、XSS、敏感数据处理

重要

⚡
performance
性能指南:模型选择、上下文管理
🎨
patterns
设计模式:常用模式、骨架项目
🤖
agents
代理委派:何时委派任务给子代理
🔗
hooks
Hook 架构:何时使用 Hook、TodoWrite

语言规则

TypeScript 规则

规则 内容
coding-style.md TypeScript 编码风格,命名约定,import 组织
patterns.md TypeScript 模式,泛型,类型推断
security.md 前端安全,XSS 防护,敏感信息处理
testing.md Jest/Vitest 测试模式,React Testing Library
hooks.md TypeScript 特定的 Hook 配置

Python 规则

规则 内容
coding-style.md PEP 8 编码风格,命名约定
patterns.md Python 惯用写法,装饰器,上下文管理器
security.md Python 安全最佳实践,SQL 注入防护
testing.md pytest 测试模式,fixture,覆盖率

其他语言

语言 规则内容
Go coding-style, patterns, security, testing, hooks
Swift coding-style, patterns, security, testing, hooks
PHP coding-style, patterns, security, testing, hooks
Java coding-style, patterns, security, testing
Kotlin coding-style, patterns, security, testing
Rust coding-style, patterns, security, testing
C++ coding-style, patterns, testing

安装规则

用户级安装(所有项目生效)

# 创建 rules 目录
mkdir -p ~/.claude/rules

# 复制通用规则
cp -r rules/common/* ~/.claude/rules/

# 复制语言规则(按需)
cp -r rules/typescript/* ~/.claude/rules/
cp -r rules/python/* ~/.claude/rules/

项目级安装(仅当前项目生效)

# 创建项目 rules 目录
mkdir -p .claude/rules

# 复制规则
cp -r rules/common/* .claude/rules/
cp -r rules/typescript/* .claude/rules/

使用安装脚本

# Windows PowerShell
.\install.ps1 typescript python

# macOS/Linux
./install.sh typescript python

创建自定义规则

为你的项目创建专属规则:

# 创建自定义规则文件
cat > .claude/rules/project-specific.md << 'EOF'
# Project Specific Rules

## API 命名规范
- REST API 端点使用 kebab-case
- 响应格式统一使用 JSON:API 规范

## 数据库规范
- 表名使用 snake_case
- 主键统一使用 UUID
- 必须有 created_at 和 updated_at 字段

## 禁止事项
- 不要使用 console.log(生产代码)
- 不要直接使用 any 类型
- 不要在循环中执行数据库查询
EOF
💡 提示

自定义规则应该放在项目级 .claude/rules/ 目录,这样团队成员也能共享这些规则。