refactor
Code refactoring with AI assistance. Use for modernizing code, converting JavaScript to TypeScript, class to hooks conversions, and systematic renaming.
What this skill does
# Refactoring Assistant
Systematic code refactoring with AI guidance.
## Prerequisites
```bash
# Gemini for analysis and suggestions
pip install google-generativeai
export GEMINI_API_KEY=your_api_key
# AST-based tools
npm install -g jscodeshift
npm install -g typescript
```
## Refactoring Operations
### JavaScript to TypeScript
```bash
# Step 1: Rename file
mv file.js file.ts
# Step 2: AI assistance for types
CODE=$(cat file.ts)
gemini -m pro -o text -e "" "Convert this JavaScript to TypeScript with proper types:
$CODE
Requirements:
- Add explicit types for function parameters and returns
- Create interfaces for object shapes
- Use strict TypeScript (no any unless necessary)
- Preserve functionality exactly"
```
### Class to React Hooks
```bash
CODE=$(cat ClassComponent.tsx)
gemini -m pro -o text -e "" "Convert this React class component to functional component with hooks:
$CODE
Requirements:
- Convert state to useState
- Convert lifecycle methods to useEffect
- Convert class methods to functions
- Preserve all functionality
- Use proper TypeScript types"
```
### Modernize Code
```bash
CODE=$(cat legacy.ts)
gemini -m pro -o text -e "" "Modernize this code to current best practices:
$CODE
Apply:
- ES2023+ features where appropriate
- Modern TypeScript patterns
- Current framework idioms
- Better error handling
- Improved readability"
```
### Systematic Rename
```bash
# Using sed for simple renames
find src -name "*.ts" -exec sed -i '' 's/oldName/newName/g' {} +
# Using ripgrep to preview
rg "oldName" src/
# With jscodeshift for AST-safe rename
npx jscodeshift -t rename-transform.ts src/
# AI-assisted rename planning
gemini -m pro -o text -e "" "I want to rename 'oldName' to 'newName' across the codebase.
Files that use it:
$(rg -l "oldName" src/)
Suggest:
1. Order of changes to avoid breaks
2. Any tricky cases to watch for
3. Tests to run after"
```
## Analysis Commands
### Find Refactoring Opportunities
```bash
gemini -m pro -o text -e "" "Analyze this code for refactoring opportunities:
$(cat src/module.ts)
Look for:
- Code duplication
- Long functions
- Deep nesting
- Magic numbers/strings
- Poor naming
- Missing abstractions
- Tight coupling"
```
### Complexity Analysis
```bash
# Find complex functions
rg "function|=>\s*{" src/ -A 50 | head -200
# Ask AI to identify complexity
gemini -m pro -o text -e "" "Identify the most complex functions in this code and suggest simplifications:
$(cat src/complex-file.ts)"
```
## Safe Refactoring Workflow
### Step 1: Ensure Test Coverage
```bash
# Check coverage before refactoring
npm test -- --coverage
# Identify untested code
npx jest --coverage --coverageReporters=text | grep -E "^(File|.*\|)"
```
### Step 2: Plan the Refactoring
```bash
gemini -m pro -o text -e "" "Plan a safe refactoring for:
CURRENT CODE:
$(cat src/file.ts)
GOAL: [what you want to improve]
Provide:
1. Step-by-step plan
2. Risk assessment
3. Rollback strategy
4. Tests to add first"
```
### Step 3: Small, Tested Changes
```bash
# Make one small change
# Run tests
npm test
# Commit if green
git add -A && git commit -m "refactor: [specific change]"
# Repeat
```
### Step 4: Verify Behavior
```bash
# Compare before/after behavior
# Run integration tests
# Manual testing if needed
```
## Common Refactorings
### Extract Function
```bash
gemini -m pro -o text -e "" "Extract a reusable function from this code:
$(cat src/file.ts | sed -n '10,50p')
Create a well-named function with:
- Clear parameters
- Typed return value
- JSDoc comment
- Single responsibility"
```
### Simplify Conditionals
```bash
gemini -m pro -o text -e "" "Simplify these conditionals:
\`\`\`typescript
$(rg -A 10 "if.*{" src/file.ts)
\`\`\`
Use:
- Early returns
- Guard clauses
- Lookup tables where appropriate
- Optional chaining"
```
### Remove Duplication
```bash
# Find similar code
gemini -m pro -o text -e "" "Find duplicate or similar code patterns in:
$(cat src/*.ts)
Suggest how to DRY it up with:
- Shared functions
- Higher-order functions
- Generics if TypeScript"
```
## jscodeshift Transforms
### Create Transform
```typescript
// rename-transform.ts
export default function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.Identifier, { name: 'oldName' })
.replaceWith(j.identifier('newName'))
.toSource();
}
```
### Run Transform
```bash
# Dry run
npx jscodeshift -d -p -t transform.ts src/
# Apply
npx jscodeshift -t transform.ts src/
```
## Best Practices
1. **Test first** - Don't refactor untested code
2. **Small steps** - One change at a time
3. **Commit often** - Each working state
4. **Preserve behavior** - Refactoring isn't rewriting
5. **Use tools** - AST transforms > find-replace
6. **Review diffs** - Verify each change
7. **Run tests continuously** - Catch breaks early
Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.