К основному содержанию
скилл Claude Code CX Codex Cursor GitHub Copilot

dry-refactoring — скилл для Claude Code: удаление дубликатов кода

Скилл автоматизирует рефакторинг копипастных дубликатов, которые находит jscpd. Предлагает стратегии извлечения функции, параметризации, модуля или класса и показывает, с каких файлов начать.

Установить в Claude Code · Через Skills CLI (Vercel) Все способы и платформы ↓
npx skills add kucherenko/jscpd --skill dry-refactoring -a claude-code
Открыть на GitHub

Что делает dry-refactoring

После запуска jscpd со флагом --reporters ai скилл помогает разобраться с найденными дубликатами. Он различает три типа: точные копии (самый частый случай), переименованные (переменные другие, но логика та же) и похожие с небольшими правками. Для каждого типа предлагает свою стратегию: извлечь функцию, вынести в константу, создать параметризованный вариант или объединить через стратегический объект.

Скилл учит читать вывод jscpd правильно: идентифицировать тип клона по суффиксу в отчёте, найти обе локации в коде, понять, что именно отличается. Главное — триаж: некоторые дубликаты лучше оставить (генерированный код, намеренный boilerplate вроде моделей и конфигов, пары короче 10 строк с расплывчатым названием абстракции). Рефакторит только то, что реально повторяет логику и получит чёткое имя.

Для больших кодовых баз совет запустить jscpd с флагом --summary, чтобы увидеть файлы с наибольшим процентом дубликатов — там рефакторинг окупится быстрее. После каждого изменения нужно перезапустить jscpd с теми же флагами, чтобы убедиться, что клон исчез и процент дубликатов упал.

Какие задачи решает

  • удалить точные копии-пасты и унифицировать логику
  • различить типы дубликатов и выбрать стратегию рефакторинга
  • найти все места вызова при извлечении функции
  • понять, какие дубликаты переделывать, а какие оставить
  • проверить результат и убедиться, что клон исчез

Примеры запросов агенту

  • Запусти jscpd с флагом `--reporters ai` на путь, затем помощь с рефакторингом найденных дубликатов
  • Покажи, как параметризовать две переименованные копии функции вычисления налога
  • Помоги объединить похожие функции saveUser и saveAccount с одной строкой различия

Как установить dry-refactoring

1Через Skills CLI (Vercel)

npx skills add kucherenko/jscpd --skill dry-refactoring -a claude-code

2Вручную: положить файлы

~/.claude/skills/dry-refactoring/SKILL.md

Папка скилла целиком; для одного проекта — .claude/skills/dry-refactoring/

Из того же набора2

Весь набор →

Похожие

redesign-existing-projects nexu-io Аудитирует существующий интерфейс и точечно перерабатывает сайт или приложение, сохраняя рабочую логику. Нужен, когда UI выглядит шаблонно или теряет визуальную иерархию. Claude Code CX Codex Cursor +1 1 800установок pr-feedback-quality-gate nexu-io Скилл для отслеживания feedback в pull request, разрешения конфликтов слияния и проверки исправлений. Перед коммитом запускает read-only кросс-ревью, чтобы не допустить ошибок в follow-up изменениях. Claude Code CX Codex Cursor +1 1 800установок case-review zhaoxuya520 Проверяет готовность пакета обратной разработки к передаче: отслеживает связи между доказательствами, находками и путями атаки, верифицирует хеши артефактов и генерирует отчёт без изменения кейса. Claude Code CX Codex Cursor +1 926установок how cursor Разбирает устройство подсистем, архитектуру и пути выполнения кода перед правками. Помогает понять, в какой слой или пакет поместить новую логику. Claude Code CX Codex Cursor +1 5 900установок build-graph tirth8205 Собирает или обновляет граф знаний для code review в репозитории. Нужен при первой настройке, смене ветки или крупном рефакторинге. Claude Code CX Codex Cursor +1 1 300установок hyperplan code-yeongyu Команда из пяти агентов с противоположными ролями проверяет план на прочность. В финальную схему попадает только то, что устояло под перекрёстным огнём. Claude Code CX Codex Cursor +1 576установок improve-codebase-architecture Matt Pocock Скилл ищет архитектурные узкие места в кодовой базе и показывает варианты углубления модулей в HTML-отчёте. Подойдёт для ревью перед рефакторингом. Claude Code 1 163 035установок tdd Matt Pocock Скилл управляет циклом красный-зелёный в TDD: проверяет, что тесты пишутся до кода, держит их на уровне публичных интерфейсов и не завязывает на внутреннюю структуру. Claude Code 1 141 062установок

