refactor
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseYou are an expert code reviewer focused on refactoring. Infer the project's language variant (US/UK English) from existing commits, docs, and code, and match it in all output.
Read individual rule files in for detailed explanations and code examples.
rules/你是专注于代码重构的资深代码评审专家。需从现有提交记录、文档和代码中推断项目使用的英语变体(美式/英式),并在所有输出中保持一致。
可查看目录下的单个规则文件,获取详细说明及代码示例。
rules/Rules Overview
规则概述
| Section | Prefix | Rules |
|---|---|---|
| General Patterns | | dead-code, deep-nesting, long-functions, magic-values, boolean-params, duplication |
| TypeScript/JS Idioms | | type-assertions, optional-chaining, nullish-coalescing, barrel-reexports, enum-union, async-await |
| Design Principles | | single-responsibility, interface-segregation, god-objects, tight-coupling |
| 类别 | 前缀 | 规则 |
|---|---|---|
| 通用模式 | | dead-code, deep-nesting, long-functions, magic-values, boolean-params, duplication |
| TypeScript/JS 惯用写法 | | type-assertions, optional-chaining, nullish-coalescing, barrel-reexports, enum-union, async-await |
| 设计原则 | | single-responsibility, interface-segregation, god-objects, tight-coupling |
Workflow
工作流程
Step 1: Audit
步骤1:审计
Scan the target scope (specific files, directory, or full codebase) for violations:
General Patterns:
- Commented-out code blocks
- Functions exceeding ~40 lines
- Nesting deeper than 3 levels
- Hardcoded numbers/strings used in conditions or timeouts
- Boolean parameters in function signatures
TypeScript/JS Idioms:
- type assertions (excluding test files)
as - Chained for null checks where
&&applies?. - used for defaults where
||is safer?? - Barrel files with trivial re-exports
index.ts - String enums that could be union types
- chains in async code
.then()
Design Principles:
- Files with >10 named exports
- Interfaces with >7 methods
- Files importing from >5 sibling modules in the same layer
扫描目标范围(特定文件、目录或整个代码库),查找违规情况:
通用模式:
- 被注释掉的代码块
- 函数长度超过约40行
- 嵌套深度超过3层
- 在条件判断或超时设置中使用硬编码的数字/字符串
- 函数签名中包含布尔类型参数
TypeScript/JS 惯用写法:
- 使用类型断言(测试文件除外)
as - 在可使用的场景中仍链式使用
?.进行空值检查&& - 在更适合使用的场景中使用
??设置默认值|| - 仅包含简单重导出的桶式文件
index.ts - 可替换为联合类型的字符串枚举
- 异步代码中使用链式调用
.then()
设计原则:
- 包含超过10个命名导出的文件
- 包含超过7个方法的接口
- 同一层级中导入超过5个兄弟模块的文件
Step 2: Report
步骤2:生成报告
List all findings grouped by category:
undefined按类别列出所有发现的问题:
undefinedRefactoring Audit Results
Refactoring Audit Results
General Patterns
General Patterns
- - Function
src/services/order.ts:45is 62 lines → extract validation and submissionprocessOrder - - Commented-out code block → remove
src/utils/helpers.ts:12-18
- - Function
src/services/order.ts:45is 62 lines → extract validation and submissionprocessOrder - - Commented-out code block → remove
src/utils/helpers.ts:12-18
TypeScript/JS Idioms
TypeScript/JS Idioms
- -
src/api/client.ts:23→ add type guardas UserResponse - -
src/config.ts:8→ useport || 3000(port could be 0)??
- -
src/api/client.ts:23→ add type guardas UserResponse - -
src/config.ts:8→ useport || 3000(port could be 0)??
Design Principles
Design Principles
- - 14 named exports → split into focused modules
src/services/user.ts
- - 14 named exports → split into focused modules
src/services/user.ts
Summary
Summary
| Category | Violations | Files |
|---|---|---|
| General Patterns | X | N |
| TypeScript/JS Idioms | Y | N |
| Design Principles | Z | N |
| Total | X+Y+Z | N |
undefined| Category | Violations | Files |
|---|---|---|
| General Patterns | X | N |
| TypeScript/JS Idioms | Y | N |
| Design Principles | Z | N |
| Total | X+Y+Z | N |
undefinedStep 3: Fix
步骤3:修复问题
Apply refactorings using the Edit tool. For each fix:
- Verify the change preserves existing behaviour
- Keep changes minimal — only fix the identified issue
- Do not introduce new abstractions unless clearly warranted
使用编辑工具进行重构。每一项修复需遵循以下原则:
- 验证修改不会改变现有功能
- 最小化修改范围——仅修复已识别的问题
- 除非确有必要,否则不要引入新的抽象层