Skip to content

Commit 7c9d585

Browse files
committed
add typescript codemod tools
1 parent 49c73dc commit 7c9d585

File tree

8 files changed

+1669
-0
lines changed

8 files changed

+1669
-0
lines changed

typescript-codemod/README.md

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# React Native Migration Toolbox
2+
3+
This directory contains ts-morph-based toolbox tools for detecting React Native migration patterns with high accuracy through AST parsing.
4+
5+
## Setup
6+
7+
1. **Install ts-morph** (required dependency):
8+
```bash
9+
npm install -g ts-morph
10+
# or in your project
11+
npm install ts-morph
12+
```
13+
14+
2. **Set AMP_TOOLBOX environment variable**:
15+
```bash
16+
export AMP_TOOLBOX="$PWD/.amp/tools"
17+
# Add to ~/.zshrc or ~/.bashrc to persist
18+
```
19+
20+
3. **Verify tools are registered**:
21+
```bash
22+
# In Amp, the tools will show up with tb__ prefix:
23+
# - tb__detect-jsx-patterns
24+
# - tb__detect-browser-apis
25+
# - tb__detect-style-usage
26+
# - tb__detect-imports
27+
```
28+
29+
## Available Tools
30+
31+
### 1. detect-jsx-patterns
32+
33+
**Purpose**: Find HTML-like JSX elements that need transformation to React Native components.
34+
35+
**Parameters**:
36+
- `filePath` (string): Absolute path to .tsx/.jsx/.ts/.js file
37+
38+
**Returns**:
39+
```json
40+
{
41+
"totalHtmlElements": 15,
42+
"elementCounts": {
43+
"div": 8,
44+
"span": 4,
45+
"button": 2,
46+
"img": 1
47+
},
48+
"patterns": [
49+
{
50+
"element": "div",
51+
"line": 10,
52+
"attributes": [...],
53+
"suggestedReplacement": "View"
54+
}
55+
],
56+
"migrationGuides": ["components-migration.md"]
57+
}
58+
```
59+
60+
**Use in Amp**:
61+
```
62+
Use tb__detect-jsx-patterns to analyze App.tsx and tell me what components need migration
63+
```
64+
65+
### 2. detect-browser-apis
66+
67+
**Purpose**: Find browser API usage (localStorage, window, document, etc.).
68+
69+
**Parameters**:
70+
- `filePath` (string): Absolute path to file
71+
72+
**Returns**:
73+
```json
74+
{
75+
"totalBrowserAPIs": 5,
76+
"patterns": {
77+
"storage": [{
78+
"api": "localStorage",
79+
"usage": "localStorage.getItem('token')",
80+
"line": 25,
81+
"suggestedReplacement": "AsyncStorage..."
82+
}],
83+
"window": [...],
84+
"document": [...]
85+
},
86+
"migrationGuides": ["browser-apis-migration.md"]
87+
}
88+
```
89+
90+
**Use in Amp**:
91+
```
92+
Analyze auth.ts with tb__detect-browser-apis to find storage issues
93+
```
94+
95+
### 3. detect-style-usage
96+
97+
**Purpose**: Find styling patterns (className, CSS imports, inline styles).
98+
99+
**Parameters**:
100+
- `filePath` (string): Absolute path to file
101+
102+
**Returns**:
103+
```json
104+
{
105+
"totalStyleIssues": 12,
106+
"patterns": {
107+
"cssImports": [{
108+
"import": "import './App.css'",
109+
"line": 2,
110+
"suggestedAction": "Remove CSS import..."
111+
}],
112+
"classNameUsage": [...],
113+
"inlineStyles": [...]
114+
},
115+
"criticalTransformations": [
116+
"Replace all className props with style props",
117+
"Convert CSS property names from kebab-case to camelCase"
118+
]
119+
}
120+
```
121+
122+
**Use in Amp**:
123+
```
124+
Use tb__detect-style-usage on Button.tsx to see what styling needs migration
125+
```
126+
127+
### 4. detect-imports
128+
129+
**Purpose**: Analyze imports and suggest React Native package equivalents.
130+
131+
**Parameters**:
132+
- `filePath` (string): Absolute path to file
133+
134+
**Returns**:
135+
```json
136+
{
137+
"summary": {
138+
"total": 15,
139+
"needsReplacement": 3,
140+
"reactNativeReady": 10,
141+
"needsReview": 2
142+
},
143+
"imports": {
144+
"needsReplacement": [{
145+
"package": "react-router-dom",
146+
"suggestedReplacement": "@react-navigation/native",
147+
"migrationGuide": "browser-apis-migration.md"
148+
}]
149+
}
150+
}
151+
```
152+
153+
**Use in Amp**:
154+
```
155+
Run tb__detect-imports on index.tsx to check package compatibility
156+
```
157+
158+
## Example Workflow
159+
160+
```bash
161+
# 1. Detect all patterns in a file
162+
Use tb__detect-jsx-patterns, tb__detect-browser-apis, tb__detect-style-usage,
163+
and tb__detect-imports on src/components/Dashboard.tsx
164+
165+
# 2. Review the results and plan migration
166+
167+
# 3. Use codemod tool with specific guidance based on detected patterns
168+
Use the codemod tool to migrate Dashboard.tsx. Based on detection:
169+
- Transform <div> to <View> (12 instances)
170+
- Replace localStorage with AsyncStorage (3 instances)
171+
- Convert className to StyleSheet (8 instances)
172+
- Replace react-router with @react-navigation
173+
```
174+
175+
## Integration with Codemod
176+
177+
These tools are designed to work with the codemod scope subagent:
178+
179+
1. **Scope phase**: Uses these tools to detect patterns
180+
2. **Planning phase**: Determines which migration guides apply
181+
3. **Execution phase**: Applies transformations based on guides
182+
183+
## Benefits Over Grep
184+
185+
**Deterministic** - AST parsing eliminates false positives
186+
**Context-aware** - Knows if code is in strings, comments, etc.
187+
**Structured output** - Returns actionable data with line numbers
188+
**Type-safe** - Understands TypeScript types and JSX
189+
**Comprehensive** - Finds nested patterns Grep would miss
190+
191+
## Limitations
192+
193+
- **JS/TS only** - Won't work on CSS, HTML, config files (use Grep for those)
194+
- **Parsing errors** - Syntax errors in source will cause tool to fail
195+
- **Performance** - Slower than Grep for simple text searches
196+
197+
## Troubleshooting
198+
199+
### "ts-morph not found"
200+
```bash
201+
npm install -g ts-morph
202+
```
203+
204+
### Tool not showing up in Amp
205+
```bash
206+
# Check AMP_TOOLBOX is set
207+
echo $AMP_TOOLBOX
208+
209+
# Verify tools are executable
210+
ls -la .amp/tools/
211+
# All should have -rwxr-xr-x permissions
212+
```
213+
214+
### JSON parse errors
215+
Ensure you're passing valid JSON to the tools. Amp handles this automatically.

0 commit comments

Comments
 (0)