技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
clean-codebase【数据与研究】Remove dead code, unused imports, fix lint warnings, and normalize formatting across a codebase without changing business logic or architecture. Use when lint warnings have piled up during rapid development, unused imports and variables clutter files, dead code paths were never removed, formatting is inconsistent, or static analysis tools report fixa…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Use when codebase has accumulated hygiene debt:
Do NOT use for architectural refactoring, bug fixes, or business logic changes. Skill focuses purely on hygiene, automated cleanup.
| Parameter | Type | Required | Description |
|---|---|---|---|
codebase_path | string | Yes | Absolute path to codebase root |
language | string | Yes | Primary language (js, python, r, rust, etc.) |
cleanup_mode | enum | No | safe (default) or aggressive |
run_tests | boolean | No | Run test suite after cleanup (default: true) |
backup | boolean | No | Create backup before deletion (default: true) |
Measure current state to quantify improvements later.
# Count lint warnings by severity
lint_tool --format json > lint_before.json
# Count lines of code
cloc . --json > cloc_before.json
# List unused symbols (language-dependent)
# JavaScript/TypeScript: ts-prune or depcheck
# Python: vulture
# R: lintr unused function checks
Got: Baseline metrics saved to lint_before.json and cloc_before.json
If fail: Lint tool not found? Skip automated fixes, focus on manual review
Apply safe automated fixes (spacing, quotes, semicolons, trailing whitespace).
JavaScript/TypeScript:
eslint --fix .
prettier --write .
Python:
black .
isort .
ruff check --fix .
R:
Rscript -e "styler::style_dir('.')"
Rust:
cargo fmt
cargo clippy --fix --allow-dirty
Got: All safe lint warnings resolved; files formatted consistently
If fail: Automated fixes introduce test failures? Revert changes, escalate
Use static analysis to find unreferenced functions, unused variables, orphaned files.
JavaScript/TypeScript:
ts-prune | tee dead_code.txt
depcheck | tee unused_deps.txt
Python:
vulture . | tee dead_code.txt
R:
Rscript -e "lintr::lint_dir('.', linters = lintr::unused_function_linter())"
General approach:
Got: dead_code.txt lists unused functions, variables, files
If fail: Static analysis tool unavailable? Manually review recent commit history for orphaned code
Clean up import blocks by removing references to packages never used.
JavaScript:
eslint --fix --rule 'no-unused-vars: error'
Python:
autoflake --remove-all-unused-imports --in-place --recursive .
R:
# Manual review: grep for library() calls, check if package used
grep -r "library(" . | cut -d: -f2 | sort | uniq
Got: All unused import statements removed
If fail: Removing imports breaks build? Used indirectly — restore, document
Safe Mode (default):
Aggressive Mode (opt-in):
For each candidate deletion:
CLEANUP_LOG.mdGot: Dead code removed; CLEANUP_LOG.md documents all deletions
If fail: Uncertain whether code truly dead? Move to archive/ directory instead
Ensure consistent formatting across all files (even if not caught by linters).
# Example: Fix line endings and trailing whitespace
find . -type f -name "*.js" -exec sed -i 's/\r$//' {} +
find . -type f -name "*.js" -exec sed -i 's/[[:space:]]*$//' {} +
Got: All files follow consistent formatting conventions
If fail: sed breaks binary files? Skip, document
Validate cleanup didn't break functionality.
# Language-specific test command
npm test # JavaScript
pytest # Python
R CMD check # R
cargo test # Rust
Got: All tests pass (or same failures as before cleanup)
If fail: Revert changes incrementally to identify breaking change, then escalate
Document all changes for review.
# Codebase Cleanup Report
**Date**: YYYY-MM-DD
**Mode**: safe | aggressive
**Language**: <language>
## Metrics
| Metric | Before | After | Change |
|--------|--------|-------|--------|
| Lint warnings | X | Y | -Z |
| Lines of code | A | B | -C |
| Unused imports | D | 0 | -D |
| Dead functions | E | F | -G |
## Changes Applied
1. Fixed X lint warnings (automated)
2. Removed Y unused imports
3. Deleted Z lines of dead code (see CLEANUP_LOG.md)
4. Normalized formatting across W files
## Escalations
- [Issue description requiring human review]
- [Uncertain deletion moved to archive/]
## Validation
- [x] All tests pass
- [x] Backup created: backup_YYYYMMDD/
- [x] CLEANUP_LOG.md updated
Got: Report saved to CLEANUP_REPORT.md in project root
If fail: (N/A — generate report regardless of outcome)
After cleanup:
CLEANUP_LOG.md documents all removed codeRemoving Code Still Used via Reflection: Static analysis misses dynamic calls (e.g., eval(), metaprogramming). Always check git history.
Breaking Implicit Dependencies: Removing imports used by dependencies. Run tests after every import removal.
Deleting Feature Flags for Active Features: Even if unused in current branch, feature flags may be active in other environments. Check deployment configs.
Over-Aggressive Formatting: Tools like black or prettier may reformat code in ways that trigger unnecessary diffs. Configure tools to match project style.
Ignoring Test Coverage: Cannot safely clean codebases without tests. Coverage low? Escalate for test additions first.
Not Backing Up: Always create backup_YYYYMMDD/ directory before deleting anything, even if using git.
Wrong R binary on hybrid systems: On WSL or Docker, Rscript may resolve to cross-platform wrapper instead of native R. Check with which Rscript && Rscript --version. Prefer native R binary (e.g., /usr/local/bin/Rscript on Linux/WSL) for reliability. See Setting Up Your Environment for R path configuration.