Skip to content

Commit 7a77465

Browse files
author
codegen-bot
committed
.
1 parent b42e67f commit 7a77465

File tree

12 files changed

+467
-348
lines changed

12 files changed

+467
-348
lines changed

docs/blog/codemod-frameworks.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ icon: "code-compare"
55
iconType: "solid"
66
---
77

8+
# Others to add
9+
- [Abracadabra](https://github.com/nicoespeon/abracadabra)
10+
- [Rope](https://rope.readthedocs.io/en/latest/overview.html#rope-overview)
11+
- [Grit](https://github.com/getgrit/gritql)
12+
13+
814
Code transformation tools have evolved significantly over the years, each offering unique approaches to programmatic code manipulation. Let's explore the strengths and limitations of major frameworks in this space.
915

1016
## Python's AST Module

docs/building-with-codegen/at-a-glance.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ Learn how to use Codegen's core APIs to analyze and transform code.
6060
Understand function call patterns and manipulate call sites.
6161
</Card>
6262
<Card
63-
title="Imports & Exports"
63+
title="Imports"
6464
icon="file-import"
65-
href="/building-with-codegen/imports-and-exports"
65+
href="/building-with-codegen/imports"
6666
>
67-
Work with module imports, exports, and manage dependencies.
67+
Work with module imports and manage dependencies.
6868
</Card>
6969
<Card
7070
title="Traversing the Call Graph"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

docs/building-with-codegen/imports-and-exports.mdx

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,117 @@ for exp in file.exports: # Iterates over all Export symbols
102102
empty.
103103
</Note>
104104

105+
## Working with Imports
106+
107+
### Working with External Modules
108+
109+
When working with imports, you often need to distinguish between imports from your project and external packages (like `react` or `lodash`). The [ExternalModule](/api-reference/core/ExternalModule) class helps with this:
110+
111+
```python
112+
# Check if an import is from an external module
113+
for imp in file.imports:
114+
if isinstance(imp.resolved_symbol, ExternalModule):
115+
print(f"External import: {imp.name} from {imp.module}")
116+
else:
117+
print(f"Local import: {imp.name}")
118+
119+
# Skip processing external imports
120+
for function in codebase.functions:
121+
for call in function.call_sites:
122+
if isinstance(call.function_definition, ExternalModule):
123+
# Skip external function calls (like React.useState)
124+
continue
125+
# Process local function calls...
126+
```
127+
128+
### Common Import Operations
129+
130+
Here are common operations you can perform on imports:
131+
132+
```python
133+
# Change the module an import comes from
134+
import_stmt = file.get_import("MyComponent")
135+
import_stmt.set_module("./new/path") # Updates import path
136+
137+
# Add/update import alias
138+
import_stmt.set_alias("MyAlias") # import X as MyAlias
139+
140+
# Convert between import styles (TypeScript)
141+
import_stmt.make_type_import() # Converts to 'import type'
142+
import_stmt.make_value_import() # Removes 'type' modifier
143+
144+
# Update multiple properties
145+
import_stmt.update(
146+
module="./new/path",
147+
alias="NewAlias",
148+
is_type=True
149+
)
150+
```
151+
152+
### Bulk Import Management
153+
154+
Here's how to perform operations on multiple imports:
155+
156+
```python
157+
# Update all imports from a specific module
158+
old_path = "./old/path"
159+
new_path = "./new/path"
160+
161+
for imp in file.imports:
162+
if imp.module == old_path:
163+
imp.set_module(new_path)
164+
165+
# Remove all unused imports
166+
for imp in file.imports:
167+
if not imp.usages and not isinstance(imp.resolved_symbol, ExternalModule):
168+
print(f"Removing unused import: {imp.name}")
169+
imp.remove()
170+
171+
# Consolidate imports from the same module
172+
from collections import defaultdict
173+
174+
# Group imports by module
175+
module_imports = defaultdict(list)
176+
for imp in file.imports:
177+
module_imports[imp.module].append(imp)
178+
179+
# Consolidate each group
180+
for module, imports in module_imports.items():
181+
if len(imports) > 1:
182+
# Create new combined import
183+
symbols = [imp.name for imp in imports]
184+
file.add_import_from_import_string(
185+
f"import {{ {', '.join(symbols)} }} from '{module}'"
186+
)
187+
# Remove old imports
188+
for imp in imports:
189+
imp.remove()
190+
```
191+
192+
### Import Resolution
193+
194+
You can trace import chains and resolve symbols:
195+
196+
```python
197+
# Follow import chain to original symbol
198+
import_stmt = file.get_import("MyComponent")
199+
original_symbol = import_stmt.resolved_symbol
200+
201+
if original_symbol:
202+
print(f"Imported from: {original_symbol.file.filepath}")
203+
print(f"Original name: {original_symbol.name}")
204+
205+
# Check if import is re-exported
206+
for imp in file.imports:
207+
if imp.is_reexported:
208+
print(f"Re-exported symbol: {imp.name}")
209+
print(f"Through files: {' -> '.join(exp.file.filepath for exp in imp.export_chain)}")
210+
```
211+
212+
<Note>
213+
When working with imports, always check if they resolve to external modules before modification. This prevents accidentally modifying imports from third-party packages.
214+
</Note>
215+
105216
## Identifying Export Types
106217

107218
Codegen provides several methods to identify and filter different types of exports in your TypeScript code. Here's how to work with various export patterns:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

docs/building-with-codegen/statements-and-code-blocks.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Codegen supports various statement types, each with specific APIs:
124124
### [Import Statements](../api-reference/core/ImportStatement) / [Export Statements](../api-reference/core/ExportStatement)
125125

126126
<Tip>
127-
See [Import and Exports](../building-with-codegen/imports-and-exports) for
127+
See [imports](/building-with-codegen/imports) and [exports](../building-with-codegen/exports) for
128128
more details.
129129
</Tip>
130130

docs/introduction/getting-started.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ if base_class:
281281

282282
<Note>
283283
Learn more about [dependencies and
284-
references](/building-with-codegen/dependencies-and-usages) or [imports
285-
and exports](/building-with-codegen/imports-and-exports).
284+
references](/building-with-codegen/dependencies-and-usages) or [imports](/building-with-codegen/imports) and [exports](/building-with-codegen/exports).
286285
</Note>
287286

288287
## What's Next?

0 commit comments

Comments
 (0)