diff --git a/napi/transform/src/context.rs b/napi/transform/src/context.rs index 68b4500046486..77cf237c29067 100644 --- a/napi/transform/src/context.rs +++ b/napi/transform/src/context.rs @@ -1,5 +1,5 @@ use std::{ - cell::{OnceCell, Ref, RefCell, RefMut}, + cell::{Ref, RefCell, RefMut}, path::Path, sync::Arc, }; @@ -19,9 +19,6 @@ pub(crate) struct TransformContext<'a> { program: RefCell>, pub trivias: Trivias, - /// Will be initialized if provided to constructor or first accessed. - /// Prevents allocations for codegen tasks that don't require options. - options: OnceCell, /// Generate source maps? source_map: bool, /// Generate `.d.ts` files? @@ -45,7 +42,7 @@ impl<'a> TransformContext<'a> { filename: &'a str, source_text: &'a str, source_type: SourceType, - options: Option, + options: Option<&TransformOptions>, ) -> Self { let ParserReturn { errors, program, trivias, .. } = Parser::new(allocator, source_text, source_type).parse(); @@ -56,19 +53,11 @@ impl<'a> TransformContext<'a> { let declarations = options.as_ref().and_then(|o| o.typescript.as_ref()).and_then(|t| t.declaration); - // Insert options into the cell if provided. Otherwise they will be - // initialized to default when first accessed. - let options_cell = OnceCell::new(); - if let Some(options) = options { - options_cell.set(options.into()).unwrap(); - } - Self { allocator, program: RefCell::new(program), trivias, - options: options_cell, source_map, declarations, @@ -94,11 +83,6 @@ impl<'a> TransformContext<'a> { self.source_text } - #[inline] - pub fn oxc_options(&self) -> oxc_transformer::TransformOptions { - self.options.get_or_init(Default::default).clone() - } - #[inline] pub(crate) fn declarations(&self) -> Option<&IsolatedDeclarationsOptions> { self.declarations.as_ref() diff --git a/napi/transform/src/isolated_declaration.rs b/napi/transform/src/isolated_declaration.rs index 33fe30e0c3285..59d4f26047f3c 100644 --- a/napi/transform/src/isolated_declaration.rs +++ b/napi/transform/src/isolated_declaration.rs @@ -43,7 +43,7 @@ pub fn isolated_declaration( &filename, &source_text, source_type, - Some(TransformOptions { sourcemap: options.sourcemap, ..Default::default() }), + Some(&TransformOptions { sourcemap: options.sourcemap, ..Default::default() }), ); let transformed_ret = build_declarations(&ctx, options); diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index 6e5ce050edbd7..662d7e4f1152f 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -74,7 +74,8 @@ pub fn transform( }; let allocator = Allocator::default(); - let ctx = TransformContext::new(&allocator, &filename, &source_text, source_type, options); + let ctx = + TransformContext::new(&allocator, &filename, &source_text, source_type, options.as_ref()); let declarations_result = source_type .is_typescript() @@ -82,7 +83,7 @@ pub fn transform( .flatten() .map(|options| isolated_declaration::build_declarations(&ctx, *options)); - let transpile_result = transpile(&ctx); + let transpile_result = transpile(&ctx, options); let (declaration, declaration_map) = declarations_result .map_or((None, None), |d| (Some(d.source_text), d.source_map.map(Into::into))); @@ -96,7 +97,7 @@ pub fn transform( } } -fn transpile(ctx: &TransformContext<'_>) -> CodegenReturn { +fn transpile(ctx: &TransformContext<'_>, options: Option) -> CodegenReturn { let semantic_ret = SemanticBuilder::new(ctx.source_text()) // Estimate transformer will triple scopes, symbols, references .with_excess_capacity(2.0) @@ -104,6 +105,8 @@ fn transpile(ctx: &TransformContext<'_>) -> CodegenReturn { .build(&ctx.program()); ctx.add_diagnostics(semantic_ret.errors); + let options = options.map(oxc_transformer::TransformOptions::from).unwrap_or_default(); + let (symbols, scopes) = semantic_ret.semantic.into_symbol_table_and_scope_tree(); let ret = Transformer::new( ctx.allocator, @@ -111,10 +114,11 @@ fn transpile(ctx: &TransformContext<'_>) -> CodegenReturn { ctx.source_type(), ctx.source_text(), ctx.trivias.clone(), - ctx.oxc_options(), + options, ) .build_with_symbols_and_scopes(symbols, scopes, &mut ctx.program_mut()); ctx.add_diagnostics(ret.errors); + ctx.codegen().build(&ctx.program()) }