Skip to content

Commit

Permalink
refactor(napi/transform): remove a call on TransformOptions::clone (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 1, 2024
1 parent 235cdba commit 54c1c53
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
20 changes: 2 additions & 18 deletions napi/transform/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
cell::{OnceCell, Ref, RefCell, RefMut},
cell::{Ref, RefCell, RefMut},
path::Path,
sync::Arc,
};
Expand All @@ -19,9 +19,6 @@ pub(crate) struct TransformContext<'a> {
program: RefCell<Program<'a>>,
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<oxc_transformer::TransformOptions>,
/// Generate source maps?
source_map: bool,
/// Generate `.d.ts` files?
Expand All @@ -45,7 +42,7 @@ impl<'a> TransformContext<'a> {
filename: &'a str,
source_text: &'a str,
source_type: SourceType,
options: Option<TransformOptions>,
options: Option<&TransformOptions>,
) -> Self {
let ParserReturn { errors, program, trivias, .. } =
Parser::new(allocator, source_text, source_type).parse();
Expand All @@ -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,

Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion napi/transform/src/isolated_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 8 additions & 4 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ 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()
.then(|| ctx.declarations())
.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)));
Expand All @@ -96,25 +97,28 @@ pub fn transform(
}
}

fn transpile(ctx: &TransformContext<'_>) -> CodegenReturn {
fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> CodegenReturn {
let semantic_ret = SemanticBuilder::new(ctx.source_text())
// Estimate transformer will triple scopes, symbols, references
.with_excess_capacity(2.0)
.with_check_syntax_error(true)
.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,
ctx.file_path(),
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())
}

0 comments on commit 54c1c53

Please sign in to comment.