From 58af4cefafec0253544042581d7a57254b67e243 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Mon, 10 Jun 2024 15:00:34 +0100 Subject: [PATCH] refactor(transformer): move `BoundIdentifier` into helpers --- crates/oxc_ast/src/ast/js.rs | 9 ++++ .../oxc_transformer/src/helpers/bindings.rs | 24 +++++++++++ crates/oxc_transformer/src/lib.rs | 1 + crates/oxc_transformer/src/react/jsx.rs | 43 +++---------------- 4 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 crates/oxc_transformer/src/helpers/bindings.rs diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 7705ba5bf87d23..8785bf686047dc 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -462,6 +462,15 @@ impl<'a> IdentifierReference<'a> { pub fn new(span: Span, name: Atom<'a>) -> Self { Self { span, name, reference_id: Cell::default(), reference_flag: ReferenceFlag::default() } } + + pub fn new_read(span: Span, name: Atom<'a>, reference_id: Option) -> Self { + Self { + span, + name, + reference_id: Cell::new(reference_id), + reference_flag: ReferenceFlag::Read, + } + } } /// Binding Identifier diff --git a/crates/oxc_transformer/src/helpers/bindings.rs b/crates/oxc_transformer/src/helpers/bindings.rs new file mode 100644 index 00000000000000..575c8428e68e8c --- /dev/null +++ b/crates/oxc_transformer/src/helpers/bindings.rs @@ -0,0 +1,24 @@ +use oxc_ast::ast::IdentifierReference; +use oxc_span::{Atom, SPAN}; +use oxc_syntax::{reference::ReferenceFlag, symbol::SymbolId}; +use oxc_traverse::TraverseCtx; + +/// Store for a created binding identifier +#[derive(Clone)] +pub struct BoundIdentifier<'a> { + pub name: Atom<'a>, + pub symbol_id: SymbolId, +} + +impl<'a> BoundIdentifier<'a> { + /// Create `IdentifierReference` referencing this binding which is read from + /// in current scope + pub fn create_read_reference(&self, ctx: &mut TraverseCtx) -> IdentifierReference<'a> { + let reference_id = ctx.create_bound_reference( + self.name.to_compact_str(), + self.symbol_id, + ReferenceFlag::Read, + ); + IdentifierReference::new_read(SPAN, self.name.clone(), Some(reference_id)) + } +} diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 19f5f8598c54a2..d640d0d48eb747 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -19,6 +19,7 @@ mod react; mod typescript; mod helpers { + pub mod bindings; pub mod module_imports; } diff --git a/crates/oxc_transformer/src/react/jsx.rs b/crates/oxc_transformer/src/react/jsx.rs index 3938f891940a80..cf07be616279fa 100644 --- a/crates/oxc_transformer/src/react/jsx.rs +++ b/crates/oxc_transformer/src/react/jsx.rs @@ -1,19 +1,19 @@ -use std::{cell::Cell, rc::Rc}; +use std::rc::Rc; use oxc_allocator::Vec; use oxc_ast::{ast::*, AstBuilder}; use oxc_span::{Atom, GetSpan, Span, SPAN}; use oxc_syntax::{ identifier::{is_irregular_whitespace, is_line_terminator}, - reference::{ReferenceFlag, ReferenceId}, - symbol::{SymbolFlags, SymbolId}, + reference::ReferenceFlag, + symbol::SymbolFlags, xml_entities::XML_ENTITIES, }; use oxc_traverse::TraverseCtx; use crate::{ context::{Ctx, TransformCtx}, - helpers::module_imports::NamedImport, + helpers::{bindings::BoundIdentifier, module_imports::NamedImport}, }; use super::diagnostics; @@ -235,24 +235,6 @@ fn get_import_source<'a>(jsx_runtime_importer: &Atom<'a>, react_importer_len: u3 Atom::from(&jsx_runtime_importer.as_str()[..react_importer_len as usize]) } -#[derive(Clone)] -pub struct BoundIdentifier<'a> { - pub name: Atom<'a>, - pub symbol_id: SymbolId, -} - -impl<'a> BoundIdentifier<'a> { - /// Create `IdentifierReference` referencing this binding which is read from - fn create_read_reference(&self, ctx: &mut TraverseCtx) -> IdentifierReference<'a> { - let reference_id = ctx.create_bound_reference( - self.name.to_compact_str(), - self.symbol_id, - ReferenceFlag::Read, - ); - create_read_identifier_reference(SPAN, self.name.clone(), Some(reference_id)) - } -} - /// Pragma used in classic mode struct Pragma<'a> { object: Atom<'a>, @@ -1012,22 +994,7 @@ fn get_read_identifier_reference<'a>( ) -> IdentifierReference<'a> { let reference_id = ctx.create_reference_in_current_scope(name.to_compact_str(), ReferenceFlag::Read); - create_read_identifier_reference(span, name, Some(reference_id)) -} - -/// Create `IdentifierReference` which is read from -#[inline] -fn create_read_identifier_reference( - span: Span, - name: Atom, - reference_id: Option, -) -> IdentifierReference { - IdentifierReference { - span, - name, - reference_id: Cell::new(reference_id), - reference_flag: ReferenceFlag::Read, - } + IdentifierReference::new_read(span, name, Some(reference_id)) } fn create_static_member_expression<'a>(