Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}