mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 13:48:55 +00:00
refactor(ast, parser, transformer, traverse)!: remove IdentifierReference::new methods (#6785)
This commit is contained in:
parent
c91ffbcb01
commit
ecc9151f99
8 changed files with 22 additions and 98 deletions
|
|
@ -307,22 +307,6 @@ impl<'a> fmt::Display for IdentifierName<'a> {
|
|||
}
|
||||
|
||||
impl<'a> IdentifierReference<'a> {
|
||||
#[allow(missing_docs)]
|
||||
#[inline]
|
||||
pub fn new(span: Span, name: Atom<'a>) -> Self {
|
||||
Self { span, name, reference_id: Cell::default() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(missing_docs)]
|
||||
pub fn new_with_reference_id(
|
||||
span: Span,
|
||||
name: Atom<'a>,
|
||||
reference_id: Option<ReferenceId>,
|
||||
) -> Self {
|
||||
Self { span, name, reference_id: Cell::new(reference_id) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(missing_docs)]
|
||||
pub fn reference_id(&self) -> Option<ReferenceId> {
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ impl<'a> ParserImpl<'a> {
|
|||
}
|
||||
let (span, name) = self.parse_identifier_kind(Kind::Ident);
|
||||
self.check_identifier(span, &name);
|
||||
Ok(IdentifierReference::new(span, name))
|
||||
Ok(self.ast.identifier_reference(span, name))
|
||||
}
|
||||
|
||||
/// `BindingIdentifier` : Identifier
|
||||
|
|
|
|||
|
|
@ -154,7 +154,7 @@ impl<'a> CoverGrammar<'a, ObjectProperty<'a>> for AssignmentTargetProperty<'a> {
|
|||
let binding = match property.key {
|
||||
PropertyKey::StaticIdentifier(ident) => {
|
||||
let ident = ident.unbox();
|
||||
IdentifierReference::new(ident.span, ident.name)
|
||||
p.ast.identifier_reference(ident.span, ident.name)
|
||||
}
|
||||
_ => return Err(p.unexpected()),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -758,7 +758,7 @@ impl<'a> ParserImpl<'a> {
|
|||
pub(crate) fn parse_ts_type_name(&mut self) -> Result<TSTypeName<'a>> {
|
||||
let span = self.start_span();
|
||||
let ident = self.parse_identifier_name()?;
|
||||
let ident = IdentifierReference::new(ident.span, ident.name);
|
||||
let ident = self.ast.identifier_reference(ident.span, ident.name);
|
||||
let mut left = TSTypeName::IdentifierReference(self.ast.alloc(ident));
|
||||
while self.eat(Kind::Dot) {
|
||||
let right = self.parse_identifier_name()?;
|
||||
|
|
|
|||
|
|
@ -1033,7 +1033,7 @@ fn get_read_identifier_reference<'a>(
|
|||
) -> IdentifierReference<'a> {
|
||||
let reference_id =
|
||||
ctx.create_reference_in_current_scope(name.to_compact_str(), ReferenceFlags::Read);
|
||||
IdentifierReference::new_with_reference_id(span, name, Some(reference_id))
|
||||
ctx.ast.identifier_reference_with_reference_id(span, name, reference_id)
|
||||
}
|
||||
|
||||
fn create_static_member_expression<'a>(
|
||||
|
|
|
|||
|
|
@ -622,10 +622,10 @@ impl<'a> Assignment<'a> {
|
|||
// Creates `this.name = name`
|
||||
fn create_this_property_assignment(&self, ctx: &mut TraverseCtx<'a>) -> Statement<'a> {
|
||||
let reference_id = ctx.create_bound_reference(self.symbol_id, ReferenceFlags::Read);
|
||||
let id = IdentifierReference::new_with_reference_id(
|
||||
let id = ctx.ast.identifier_reference_with_reference_id(
|
||||
self.span,
|
||||
self.name.clone(),
|
||||
Some(reference_id),
|
||||
reference_id,
|
||||
);
|
||||
|
||||
ctx.ast.statement_expression(
|
||||
|
|
|
|||
|
|
@ -405,9 +405,6 @@ impl<'a> TraverseCtx<'a> {
|
|||
}
|
||||
|
||||
/// Create an `IdentifierReference` bound to a `SymbolId`.
|
||||
///
|
||||
/// This is a shortcut for `ctx.scoping.create_bound_reference_id`.
|
||||
#[inline]
|
||||
pub fn create_bound_reference_id(
|
||||
&mut self,
|
||||
span: Span,
|
||||
|
|
@ -415,7 +412,8 @@ impl<'a> TraverseCtx<'a> {
|
|||
symbol_id: SymbolId,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
self.scoping.create_bound_reference_id(span, name, symbol_id, flags)
|
||||
let reference_id = self.create_bound_reference(symbol_id, flags);
|
||||
self.ast.identifier_reference_with_reference_id(span, name, reference_id)
|
||||
}
|
||||
|
||||
/// Create an unbound reference.
|
||||
|
|
@ -431,16 +429,14 @@ impl<'a> TraverseCtx<'a> {
|
|||
}
|
||||
|
||||
/// Create an unbound `IdentifierReference`.
|
||||
///
|
||||
/// This is a shortcut for `ctx.scoping.create_unbound_reference_id`.
|
||||
#[inline]
|
||||
pub fn create_unbound_reference_id(
|
||||
&mut self,
|
||||
span: Span,
|
||||
name: Atom<'a>,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
self.scoping.create_unbound_reference_id(span, name, flags)
|
||||
let reference_id = self.create_unbound_reference(name.to_compact_str(), flags);
|
||||
self.ast.identifier_reference_with_reference_id(span, name, reference_id)
|
||||
}
|
||||
|
||||
/// Create a reference optionally bound to a `SymbolId`.
|
||||
|
|
@ -463,9 +459,6 @@ impl<'a> TraverseCtx<'a> {
|
|||
///
|
||||
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference_id`
|
||||
/// or `TraverseCtx::create_unbound_reference_id`.
|
||||
///
|
||||
/// This is a shortcut for `ctx.scoping.create_reference_id`.
|
||||
#[inline]
|
||||
pub fn create_reference_id(
|
||||
&mut self,
|
||||
span: Span,
|
||||
|
|
@ -473,7 +466,11 @@ impl<'a> TraverseCtx<'a> {
|
|||
symbol_id: Option<SymbolId>,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
self.scoping.create_reference_id(span, name, symbol_id, flags)
|
||||
if let Some(symbol_id) = symbol_id {
|
||||
self.create_bound_reference_id(span, name, symbol_id, flags)
|
||||
} else {
|
||||
self.create_unbound_reference_id(span, name, flags)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create reference in current scope, looking up binding for `name`,
|
||||
|
|
@ -509,15 +506,17 @@ impl<'a> TraverseCtx<'a> {
|
|||
/// This method makes a lookup of the `SymbolId` for the reference. If you need to create multiple
|
||||
/// `IdentifierReference`s for the same binding, it is better to look up the `SymbolId` only once,
|
||||
/// and generate `IdentifierReference`s with `TraverseCtx::create_reference_id`.
|
||||
///
|
||||
/// This is a shortcut for `ctx.scoping.clone_identifier_reference`.
|
||||
#[inline]
|
||||
pub fn clone_identifier_reference(
|
||||
&mut self,
|
||||
ident: &IdentifierReference<'a>,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
self.scoping.clone_identifier_reference(ident, flags)
|
||||
let reference =
|
||||
self.symbols().get_reference(ident.reference_id.get().unwrap_or_else(|| {
|
||||
unreachable!("IdentifierReference must have a reference_id");
|
||||
}));
|
||||
let symbol_id = reference.symbol_id();
|
||||
self.create_reference_id(ident.span, ident.name.clone(), symbol_id, flags)
|
||||
}
|
||||
|
||||
/// Determine whether evaluating the specific input `node` is a consequenceless reference.
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use rustc_hash::FxHashSet;
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::{ast::*, visit::Visit};
|
||||
use oxc_semantic::{NodeId, Reference, ScopeTree, SymbolTable};
|
||||
use oxc_span::{Atom, CompactStr, Span};
|
||||
use oxc_span::CompactStr;
|
||||
use oxc_syntax::{
|
||||
reference::{ReferenceFlags, ReferenceId},
|
||||
scope::{ScopeFlags, ScopeId},
|
||||
|
|
@ -266,18 +266,6 @@ impl TraverseScoping {
|
|||
reference_id
|
||||
}
|
||||
|
||||
/// Create an `IdentifierReference` bound to a `SymbolId`
|
||||
pub fn create_bound_reference_id<'a>(
|
||||
&mut self,
|
||||
span: Span,
|
||||
name: Atom<'a>,
|
||||
symbol_id: SymbolId,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
let reference_id = self.create_bound_reference(symbol_id, flags);
|
||||
IdentifierReference::new_with_reference_id(span, name, Some(reference_id))
|
||||
}
|
||||
|
||||
/// Create an unbound reference
|
||||
pub fn create_unbound_reference(
|
||||
&mut self,
|
||||
|
|
@ -290,17 +278,6 @@ impl TraverseScoping {
|
|||
reference_id
|
||||
}
|
||||
|
||||
/// Create an unbound `IdentifierReference`
|
||||
pub fn create_unbound_reference_id<'a>(
|
||||
&mut self,
|
||||
span: Span,
|
||||
name: Atom<'a>,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
let reference_id = self.create_unbound_reference(name.to_compact_str(), flags);
|
||||
IdentifierReference::new_with_reference_id(span, name, Some(reference_id))
|
||||
}
|
||||
|
||||
/// Create a reference optionally bound to a `SymbolId`.
|
||||
///
|
||||
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference`
|
||||
|
|
@ -318,24 +295,6 @@ impl TraverseScoping {
|
|||
}
|
||||
}
|
||||
|
||||
/// Create an `IdentifierReference` optionally bound to a `SymbolId`.
|
||||
///
|
||||
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference_id`
|
||||
/// or `TraverseCtx::create_unbound_reference_id`.
|
||||
pub fn create_reference_id<'a>(
|
||||
&mut self,
|
||||
span: Span,
|
||||
name: Atom<'a>,
|
||||
symbol_id: Option<SymbolId>,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
if let Some(symbol_id) = symbol_id {
|
||||
self.create_bound_reference_id(span, name, symbol_id, flags)
|
||||
} else {
|
||||
self.create_unbound_reference_id(span, name, flags)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create reference in current scope, looking up binding for `name`
|
||||
pub fn create_reference_in_current_scope(
|
||||
&mut self,
|
||||
|
|
@ -365,24 +324,6 @@ impl TraverseScoping {
|
|||
self.delete_reference(ident.reference_id().unwrap(), &ident.name);
|
||||
}
|
||||
|
||||
/// Clone `IdentifierReference` based on the original reference's `SymbolId` and name.
|
||||
///
|
||||
/// This method makes a lookup of the `SymbolId` for the reference. If you need to create multiple
|
||||
/// `IdentifierReference`s for the same binding, it is better to look up the `SymbolId` only once,
|
||||
/// and generate `IdentifierReference`s with `TraverseScoping::create_reference_id`.
|
||||
pub fn clone_identifier_reference<'a>(
|
||||
&mut self,
|
||||
ident: &IdentifierReference<'a>,
|
||||
flags: ReferenceFlags,
|
||||
) -> IdentifierReference<'a> {
|
||||
let reference =
|
||||
self.symbols().get_reference(ident.reference_id.get().unwrap_or_else(|| {
|
||||
unreachable!("IdentifierReference must have a reference_id");
|
||||
}));
|
||||
let symbol_id = reference.symbol_id();
|
||||
self.create_reference_id(ident.span, ident.name.clone(), symbol_id, flags)
|
||||
}
|
||||
|
||||
/// Determine whether evaluating the specific input `node` is a consequenceless reference.
|
||||
///
|
||||
/// I.E evaluating it won't result in potentially arbitrary code from being ran. The following are
|
||||
|
|
|
|||
Loading…
Reference in a new issue