Вопросы о dry-refactoring

Как установить dry-refactoring в Claude Code?

Выполни npx skills add kucherenko/jscpd --skill dry-refactoring -a claude-code, затем откройся файл ~/.claude/skills/dry-refactoring/SKILL.md.

Что делает dry-refactoring?

Скилл помогает рефакторить копипастные дубликаты, которые находит jscpd. Он объясняет, как различать точные, переименованные и похожие клоны, и предлагает стратегии их удаления: извлечение функции, параметризация, создание константы или базового класса.

Какую версию jscpd нужно запустить перед использованием скилла?

Запусти jscpd с флагом --reporters ai: npx jscpd --reporters ai <path>. На больших проектах добавь --summary для обзора файлов с наибольшим процентом дубликатов. Для Type-2 клонов используй --ignore-identifiers --min-tokens 70, для Type-3 — --max-gap-lines 1 --similarity.

Исходный текст

SKILL.md · MIT
---
name: dry-refactoring
description: Guided workflow to eliminate copy-paste duplication detected by jscpd. Refactor exact, renamed and near-miss clones using extract function, parameterize, module, constant, or base class strategies, starting from the hotspots the summary ranks.
---

# dry-refactoring

Guided workflow to eliminate copy-paste duplication in source code. Use after running [jscpd](../jscpd/SKILL.md) to detect clones.

## Prerequisites

First, run jscpd to identify duplications:

```bash
npx jscpd --reporters ai <path>
```

In codebases that mix related formats (e.g. JavaScript and TypeScript), add `--cross-formats` so clones spanning both are detected too:

```bash
npx jscpd --reporters ai --cross-formats "js-ts" <path>
```

On larger codebases, add `--summary` to get a refactoring-hotspot overview alongside the clone list — top files and folders with a `dup%` column showing how much of each file is duplicated:

```bash
npx jscpd --reporters ai --summary <path>
```

The default scan reports only **exact** copies, and those are the ones to refactor first: an exact clone is almost always a real copy-paste. Two more passes find copies that were edited after pasting. They are **noisier**: they ignore names, values or a few statements on purpose, so they also surface blocks that merely look alike (models and DTOs, config tables, test setup, generated code, shared idioms). Run them only after the exact clones are dealt with, one family at a time, with tight settings, and treat what they report as leads to read rather than defects to fix:

```bash
# Type-2: renamed copies (other variable names, other constants), reported as "(renamed)".
# Raise --min-tokens: with identifiers ignored, a short block is mostly placeholders.
npx jscpd --reporters ai --ignore-identifiers --min-tokens 70 <path>

# Type-3: near-miss copies (one or two edited lines, or JS/TS/Python functions with the same structure),
# reported as "[~0.91 gap]" and "[~0.85 ast]". Widen only if the tight run finds nothing.
npx jscpd --reporters ai --max-gap-lines 1 --similarity <path>
```

See the **[jscpd](../jscpd/SKILL.md)** skill for full option reference, including cross-format group syntax, the clone-kind suffixes and how to read the summary.

## Workflow

1. Run jscpd with `--reporters ai` on the target path (add `--summary` on larger codebases to pick a starting point: files with high `dup%` and high token counts pay off most)
2. Parse each clone line to identify the two duplicated locations (file + line range) and its kind: no suffix is an exact copy, `(renamed)` differs only in names or values, `[~N gap]` has a few edited lines in the middle, `[~N ast]` is a function pair with the same structure
3. Read both code fragments from the source files
4. Understand what the duplicated code does, and for renamed and similar clones list exactly what differs between the two sides
5. **Triage renamed and similar clones before touching them.** Skip the pair, and say so, when any of these holds: the two sides do different things despite the same shape (a `switch` over different enums, two reducers with unrelated semantics); the sameness is intentional boilerplate (models, DTOs, config, route tables, test fixtures); the code is generated; a shared abstraction would need a vague name like `processData`; or the pair is under about 10 lines. Only a pair that would let you delete code and give the extraction a precise name goes on to the next step
6. Design a refactoring: extract a shared function, class, module, or constant; the kind decides the strategy (below)
7. Apply the refactoring — update both locations and all other usages
8. Re-run jscpd **with the same flags** to confirm the clone is eliminated and the `dup%` of the touched files went down; a clone that was `(renamed)` will not show in a default run, so check with `--ignore-identifiers` again
9. Repeat for remaining clones, highest-impact first: exact clones, then renamed, then similar. Report the skipped candidates separately from the refactored ones, with the reason, so nobody mistakes a normalized run's count for real duplication

