refactor-cleaner
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRefactor & 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:
- Query for Kavak-specific patterns that might look like dead code but are used dynamicallykavak-documentation- Use
MCP tool to verify before removing Kavak SDK/platform codekavak-platform/plati_query
识别并移除死代码、重复代码和未使用的导出,保持代码库精简且易于维护。采用安全优先的方法,并提供全面的文档说明。
相关技能:
- 查询Kavak特定模式,这些模式看似死代码但实际被动态使用kavak-documentation- 在移除Kavak SDK/平台代码前,使用
MCP工具进行验证kavak-platform/plati_query
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 exportsGo:
bash
go mod tidy # remove unused deps
deadcode ./... # find unreachable code (golang.org/x/tools)
staticcheck ./... # includes unused code detectionPython:
bash
vulture . # find dead code
pip-autoremove # unused dependenciesJava:
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进行死代码检测
undefinedundefinedWorkflow
工作流程
1. Analysis Phase
1. 分析阶段
Run detection tools and categorize findings:
| Risk Level | Examples | Action |
|---|---|---|
| SAFE | Unused exports, unused deps | Remove after grep verify |
| CAREFUL | Dynamic imports possible | Manual review required |
| RISKY | Public API, shared utils | Do 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 separatelya) 仅从安全等级的项开始处理
b) 每次移除一类内容:
1. 未使用的npm依赖
2. 未使用的内部导出
3. 未使用的文件
4. 重复代码
c) 每批操作后运行测试
d) 分批提交更改4. Document Deletions
4. 记录删除操作
Update after each session:
docs/DELETION_LOG.mdmarkdown
undefined每次操作后更新:
docs/DELETION_LOG.mdmarkdown
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
undefinedSafety 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
undefinedbash
undefinedImmediate 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
参考资料
| Reference | Purpose |
|---|---|
| Tool commands and usage |
| Detailed safety procedures |
| Log format and examples |
| Common dead code patterns |
| PR template for cleanup |
| 参考资料 | 用途 |
|---|---|
| 工具命令及使用说明 |
| 详细安全流程 |
| 日志格式及示例 |
| 常见死代码模式 |
| 清理操作的PR模板 |