Skip to content

Commit d9eef85

Browse files
author
codegen-bot
committed
adds README.md
1 parent 68f4323 commit d9eef85

File tree

2 files changed

+100
-23
lines changed

2 files changed

+100
-23
lines changed

remove_default_exports/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Remove Default Exports in TypeScript
2+
3+
This codemod demonstrates how to automatically convert default exports to named exports in your TypeScript codebase. The migration script makes this process simple by handling all the tedious manual updates automatically.
4+
5+
## How the Migration Script Works
6+
7+
The script automates the entire migration process in a few key steps:
8+
9+
1. **File Detection and Analysis**
10+
```python
11+
codebase = Codebase("./")
12+
for file in codebase.files:
13+
if "/shared/" not in file.filepath:
14+
continue
15+
```
16+
- Automatically identifies shared TypeScript files
17+
- Analyzes export structures
18+
- Determines necessary export modifications
19+
20+
2. **Export Conversion**
21+
```python
22+
for export in file.exports:
23+
if export.is_default_export():
24+
export.make_non_default()
25+
```
26+
- Converts default exports to named exports
27+
- Ensures corresponding non-shared files are updated
28+
- Preserves existing export configurations
29+
30+
## Common Migration Patterns
31+
32+
### Default Export Conversion
33+
```typescript
34+
// Before
35+
export default function myFunction() {}
36+
37+
// After
38+
export function myFunction() {}
39+
```
40+
41+
### Re-export Conversion
42+
```typescript
43+
// Before
44+
export { default } from './module';
45+
46+
// After
47+
export { myFunction } from './module';
48+
```
49+
50+
## Running the Migration
51+
52+
```bash
53+
# Install Codegen
54+
pip install codegen
55+
# Run the migration
56+
python run.py
57+
```
58+
59+
## Learn More
60+
61+
- [TypeScript Documentation](https://www.typescriptlang.org/docs/)
62+
- [Codegen Documentation](https://docs.codegen.com)
63+
64+
## Contributing
65+
66+
Feel free to submit issues and enhancement requests!

remove_default_exports/run.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
1+
import codegen
12
from codegen import Codebase
23
from codegen.sdk.typescript.file import TSFile
34

4-
codebase = Codebase("./")
5+
@codegen.function("remove-default-exports")
6+
def run(codebase: Codebase):
7+
"""Convert default exports to named exports in TypeScript files.
58
6-
for file in codebase.files:
7-
target_file = file.filepath
8-
if not target_file:
9-
print(f"⚠️ Target file not found: {target_file} in codebase")
10-
continue
9+
This script:
10+
1. Identifies shared TypeScript files with default exports.
11+
2. Converts default exports to named exports.
12+
3. Ensures corresponding non-shared files are updated.
13+
"""
14+
for file in codebase.files:
15+
target_file = file.filepath
16+
if not target_file:
17+
print(f"⚠️ Target file not found: {target_file} in codebase")
18+
continue
1119

12-
# Get corresponding non-shared file
13-
non_shared_path = file.filepath.replace('/shared/', '/')
14-
if not codebase.has_file(non_shared_path):
15-
print(f"⚠️ No matching non-shared file for: {non_shared_path}")
16-
continue
20+
# Get corresponding non-shared file
21+
non_shared_path = file.filepath.replace('/shared/', '/')
22+
if not codebase.has_file(non_shared_path):
23+
print(f"⚠️ No matching non-shared file for: {non_shared_path}")
24+
continue
1725

18-
non_shared_file = codebase.get_file(non_shared_path)
19-
20-
# Process individual exports
21-
if isinstance(file, TSFile):
26+
non_shared_file = codebase.get_file(non_shared_path)
2227
print(f"📄 Processing {file.filepath}")
2328

24-
for export in file.exports:
25-
# Handle default exports
26-
if export.is_reexport() and export.is_default_export():
27-
print(f" 🔄 Converting default export '{export.name}'")
28-
default_export = next((e for e in non_shared_file.default_exports), None)
29-
if default_export:
30-
default_export.make_non_default()
29+
# Process individual exports
30+
if isinstance(file, TSFile):
31+
for export in file.exports:
32+
# Handle default exports
33+
if export.is_reexport() and export.is_default_export():
34+
print(f" 🔄 Converting default export '{export.name}'")
35+
default_export = next((e for e in non_shared_file.default_exports), None)
36+
if default_export:
37+
default_export.make_non_default()
38+
39+
print(f"✨ Fixed exports in {file.filepath}")
3140

32-
print(f"✨ Fixed exports in {file.filepath}")
41+
if __name__ == "__main__":
42+
codebase = Codebase("./")
43+
run(codebase)

0 commit comments

Comments
 (0)