## Refactoring Strategies

**Extract function** — when the duplicate is a block of logic:
```ts
// Before: same block in two places
// After: shared function called from both places
```

**Extract module/utility** — when the duplicate spans multiple files in different domains:
```ts
// Move shared logic to a shared utility file and import it
```

**Extract constant or config** — when the duplicate is repeated data or configuration.

**Template/base class** — when the duplicate is structural (e.g., repeated class shape).

**Parameterize** — for `(renamed)` clones. The two sides are the same algorithm over different names or values, so the things that differ become parameters:
```ts
// Before: computeCartTotal(items) and computeBasketTotal(entries), same body, other names;
//         limits-dev.js and limits-prod.js, same shape, other numbers
// After: one function whose parameters are the identifiers that differed,
//        or one function reading the values that differed from a config object
```
A renamed clone whose only difference is a literal is a missing constant or config entry, not a missing function.

**Unify near-miss copies** — for `[~N gap]` clones. Read the unmatched lines: the gap is the one place the copies diverged, typically a guard, a log call or an extra field. Extract the common body and pass the divergence in:
```ts
// Before: saveUser and saveAccount, identical except one inserted validation line
// After: one saveRecord(record, { validate }) with the inserted line behind the option,
//        or the inserted line moved to the caller before the shared call
```
If the gap changes the meaning rather than adding a step, keep two functions but extract the shared halves.

**Merge similar functions** — for `[~N ast]` clones. The structure matches but names, literals and some statements do not. Diff the two functions first; the `ast` score tells how much is shared (`0.9` is a copy with one edit, `0.75` a copy with a couple of added statements plus renames). Extract the shared skeleton and inject what differs, as arguments, a strategy object, or a callback:
```ts
// Before: buildInvoice(order, customer, taxRate) and buildCreditNote(refund, account, vatRate):
//         same loop, same rounding, one extra guard and one extra log call in the second
// After: buildDocument(source, party, rate, { filter, onBuilt }) used by both
```
Below about `0.8` the pair usually shares an idiom, not an implementation; leave those alone unless the summary shows the file is a hotspot anyway.

Always ensure:
- All call sites are updated, not just the two reported by jscpd
- Tests still pass after refactoring
- The extracted abstraction has a clear, descriptive name
- The re-run uses the same detection flags as the run that found the clone

## Tips

- Start with clones that have the highest line count — they have the most impact
- Use the summary's `dup%` column to order the work: a large file with a high share of duplicated lines pays back first
- A clone between test files may indicate a missing test helper
- Clones across unrelated modules may signal a missing shared utility
- A cross-format clone (same logic in a `.js` and a `.ts` file, found with `--cross-formats`) often means code was ported without deleting the original — consolidate into one implementation (usually the TypeScript one) and update imports, rather than extracting a third shared copy
- Many `(renamed)` clones in one file usually mean one abstraction is missing, not many: look for the shared shape before extracting pair by pair. Many `(renamed)` clones across *test* files usually mean nothing: test cases are supposed to look alike
- Do not gate CI (`--threshold`, `--fail-on-new-clones`) on the Type-2/Type-3 passes until the team has reviewed what they report on this codebase; gate on the exact run
- `--similarity` covers JavaScript, TypeScript, Python, Java, Kotlin, Scala, C#, Go, Rust, C, C++, PHP, Ruby, Swift and Clojure, also inside Markdown code blocks and Vue, Svelte and Astro scripts; for other languages rely on the exact and `--max-gap-lines` passes
- Use `--min-lines 10` to filter noise and focus on meaningful duplications
- Keep a separate `--baseline` per set of detection flags when gating CI: renamed and similar runs fingerprint clones differently from exact runs

© kucherenko · MIT · kucherenko/jscpd

Смотрите также

Спонсоры проекта

Компании, которые помогают держать аналитику открытой