|
| 1 | +//! ES2020: Export Namespace From |
| 2 | +//! |
| 3 | +//! This plugin transforms `export * as ns from "mod"` to `import * as _ns from "mod"; export { _ns as ns }`. |
| 4 | +//! |
| 5 | +//! > This plugin is included in `preset-env`, in ES2020 |
| 6 | +//! |
| 7 | +//! ## Example |
| 8 | +//! |
| 9 | +//! Input: |
| 10 | +//! ```js |
| 11 | +//! export * as ns from "mod"; |
| 12 | +//! ``` |
| 13 | +//! |
| 14 | +//! Output: |
| 15 | +//! ```js |
| 16 | +//! import * as _ns from "mod"; |
| 17 | +//! export { _ns as ns }; |
| 18 | +//! ``` |
| 19 | +//! |
| 20 | +//! ## Implementation |
| 21 | +//! |
| 22 | +//! Implementation based on [@babel/plugin-transform-export-namespace-from](https://babeljs.io/docs/babel-plugin-transform-export-namespace-from). |
| 23 | +//! |
| 24 | +//! ## References: |
| 25 | +//! * Babel plugin implementation: <https://github.com/babel/babel/tree/v7.28.4/packages/babel-plugin-transform-export-namespace-from> |
| 26 | +//! * "export ns from" TC39 proposal: <https://github.com/tc39/proposal-export-ns-from> |
| 27 | +
|
| 28 | +use oxc_allocator::TakeIn; |
| 29 | +use oxc_ast::{NONE, ast::*}; |
| 30 | +use oxc_semantic::SymbolFlags; |
| 31 | +use oxc_span::SPAN; |
| 32 | +use oxc_traverse::{Traverse, ast_operations::to_identifier}; |
| 33 | + |
| 34 | +use crate::{context::TraverseCtx, state::TransformState}; |
| 35 | + |
| 36 | +pub struct ExportNamespaceFrom<'a, 'ctx> { |
| 37 | + _ctx: &'ctx crate::context::TransformCtx<'a>, |
| 38 | +} |
| 39 | + |
| 40 | +impl<'a, 'ctx> ExportNamespaceFrom<'a, 'ctx> { |
| 41 | + pub fn new(ctx: &'ctx crate::context::TransformCtx<'a>) -> Self { |
| 42 | + Self { _ctx: ctx } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<'a> Traverse<'a, TransformState<'a>> for ExportNamespaceFrom<'a, '_> { |
| 47 | + fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { |
| 48 | + let mut new_statements = ctx.ast.vec_with_capacity(program.body.len()); |
| 49 | + |
| 50 | + for stmt in program.body.take_in(ctx.ast) { |
| 51 | + match stmt { |
| 52 | + Statement::ExportAllDeclaration(export_all) if export_all.exported.is_some() => { |
| 53 | + // Transform `export * as ns from "mod"` to: |
| 54 | + // `import * as _ns from "mod"; export { _ns as ns };` |
| 55 | + |
| 56 | + let ExportAllDeclaration { span, exported, source, export_kind, .. } = |
| 57 | + export_all.unbox(); |
| 58 | + |
| 59 | + // Create a unique binding for the import |
| 60 | + let binding = ctx.generate_uid( |
| 61 | + &to_identifier(exported.as_ref().unwrap().name().to_string()), |
| 62 | + program.scope_id(), |
| 63 | + SymbolFlags::Import, |
| 64 | + ); |
| 65 | + |
| 66 | + // Create `import * as _ns from "mod"` |
| 67 | + let import_specifier = ImportDeclarationSpecifier::ImportNamespaceSpecifier( |
| 68 | + ctx.ast.alloc_import_namespace_specifier( |
| 69 | + SPAN, |
| 70 | + binding.create_binding_identifier(ctx), |
| 71 | + ), |
| 72 | + ); |
| 73 | + |
| 74 | + let import_decl = ctx.ast.alloc_import_declaration( |
| 75 | + SPAN, |
| 76 | + Some(ctx.ast.vec1(import_specifier)), |
| 77 | + source, |
| 78 | + None, |
| 79 | + NONE, |
| 80 | + export_kind, |
| 81 | + ); |
| 82 | + new_statements.push(Statement::ImportDeclaration(import_decl)); |
| 83 | + |
| 84 | + // Create `export { _ns as ns }` |
| 85 | + let local = |
| 86 | + ModuleExportName::IdentifierReference(binding.create_read_reference(ctx)); |
| 87 | + let export_specifier = |
| 88 | + ctx.ast.export_specifier(span, local, exported.unwrap(), export_kind); |
| 89 | + |
| 90 | + let export_named_decl = ctx.ast.alloc_export_named_declaration( |
| 91 | + SPAN, |
| 92 | + None, |
| 93 | + ctx.ast.vec1(export_specifier), |
| 94 | + None, |
| 95 | + export_kind, |
| 96 | + NONE, |
| 97 | + ); |
| 98 | + new_statements.push(Statement::ExportNamedDeclaration(export_named_decl)); |
| 99 | + } |
| 100 | + _ => { |
| 101 | + new_statements.push(stmt); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + program.body = new_statements; |
| 107 | + } |
| 108 | +} |
0 commit comments