codebelt-code-style
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCodeBelt Code Style Guide
CodeBelt代码风格指南
Opinionated TypeScript and React code style for consistency, maintainability, and scalability. Apply these rules when writing new code, reviewing existing code, or refactoring.
这是一套具有主见的TypeScript与React代码风格规范,旨在保障代码的一致性、可维护性与可扩展性。编写新代码、评审现有代码或重构时请遵循这些规则。
Quick Reference
快速参考
File Placement
文件存放位置
| Code Type | Location |
|---|---|
| React component | |
| API logic | |
| Reusable function | |
| React hook | |
| Third-party config | |
| 代码类型 | 存放路径 |
|---|---|
| React组件 | |
| API逻辑 | |
| 可复用函数 | |
| React Hook | |
| 第三方配置 | |
File Extensions
文件扩展名
| Extension | Purpose |
|---|---|
| Main code ( |
| Tests |
| TypeScript types |
| Helper functions |
| Tests for utility functions |
| Zod validation schemas |
| Tests for schemas |
| Static objects and constants |
| 扩展名 | 用途 |
|---|---|
| 主代码文件(仅当文件包含JSX时使用 |
| 测试文件 |
| TypeScript类型定义文件 |
| 辅助函数文件 |
| 辅助函数测试文件 |
| Zod校验schema文件 |
| Schema测试文件 |
| 静态对象与常量文件 |
Naming Conventions
命名规范
| Context | Convention | Example |
|---|---|---|
| Component folders | camelCase | |
| Component files | PascalCase | |
| Everything else | camelCase | |
| Variables/params | Descriptive (no single chars) | |
| Constants | camelCase | |
| 场景 | 规范 | 示例 |
|---|---|---|
| 组件文件夹 | 小驼峰式(camelCase) | |
| 组件文件 | 大驼峰式(PascalCase) | |
| 其他所有文件 | 小驼峰式(camelCase) | |
| 变量/参数 | 描述性命名(禁止单字符) | |
| 常量 | 小驼峰式(camelCase) | |
Exports
导出规则
All files use named exports only:
typescript
export function Component() {}No default exports. No barrel files in application code.
所有文件仅使用命名导出:
typescript
export function Component() {}禁止使用默认导出。应用代码中禁止使用桶文件(barrel files)。
Workflows
工作流程
Creating a New Component
创建新组件
- Create folder:
components/{pages|shared|ui}/{componentName}/ - Create component file: with Props type and JSX only
{ComponentName}.tsx - Extract types to (if needed beyond Props)
{ComponentName}.types.ts - Extract constants to (if any)
{ComponentName}.constants.ts - Extract helpers to (if any)
{ComponentName}.utils.ts - Verify: one component per file, Props not exported, named export
- 创建文件夹:
components/{pages|shared|ui}/{componentName}/ - 创建组件文件:,仅包含Props类型与JSX代码
{ComponentName}.tsx - 若Props之外还有其他类型定义,将其提取至
{ComponentName}.types.ts - 若有静态常量,将其提取至
{ComponentName}.constants.ts - 若有辅助函数,将其提取至
{ComponentName}.utils.ts - 检查:每个文件对应一个组件,Props不对外导出,使用命名导出
Creating a New Service
创建新服务
- Create folder:
services/{provider}/{serviceName}/ - Create main file: with fetch functions and query hooks
{serviceName}.ts - Create schema file: with Zod schemas
{serviceName}.schemas.ts - Create constants file: with query keys
{serviceName}.constants.ts - Verify: schema and type share same name, comment with HTTP method and path
- 创建文件夹:
services/{provider}/{serviceName}/ - 创建主文件:,包含请求函数与查询Hook
{serviceName}.ts - 创建schema文件:,包含Zod校验schema
{serviceName}.schemas.ts - 创建常量文件:,包含查询键
{serviceName}.constants.ts - 检查:schema与类型使用相同名称,添加包含HTTP方法与路径的注释
Creating a New Hook
创建新Hook
- Create folder:
hooks/use{HookName}/ - Create hook file:
use{HookName}.ts - Create test file:
use{HookName}.test.ts
- 创建文件夹:
hooks/use{HookName}/ - 创建Hook文件:
use{HookName}.ts - 创建测试文件:
use{HookName}.test.ts
Creating a New Utility
创建新工具函数
- Create folder:
utils/{utilName}/ - Create utility file: (no
{utilName}.tssuffix inside.utils.ts)utils/ - Create test file:
{utilName}.test.ts
- 创建文件夹:
utils/{utilName}/ - 创建工具函数文件:(
{utilName}.ts目录下的文件无需添加utils/后缀).utils.ts - 创建测试文件:
{utilName}.test.ts
Detailed References
详细参考
- Component patterns and hierarchy: See COMPONENTS.md
- Service and schema patterns: See SERVICES.md
- Testing patterns: See TESTING.md
- Common mistakes to avoid: See MISTAKES.md
- 组件模式与层级结构:参见 COMPONENTS.md
- 服务与Schema模式:参见 SERVICES.md
- 测试模式:参见 TESTING.md
- 需避免的常见错误:参见 MISTAKES.md