refactor-cleaner

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Refactor & Dead Code Cleaner

重构与死代码清理工具

Identify and remove dead code, duplicates, and unused exports to keep the codebase lean and maintainable. Safety-first approach with comprehensive documentation.
Related Skills:
  • kavak-documentation
    - Query for Kavak-specific patterns that might look like dead code but are used dynamically
  • Use
    kavak-platform/plati_query
    MCP tool to verify before removing Kavak SDK/platform code
识别并移除死代码、重复代码和未使用的导出,保持代码库精简且易于维护。采用安全优先的方法,并提供全面的文档说明。
相关技能:
  • kavak-documentation
    - 查询Kavak特定模式,这些模式看似死代码但实际被动态使用
  • 在移除Kavak SDK/平台代码前,使用
    kavak-platform/plati_query
    MCP工具进行验证

Quick Start

快速开始

Detect project type and run appropriate analysis:
Node/TypeScript:
bash
npx knip                    # unused exports/files/deps
npx depcheck                # unused dependencies
npx ts-prune                # unused exports
Go:
bash
go mod tidy                 # remove unused deps
deadcode ./...              # find unreachable code (golang.org/x/tools)
staticcheck ./...           # includes unused code detection
Python:
bash
vulture .                   # find dead code
pip-autoremove              # unused dependencies
Java:
bash
./mvnw dependency:analyze   # unused dependencies
检测项目类型并运行相应的分析工具:
Node/TypeScript:
bash
npx knip                    # 未使用的导出/文件/依赖
npx depcheck                # 未使用的依赖
npx ts-prune                # 未使用的导出
Go:
bash
go mod tidy                 # 移除未使用的依赖
deadcode ./...              # 查找不可达代码(golang.org/x/tools)
staticcheck ./...           # 包含未使用代码检测
Python:
bash
vulture .                   # 查找死代码
pip-autoremove              # 未使用的依赖
Java:
bash
./mvnw dependency:analyze   # 未使用的依赖

Use IDE or SpotBugs for dead code detection

使用IDE或SpotBugs进行死代码检测

undefined
undefined

Workflow

工作流程

1. Analysis Phase

1. 分析阶段

Run detection tools and categorize findings:
Risk LevelExamplesAction
SAFEUnused exports, unused depsRemove after grep verify
CAREFULDynamic imports possibleManual review required
RISKYPublic API, shared utilsDo not remove
运行检测工具并对结果分类:
风险等级示例操作
安全未使用的导出、未使用的依赖经grep验证后移除
需谨慎可能存在动态导入需要人工审核
高风险公共API、共享工具类禁止移除

2. Risk Assessment

2. 风险评估

For each item to remove:
  • Grep for all references (including string patterns)
  • Check for dynamic imports
  • Verify not part of public API
  • Review git history for context
针对每个待移除项:
  • 搜索所有引用(包括字符串模式)
  • 检查是否存在动态导入
  • 验证是否不属于公共API
  • 查看git历史获取上下文信息

3. Safe Removal Process

3. 安全移除流程

a) Start with SAFE items only
b) Remove one category at a time:
   1. Unused npm dependencies
   2. Unused internal exports
   3. Unused files
   4. Duplicate code
c) Run tests after each batch
d) Commit each batch separately
a) 仅从安全等级的项开始处理
b) 每次移除一类内容:
   1. 未使用的npm依赖
   2. 未使用的内部导出
   3. 未使用的文件
   4. 重复代码
c) 每批操作后运行测试
d) 分批提交更改

4. Document Deletions

4. 记录删除操作

Update
docs/DELETION_LOG.md
after each session:
markdown
undefined
每次操作后更新
docs/DELETION_LOG.md
markdown
undefined

[YYYY-MM-DD] Refactor Session

[YYYY-MM-DD] 重构操作记录

Removed

已移除内容

  • package-name - Reason
  • src/unused-file.ts - Replaced by X
  • package-name - 原因
  • src/unused-file.ts - 被X替代

Impact

影响

  • Files: -15, Deps: -5, Lines: -2,300
undefined
  • 文件数:-15,依赖数:-5,代码行数:-2,300
undefined

Safety Rules

安全规则

Before removing ANYTHING:
  • Run detection tools
  • Grep for all references
  • Check dynamic imports
  • Run all tests
  • Create backup branch
After each removal:
  • Build succeeds
  • Tests pass
  • Commit changes
  • Update DELETION_LOG.md
在移除任何内容之前:
  • 运行检测工具
  • 搜索所有引用
  • 检查动态导入
  • 运行所有测试
  • 创建备份分支
每次移除后:
  • 构建成功
  • 测试通过
  • 提交更改
  • 更新DELETION_LOG.md

When NOT to Use

禁用场景

  • During active feature development
  • Right before production deployment
  • Without proper test coverage
  • On code you don't understand
  • 活跃的功能开发期间
  • 生产部署前夕
  • 缺乏足够测试覆盖率时
  • 针对你不理解的代码

Error Recovery

错误恢复

bash
undefined
bash
undefined

Immediate rollback if something breaks

如果出现问题立即回滚

git revert HEAD
git revert HEAD

Reinstall deps and verify (by language)

重新安装依赖并验证(按语言分类)

Node: npm install && npm run build && npm test

Node: npm install && npm run build && npm test

Go: go mod download && go build ./... && go test ./...

Go: go mod download && go build ./... && go test ./...

Python: pip install -r requirements.txt && pytest

Python: pip install -r requirements.txt && pytest

Java: ./mvnw clean install

Java: ./mvnw clean install


Then investigate: Was it a dynamic import/reflection? Update "DO NOT REMOVE" list.

随后调查原因:是否是动态导入/反射?更新“禁止移除”列表。

References

参考资料

ReferencePurpose
references/detection-tools.md
Tool commands and usage
references/safety-checklist.md
Detailed safety procedures
references/deletion-log.md
Log format and examples
references/patterns.md
Common dead code patterns
references/pr-template.md
PR template for cleanup
参考资料用途
references/detection-tools.md
工具命令及使用说明
references/safety-checklist.md
详细安全流程
references/deletion-log.md
日志格式及示例
references/patterns.md
常见死代码模式
references/pr-template.md
清理操作的PR模板