refactor(transformer): move BoundIdentifier into helpers (#3610)

Pure refactor. Move `BoundIdentifier` into helpers module so it can be reused by other transforms.
This commit is contained in:
overlookmotel 2024-06-11 06:41:11 +00:00
parent 5793ff1986
commit 89bcbd5b8d
4 changed files with 39 additions and 38 deletions

View file

@ -462,6 +462,15 @@ impl<'a> IdentifierReference<'a> {
pub fn new(span: Span, name: Atom<'a>) -> Self { pub fn new(span: Span, name: Atom<'a>) -> Self {
Self { span, name, reference_id: Cell::default(), reference_flag: ReferenceFlag::default() } 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 /// Binding Identifier

View file

@ -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))
}
}

View file

@ -19,6 +19,7 @@ mod react;
mod typescript; mod typescript;
mod helpers { mod helpers {
pub mod bindings;
pub mod module_imports; pub mod module_imports;
} }

View file

@ -1,19 +1,19 @@
use std::{cell::Cell, rc::Rc}; use std::rc::Rc;
use oxc_allocator::Vec; use oxc_allocator::Vec;
use oxc_ast::{ast::*, AstBuilder}; use oxc_ast::{ast::*, AstBuilder};
use oxc_span::{Atom, GetSpan, Span, SPAN}; use oxc_span::{Atom, GetSpan, Span, SPAN};
use oxc_syntax::{ use oxc_syntax::{
identifier::{is_irregular_whitespace, is_line_terminator}, identifier::{is_irregular_whitespace, is_line_terminator},
reference::{ReferenceFlag, ReferenceId}, reference::ReferenceFlag,
symbol::{SymbolFlags, SymbolId}, symbol::SymbolFlags,
xml_entities::XML_ENTITIES, xml_entities::XML_ENTITIES,
}; };
use oxc_traverse::TraverseCtx; use oxc_traverse::TraverseCtx;
use crate::{ use crate::{
context::{Ctx, TransformCtx}, context::{Ctx, TransformCtx},
helpers::module_imports::NamedImport, helpers::{bindings::BoundIdentifier, module_imports::NamedImport},
}; };
use super::diagnostics; 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]) 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 /// Pragma used in classic mode
struct Pragma<'a> { struct Pragma<'a> {
object: Atom<'a>, object: Atom<'a>,
@ -1012,22 +994,7 @@ fn get_read_identifier_reference<'a>(
) -> IdentifierReference<'a> { ) -> IdentifierReference<'a> {
let reference_id = let reference_id =
ctx.create_reference_in_current_scope(name.to_compact_str(), ReferenceFlag::Read); ctx.create_reference_in_current_scope(name.to_compact_str(), ReferenceFlag::Read);
create_read_identifier_reference(span, name, Some(reference_id)) IdentifierReference::new_read(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,
}
} }
fn create_static_member_expression<'a>( fn create_static_member_expression<'a>(