Skip to content

Commit

Permalink
feat(transformer): add import helpers to manage module imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Apr 15, 2024
1 parent fd5002b commit 2a6369f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use oxc_ast::AstBuilder;
use oxc_diagnostics::Error;
use oxc_semantic::Semantic;

use crate::helpers::module_imports::ModuleImports;

pub type Ctx<'a> = Rc<TransformCtx<'a>>;

pub struct TransformCtx<'a> {
Expand All @@ -16,6 +18,10 @@ pub struct TransformCtx<'a> {
filename: String,

errors: RefCell<Vec<Error>>,

// Helpers
/// Manage import statement globally
module_imports: ModuleImports<'a>,
}

impl<'a> TransformCtx<'a> {
Expand All @@ -25,7 +31,8 @@ impl<'a> TransformCtx<'a> {
.file_stem() // omit file extension
.map_or_else(|| String::from("unknown"), |name| name.to_string_lossy().to_string());
let errors = RefCell::new(vec![]);
Self { ast, semantic, filename, errors }
let module_imports = ModuleImports::new(allocator);
Self { ast, semantic, filename, errors, module_imports }
}

pub fn take_errors(&self) -> Vec<Error> {
Expand Down
15 changes: 15 additions & 0 deletions crates/oxc_transformer/src/helpers/module_imports/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use oxc_allocator::Allocator;
use oxc_ast::AstBuilder;

/// Manage import statement globally
/// <https://github.com/nicolo-ribaudo/babel/tree/main/packages/babel-helper-module-imports>
pub struct ModuleImports<'a> {
ast: AstBuilder<'a>,
}

impl<'a> ModuleImports<'a> {
pub fn new(allocator: &'a Allocator) -> ModuleImports<'a> {
let ast = AstBuilder::new(allocator);
Self { ast }
}
}
4 changes: 4 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ mod options;
mod react;
mod typescript;

mod helpers {
pub mod module_imports;
}

use std::{path::Path, rc::Rc};

use oxc_allocator::{Allocator, Vec};
Expand Down

0 comments on commit 2a6369f

Please sign in to comment.