refactor(napi/transform): remove a call on TransformOptions::clone (#6210)

This commit is contained in:
Boshen 2024-10-01 09:52:39 +00:00
parent 235cdba927
commit 54c1c53e69
3 changed files with 11 additions and 23 deletions

View file

@ -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<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?
@ -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();
@ -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()

View file

@ -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);

View file

@ -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<TransformOptions>) -> 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())
}