codebase-refactoring
它能做什么
它是针对整个项目的引导式清理计划,涵盖重复代码、未使用的代码和过于复杂的文件。适用于希望减少维护工作的开发者。
适用场景
- ✓当团队要求清理项目或偿还技术债务时。
- ✓当开发者想知道代码库中哪一部分最需要先处理时。
- ✓当团队想在清理前后衡量项目的健康评分时。
技能链接
https://github.com/kucherenko/jscpd/blob/master/skills/codebase-refactoring/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请清理这个项目。先处理最大的问题,然后给我看清理后的健康评分。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能运行代码分析工具,对问题进行排序,然后引导 AI 修复重复代码、无用代码和复杂文件,并在最后再次衡量评分。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
codebase-refactoring
A guided pass over a codebase's three biggest maintenance costs — duplicated code, dead code, and complexity — using jscpd to find each and a strategy to fix it. Use this skill when asked to "clean up", "improve", "refactor" or "pay down tech debt in" a codebase, rather than to fix one specific bug.
Start here: measure, then prioritize
npx jscpd --health --reporters ai <path>
health 74 B (duplication 75, dead-code 72, complexity 76; 93 code lines)
The health score is a weighted mix of three sub-scores, each a share of the
code lines — high is good, 100 is clean. The lowest of the three names where
the codebase actually hurts most; if it isn't the order below, do that
dimension first instead. n/a on a dimension (e.g. dead code n/a) means
jscpd could not measure it — dead-code detection needs JavaScript,
TypeScript or Python to be a real share of the code — skip that step rather
than treating it as clean.
For the full picture behind that score — largest duplicated formats, most
complex files, largest dead-code findings, all in one screen — run npx jscpd --dashboard instead. Its console output (the default) is the only
reporter with that per-file detail; --reporters ai on --dashboard prints
the same one-line summary shown above, not the breakdown, so don't reach for
it expecting more than the score. Use --reporters json on --dashboard if
you need the detail in a parseable form.
The three passes below are otherwise independent and safe to run in any order; duplication → dead code → complexity is the default because fixing duplication first means step 2 isn't wasting effort tracing call sites that a later extract-function pass will move anyway, and clearing dead code before step 3 means the files ranked for simplification are ones actually worth simplifying, not ones about to be deleted.
Step 1 — Duplicated code
npx jscpd --reporters ai --summary <path>
Find and read each clone, decide whether it is a real copy worth merging, and apply the matching refactoring strategy (extract function, parameterize, extract a shared module or constant, or a base class/template for structural duplication). Full guided workflow, triage rules for renamed/near-miss clones, and worked examples per clone kind:
→ dry-refactoring — run this now, then
come back here for steps 2 and 3. Its own --summary hotspot ranking is the
same one the health score's duplication dimension reads from, so a file it
flags as a duplication hotspot is the same file that dragged the score down.
Step 2 — Dead code
npx jscpd --dead-code --reporters ai <path>
basta dead code report — 18 findings across 30 files (22.1% of 272 lines)
Confidence is 0-100; anything below 90 has a listed reason it may be wrong. Verify a finding before deleting the code.
unused-file typescript/src/legacy-export.ts:1:1 confidence=95 typescript/src/legacy-export.ts is never imported and is not an entry point
unused-export typescript/src/invoice.ts:21:17 confidence=85 exported function `renderReceipt` is never imported
unused-symbol typescript/src/invoice.ts:29:10 confidence=90 function `describeTotal` is never used in typescript/src/invoice.ts
unused-import typescript/src/invoice.ts:2:10 confidence=100 `roundToCents` is imported but never used
This is a graph traversal from the project's entry points (package.json
main/bin/exports/scripts, pyproject.toml scripts, framework
conventions, or --entry <glob>), not a reference count — a helper whose
only caller is itself dead is reported too, so deleting one finding can make
another true that wasn't reported yet. Covers JavaScript, TypeScript, JSX,
TSX, Vue, Svelte, Astro and Python.
The four categories, and what to do with each
| Category | Meaning | Usual fix |
|---|---|---|
unused-file |
Never imported anywhere, not an entry point | Delete the file |
unused-export |
Exported, but no other file imports it | Delete it, or drop the export keyword if something in the same file still uses it |
unused-symbol |
A module-private declaration nothing in its own file references | Delete it |
unused-import |
Imported but never referenced in the file | Delete the import line |
Workflow
- Run with
--reporters ai(add--dead-code-categoriesto focus on one category,--include-testsif test-only usage shouldn't count as "used", `--i
常见问题
它如何决定先修复什么?
它使用一个综合重复、无用代码和复杂度的健康评分,并从最低的那一项开始。
它适用于所有语言吗?
无用代码检测需要 JavaScript、TypeScript 或 Python 在代码中占较大比例。
它会修复错误吗?
它针对的是维护问题,而不是某一个特定的错误。
详情
作者的原始描述
Three-part workflow to improve overall codebase health with jscpd — find and fix duplicated code, find and remove or refactor dead code, then find and simplify the largest/most complex files. Starts from --dashboard/--health to prioritize and ends by re-measuring the score.
repo: kucherenko/jscpd
热度
- GitHub 星标
- 6.5k
- 排名
- #14188
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
code-migration
6.5k帮你的智能体逐个函数地将代码从一种语言或框架移植到另一种,先写测试,并以原始代码为基准衡量进度。
compare-codebases
6.5k帮你的智能体逐个函数地比较两个代码文件夹,即使跨越不同语言,也能看出哪些函数相互对应,哪些缺失。
dry-refactoring
6.5k帮你的智能体通过将重复的代码块合并为共享函数来删除项目中复制粘贴的代码,并从重复最多的部分开始处理。
jscpd
6.5k帮你的智能体在项目中查找复制粘贴的代码,包括完全相同、重命名或近似的副本,并衡量有多少代码是重复的。