Skip to content

Commit

Permalink
refactor(transformer): move BoundIdentifier into helpers (#3610)
Browse files Browse the repository at this point in the history
Pure refactor. Move `BoundIdentifier` into helpers module so it can be reused by other transforms.
  • Loading branch information
overlookmotel committed Jun 11, 2024
1 parent 5793ff1 commit 89bcbd5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
9 changes: 9 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReferenceId>) -> Self {
Self {
span,
name,
reference_id: Cell::new(reference_id),
reference_flag: ReferenceFlag::Read,
}
}
}

/// Binding Identifier
Expand Down
24 changes: 24 additions & 0 deletions crates/oxc_transformer/src/helpers/bindings.rs
Original file line number Diff line number Diff line change
@@ -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))
}
}
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod react;
mod typescript;

mod helpers {
pub mod bindings;
pub mod module_imports;
}

Expand Down
43 changes: 5 additions & 38 deletions crates/oxc_transformer/src/react/jsx.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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<ReferenceId>,
) -> 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>(
Expand Down

0 comments on commit 89bcbd5

Please sign in to comment.