clean-codebase
关于
This skill performs automated codebase hygiene cleanup by removing dead code and unused imports while fixing lint warnings and normalizing formatting. It's designed for use when technical debt accumulates during rapid development without altering business logic or architecture. The tool focuses on fixable static analysis issues and formatting inconsistencies across the codebase.
快速安装
Claude Code
推荐npx skills add pjt222/agent-almanac -a claude-code/plugin add https://github.com/pjt222/agent-almanacgit clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/clean-codebase在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
clean-codebase
Use When
Codebase has hygiene debt:
- Lint warns piled up during rapid dev
- Unused imports + vars clutter files
- Dead code paths never removed
- Formatting inconsistent across files
- Static analysis reports fixable issues
Do NOT use for architectural refactor, bug fixes, or business logic changes. This = hygiene + automated cleanup only.
In
| Param | 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) |
Do
Step 1: Pre-Cleanup Assessment
Measure current state → quantify gains 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
→ Baseline metrics saved to lint_before.json + cloc_before.json
If err: Lint tool not found → skip automated fixes, manual review
Step 2: Fix Automated Lint Warnings
Apply safe auto fixes (spacing, quotes, semis, trailing ws).
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
→ All safe lint warns resolved; files formatted consistent
If err: Auto fixes break tests → revert, escalate
Step 3: Identify Dead Code Paths
Static analysis → unreferenced fns, unused vars, 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:
- Grep fn defs
- Grep fn calls
- Report fns defined but never called
→ dead_code.txt lists unused fns, vars, files
If err: Static analysis tool unavail → manual review recent commit history for orphans
Step 4: Remove Unused Imports
Clean import blocks → drop refs to pkgs 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
→ All unused imports removed
If err: Removing imports breaks build → used indirectly → restore + doc
Step 5: Remove Dead Code (Mode-Dependent)
Safe Mode (default):
- Remove code explicit marked deprecated
- Remove commented-out blocks (>10 lines + >6 months old)
- Remove TODO comments for completed issues
Aggressive Mode (opt-in):
- Remove all unused fns from Step 3
- Remove private methods w/ zero refs
- Remove feature flags for deprecated features
Each candidate deletion:
- Valid. zero refs in codebase
- Check git history → skip if modified last 30 days
- Remove + add entry to
CLEANUP_LOG.md
→ Dead code removed; CLEANUP_LOG.md documents all deletions
If err: Uncertain code truly dead → move to archive/ dir vs. delete
Step 6: Normalize Formatting
Consistent formatting all files (even if linters miss).
- Normalize line endings (LF vs CRLF)
- Single newline at EOF
- Remove trailing ws
- Normalize indentation (spaces vs tabs, width)
# 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:]]*$//' {} +
→ All files follow consistent formatting conventions
If err: sed breaks binary files → skip + doc
Step 7: Run Tests
Valid. cleanup didn't break functionality.
# Language-specific test command
npm test # JavaScript
pytest # Python
R CMD check # R
cargo test # Rust
→ All tests pass (or same fails as pre-cleanup)
If err: Revert incrementally → identify breaking change → escalate
Step 8: Generate Cleanup Report
Doc 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
→ Report saved to CLEANUP_REPORT.md in project root
If err: (N/A — generate report regardless)
Check
Post-cleanup:
- All tests pass (or same fails as before)
- No new lint warns introduced
- Backup created pre-delete
-
CLEANUP_LOG.mddocuments all removed code - Cleanup report generated w/ metrics
- Git diff reviewed for unexpected changes
- CI pipeline passes
Traps
-
Remove Code Still Used via Reflection: Static analysis misses dynamic calls (e.g.,
eval(), metaprogramming). Always check git history. -
Break Implicit Deps: Removing imports used by deps. Run tests after every import removal.
-
Delete Feature Flags for Active Features: Unused in current branch, but maybe active in other envs. Check deployment configs.
-
Over-Aggressive Formatting: Tools like
black/prettierreformat → unnecessary diffs. Configure tools → project style. -
Ignore Test Coverage: Can't safely clean codebases w/o tests. Low coverage → escalate for test additions first.
-
No Backup: Always create
backup_YYYYMMDD/dir pre-delete, even w/ git. -
Wrong R binary on hybrid systems: WSL / Docker,
Rscriptmaybe resolves to cross-platform wrapper vs. native R. Check w/which Rscript && Rscript --version. Prefer native R binary (e.g.,/usr/local/bin/RscriptLinux/WSL) for reliability. See Setting Up Your Environment for R path config.
→
- tidy-project-structure — Organize dir layout, update READMEs
- repair-broken-references — Fix dead links + imports
- escalate-issues — Route complex problems to specialists
- r-packages/run-r-cmd-check — Full R pkg checks
- devops/dependency-audit — Check outdated deps
GitHub 仓库
相关推荐技能
railway-docs
文档Railway Docs Skill可实时获取最新的Railway官方文档,确保回答的准确性。当开发者询问Railway功能特性、工作原理或分享docs.railway.com链接时,应优先使用此技能。它通过专门的LLM优化文档源提供最新信息,避免依赖过时记忆来回答技术问题。
n8n-code-python
文档该Skill为在n8n平台的Python代码节点中编写代码提供专家指导,特别适用于需要使用_input/_json/_node语法、Python标准库或了解n8n中Python限制的场景。它强调JavaScript应作为首选方案,仅当需要特定Python功能或对Python语法更熟悉时才使用Python。Skill提供了快速入门模板和关键注意事项,帮助开发者在n8n中高效编写Python代码。
archon
文档Archon Skill为开发者提供了基于RAG的语义搜索和项目任务管理功能,可通过REST API访问知识库。它支持文档搜索、网站爬取、文件上传和版本控制,适用于技术文档查询和项目管理场景。首次使用时需要配置Archon主机地址,建议在处理外部文档时优先使用该Skill。
n8n-code-javascript
文档这个Skill为n8n工作流中的JavaScript代码节点提供专业指导,涵盖数据处理、HTTP请求和日期操作等核心场景。它详细解释了如何正确使用n8n特有的`$input`/`$json`语法、`$helpers`工具以及DateTime对象,并包含关键的错误排查和模式选择建议。开发者通过该Skill能快速掌握Code节点的正确返回格式、数据访问方法和常见陷阱解决方案。
