Project 31: Codebase Migration Assistant
Project 31: Codebase Migration Assistant
Build a Kiro CLI workflow that automates codebase migrations between languages or frameworks - converting JavaScript to TypeScript, React Class components to Hooks, or even entire framework migrations.
Quick Reference
| Attribute | Value |
|---|---|
| Difficulty | Expert (Level 4) |
| Time Estimate | 1 month |
| Primary Language | TypeScript |
| Alternative Languages | Python |
| Prerequisites | Projects 1-25, deep understanding of AST manipulation |
| Key Topics | Semantic preservation, type inference, incremental migration, behavior equivalence |
| Kiro CLI Concepts | Custom agents, file system operations, hooks, checkpoints, testing integration |
| Main Book Reference | โWorking Effectively with Legacy Codeโ by Michael Feathers |
1. Learning Objectives
By completing this project, you will:
- Master semantic-preserving code transformations: Understand how to convert code between languages while maintaining identical behavior
- Implement incremental migration strategies: Learn to break large migrations into safe, reversible steps with checkpoint-based rollback
- Design type inference systems: Build logic that derives TypeScript types from JavaScript runtime behavior and usage patterns
- Create automated test validation workflows: Ensure migration correctness by running tests after each transformation
- Build production-grade migration tooling: Understand the patterns used by tools like jscodeshift, ts-migrate, and React codemods
2. Deep Theoretical Foundation
2.1 What Is Code Migration?
Code migration is the systematic transformation of a codebase from one form to another while preserving its semantic behavior. This is fundamentally different from rewriting - migration maintains the logical structure while changing the syntactic representation.
THE MIGRATION TRANSFORMATION SPECTRUM
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ SYNTACTIC SEMANTIC โ
โ CHANGES CHANGES โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโถ โ
โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ Rename โ โ JS โ TS โ โ Class โ โ โ REST โ โ โ Monolith โ โ
โ โ Variable โ โ (add โ โ Hooks โ โ GraphQL โ โ โ Micro- โ โ
โ โ โ โ types) โ โ โ โ โ โ services โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ โ โ โ โ โ
โ โผ โผ โผ โผ โผ โ
โ Mechanical Type Pattern API Architecture โ
โ (AST-only) Addition Transform Redesign Decomposition โ
โ โ
โ RISK: Low RISK: Med RISK: High RISK: High RISK: V.High โ
โ AUTOMATION: AUTOMATION: AUTOMATION: AUTOMATION: AUTOMATION: โ
โ 100% 80% 60% 40% 20% โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Key Insight: The further right you go on this spectrum, the more human judgment is required. AI can automate the mechanical parts but should defer to humans for semantic decisions.
2.2 The Three Laws of Safe Migration
From โWorking Effectively with Legacy Codeโ (Feathers, Ch. 2):
THE THREE LAWS OF SAFE MIGRATION
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ LAW 1: BEHAVIOR PRESERVATION โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ "If you can't prove the new code behaves identically, โ
โ you haven't migrated - you've rewritten." โ
โ โ
โ Verification methods: โ
โ โข Unit tests (before = after) โ
โ โข Property-based tests (random inputs, same outputs) โ
โ โข Snapshot tests (serialized output comparison) โ
โ โข Type checks (if adding types) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ LAW 2: INCREMENTAL TRANSFORMATION โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ "Never migrate more than you can safely roll back." โ
โ โ
โ Strategies: โ
โ โข File-by-file migration โ
โ โข Function-by-function within files โ
โ โข Checkpoint after each successful step โ
โ โข Keep old and new code running in parallel โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ LAW 3: DEPENDENCY ORDER โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ "Migrate leaves before branches, imports before exports." โ
โ โ
โ Order of operations: โ
โ 1. Utility modules (no internal dependencies) โ
โ 2. Data models / types โ
โ 3. Business logic โ
โ 4. API layer โ
โ 5. UI components โ
โ 6. Entry points โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.3 JavaScript to TypeScript: The Canonical Migration
The JSโTS migration is the most common and best-understood migration path. Understanding it deeply teaches principles applicable to all migrations.
JS โ TS MIGRATION PIPELINE
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ PHASE 1: INFRASTRUCTURE โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ 1. Add tsconfig.json with permissive settings โ โ
โ โ โข "allowJs": true โ โ
โ โ โข "noImplicitAny": false (initially) โ โ
โ โ โข "strict": false (initially) โ โ
โ โ โ โ
โ โ 2. Install TypeScript and @types/* packages โ โ
โ โ โข npm install --save-dev typescript โ โ
โ โ โข npm install --save-dev @types/node @types/react ... โ โ
โ โ โ โ
โ โ 3. Update build scripts โ โ
โ โ โข tsc for type checking โ โ
โ โ โข ts-node for development โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ PHASE 2: FILE CONVERSION (Per File) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ
โ โ helpers.js โ โโโถ โ helpers.ts โ โโโถ โ Tests Pass? โ โ
โ โ โ โ (renamed) โ โ โ โ
โ โ function add( โ โ function add( โ โ Yes: Continue โ โ
โ โ a, b โ โ a: number, โ โ No: Rollback โ โ
โ โ ) { ... } โ โ b: number โ โ โ โ
โ โ โ โ ): number {...} โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ
โ โ
โ Transformation steps: โ
โ 1. Rename .js โ .ts โ
โ 2. Fix import extensions (.js โ .ts or remove) โ
โ 3. Add explicit types to function parameters โ
โ 4. Add explicit return types โ
โ 5. Fix any type errors โ
โ 6. Run tests โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ PHASE 3: STRICTNESS ESCALATION โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Enable strict mode incrementally: โ
โ โ
โ Round 1: "noImplicitAny": true โ
โ Fix all implicit any errors โ
โ โ
โ Round 2: "strictNullChecks": true โ
โ Handle null/undefined everywhere โ
โ โ
โ Round 3: "strict": true โ
โ Full TypeScript strictness โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.4 Type Inference Strategies
When migrating from JavaScript, types must be inferred. This is where AI provides significant value:
TYPE INFERENCE HIERARCHY
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ INFERENCE SOURCE CONFIDENCE EXAMPLE โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ 1. JSDoc annotations HIGHEST /** @param {n} โ
โ Already documented types * @returns {s} โ
โ */ โ
โ โ
โ 2. TypeScript inference HIGH const x = 5; โ
โ When TS can determine type // number โ
โ โ
โ 3. Usage analysis MEDIUM function f(x) { โ
โ How the value is used return x + 1; โ
โ } โ
โ // x: number โ
โ โ
โ 4. Test data analysis MEDIUM it('test', () =>โ
โ What values are passed in tests f(42)); โ
โ // f: (n) => ? โ
โ โ
โ 5. AI semantic analysis LOWER // variable nameโ
โ Based on naming conventions userId โ
โ // likely stringโ
โ โ
โ 6. Fallback: explicit any LOWEST function f( โ
โ When all else fails x: any โ
โ ) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
AI-ASSISTED TYPE INFERENCE
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ INPUT (JavaScript): โ
โ โโโโโโโโโโโโโโโโโโโ โ
โ function processUser(user) { โ
โ const name = user.firstName + ' ' + user.lastName; โ
โ const age = new Date().getFullYear() - user.birthYear; โ
โ return { name, age, email: user.email }; โ
โ } โ
โ โ
โ AI ANALYSIS: โ
โ โโโโโโโโโโโโโ โ
โ โข user.firstName, user.lastName โ string (concatenation) โ
โ โข user.birthYear โ number (arithmetic operation) โ
โ โข user.email โ string (common pattern) โ
โ โข Return type: { name: string, age: number, email: string } โ
โ โ
โ OUTPUT (TypeScript): โ
โ โโโโโโโโโโโโโโโโโโโโ โ
โ interface User { โ
โ firstName: string; โ
โ lastName: string; โ
โ birthYear: number; โ
โ email: string; โ
โ } โ
โ โ
โ interface ProcessedUser { โ
โ name: string; โ
โ age: number; โ
โ email: string; โ
โ } โ
โ โ
โ function processUser(user: User): ProcessedUser { โ
โ const name = user.firstName + ' ' + user.lastName; โ
โ const age = new Date().getFullYear() - user.birthYear; โ
โ return { name, age, email: user.email }; โ
โ } โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.5 React Class to Hooks Migration
This migration is more complex because it involves behavioral patterns, not just types:
CLASS โ HOOKS TRANSFORMATION
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ CLASS COMPONENT HOOKS EQUIVALENT โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ
โ class Counter extends Component { function Counter() { โ
โ โ
โ state = { count: 0 }; โโโถ const [count, setCount] โ
โ = useState(0); โ
โ โ
โ componentDidMount() { โโโถ useEffect(() => { โ
โ fetchData(); fetchData(); โ
โ } }, []); โ
โ โ
โ componentDidUpdate(prev) { โโโถ useEffect(() => { โ
โ if (prev.id !== this.props.id) // runs on id change โ
โ refetch(); refetch(); โ
โ } }, [id]); โ
โ โ
โ componentWillUnmount() { โโโถ useEffect(() => { โ
โ cleanup(); return () => cleanup(); โ
โ } }, []); โ
โ โ
โ handleClick = () => { โโโถ const handleClick = () => { โ
โ this.setState({ count: setCount(c => c + 1); โ
โ this.state.count + 1 }; โ
โ }); โ
โ }; โ
โ โ
โ render() { โโโถ return ( โ
โ return ( <button โ
โ <button onClick={handleClick}> โ
โ onClick={this.handleClick}> {count} โ
โ {this.state.count} </button> โ
โ </button> ); โ
โ ); } โ
โ } โ
โ } โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
LIFECYCLE MAPPING DIAGRAM
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ CLASS LIFECYCLE โโโโโโโโโโโโโโโโโโโ HOOKS โ
โ โโโโโโโโโโโโโโโ โ useEffect() โ โโโโโ โ
โ โ โ โ
โ componentDidMount โโโโถ โ deps: [] โ (run once on mount) โ
โ โ โ โ
โ componentDidUpdate โโโโถ โ deps: [x,y] โ (run when x,y change) โ
โ โ โ โ
โ componentWillUnmountโโโโถ โ return () => โ (cleanup function) โ
โ โ cleanup() โ โ
โ โ โ โ
โ getDerivedState โโโโโถ โ useMemo() โ (derived calculations) โ
โ FromProps โ โ โ
โ โ โ โ
โ shouldComponentUpdateโโโถ โ React.memo() โ (memoize component) โ
โ โ โ โ
โ getSnapshotBefore โโโโโถ โ useRef() + โ (capture before update) โ
โ Update โ useLayoutEffectโ โ
โ โโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.6 The Checkpoint Pattern for Safe Migration
Kiro CLIโs checkpoint system is essential for migration safety:
CHECKPOINT-BASED MIGRATION FLOW
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ Start: migration-start โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโ โ
โ โ Migrate file 1 โโโโโถ Tests pass? โโโโถ Create checkpoint-file-1 โ
โ โ helpers.js โ .ts โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโ โ โผ โ
โ โ โโโโโโโโโโโโโโโโโโโโ โ
โ โ โ Migrate file 2 โ โ
โ โ โ utils.js โ .ts โ โ
โ โ โโโโโโโโโโฌโโโโโโโโโโ โ
โ โ โ โ
โ โ โผ โ
โ โ Tests pass? โ
โ โ โ โ โ
โ โ Yes โ โ No โ
โ โ โผ โ โ
โ โ checkpoint- โ โ
โ โ file-2 โ โ
โ โ โ โ โ
โ โ โผ โผ โ
โ โ Continue โโโโโโโโโโโโโโโโโโโโโ โ
โ โ ... โ ROLLBACK to โ โ
โ โ โ checkpoint-file-1 โ โ
โ โ โ โ โ
โ โ โ Analyze failure โ โ
โ โ โ Fix the issue โ โ
โ โ โ Retry migration โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ Tests fail during โโโโโโโโโโโ โ
โ any migration step โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Checkpoint naming convention:
migration-{timestamp}-{phase}-{file-or-step}
Examples:
โข migration-20241222-infrastructure-tsconfig
โข migration-20241222-file-src-utils-helpers
โข migration-20241222-strictness-noImplicitAny
3. Complete Project Specification
3.1 What You Will Build
A Kiro CLI agent that:
- Analyzes a JavaScript/React codebase to create a migration plan
- Converts files incrementally with checkpoint-based safety
- Infers and adds TypeScript types using AI
- Transforms React Class components to Hooks
- Validates each step with tests before proceeding
- Provides rollback capability on failures
3.2 Functional Requirements
| Requirement | Description |
|---|---|
| Codebase Analysis | Scan project structure, identify file types, map dependencies |
| Migration Planning | Create ordered list of files to migrate based on dependency graph |
| File Transformation | Convert .js to .ts, add types, update imports |
| Pattern Transformation | Convert Class โ Hooks, CommonJS โ ESM |
| Type Inference | Use AI to infer types from usage patterns |
| Test Validation | Run tests after each file, abort on failure |
| Checkpoint Management | Create/restore checkpoints at each step |
| Progress Reporting | Show migration status and remaining work |
3.3 Non-Functional Requirements
- Safety: Never leave codebase in broken state
- Reversibility: Any change can be undone
- Idempotency: Running migration twice produces same result
- Observability: Clear logging of all transformations
4. Solution Architecture
4.1 System Architecture Diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MIGRATION ASSISTANT ARCHITECTURE โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ CLI INTERFACE โ โ
โ โ kiro-cli --agent migration-assistant โ โ
โ โ > "Migrate this codebase from JavaScript to TypeScript" โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ MIGRATION ORCHESTRATOR โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ โ
โ โ โ Analyzer โ โ Planner โ โ Executor โ โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ โ โข Scan files โ โ โข Order files โ โ โข Transform โ โ โ
โ โ โ โข Map deps โ โ โข Create plan โ โ โข Validate โ โ โ
โ โ โ โข Detect types โ โ โข Est. effort โ โ โข Checkpoint โ โ โ
โ โ โโโโโโโโโฌโโโโโโโโโ โโโโโโโโโฌโโโโโโโโโ โโโโโโโโโฌโโโโโโโโโ โ โ
โ โ โ โ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ TRANSFORMATION ENGINE โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ โ
โ โ โ Type Inferrer โ โ Code โ โ Pattern โ โ โ
โ โ โ โ โ Transformer โ โ Converter โ โ โ
โ โ โ โข JSDoc parse โ โ โ โ โ โ โ
โ โ โ โข Usage anal. โ โ โข Rename file โ โ โข ClassโHooks โ โ โ
โ โ โ โข AI inference โ โ โข Add types โ โ โข CJSโESM โ โ โ
โ โ โ โ โ โข Fix imports โ โ โข Default exp. โ โ โ
โ โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ SAFETY LAYER โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ โ
โ โ โ Test Runner โ โ Checkpoint โ โ Rollback โ โ โ
โ โ โ (Hook) โ โ Manager โ โ Handler โ โ โ
โ โ โ โ โ โ โ โ โ โ
โ โ โ โข Jest/Vitest โ โ โข Save state โ โ โข Detect fail โ โ โ
โ โ โ โข Validate โ โ โข Track files โ โ โข Restore โ โ โ
โ โ โ โข Report โ โ โข Timestamp โ โ โข Report โ โ โ
โ โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
4.2 Data Flow Diagram
MIGRATION DATA FLOW
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ USER REQUEST โ
โ "Migrate src/utils/ to TypeScript" โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ ANALYSIS PHASE โ โ
โ โ โ โ
โ โ Input: Source directory โ โ
โ โ Output: FileAnalysis[] โ โ
โ โ โ โ
โ โ { โ โ
โ โ path: "src/utils/helpers.js", โ โ
โ โ imports: ["lodash", "./constants"], โ โ
โ โ exports: ["formatDate", "parseNumber"], โ โ
โ โ hasJSDoc: true, โ โ
โ โ linesOfCode: 145, โ โ
โ โ complexity: "medium" โ โ
โ โ } โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ PLANNING PHASE โ โ
โ โ โ โ
โ โ Input: FileAnalysis[] โ โ
โ โ Output: MigrationPlan โ โ
โ โ โ โ
โ โ { โ โ
โ โ phases: [ โ โ
โ โ { name: "infrastructure", files: [...] }, โ โ
โ โ { name: "utilities", files: [...] }, โ โ
โ โ { name: "components", files: [...] } โ โ
โ โ ], โ โ
โ โ estimatedTime: "2 hours", โ โ
โ โ totalFiles: 12 โ โ
โ โ } โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ EXECUTION PHASE (Per File) โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโ โโโโโโโโโโโโโ โโโโโโโโโโโโโ โโโโโโโโโโโโโ โ โ
โ โ โ Checkpointโโโถโ Transform โโโถโ Test โโโถโ Next or โ โ โ
โ โ โ Create โ โ File โ โ Validate โ โ Rollback โ โ โ
โ โ โโโโโโโโโโโโโ โโโโโโโโโโโโโ โโโโโโโโโโโโโ โโโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
4.3 Key Data Structures
// Core types for migration assistant
interface FileAnalysis {
path: string;
type: 'javascript' | 'typescript' | 'jsx' | 'tsx';
imports: ImportAnalysis[];
exports: ExportAnalysis[];
hasJSDoc: boolean;
hasReactClasses: boolean;
linesOfCode: number;
complexity: 'low' | 'medium' | 'high';
dependencyDepth: number; // How many files depend on this
}
interface MigrationPlan {
phases: MigrationPhase[];
estimatedTime: string;
totalFiles: number;
warnings: string[];
}
interface MigrationPhase {
name: string;
description: string;
files: FileMigration[];
dependencies: string[]; // Phase names this depends on
}
interface FileMigration {
sourcePath: string;
targetPath: string;
transformations: Transformation[];
estimatedEffort: 'low' | 'medium' | 'high';
}
interface Transformation {
type: 'rename' | 'addTypes' | 'classToHooks' | 'updateImports';
description: string;
automated: boolean;
requiresReview: boolean;
}
interface MigrationState {
plan: MigrationPlan;
currentPhase: number;
currentFile: number;
completedFiles: string[];
failedFiles: FileMigrationError[];
checkpoints: Checkpoint[];
startedAt: Date;
status: 'planning' | 'executing' | 'paused' | 'completed' | 'failed';
}
interface Checkpoint {
id: string;
timestamp: Date;
phase: string;
file: string;
gitCommit: string;
description: string;
}
5. Phased Implementation Guide
Phase 1: Analysis Foundation (Days 1-7)
Goals:
- Build file scanning and analysis
- Create dependency graph
- Implement basic type detection
Tasks:
- Create file scanner that identifies JS/TS/JSX/TSX files
- Parse imports/exports using AST (babel parser)
- Build dependency graph using topological sort
- Detect JSDoc annotations
- Identify React class components
Milestone Checkpoint:
$ kiro-cli --agent migration-assistant
> "Analyze src/ for migration"
[Analysis] Scanning src/...
โโโ Found 156 JavaScript files
โโโ Found 23 React class components
โโโ Dependency depth: 5 levels
โโโ Estimated migration effort: 45 files/hour
Hint 1: Start with a simple regex-based import detector before moving to full AST parsing.
Hint 2: Use @babel/parser with { sourceType: 'module', plugins: ['jsx'] } for JavaScript files.
Phase 2: Migration Planning (Days 8-14)
Goals:
- Create ordered migration plan
- Estimate effort per file
- Generate human-readable plan
Tasks:
- Implement topological sort for file ordering
- Group files into logical phases
- Estimate effort based on complexity metrics
- Generate plan preview for user approval
- Store plan as JSON for resumability
Milestone Checkpoint:
> "Create migration plan"
[Plan] Generated migration plan:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Phase 1: Infrastructure (3 files) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ - Add tsconfig.json โ
โ - Add @types packages โ
โ - Update build scripts โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Phase 2: Utilities (12 files, ~30 min) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ - src/utils/helpers.js โ .ts โ
โ - src/utils/validators.js โ .ts โ
โ - ... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Proceed with migration? [Y/n]
Hint 3: Leaf modules (no internal dependencies) should be migrated first - they are the safest starting point.
Phase 3: Type Inference Engine (Days 15-21)
Goals:
- Extract types from JSDoc
- Infer types from usage patterns
- Use AI for complex inference
Tasks:
- Parse JSDoc comments with
doctrine - Build usage analyzer (track how values are used)
- Create AI prompts for type inference
- Generate TypeScript interfaces
- Handle common patterns (arrays, objects, callbacks)
Milestone Checkpoint:
> "Migrate src/utils/helpers.js"
[Inference] Analyzing helpers.js...
โโโ Found 3 JSDoc annotations
โโโ Inferred 5 types from usage
โโโ AI suggested 2 additional types
[Types] Generated interfaces:
interface User {
id: string;
name: string;
email: string;
}
Proceed with transformation? [Y/n]
Hint 4: For AI inference, provide 3-5 lines of context around each function, including any calls to it.
Phase 4: Transformation Engine (Days 22-28)
Goals:
- Rename files safely
- Add type annotations
- Update imports across codebase
Tasks:
- Implement file renaming with git mv
- Build AST transformer for type insertion
- Update import statements project-wide
- Handle edge cases (re-exports, barrel files)
- Integrate with test runner hook
Milestone Checkpoint:
> "Start migration"
[Migration] Phase 2: Utilities
[File 1/12] helpers.js โ helpers.ts
โโโ Added 8 type annotations
โโโ Inferred return types for 3 functions
โโโ Updated 15 import statements in other files
โโโ [Hook: test] Running tests... โ (24/24 passing)
โโโ โ Checkpoint created: migration-20241222-utils-helpers
[File 2/12] validators.js โ validators.ts...
Phase 5: Pattern Transformation (Days 29-35)
Goals:
- Convert React class components to hooks
- Handle lifecycle methods correctly
- Preserve component behavior
Tasks:
- Detect class component patterns
- Map lifecycle methods to hooks
- Convert state to useState
- Handle refs, context, error boundaries
- Validate with React-specific tests
Hint 5: Error boundaries cannot be converted to hooks - they must remain class components.
6. Testing Strategy
6.1 Unit Tests
describe('TypeInference', () => {
it('should infer number from arithmetic usage', () => {
const code = `
function double(x) {
return x * 2;
}
`;
const types = inferTypes(code);
expect(types.parameters.x).toBe('number');
expect(types.returnType).toBe('number');
});
it('should extract types from JSDoc', () => {
const code = `
/**
* @param {string} name
* @returns {boolean}
*/
function isValid(name) {
return name.length > 0;
}
`;
const types = extractJSDocTypes(code);
expect(types.parameters.name).toBe('string');
expect(types.returnType).toBe('boolean');
});
});
6.2 Integration Tests
describe('FileMigration', () => {
it('should migrate JS file to TS with types', async () => {
const input = `
export function add(a, b) {
return a + b;
}
`;
const output = await migrateFile(input, 'helpers.js');
expect(output).toContain('function add(a: number, b: number): number');
expect(output).not.toContain('any');
});
it('should update imports in dependent files', async () => {
await migrateFile('helpers.js');
const consumer = fs.readFileSync('consumer.ts', 'utf-8');
expect(consumer).toContain("from './helpers'");
expect(consumer).not.toContain("from './helpers.js'");
});
});
6.3 Rollback Tests
describe('Checkpoint Rollback', () => {
it('should restore to checkpoint on test failure', async () => {
const checkpoint = await createCheckpoint('before-migration');
await migrateFile('helpers.js');
// Simulate test failure
mockTestRunner.mockReturnValue({ passed: false });
await runTests();
// Should auto-rollback
const currentFile = fs.readFileSync('helpers.js', 'utf-8');
expect(currentFile).toBe(originalContent);
});
});
7. Common Pitfalls and Debugging
7.1 Common Migration Failures
| Pitfall | Symptom | Solution |
|---|---|---|
| Circular dependencies | Migration order fails | Detect cycles in dependency graph, migrate together |
| Dynamic imports | Missed import updates | Search for require() and dynamic import() |
| Implicit any spreading | Types donโt propagate | Enable noImplicitAny early to catch issues |
| React context types | Runtime errors | Extract context types separately before migration |
| Test file migration | Tests fail after migration | Migrate test files with their source files |
7.2 Debugging Strategies
1. Enable verbose transformation logging:
const transformer = createTransformer({
onTransform: (node, change) => {
console.log(`[Transform] ${node.type}: ${change.description}`);
}
});
2. Validate AST before and after:
const beforeAST = parse(originalCode);
const afterAST = parse(transformedCode);
// Compare structure
expect(afterAST.body.length).toBe(beforeAST.body.length);
3. Use TypeScript compiler API for validation:
import * as ts from 'typescript';
function validateTypes(code: string): ts.Diagnostic[] {
const program = ts.createProgram(['temp.ts'], {
noEmit: true,
strict: true
});
return ts.getPreEmitDiagnostics(program);
}
8. Extensions and Challenges
8.1 Beginner Extensions
- Add support for migrating test files alongside source files
- Generate migration report with statistics
- Add dry-run mode that shows changes without applying
8.2 Intermediate Extensions
- Support React Class to Hooks conversion
- Handle monorepo structures with multiple packages
- Add ESLint auto-fix for common issues post-migration
8.3 Advanced Extensions
- Multi-language migration (Python 2 to 3, Vue 2 to 3)
- Machine learning model to predict migration difficulty
- Parallel migration with conflict resolution
9. Real-World Connections
9.1 Industry Tools Using These Patterns
| Tool | Pattern Used | Company |
|---|---|---|
| ts-migrate | Incremental TS migration | Airbnb |
| jscodeshift | AST transformation | |
| react-codemod | Class to Hooks | React Team |
| flow-to-ts | Type system migration | Khan Academy |
9.2 Production Migration Stories
- Airbnbโs TypeScript Migration: 2+ million lines over 2 years using ts-migrate
- Stripeโs React Migration: 500+ class components to hooks with automated testing
- Microsoftโs TypeScript Adoption: Gradual strictness escalation pattern
10. Self-Assessment Checklist
Understanding Verification
- I can explain why leaf modules should be migrated first
- I can describe the difference between syntactic and semantic migration
- I can list at least 3 sources of type inference (JSDoc, usage, AI)
- I can explain why checkpoints are essential for safe migration
- I can describe the lifecycle method to hooks mapping
Implementation Verification
- My analyzer correctly identifies file dependencies
- My planner creates a valid topological order
- Type inference works for simple functions
- Checkpoints are created before each file transformation
- Rollback works when tests fail
- Import statements are updated across the codebase
Integration Verification
- The full migration flow works on a sample project
- AI inference improves type coverage beyond whatโs possible with JSDoc alone
- The migration can be paused and resumed
- Progress is visible throughout the migration
11. Completion Criteria
Minimum Viable Completion
- Analyzes JavaScript codebase and creates migration plan
- Renames .js to .ts files
- Adds explicit
anytypes where needed - Creates checkpoints after each file
- Runs tests after each transformation
Full Completion
- Infers types from JSDoc and usage patterns
- Uses AI for complex type inference
- Converts React class components to hooks
- Updates all import statements automatically
- Provides detailed migration report
Excellence (Going Above and Beyond)
- Handles monorepo structures
- Parallel migration with smart ordering
- Machine learning-based difficulty prediction
- Visual diff tool for reviewing changes
- Resume from any checkpoint
This guide was expanded from LEARN_KIRO_CLI_MASTERY.md. For the complete learning path, see the README.