-
-
Notifications
You must be signed in to change notification settings - Fork 881
feat(transformer): add ES2020 export namespace from transformation #14277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Dunqing
merged 5 commits into
main
from
copilot/fix-ced06a9a-b046-4d87-90de-6fe4af5bb650
Oct 11, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6f30b10
feat(transformer): add ES2020 export namespace from transformation
sapphi-red d71e4b4
refactor: address code review feedback
Copilot 3b3f821
perf: implement GatherNodeParts for ModuleExportName
Copilot 96fd805
[autofix.ci] apply automated fixes
autofix-ci[bot] 7b7c67b
Apply suggestion from @graphite-app[bot]
Dunqing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
crates/oxc_transformer/src/es2020/export_namespace_from.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| //! ES2020: Export Namespace From | ||
| //! | ||
| //! This plugin transforms `export * as ns from "mod"` to `import * as _ns from "mod"; export { _ns as ns }`. | ||
| //! | ||
| //! > This plugin is included in `preset-env`, in ES2020 | ||
| //! | ||
| //! ## Example | ||
| //! | ||
| //! Input: | ||
| //! ```js | ||
| //! export * as ns from "mod"; | ||
| //! ``` | ||
| //! | ||
| //! Output: | ||
| //! ```js | ||
| //! import * as _ns from "mod"; | ||
| //! export { _ns as ns }; | ||
| //! ``` | ||
| //! | ||
| //! ## Implementation | ||
| //! | ||
| //! Implementation based on [@babel/plugin-transform-export-namespace-from](https://babeljs.io/docs/babel-plugin-transform-export-namespace-from). | ||
| //! | ||
| //! ## References: | ||
| //! * Babel plugin implementation: <https://github.com/babel/babel/tree/v7.28.4/packages/babel-plugin-transform-export-namespace-from> | ||
| //! * "export ns from" TC39 proposal: <https://github.com/tc39/proposal-export-ns-from> | ||
|
|
||
| use oxc_allocator::TakeIn; | ||
| use oxc_ast::{NONE, ast::*}; | ||
| use oxc_semantic::SymbolFlags; | ||
| use oxc_span::SPAN; | ||
| use oxc_traverse::Traverse; | ||
|
|
||
| use crate::{ | ||
| context::{TransformCtx, TraverseCtx}, | ||
| state::TransformState, | ||
| }; | ||
|
|
||
| pub struct ExportNamespaceFrom<'a, 'ctx> { | ||
| _ctx: &'ctx TransformCtx<'a>, | ||
| } | ||
|
|
||
| impl<'a, 'ctx> ExportNamespaceFrom<'a, 'ctx> { | ||
| pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self { | ||
| Self { _ctx: ctx } | ||
| } | ||
| } | ||
|
|
||
| impl<'a> Traverse<'a, TransformState<'a>> for ExportNamespaceFrom<'a, '_> { | ||
| fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { | ||
| // Early return if there's no `export * as ns from "mod"` to transform | ||
| let has_export_namespace = program.body.iter().any( | ||
| |stmt| matches!(stmt, Statement::ExportAllDeclaration(decl) if decl.exported.is_some()), | ||
| ); | ||
| if !has_export_namespace { | ||
| return; | ||
| } | ||
|
|
||
| let mut new_statements = ctx.ast.vec_with_capacity(program.body.len()); | ||
|
|
||
| for stmt in program.body.take_in(ctx.ast) { | ||
| match stmt { | ||
| Statement::ExportAllDeclaration(export_all) if export_all.exported.is_some() => { | ||
| // Transform `export * as ns from "mod"` to: | ||
| // `import * as _ns from "mod"; export { _ns as ns };` | ||
|
|
||
| let ExportAllDeclaration { span, exported, source, export_kind, .. } = | ||
| export_all.unbox(); | ||
| let exported_name = exported.unwrap(); | ||
|
|
||
| // Create a unique binding for the import based on the exported name | ||
| let binding = ctx.generate_uid_based_on_node( | ||
| &exported_name, | ||
| program.scope_id(), | ||
| SymbolFlags::Import, | ||
| ); | ||
|
|
||
| // Create `import * as _ns from "mod"` | ||
| let import_specifier = ImportDeclarationSpecifier::ImportNamespaceSpecifier( | ||
| ctx.ast.alloc_import_namespace_specifier( | ||
| SPAN, | ||
| binding.create_binding_identifier(ctx), | ||
| ), | ||
| ); | ||
|
|
||
| let import_decl = ctx.ast.alloc_import_declaration( | ||
| SPAN, | ||
| Some(ctx.ast.vec1(import_specifier)), | ||
| source, | ||
| None, | ||
| NONE, | ||
| export_kind, | ||
| ); | ||
| new_statements.push(Statement::ImportDeclaration(import_decl)); | ||
|
|
||
| // Create `export { _ns as ns }` | ||
| let local = | ||
| ModuleExportName::IdentifierReference(binding.create_read_reference(ctx)); | ||
| let export_specifier = | ||
| ctx.ast.export_specifier(span, local, exported_name, export_kind); | ||
|
|
||
| let export_named_decl = ctx.ast.alloc_export_named_declaration( | ||
| span, | ||
| None, | ||
| ctx.ast.vec1(export_specifier), | ||
| None, | ||
| export_kind, | ||
| NONE, | ||
| ); | ||
Dunqing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| new_statements.push(Statement::ExportNamedDeclaration(export_named_decl)); | ||
| } | ||
| _ => { | ||
| new_statements.push(stmt); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| program.body = new_statements; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.