Execute mechanical, multi-file code refactoring using ts-morph for structural consistency.

Install

mkdir -p .claude/skills/codemod && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/9593" && unzip -o skill.zip -d .claude/skills/codemod && rm skill.zip

Installs to .claude/skills/codemod

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Mechanical, multi-file, type-aware refactors powered by ts-morph. Use when a
76 chars · catalog description✓ has a “when” trigger
Advanced

Key capabilities

  • Perform multi-file refactors
  • Apply type-aware transformations
  • Automate structural changes

How it works

It uses ts-morph to perform type-aware AST transformations across multiple files.

Inputs & outputs

You give it
transform name
You get back
refactored code

When to use codemod

  • Rename interfaces across packages
  • Migrate component structures
  • Apply batch code fixes

About this skill

🔧 Codemods

Mechanical, multi-file, type-aware refactors powered by ts-morph. Use when a pattern repeats across many files and regex is too fragile.

When to use

SituationApproach
< 5 files, simple renameFind-and-replace or manual
5–20 files, structural patternCodemod
Cross-package type migrationCodemod + build verification
One-off exploratory refactorCodemod in dry-run as a scout

How to run

# Dry run (default) — reports what would change without writing:
npx tsx codemods/run.ts <transform-name>

# Apply for real:
npx tsx codemods/run.ts <transform-name> --apply

Always dry-run first to scout the pattern before committing to a rewrite shape.

Writing a transform

Create codemods/transforms/<name>.ts exporting three things:

import type { SourceFile } from "ts-morph";

/** Human-readable summary shown by the runner. */
export const description = "Short summary of what this does";

/** Glob patterns relative to repo root. */
export const include = ["packages/visual-editor/src/**/*.ts"];

/**
 * Called once per matched file. Mutate `file` in place via ts-morph's API.
 * Return `true` if changes were made, `false` otherwise.
 */
export function transform(file: SourceFile): boolean {
  // ... your transform logic
  return changed;
}

The runner (codemods/run.ts) handles Project creation, file matching, dry-run reporting, and saving — transforms only need to focus on the AST logic.

Best practices

  • Scout first — Start with a detection-only transform (return false always) to find and count all instances before deciding on the rewrite shape.
  • Bottom-up node replacement — When replacing multiple nodes in the same file, process them in reverse order to avoid position shifts invalidating later references.
  • Capture before replace — Any data you need from a node (line number, text) must be captured before calling replaceWithText, which invalidates the node reference.
  • Verify the build — After applying, always run tsc --noEmit and the relevant test suite. Codemods can introduce type errors in consumers.

Existing transforms

TransformDescription
unseen-castRewrites unsafe (evt as StateEvent<T>).detail casts in actions to typed parameters

When not to use it

  • Simple renames
  • One-off exploratory changes
  • Regex-based changes

Prerequisites

ts-morph

Limitations

  • Requires AST logic
  • Requires build verification

How it compares

It is a mechanical, type-aware tool for large-scale refactors where regex is too fragile.

Compared to similar skills

codemod side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
codemod (this skill)05moReviewAdvanced
typescript-expert106moReviewAdvanced
agent-coder36moNo flagsIntermediate
functional32moNo flagsIntermediate

Try saying

Example prompts that trigger this skill in your AI assistant.

Search skills

Search the agent skills registry