Automatic Code Refactor
Cross-file dependency understanding, satu instruction refactor seluruh project—bukan variable rename, ini structural refactor beneran
Refactor tradisional sesakit apa? Lo pasti tahu
Ganti nama satu fungsi, harus search-replace 100 file. Setelah selesai, takut ada yang ketinggalan. Jalanin test, ternyata ada 3 tempat yang error. Mulai debug pake binary search. Makin gede project, makin takut sentuh, ujung-ujungnya technical debt terus numpuk. Siapa juga yang mau liat-liat kode yang aneh itu. Apalagi refactor besar kayak split class atau extract module—manual dorong minimal seminggu.
OpenClaw gimana cara solve problem ini
OpenClaw bukan cuma liat file yang lo buka—dia baca seluruh project directory, analisis import chain, function call relationship, type definition, module dependency. Terus baru mulai refactor. Habis itu auto-run test pastiin nggak break existing functionality. Lo cukup bilang "ubah project ini dari class jadi hooks", sisanya dia handle.
Prompt siap pakai, langsung pake
Tiga Prompt ini cover refactor scenario paling umum. Kopas-paste aja.
Analisis semua React Class Component file di project.
Jalanin refactor:
1. Konversi tiap class component jadi function component + Hooks
2. this.state / this.setState → useState
3. componentDidMount / componentWillUnmount → useEffect
4. Extract reusable logic jadi custom Hook
5. Maintain props interface, parent component nggak perlu ubah
6. Jalanin test yang existing, pastiin nggak break
Untuk tiap file, list perubahan yang dilakukan, jelasin kenapa.
Analisis semua error handling code di project.
Audit status sekarang:
- Cari semua try-catch block, .catch() call, error callback
- List tempat yang nggak konsisten (ada yang suppress error, ada yang console.log aja, ada yang throw)
Standardisasi solution:
1. Buat AppError class yang unified (contain code, message, cause)
2. Buat global error handling middleware
3. Semua business layer pake AppError throw, jangan langsung throw new Error
4. API layer return { success: false, error: { code, message } } secara konsisten
5. Kasih list file yang diubah dan diff-nya
Gw mau migrasi JavaScript project ke TypeScript.
Jalanin step ini:
1. Analisis project structure, generate tsconfig.json (strict mode)
2. Mulai dari entry file, migrasi .js → .ts/.tsx sesuai dependency order
3. Tambahin type annotation buat semua function parameter dan return value
4. Minimize penggunaan `any`, pake specific type sebanyak mungkin
5. Install @types package buat third-party library
6. Buat types/ directory buat common type definition
7. Pastiin tsc --noEmit success, nggak ada type error
Untuk tiap file, list summary dari perubahan. Kalo ada tempat yang harus pake `any`, jelaskan alasannya.
Konfigurasi rekomendasi
Refactor task recommend setup ini, bikin AI lebih hati-hati dan kontrol.
# .openclaw/skill_config.yaml
refactor:
model: claude-opus-4-6 # Opus pilihan utama refactor, paham cross-file paling bagus
context_depth: full # Baca project structure lengkap
safety:
dry_run: true # Preview dulu, confirm baru tulis
run_tests: true # Auto-run test setelah refactor
backup: true # Backup dulu sebelum refactor
ignore:
- node_modules/
- dist/
- "*.min.js"
OpenClaw vs Copilot — perbandingan refactor ability
- Paham dependency graph keseluruhan project, ubah satu tempat auto-update semua reference
- Rename batch antar file, extract common module, split giant file
- Auto-run test setelah refactor, confirm nggak break, langsung rollback kalo ada issue
- Support custom refactor rule dan blacklist
- Mostly completion dan suggestion dalam current file
- Cross-file refactor perlu manually buka file satu-satu
- Nggak ada built-in test validation flow
- Depend on IDE refactor feature, limited by IDE plugin
Detail comparison lebih lengkap 👉 OpenClaw vs Copilot · OpenClaw vs Cursor
Real scenario: large project refactor
Model mana yang pakai buat refactor
Refactor minta code understanding ability paling tinggi, jangan hemat disini.
- Claude Opus 4.6 —— First choice buat large refactor, cross-file dependency tracking terbaik
- GPT-4o —— Small-medium refactor cukup, speed lebih cepat
- DeepSeek V3.2 —— Backup option kalo budget terbatas, hasil juga oke