Skip to content

Commit 2f114e1

Browse files
committed
Adds fragment to shorthand example
1 parent c70925b commit 2f114e1

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Transform React Fragment to Shorthand Syntax
2+
3+
This example demonstrates how to use Codegen to automatically convert React Fragment components to the shorthand syntax (<>). The script makes this process simple by handling all the tedious manual updates automatically.
4+
5+
> [!NOTE]
6+
> This codemod helps modernize React codebases by using the more concise fragment syntax while maintaining functionality.
7+
8+
## How the Migration Script Works
9+
10+
The script automates the entire conversion process in a few key steps:
11+
12+
1. **Fragment Detection**
13+
```jsx
14+
// From:
15+
<Fragment>
16+
<div>Hello</div>
17+
<div>World</div>
18+
</Fragment>
19+
20+
// To:
21+
<>
22+
<div>Hello</div>
23+
<div>World</div>
24+
</>
25+
```
26+
27+
2. **Import Cleanup**
28+
```typescript
29+
// From:
30+
import React, { Fragment } from 'react';
31+
32+
// To:
33+
import React from 'react';
34+
```
35+
36+
## Why This Makes Migration Easy
37+
38+
1. **Zero Manual Updates**
39+
- Codegen SDK handles all Fragment replacements
40+
- Automatically cleans up imports
41+
42+
2. **Consistent Changes**
43+
- Ensures all Fragments are converted
44+
- Maintains code functionality
45+
46+
3. **Safe Transformations**
47+
- Preserves JSX structure
48+
- Handles nested Fragments correctly
49+
50+
## Running the Migration
51+
52+
53+
The script will:
54+
1. Find all Fragment components
55+
2. Convert them to shorthand syntax
56+
3. Clean up Fragment imports
57+
4. Preserve other React imports
58+
59+
## Learn More
60+
61+
- [React Fragments](https://react.dev/reference/react/Fragment)
62+
- [JSX Fragments](https://react.dev/reference/jsx#jsx-fragments)
63+
- [Codegen Documentation](https://docs.codegen.com)
64+
- [More on Codegen SDK jsx elements API](https://docs.codegen.com/api-reference/typescript/JSXElement#jsxelement)
65+
## Contributing
66+
67+
Feel free to submit issues and enhancement requests!
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import codegen
2+
from codegen import Codebase
3+
from codegen.sdk.enums import ProgrammingLanguage
4+
5+
@codegen.function("fragment_to_shorthand")
6+
def run(codebase: Codebase):
7+
print("🔍 Starting Fragment syntax conversion...")
8+
9+
for file in codebase.files:
10+
print(f"📁 Processing: {file.filepath}")
11+
12+
fragments_found = False
13+
14+
# Convert Fragment components to shorthand
15+
for element in file.jsx_elements:
16+
if element.name == "Fragment":
17+
print(f"🔄 Converting Fragment in {file.filepath}")
18+
element.set_name("") # Convert to <> syntax
19+
fragments_found = True
20+
21+
# Clean up Fragment imports if we found and converted any
22+
if fragments_found:
23+
for import_stmt in file.import_statements:
24+
for imp in import_stmt.imports:
25+
if imp.name == "Fragment":
26+
print(f"🧹 Removing Fragment import from {file.filepath}")
27+
imp.remove()
28+
29+
if fragments_found:
30+
print(f"✨ Completed conversion in {file.filepath}")
31+
codebase.commit()
32+
33+
if __name__ == "__main__":
34+
print("🎯 Starting Fragment to shorthand conversion...")
35+
codebase = Codebase.from_repo("RocketChat/Rocket.Chat",commit="a4f2102af1c2e875c60cafebd0163105bdaca678",programming_language=ProgrammingLanguage.TYPESCRIPT)
36+
run(codebase)
37+
print("✅ Done! All Fragments converted to shorthand syntax!")

0 commit comments

Comments
 (0)