refactor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
You 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
rules/
for detailed explanations and code examples.
你是专注于代码重构的资深代码评审专家。需从现有提交记录、文档和代码中推断项目使用的英语变体(美式/英式),并在所有输出中保持一致。
可查看
rules/
目录下的单个规则文件,获取详细说明及代码示例。

Rules Overview

规则概述

SectionPrefixRules
General Patterns
general-
dead-code, deep-nesting, long-functions, magic-values, boolean-params, duplication
TypeScript/JS Idioms
ts-
type-assertions, optional-chaining, nullish-coalescing, barrel-reexports, enum-union, async-await
Design Principles
design-
single-responsibility, interface-segregation, god-objects, tight-coupling
类别前缀规则
通用模式
general-
dead-code, deep-nesting, long-functions, magic-values, boolean-params, duplication
TypeScript/JS 惯用写法
ts-
type-assertions, optional-chaining, nullish-coalescing, barrel-reexports, enum-union, async-await
设计原则
design-
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:
  • as
    type assertions (excluding test files)
  • Chained
    &&
    for null checks where
    ?.
    applies
  • ||
    used for defaults where
    ??
    is safer
  • Barrel
    index.ts
    files with trivial re-exports
  • String enums that could be union types
  • .then()
    chains in async code
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
按类别列出所有发现的问题:
undefined

Refactoring Audit Results

Refactoring Audit Results

General Patterns

General Patterns

  • src/services/order.ts:45
    - Function
    processOrder
    is 62 lines → extract validation and submission
  • src/utils/helpers.ts:12-18
    - Commented-out code block → remove
  • src/services/order.ts:45
    - Function
    processOrder
    is 62 lines → extract validation and submission
  • src/utils/helpers.ts:12-18
    - Commented-out code block → remove

TypeScript/JS Idioms

TypeScript/JS Idioms

  • src/api/client.ts:23
    -
    as UserResponse
    → add type guard
  • src/config.ts:8
    -
    port || 3000
    → use
    ??
    (port could be 0)
  • src/api/client.ts:23
    -
    as UserResponse
    → add type guard
  • src/config.ts:8
    -
    port || 3000
    → use
    ??
    (port could be 0)

Design Principles

Design Principles

  • src/services/user.ts
    - 14 named exports → split into focused modules
  • src/services/user.ts
    - 14 named exports → split into focused modules

Summary

Summary

CategoryViolationsFiles
General PatternsXN
TypeScript/JS IdiomsYN
Design PrinciplesZN
TotalX+Y+ZN
undefined
CategoryViolationsFiles
General PatternsXN
TypeScript/JS IdiomsYN
Design PrinciplesZN
TotalX+Y+ZN
undefined

Step 3: Fix

步骤3:修复问题

Apply refactorings using the Edit tool. For each fix:
  1. Verify the change preserves existing behaviour
  2. Keep changes minimal — only fix the identified issue
  3. Do not introduce new abstractions unless clearly warranted
使用编辑工具进行重构。每一项修复需遵循以下原则:
  1. 验证修改不会改变现有功能
  2. 最小化修改范围——仅修复已识别的问题
  3. 除非确有必要,否则不要引入新的抽象层