timothywarner/copilot-dev/legacy-code-refactor
Claude Code and agent skill by timothywarner/copilot-dev
- community
Checking that the SKILL.md file is still in the repository.
Systematically modernize legacy JavaScript or TypeScript code. Use when asked to refactor, modernize, clean up, or update old code. Identifies anti-patterns (var declarations, callback hell, prototype chains, magic numbers, missing error handling), explains each change, applies safe transformations, and adds tests. Step-by-step methodology ensures the code behaves identically before and after refactoring.
Install
npx vskill@latest install timothywarner/copilot-dev/legacy-code-refactorSKILL.md
Legacy Code Refactor Skill
You are an expert in safely modernizing legacy JavaScript/TypeScript codebases. When this skill activates, follow the five-step methodology below precisely. Never change observable behavior — only improve the structure, readability, and maintainability.
The Core Principle
"Refactoring is changing the internal structure of software without changing its observable behavior."— Martin Fowler
Every transformation must be provably safe: the code must do the same thing before and after.
Step 1: Audit — Identify All Anti-Patterns
Read the target file completely, then produce an audit table listing every issue found:
| # | Line | Anti-Pattern | Why It's a Problem | Safe Fix | |
|---|---|---|---|---|---|
| 1 | 3 | var in function scope | var is function-scoped and hoisted; causes subtle bugs | Replace with const or let | |
| 2 | 12 | Nested callback (callback hell) | Impossible to reason about error flow; unreadable | Rewrite with async/await | |
| 3 | 25 | prototype method | No encapsulation; harder to subclass; verbose | Convert to class with methods | |
| 4 | 31 | Magic number 86400000 | Intent is invisible; breaking change if the value changes | Extract to named constant MS_PER_DAY | |
| 5 | 44 | No error handling | Unhandled rejections crash Node.js silently | Wrap in try/catch | |
| 6 | 55 | Mixed concerns | Validation + persistence + response in one function | Separate into distinct functions |
Present this table to the user before making any changes.
Step 2: Catalog of Anti-Patterns and Safe Fixes
2.1 var → const / let
Safe when: The variable is not re-declared in the same scope (var allows re-declaration; let/const do not).
2.2 Callback Hell → async/await
Safe when: All callers can be updated to use await or .then(). If the function is part of a public API using the Node.js callback convention, expose both forms during a transition period.
2.3 Prototype Chains → ES2022 Classes
Safe when: No code directly accesses .prototype on the constructor function. Check for Animal.prototype.newMethod = ... patterns outside the class definition before converting.
2.4 Magic Numbers → Named Constants
2.5 No Error Handling → try/catch + Typed Errors
2.6 String Concatenation → Template Literals
2.7 Equality (==) → Strict Equality (===)
Exception: value == null (checking for both null and undefined) is an accepted pattern when intentional — comment it.
2.8 Mixed Concerns → Single Responsibility
Step 3: Apply Transformations Safely
Apply changes in this sequence to minimize risk:
- Constants first — rename magic numbers and magic strings (zero behavioral change)
- `var` → `const`/`let` — mechanical substitution; fix any re-declarations
- Template literals — string concatenation to template literals (zero behavioral change)
- `==` → `===` — go one-by-one; verify each comparison's intent before changing
- Prototype → class — rewrite the shape, then verify instance checks (
instanceof) still work - Callbacks → async/await — rewrite the function, then update every call site
- Error handling — add try/catch around every
awaitthat can throw - Concern separation — extract functions last, after the code is clean
After each step: run the tests (or note if no tests exist).
Step 4: Document Every Change
For every changed line or block, add a comment explaining:
Step 5: Write Tests for the Refactored Code
After refactoring, generate a Jest test file that:
- Tests the public API of each function (same inputs → same outputs as before)
- Tests error paths that now have explicit handling
- Uses
describeblocks matching function names - Includes a comment:
// These tests verify behavior was preserved during refactoring
Output Checklist
Before delivering refactored code:
- [ ] Audit table presented before any changes
- [ ] Every anti-pattern from the catalog addressed
- [ ] No
vardeclarations remain (unless intentionally preserved with comment) - [ ] No callback-style async code (all converted to async/await)
- [ ] No magic numbers (all extracted to named constants)
- [ ] Every
awaitwrapped in try/catch or delegated via throw - [ ]
===used everywhere (no bare==without explanation) - [ ] Refactor comments added to every changed section
- [ ] Test file generated covering happy path + error paths
Reference Files
legacy-example.js— Legacy JavaScript file with intentional anti-patterns (the "before")refactored-example.js— Same code after applying this skill (the "after")
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryChecking.github/skills/legacy-code-refactor/SKILL.md
Popularity
- Repository stars
- 43
- Repository forks
- 44
- 7-day trend score
- 19
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 19 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/timothywarner/copilot-dev/legacy-code-refactor)