// One ExportStatement containing multiple Export objectsexport { foo, bar as default, type User };// Creates:// - Export for 'foo'// - Export for 'bar' as default// - Export for 'User' as a type// Direct exports create one ExportStatement per exportexport const value = 42;export function process() {}
You can access these through your file’s collections:
Copy
Ask AI
# Access all exports in the codebasefor export in codebase.exports: ...# Access all export statementsfor stmt in file.export_statements: for exp in stmt.exports: ...
ExportStatement inherits from Statement, providing operations like remove() and insert_before(). This is particularly useful when you want to manipulate the entire export declaration.
// Direct exportsexport const value = 42; // Value exportexport function myFunction() {} // Function exportexport class MyClass {} // Class exportexport type MyType = string; // Type exportexport interface MyInterface {} // Interface exportexport enum MyEnum {} // Enum export// Re-exportsexport { foo, bar } from './other-file'; // Named re-exportsexport type { Type } from './other-file'; // Type re-exportsexport * from './other-file'; // Wildcard re-exportsexport * as utils from './other-file'; // Namespace re-exports// Aliased exportsexport { foo as foop }; // Basic aliasexport { foo as default }; // Default export aliasexport { bar as baz } from './other-file'; // Re-export with alias
for exp in file.exports: if exp.is_reexport(): # Get original and current symbols current = exp.exported_symbol original = exp.resolved_symbol print(f"Re-exporting {original.name} from {exp.from_file.filepath}") print(f"Through: {' -> '.join(e.file.filepath for e in exp.export_chain)}")
# Create public APIindex_file = codebase.get_file("index.ts")# Re-export from internal filesfor internal_file in codebase.files: if internal_file.name != "index": for symbol in internal_file.symbols: if symbol.is_public: index_file.add_export( symbol, from_file=internal_file )# Convert default to named exportsfor exp in file.exports: if exp.is_default_export(): exp.make_named_export()# Consolidate re-exportsfrom collections import defaultdictfile_exports = defaultdict(list)for exp in file.exports: if exp.is_reexport(): file_exports[exp.from_file].append(exp)for from_file, exports in file_exports.items(): if len(exports) > 1: # Create consolidated re-export names = [exp.name for exp in exports] file.add_export_from_source( f"export {{ {', '.join(names)} }} from '{from_file.filepath}'" ) # Remove individual exports for exp in exports: exp.remove()
When managing exports, consider the impact on your module’s public API. Not all symbols that can be exported should be exported.