Skip to content

Commit

Permalink
refactor(transformer): pass Rcs by value (#3550)
Browse files Browse the repository at this point in the history
`Rc<T>` is already a pointer, so passing `&Rc<T>` to functions adds unnecessary indirection (reference to a reference).
  • Loading branch information
overlookmotel committed Jun 6, 2024
1 parent 7982b93 commit 0948124
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 44 deletions.
6 changes: 2 additions & 4 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_span::{Atom, SPAN};
Expand Down Expand Up @@ -39,9 +37,9 @@ pub struct ArrowFunctions<'a> {
}

impl<'a> ArrowFunctions<'a> {
pub fn new(options: ArrowFunctionsOptions, ctx: &Ctx<'a>) -> Self {
pub fn new(options: ArrowFunctionsOptions, ctx: Ctx<'a>) -> Self {
Self {
ctx: Rc::clone(ctx),
ctx,
_options: options,
uid: 0,
has_this: false,
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/es2015/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ pub struct ES2015<'a> {
}

impl<'a> ES2015<'a> {
pub fn new(options: ES2015Options, ctx: &Ctx<'a>) -> Self {
pub fn new(options: ES2015Options, ctx: Ctx<'a>) -> Self {
Self {
arrow_functions: ArrowFunctions::new(
options.arrow_function.clone().unwrap_or_default(),
ctx,
Rc::clone(&ctx),
),
ctx: Rc::clone(ctx),
ctx,
options,
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ impl<'a> Transformer<'a> {
));
Self {
ctx: Rc::clone(&ctx),
x0_typescript: TypeScript::new(options.typescript, &ctx),
x1_react: React::new(options.react, &ctx),
x3_es2015: ES2015::new(options.es2015, &ctx),
x0_typescript: TypeScript::new(options.typescript, Rc::clone(&ctx)),
x1_react: React::new(options.react, Rc::clone(&ctx)),
x3_es2015: ES2015::new(options.es2015, ctx),
}
}

Expand Down
6 changes: 2 additions & 4 deletions crates/oxc_transformer/src/react/display_name/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use oxc_allocator::Box;
use oxc_ast::ast::*;
use oxc_span::{Atom, SPAN};
Expand All @@ -20,8 +18,8 @@ pub struct ReactDisplayName<'a> {
}

impl<'a> ReactDisplayName<'a> {
pub fn new(ctx: &Ctx<'a>) -> Self {
Self { ctx: Rc::clone(ctx) }
pub fn new(ctx: Ctx<'a>) -> Self {
Self { ctx }
}
}

Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_transformer/src/react/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ impl<'a> Pragma<'a> {

// Transforms
impl<'a> ReactJsx<'a> {
pub fn new(options: &Rc<ReactOptions>, ctx: &Ctx<'a>) -> Self {
pub fn new(options: Rc<ReactOptions>, ctx: Ctx<'a>) -> Self {
let bindings = match options.runtime {
ReactJsxRuntime::Classic => {
if options.import_source.is_some() {
ctx.error(diagnostics::import_source_cannot_be_set());
}
let pragma = Pragma::parse(options.pragma.as_ref(), "createElement", ctx);
let pragma_frag = Pragma::parse(options.pragma_frag.as_ref(), "Fragment", ctx);
let pragma = Pragma::parse(options.pragma.as_ref(), "createElement", &ctx);
let pragma_frag = Pragma::parse(options.pragma_frag.as_ref(), "Fragment", &ctx);
Bindings::Classic(ClassicBindings { pragma, pragma_frag })
}
ReactJsxRuntime::Automatic => {
Expand Down Expand Up @@ -338,24 +338,24 @@ impl<'a> ReactJsx<'a> {

if ctx.source_type.is_script() {
Bindings::AutomaticScript(AutomaticScriptBindings::new(
Rc::clone(ctx),
Rc::clone(options),
Rc::clone(&ctx),
Rc::clone(&options),
jsx_runtime_importer,
))
} else {
Bindings::AutomaticModule(AutomaticModuleBindings::new(
Rc::clone(ctx),
Rc::clone(options),
Rc::clone(&ctx),
Rc::clone(&options),
jsx_runtime_importer,
))
}
}
};

Self {
options: Rc::clone(options),
ctx: Rc::clone(ctx),
jsx_self: ReactJsxSelf::new(ctx),
options,
ctx: Rc::clone(&ctx),
jsx_self: ReactJsxSelf::new(Rc::clone(&ctx)),
jsx_source: ReactJsxSource::new(ctx),
bindings,
can_add_filename_statement: false,
Expand Down
6 changes: 2 additions & 4 deletions crates/oxc_transformer/src/react/jsx_self/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Span, SPAN};
Expand All @@ -26,8 +24,8 @@ pub struct ReactJsxSelf<'a> {
}

impl<'a> ReactJsxSelf<'a> {
pub fn new(ctx: &Ctx<'a>) -> Self {
Self { ctx: Rc::clone(ctx) }
pub fn new(ctx: Ctx<'a>) -> Self {
Self { ctx }
}

pub fn transform_jsx_opening_element(&self, elem: &mut JSXOpeningElement<'a>) {
Expand Down
6 changes: 2 additions & 4 deletions crates/oxc_transformer/src/react/jsx_source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Span, SPAN};
Expand All @@ -25,8 +23,8 @@ pub struct ReactJsxSource<'a> {
}

impl<'a> ReactJsxSource<'a> {
pub fn new(ctx: &Ctx<'a>) -> Self {
Self { ctx: Rc::clone(ctx) }
pub fn new(ctx: Ctx<'a>) -> Self {
Self { ctx }
}

pub fn transform_jsx_opening_element(&mut self, elem: &mut JSXOpeningElement<'a>) {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/react/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ pub struct React<'a> {

// Constructors
impl<'a> React<'a> {
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
pub fn new(options: ReactOptions, ctx: Ctx<'a>) -> Self {
let mut options = options;
if options.is_jsx_plugin_enabled() {
options.update_with_comments(ctx);
options.update_with_comments(&ctx);
}
let options = Rc::new(options);
Self {
options: Rc::clone(&options),
jsx: ReactJsx::new(&options, ctx),
jsx: ReactJsx::new(options, Rc::clone(&ctx)),
display_name: ReactDisplayName::new(ctx),
}
}
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 @@ -45,7 +45,7 @@ impl<'a> TypeScriptAnnotations<'a> {
has_super_call: false,
assignments: ctx.ast.new_vec(),
options: Rc::clone(options),
ctx: Rc::clone(ctx),
ctx,
has_jsx_element: false,
has_jsx_fragment: false,
jsx_element_import_name,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/typescript/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct TypeScriptEnum<'a> {
}

impl<'a> TypeScriptEnum<'a> {
pub fn new(ctx: &Ctx<'a>) -> Self {
Self { ctx: Rc::clone(ctx), enums: FxHashMap::default() }
pub fn new(ctx: Ctx<'a>) -> Self {
Self { ctx, enums: FxHashMap::default() }
}
/// ```TypeScript
/// enum Foo {
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ pub struct TypeScript<'a> {
}

impl<'a> TypeScript<'a> {
pub fn new(options: TypeScriptOptions, ctx: &Ctx<'a>) -> Self {
let options = Rc::new(options.update_with_comments(ctx));
pub fn new(options: TypeScriptOptions, ctx: Ctx<'a>) -> Self {
let options = Rc::new(options.update_with_comments(&ctx));

Self {
annotations: TypeScriptAnnotations::new(&options, ctx),
r#enum: TypeScriptEnum::new(ctx),
annotations: TypeScriptAnnotations::new(&options, Rc::clone(&ctx)),
r#enum: TypeScriptEnum::new(Rc::clone(&ctx)),
options,
ctx: Rc::clone(ctx),
ctx,
}
}
}
Expand Down

0 comments on commit 0948124

Please sign in to comment.