When to Use
Use this Skill in the following situations:
- Regular code health checks
- Project cleanup and consolidation
- Removing deprecated features
- Reducing package size
- Consolidating duplicate code
When NOT to Use
- When developing new features
- On the eve of production deployment
- When the codebase is unstable
- When test coverage is insufficient
Core Responsibilities
- Dead code detection - Find unused code, exports, dependencies
- Duplicate elimination - Identify and consolidate duplicate code
- Dependency cleanup - Remove unused packages and imports
- Safe refactoring - Ensure changes do not break functionality
- Documentation - Track all deletions in DELETION_LOG.md
Detection Tools
# knip - 找出未使用的檔案、exports、依賴
npx knip
# depcheck - 識別未使用的 npm 依賴
npx depcheck
# ts-prune - 找出未使用的 TypeScript exports
npx ts-prune
# eslint - 檢查未使用的變數
npx eslint . --report-unused-disable-directives
Refactoring Workflow
1. Analysis Phase
a) 平行執行檢測工具
b) 收集所有發現
c) 依風險分類:
- 安全: 未使用的 exports、依賴
- 小心: 可能透過動態 import 使用
- 風險: 公開 API、共用工具
2. Risk Assessment
For each item to be removed:
- Search for all references (grep)
- Confirm there are no dynamic imports
- Check whether it is a public API
- Review git history to understand the reason
- Test the impact on builds/tests
3. Safe Removal Workflow
a) 只從「安全」項目開始
b) 一次移除一個類別:
1. 未使用的 npm 依賴
2. 未使用的內部 exports
3. 未使用的檔案
4. 重複程式碼
c) 每批次後執行測試
d) 每批次建立 git commit
4. Duplicate Consolidation
a) 找出重複的元件/工具
b) 選擇最佳實作:
- 功能最完整
- 測試最完善
- 最近使用的
c) 更新所有 imports 使用選定版本
d) 刪除重複
e) 驗證測試仍通過
Deletion Log Format
Create/update docs/DELETION_LOG.md:
# 程式碼刪除日誌
## [YYYY-MM-DD] 重構作業
### 移除的依賴
- package-name@version - 最後使用: 從未, 大小: XX KB
- another-package@version - 已被取代: better-package
### 刪除的檔案
- src/old-component.tsx - 取代為: src/new-component.tsx
- lib/deprecated-util.ts - 功能移至: lib/utils.ts
### 整合的重複程式碼
- src/components/Button1.tsx + Button2.tsx -> Button.tsx
- 原因: 兩個實作完全相同
### 移除的 Exports
- src/utils/helpers.ts - 函數: foo(), bar()
- 原因: 程式碼庫中無引用
### 影響
- 刪除檔案: 15
- 移除依賴: 5
- 移除程式碼行數: 2,300
- 套件大小減少: ~45 KB
### 測試
- 所有單元測試通過
- 所有整合測試通過
- 手動測試完成
Safety Checklist
Before removing anything:
- Run detection tools
- Grep search for all references
- Check dynamic imports
- Review git history
- Confirm it is not a public API
- Run all tests
- Create a backup branch
- Record in DELETION_LOG.md
After each removal:
- Build succeeds
- Tests pass
- No console errors
- Commit changes
- Update DELETION_LOG.md
Common Removal Patterns
1. Unused Imports
// 移除未使用的 imports
import { useState, useEffect, useMemo } from 'react' // 只用到 useState
// 只保留使用的
import { useState } from 'react'
2. Dead Code Branches
// 移除不可達程式碼
if (false) {
// 永遠不會執行
doSomething()
}
// 移除未使用的函數
export function unusedHelper() {
// 程式碼庫中無引用
}
3. Duplicate Components
// 多個相似元件
components/Button.tsx
components/PrimaryButton.tsx
components/NewButton.tsx
// 整合為一個
components/Button.tsx (使用 variant prop)
4. Unused Dependencies
// 已安裝但未 import 的套件
{
"dependencies": {
"lodash": "^4.17.21", // 無處使用
"moment": "^2.29.4" // 已被 date-fns 取代
}
}
Emergency Recovery
If something goes wrong after removal:
# 1. 立即回滾
git revert HEAD
npm install
npm run build
npm test
# 2. 調查原因
# - 什麼失敗了?
# - 是動態 import?
# - 檢測工具遺漏了什麼?
# 3. 修正向前
# - 標記為「禁止移除」
# - 記錄為何檢測工具遺漏
# - 加上明確類型註解
Best Practices
- Start small - Remove one category at a time
- Test frequently - Run tests after each batch
- Document thoroughly - Update DELETION_LOG.md
- Be conservative - If in doubt, do not remove
- Git Commits - One commit per logical removal batch
- Branch protection - Always work on a feature branch
- Peer review - Have deletions reviewed before merging
- Monitor production - Watch for errors after deployment
Pull Request Template
## 重構: 程式碼清理
### 摘要
死代碼清理,移除未使用的 exports、依賴和重複。
### 變更
- 移除 X 個未使用的檔案
- 移除 Y 個未使用的依賴
- 整合 Z 個重複元件
- 詳見 docs/DELETION_LOG.md
### 測試
- [x] 建構通過
- [x] 所有測試通過
- [x] 手動測試完成
- [x] 無 console 錯誤
### 影響
- 套件大小: -XX KB
- 程式碼行數: -XXXX
- 依賴: -X 套件
### 風險等級
低 - 僅移除可驗證的未使用程式碼
詳見 DELETION_LOG.md。
Success Criteria
After cleanup work:
- All tests pass
- Build succeeds
- No console errors
- DELETION_LOG.md updated
- Bundle size reduced
- No regressions in production