Skip to content

Commit

Permalink
refactor(transformer): reduce cloning and referencing Rcs (#3576)
Browse files Browse the repository at this point in the history
Similar to #3550. Avoid cloning an `Rc` in one place and pass `Rc`s as
values not references in others.
  • Loading branch information
overlookmotel committed Jun 6, 2024
1 parent 1dbc234 commit f2113ae
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
13 changes: 10 additions & 3 deletions crates/oxc_transformer/src/react/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use oxc_syntax::{
};
use oxc_traverse::TraverseCtx;

use crate::{context::Ctx, helpers::module_imports::NamedImport};
use crate::{
context::{Ctx, TransformCtx},
helpers::module_imports::NamedImport,
};

use super::utils::get_line_column;
pub use super::{
Expand Down Expand Up @@ -256,7 +259,11 @@ impl<'a> Pragma<'a> {
/// Parse `options.pragma` or `options.pragma_frag`.
///
/// If provided option is invalid, raise an error and use default.
fn parse(pragma: Option<&String>, default_property_name: &'static str, ctx: &Ctx<'a>) -> Self {
fn parse(
pragma: Option<&String>,
default_property_name: &'static str,
ctx: &TransformCtx<'a>,
) -> Self {
if let Some(pragma) = pragma {
let mut parts = pragma.split('.');

Expand All @@ -282,7 +289,7 @@ impl<'a> Pragma<'a> {
}
}

fn invalid(default_property_name: &'static str, ctx: &Ctx<'a>) -> Self {
fn invalid(default_property_name: &'static str, ctx: &TransformCtx<'a>) -> Self {
ctx.error(diagnostics::invalid_pragma());
Self::default(default_property_name)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/react/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;

use crate::Ctx;
use crate::TransformCtx;

#[inline]
fn default_as_true() -> bool {
Expand Down Expand Up @@ -147,7 +147,7 @@ impl ReactOptions {
/// otherwise `JSDoc` could be used instead.
///
/// This behavior is aligned with babel.
pub(crate) fn update_with_comments(&mut self, ctx: &Ctx) {
pub(crate) fn update_with_comments(&mut self, ctx: &TransformCtx) {
for (_, span) in ctx.trivias.comments() {
let mut comment = span.source_text(ctx.source_text).trim_start();
// strip leading jsdoc comment `*` and then whitespaces
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct TypeScriptAnnotations<'a> {
}

impl<'a> TypeScriptAnnotations<'a> {
pub fn new(options: &Rc<TypeScriptOptions>, ctx: Ctx<'a>) -> Self {
pub fn new(options: Rc<TypeScriptOptions>, ctx: Ctx<'a>) -> Self {
let jsx_element_import_name = if options.jsx_pragma.contains('.') {
options.jsx_pragma.split('.').next().map(String::from).unwrap()
} else {
Expand All @@ -44,7 +44,7 @@ impl<'a> TypeScriptAnnotations<'a> {
Self {
has_super_call: false,
assignments: ctx.ast.new_vec(),
options: Rc::clone(options),
options,
ctx,
has_jsx_element: false,
has_jsx_fragment: false,
Expand Down
20 changes: 9 additions & 11 deletions crates/oxc_transformer/src/typescript/enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use oxc_allocator::{Box, Vec};
use oxc_ast::{ast::*, visit::walk_mut, VisitMut};
use oxc_span::{Atom, SPAN};
Expand Down Expand Up @@ -173,7 +171,7 @@ impl<'a> TypeScriptEnum<'a> {
IdentifierReferenceRename::new(
enum_name.clone(),
previous_enum_members.clone(),
&self.ctx,
ctx,
)
.visit_expression(&mut new_initializer);
}
Expand Down Expand Up @@ -507,23 +505,23 @@ impl<'a> TypeScriptEnum<'a> {
/// d = A.c,
/// }
/// ```
struct IdentifierReferenceRename<'a> {
struct IdentifierReferenceRename<'a, 'b> {
enum_name: Atom<'a>,
ctx: Ctx<'a>,
ctx: &'b TraverseCtx<'a>,
previous_enum_members: FxHashMap<Atom<'a>, ConstantValue>,
}

impl IdentifierReferenceRename<'_> {
fn new<'a>(
impl<'a, 'b> IdentifierReferenceRename<'a, 'b> {
fn new(
enum_name: Atom<'a>,
previous_enum_members: FxHashMap<Atom<'a>, ConstantValue>,
ctx: &Ctx<'a>,
) -> IdentifierReferenceRename<'a> {
IdentifierReferenceRename { enum_name, ctx: Rc::clone(ctx), previous_enum_members }
ctx: &'b TraverseCtx<'a>,
) -> Self {
IdentifierReferenceRename { enum_name, ctx, previous_enum_members }
}
}

impl<'a> VisitMut<'a> for IdentifierReferenceRename<'a> {
impl<'a, 'b> VisitMut<'a> for IdentifierReferenceRename<'a, 'b> {
fn visit_expression(&mut self, expr: &mut Expression<'a>) {
let new_expr = match expr {
match_member_expression!(Expression) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a> TypeScript<'a> {
let options = Rc::new(options.update_with_comments(&ctx));

Self {
annotations: TypeScriptAnnotations::new(&options, Rc::clone(&ctx)),
annotations: TypeScriptAnnotations::new(Rc::clone(&options), Rc::clone(&ctx)),
r#enum: TypeScriptEnum::new(Rc::clone(&ctx)),
options,
ctx,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/typescript/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;

use serde::Deserialize;

use crate::context::Ctx;
use crate::context::TransformCtx;

fn default_for_jsx_pragma() -> Cow<'static, str> {
Cow::Borrowed("React.createElement")
Expand Down Expand Up @@ -54,7 +54,7 @@ impl TypeScriptOptions {
/// otherwise `JSDoc` could be used instead.
///
/// This behavior is aligned with babel.
pub(crate) fn update_with_comments(mut self, ctx: &Ctx) -> Self {
pub(crate) fn update_with_comments(mut self, ctx: &TransformCtx) -> Self {
for (_, span) in ctx.trivias.comments() {
let mut comment = span.source_text(ctx.source_text).trim_start();
// strip leading jsdoc comment `*` and then whitespaces
Expand Down

0 comments on commit f2113ae

Please sign in to comment.