Skip to content

Commit b271efc

Browse files
jayhackcodegen-bot
andauthored
docs: fixed up export docs (#106)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed - [ ] I have read and agree to the [Contributor License Agreement](../CLA.md) --------- Co-authored-by: codegen-bot <[email protected]>
1 parent 4088aab commit b271efc

File tree

2 files changed

+69
-424
lines changed

2 files changed

+69
-424
lines changed

docs/building-with-codegen/exports.mdx

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ iconType: "solid"
77

88
The [Export](/api-reference/core/Export) API provides tools for managing exports and module boundaries in TypeScript codebases.
99

10+
<Note>Exports are a TS-only language feature</Note>
11+
1012
## Export Statements vs Exports
1113

1214
Similar to imports, Codegen provides two levels of abstraction for working with exports:
@@ -30,19 +32,76 @@ export function process() {}
3032
You can access these through your file's collections:
3133

3234
```python
35+
# Access all exports in the codebase
36+
for export in codebase.exports:
37+
...
38+
3339
# Access all export statements
3440
for stmt in file.export_statements:
35-
print(f"Statement: {stmt.source}")
36-
37-
# Access individual exports in the statement
3841
for exp in stmt.exports:
39-
print(f" Export: {exp.name}")
42+
...
4043
```
4144

4245
<Note>
4346
ExportStatement inherits from [Statement](/building-with-codegen/statements-and-code-blocks), providing operations like `remove()` and `insert_before()`. This is particularly useful when you want to manipulate the entire export declaration.
4447
</Note>
4548

49+
## Common Operations
50+
51+
Here are common operations for working with exports:
52+
53+
```python
54+
# Add exports from source code
55+
file.add_export_from_source("export { MyComponent };")
56+
file.add_export_from_source("export type { MyType } from './types';")
57+
58+
# Export existing symbols
59+
component = file.get_function("MyComponent")
60+
file.add_export(component) # export { MyComponent }
61+
file.add_export(component, alias="default") # export { MyComponent as default }
62+
63+
# Convert to type export
64+
export = file.get_export("MyType")
65+
export.make_type_export()
66+
67+
# Remove exports
68+
export = file.get_export("MyComponent")
69+
export.remove() # Removes export but keeps the symbol
70+
71+
# Remove multiple exports
72+
for export in file.exports:
73+
if not export.is_type_export():
74+
export.remove()
75+
76+
# Update export properties
77+
export.update(
78+
name="NewName",
79+
is_type=True,
80+
is_default=False
81+
)
82+
83+
# Export from another file
84+
other_file = codebase.get_file("./components.ts")
85+
component = other_file.get_class("Button")
86+
file.add_export(component, from_file=other_file) # export { Button } from './components';
87+
88+
# Analyze symbols being exported
89+
for export in file.exports:
90+
if isinstance(export.exported_symbol, ExternalModule):
91+
print('Exporting ExternalModule')
92+
else:
93+
...
94+
```
95+
96+
<Note>
97+
When adding exports, you can:
98+
- Add from source code with `add_export_from_source()`
99+
- Export existing symbols with `add_export()`
100+
- Re-export from other files by specifying `from_file`
101+
102+
The export will automatically handle adding any required imports.
103+
</Note>
104+
46105
## Export Types
47106

48107
Codegen supports several types of exports:
@@ -68,9 +127,13 @@ export { foo as default }; // Default export alias
68127
export { bar as baz } from './other-file'; // Re-export with alias
69128
```
70129

71-
## Working with Exports
130+
## Identifying Export Types
72131

73132
The Export API provides methods to identify and filter exports:
133+
- [.is_type_export()](/api-reference/typescript/TSExport#is-type-export)
134+
- [.is_default_export()](/api-reference/typescript/TSExport#is-default-export)
135+
- [.is_wildcard_export()](/api-reference/typescript/TSExport#is-wildcard-export)
136+
74137

75138
```python
76139
# Check export types
@@ -81,14 +144,6 @@ for exp in file.exports:
81144
print(f"Default export: {exp.name}")
82145
elif exp.is_wildcard_export():
83146
print(f"Wildcard export from: {exp.from_file.filepath}")
84-
85-
# Work with re-exports
86-
for exp in file.exports:
87-
if exp.is_reexport():
88-
if exp.is_external_export:
89-
print(f"External re-export: {exp.name} from {exp.from_file.filepath}")
90-
else:
91-
print(f"Internal re-export: {exp.name}")
92147
```
93148

94149
## Export Resolution
@@ -106,35 +161,9 @@ for exp in file.exports:
106161
print(f"Through: {' -> '.join(e.file.filepath for e in exp.export_chain)}")
107162
```
108163

109-
## Common Operations
110-
111-
Here are common operations for working with exports:
112-
113-
```python
114-
# Add new export
115-
file.add_export("MyComponent")
116-
117-
# Add export with alias
118-
file.add_export("MyComponent", alias="default")
119-
120-
# Convert to type export
121-
export = file.get_export("MyType")
122-
export.make_type_export()
123-
124-
# Remove export
125-
export.remove() # Removes export but keeps symbol
126-
127-
# Update export properties
128-
export.update(
129-
name="NewName",
130-
is_type=True,
131-
is_default=False
132-
)
133-
```
134-
135164
## Managing Re-exports
136165

137-
Common patterns for working with re-exports:
166+
You can manage re-exports with the [TSExport.is_reexport()](/api-reference/typescript/TSExport#is-reexport) API:
138167

139168
```python
140169
# Create public API

0 commit comments

Comments
 (0)