Arguments: $ARGUMENTS
Purpose
Apply targeted refactors to specific files or components following loaded skills and CLAUDE.md rules. This is the write counterpart to /analyze (read-only).
Silent Execution
- Do NOT ask permission for ANYTHING. This flow has permissions to read and write files.
- Do NOT create tasks (no TaskCreate or TaskUpdate).
- SCOPED CHANGES ONLY: Only modify the files explicitly targeted. Never touch files outside the scope unless a rename or move is part of the refactor.
Argument Parsing
Valid flags: --file <path>, --finding <id>, --dry-run.
--file <path>: Refactor a specific file. Can be repeated (e.g.,--file src/hooks/useFoo.js --file src/hooks/useBar.js).--finding <id>: Fix a specific finding from an/analyzereport by its number (e.g.,--finding 3). Requires a prior/analyzerun in the same session.--dry-run: Show what changes would be made without writing them. Outputs a diff preview.- No flags: Refactor all files changed in the current branch (same scope as
/analyzewithout--all).
Any flag not in this list is invalid. If an unknown flag is found:
- Log:
[WARN] Unknown flag: "--fixall". Ignored. Valid flags: --file <path>, --finding <id>, --dry-run - Continue execution without it.
Flow
1. Determine scope
- If
--file: use only those files. - If
--finding: look up the finding from the last/analyzereport in this session. Extract the file(s) and rule that generated it. - If no flags: run
git diff --name-only --diff-filter=d origin/main -- '*.js' '*.jsx' '*.ts' '*.tsx'to get changed files. - If no files found: show "No files to refactor." and stop.
Show: Scope: N files — [file list]
2. Load rules
Same process as /analyze step 3:
a) Load all available skills and Read each SKILL.md file completely.
b) CLAUDE.md is already in context — use it as the highest priority rule source.
c) Rule precedence: CLAUDE.md > skills.
Show: Rules: CLAUDE.md + [skill names]
3. Read current state
Read each file in scope completely. Understand the full context before making changes — imports, exports, dependencies, component structure.
4. Plan changes
For each file, identify all applicable rule violations and plan the fix. Group related changes together.
Change categories (apply in this order):
- Structure — Extract components, split files, move logic to hooks
- Patterns — Fix anti-patterns (ternary rendering → early returns, derived state, missing cleanup, etc.)
- Cleanup — Remove dead code, commented code, console.log, unused imports
Rules for planning:
- Never rename exported symbols without updating all importers in the codebase.
- If a refactor requires creating a new file (e.g., extracting a hook), follow the folder structure from CLAUDE.md.
- If a refactor would change the public API of a component (props interface), flag it and ask the user before proceeding.
- If
--dry-run: skip to step 6 after planning.
Show plan:
Plan for src/components/OrderPage/OrderPage.jsx:
1. [Structure] Extract fetch logic to hooks/useOrders.js (business-logic-in-view)
2. [Pattern] Replace ternary rendering with early returns (no-ternary-rendering)
3. [Cleanup] Remove 2 console.log calls (console-remnants)
5. Apply changes
Execute the plan file by file. For each file:
- Make all planned changes.
- Preserve existing functionality — a refactor must NOT change behavior.
- Follow ALL CLAUDE.md rules while writing (no comments, camelCase, import Styles pattern, etc.).
- If creating new files, add them to the appropriate location per CLAUDE.md folder structure.
After each file:
- Show a brief summary:
✓ OrderPage.jsx — 3 changes applied
6. Generate report
If --dry-run:
====================================
REFACTOR — DRY RUN
====================================
src/components/OrderPage/OrderPage.jsx:
1. [Structure] Would extract fetch logic to hooks/useOrders.js
2. [Pattern] Would replace ternary rendering with early returns (lines 45-62)
3. [Cleanup] Would remove console.log (lines 12, 38)
src/hooks/useFoo.js:
1. [Cleanup] Would remove commented-out code (lines 22-28)
No files were modified.
====================================
If not dry-run:
====================================
REFACTOR — COMPLETE
====================================
Files modified:
✓ src/components/OrderPage/OrderPage.jsx — 3 changes
✓ src/hooks/useFoo.js — 1 change
Files created:
+ src/components/OrderPage/hooks/useOrders.js
Changes: 4 applied across 2 files (1 new file)
Rules applied: business-logic-in-view, no-ternary-rendering, console-remnants, dead-code
⚠ Run `/preflight` before committing to verify linter and tests pass.
====================================
Safety Rules
- NEVER delete a file. Only modify or create.
- NEVER rewrite test logic (assertions, test descriptions, mock setups). However, DO update test files when the refactor requires it: updating imports for renamed/moved files, adding mocks for newly extracted hooks, or adjusting render calls to pass new required props. The goal is to keep tests passing after the refactor — not to freeze them.
- NEVER change behavior. Refactors are structural only. If a fix would change what the component renders or when it calls an API, stop and ask the user.
- If unsure about a change, skip it. Log:
[SKIP] Could not safely refactor X — manual review needed. - ASK the user before: renaming exports, changing component props, deleting exported functions, or splitting a file into multiple files. Proceed without asking for everything else.