- name
- gitnexus-refactoring
- description
- Use when the user wants to rename, extract, split, move, or restructure code safely. Examples: "Rename this function", "Extract this into a module", "Refactor this class", "Move this to a separate file"
# Refactoring with GitNexus
## When to Use
- "Rename this function safely"
- "Extract this into a module"
- "Split this service"
- "Move this to a new file"
- Any task involving renaming, extracting, splitting, or restructuring code
## Workflow
```
1. impact({target: "X", direction: "upstream"}) โ Map all dependents
2. query({query: "X"}) โ Find execution flows involving X
3. context({name: "X"}) โ See all incoming/outgoing refs
4. Plan update order: interfaces โ implementations โ callers โ tests
```
> If "Index is stale" โ run `node .gitnexus/run.cjs analyze` in terminal.
## Checklists
### Rename Symbol
```
- [ ] rename({symbol_name: "oldName", new_name: "newName", dry_run: true}) โ preview all edits
- [ ] Review graph edits (high confidence) and ast_search edits (review carefully)
- [ ] If satisfied: rename({..., dry_run: false}) โ apply edits
- [ ] detect_changes() โ verify only expected files changed
- [ ] Run tests for affected processes
```
### Extract Module
```
- [ ] context({name: target}) โ see all incoming/outgoing refs
- [ ] impact({target, direction: "upstream"}) โ find all external callers
- [ ] Define new module interface
- [ ] Extract code, update imports
- [ ] detect_changes() โ verify affected scope
- [ ] Run tests for affected processes
```
### Split Function/Service
```
- [ ] context({name: target}) โ understand all callees
- [ ] Group callees by responsibility
- [ ] impact({target, direction: "upstream"}) โ map callers to update
- [ ] Create new functions/services
- [ ] Update callers
- [ ] detect_changes() โ verify affected scope
- [ ] Run tests for affected processes
```
## Tools
**rename** โ automated multi-file rename:
```
rename({symbol_name: "validateUser", new_name: "authenticateUser", dry_run: true})
โ 12 edits across 8 files
โ 10 graph edits (high confidence), 2 ast_search edits (review)
โ Changes: [{file_path, edits: [{line, old_text, new_text, confidence}]}]
```
**impact** โ map all dependents first:
```
impact({target: "validateUser", direction: "upstream"})
โ d=1: loginHandler, apiMiddleware, testUtils
โ Affected Processes: LoginFlow, TokenRefresh
```
**detect_changes** โ verify your changes after refactoring:
```
detect_changes({scope: "all"})
โ Changed: 8 files, 12 symbols
โ Affected processes: LoginFlow, TokenRefresh
โ Risk: MEDIUM
```
**cypher** โ custom reference queries:
```cypher
MATCH (caller)-[:CodeRelation {type: 'CALLS'}]->(f:Function {name: "validateUser"})
RETURN caller.name, caller.filePath ORDER BY caller.filePath
```
## Risk Rules
| Risk Factor | Mitigation |
| ------------------- | ----------------------------------------- |
| Many callers (>5) | Use rename for automated updates |
| Cross-area refs | Use detect_changes after to verify scope |
| String/dynamic refs | query to find them |
| External/public API | Version and deprecate properly |
## Example: Rename `validateUser` to `authenticateUser`
```
1. rename({symbol_name: "validateUser", new_name: "authenticateUser", dry_run: true})
โ 12 edits: 10 graph (safe), 2 ast_search (review)
โ Files: validator.ts, login.ts, middleware.ts, config.json...
2. Review ast_search edits (config.json: dynamic reference!)
3. rename({symbol_name: "validateUser", new_name: "authenticateUser", dry_run: false})
โ Applied 12 edits across 8 files
4. detect_changes({scope: "all"})
โ Affected: LoginFlow, TokenRefresh
โ Risk: MEDIUM โ run tests for these flows
```
View on GitHub