mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(transformer): reduce cloning and referencing Rcs (#3576)
Similar to #3550. Avoid cloning an `Rc` in one place and pass `Rc`s as values not references in others.
This commit is contained in:
parent
1dbc23417d
commit
f2113aea27
6 changed files with 26 additions and 21 deletions
|
|
@ -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::{
|
||||
|
|
@ -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('.');
|
||||
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
use crate::Ctx;
|
||||
use crate::TransformCtx;
|
||||
|
||||
#[inline]
|
||||
fn default_as_true() -> bool {
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue