Skip to content

Commit ecc9151

Browse files
committed
refactor(ast, parser, transformer, traverse)!: remove IdentifierReference::new methods (#6785)
1 parent c91ffbc commit ecc9151

File tree

8 files changed

+22
-98
lines changed

8 files changed

+22
-98
lines changed

crates/oxc_ast/src/ast_impl/js.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,6 @@ impl<'a> fmt::Display for IdentifierName<'a> {
307307
}
308308

309309
impl<'a> IdentifierReference<'a> {
310-
#[allow(missing_docs)]
311-
#[inline]
312-
pub fn new(span: Span, name: Atom<'a>) -> Self {
313-
Self { span, name, reference_id: Cell::default() }
314-
}
315-
316-
#[inline]
317-
#[allow(missing_docs)]
318-
pub fn new_with_reference_id(
319-
span: Span,
320-
name: Atom<'a>,
321-
reference_id: Option<ReferenceId>,
322-
) -> Self {
323-
Self { span, name, reference_id: Cell::new(reference_id) }
324-
}
325-
326310
#[inline]
327311
#[allow(missing_docs)]
328312
pub fn reference_id(&self) -> Option<ReferenceId> {

crates/oxc_parser/src/js/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> ParserImpl<'a> {
6767
}
6868
let (span, name) = self.parse_identifier_kind(Kind::Ident);
6969
self.check_identifier(span, &name);
70-
Ok(IdentifierReference::new(span, name))
70+
Ok(self.ast.identifier_reference(span, name))
7171
}
7272

7373
/// `BindingIdentifier` : Identifier

crates/oxc_parser/src/js/grammar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a> CoverGrammar<'a, ObjectProperty<'a>> for AssignmentTargetProperty<'a> {
154154
let binding = match property.key {
155155
PropertyKey::StaticIdentifier(ident) => {
156156
let ident = ident.unbox();
157-
IdentifierReference::new(ident.span, ident.name)
157+
p.ast.identifier_reference(ident.span, ident.name)
158158
}
159159
_ => return Err(p.unexpected()),
160160
};

crates/oxc_parser/src/ts/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ impl<'a> ParserImpl<'a> {
758758
pub(crate) fn parse_ts_type_name(&mut self) -> Result<TSTypeName<'a>> {
759759
let span = self.start_span();
760760
let ident = self.parse_identifier_name()?;
761-
let ident = IdentifierReference::new(ident.span, ident.name);
761+
let ident = self.ast.identifier_reference(ident.span, ident.name);
762762
let mut left = TSTypeName::IdentifierReference(self.ast.alloc(ident));
763763
while self.eat(Kind::Dot) {
764764
let right = self.parse_identifier_name()?;

crates/oxc_transformer/src/react/jsx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ fn get_read_identifier_reference<'a>(
10331033
) -> IdentifierReference<'a> {
10341034
let reference_id =
10351035
ctx.create_reference_in_current_scope(name.to_compact_str(), ReferenceFlags::Read);
1036-
IdentifierReference::new_with_reference_id(span, name, Some(reference_id))
1036+
ctx.ast.identifier_reference_with_reference_id(span, name, reference_id)
10371037
}
10381038

10391039
fn create_static_member_expression<'a>(

crates/oxc_transformer/src/typescript/annotations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,10 @@ impl<'a> Assignment<'a> {
622622
// Creates `this.name = name`
623623
fn create_this_property_assignment(&self, ctx: &mut TraverseCtx<'a>) -> Statement<'a> {
624624
let reference_id = ctx.create_bound_reference(self.symbol_id, ReferenceFlags::Read);
625-
let id = IdentifierReference::new_with_reference_id(
625+
let id = ctx.ast.identifier_reference_with_reference_id(
626626
self.span,
627627
self.name.clone(),
628-
Some(reference_id),
628+
reference_id,
629629
);
630630

631631
ctx.ast.statement_expression(

crates/oxc_traverse/src/context/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,15 @@ impl<'a> TraverseCtx<'a> {
405405
}
406406

407407
/// Create an `IdentifierReference` bound to a `SymbolId`.
408-
///
409-
/// This is a shortcut for `ctx.scoping.create_bound_reference_id`.
410-
#[inline]
411408
pub fn create_bound_reference_id(
412409
&mut self,
413410
span: Span,
414411
name: Atom<'a>,
415412
symbol_id: SymbolId,
416413
flags: ReferenceFlags,
417414
) -> IdentifierReference<'a> {
418-
self.scoping.create_bound_reference_id(span, name, symbol_id, flags)
415+
let reference_id = self.create_bound_reference(symbol_id, flags);
416+
self.ast.identifier_reference_with_reference_id(span, name, reference_id)
419417
}
420418

421419
/// Create an unbound reference.
@@ -431,16 +429,14 @@ impl<'a> TraverseCtx<'a> {
431429
}
432430

433431
/// Create an unbound `IdentifierReference`.
434-
///
435-
/// This is a shortcut for `ctx.scoping.create_unbound_reference_id`.
436-
#[inline]
437432
pub fn create_unbound_reference_id(
438433
&mut self,
439434
span: Span,
440435
name: Atom<'a>,
441436
flags: ReferenceFlags,
442437
) -> IdentifierReference<'a> {
443-
self.scoping.create_unbound_reference_id(span, name, flags)
438+
let reference_id = self.create_unbound_reference(name.to_compact_str(), flags);
439+
self.ast.identifier_reference_with_reference_id(span, name, reference_id)
444440
}
445441

446442
/// Create a reference optionally bound to a `SymbolId`.
@@ -463,17 +459,18 @@ impl<'a> TraverseCtx<'a> {
463459
///
464460
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference_id`
465461
/// or `TraverseCtx::create_unbound_reference_id`.
466-
///
467-
/// This is a shortcut for `ctx.scoping.create_reference_id`.
468-
#[inline]
469462
pub fn create_reference_id(
470463
&mut self,
471464
span: Span,
472465
name: Atom<'a>,
473466
symbol_id: Option<SymbolId>,
474467
flags: ReferenceFlags,
475468
) -> IdentifierReference<'a> {
476-
self.scoping.create_reference_id(span, name, symbol_id, flags)
469+
if let Some(symbol_id) = symbol_id {
470+
self.create_bound_reference_id(span, name, symbol_id, flags)
471+
} else {
472+
self.create_unbound_reference_id(span, name, flags)
473+
}
477474
}
478475

479476
/// Create reference in current scope, looking up binding for `name`,
@@ -509,15 +506,17 @@ impl<'a> TraverseCtx<'a> {
509506
/// This method makes a lookup of the `SymbolId` for the reference. If you need to create multiple
510507
/// `IdentifierReference`s for the same binding, it is better to look up the `SymbolId` only once,
511508
/// and generate `IdentifierReference`s with `TraverseCtx::create_reference_id`.
512-
///
513-
/// This is a shortcut for `ctx.scoping.clone_identifier_reference`.
514-
#[inline]
515509
pub fn clone_identifier_reference(
516510
&mut self,
517511
ident: &IdentifierReference<'a>,
518512
flags: ReferenceFlags,
519513
) -> IdentifierReference<'a> {
520-
self.scoping.clone_identifier_reference(ident, flags)
514+
let reference =
515+
self.symbols().get_reference(ident.reference_id.get().unwrap_or_else(|| {
516+
unreachable!("IdentifierReference must have a reference_id");
517+
}));
518+
let symbol_id = reference.symbol_id();
519+
self.create_reference_id(ident.span, ident.name.clone(), symbol_id, flags)
521520
}
522521

523522
/// Determine whether evaluating the specific input `node` is a consequenceless reference.

crates/oxc_traverse/src/context/scoping.rs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hash::FxHashSet;
77
#[allow(clippy::wildcard_imports)]
88
use oxc_ast::{ast::*, visit::Visit};
99
use oxc_semantic::{NodeId, Reference, ScopeTree, SymbolTable};
10-
use oxc_span::{Atom, CompactStr, Span};
10+
use oxc_span::CompactStr;
1111
use oxc_syntax::{
1212
reference::{ReferenceFlags, ReferenceId},
1313
scope::{ScopeFlags, ScopeId},
@@ -266,18 +266,6 @@ impl TraverseScoping {
266266
reference_id
267267
}
268268

269-
/// Create an `IdentifierReference` bound to a `SymbolId`
270-
pub fn create_bound_reference_id<'a>(
271-
&mut self,
272-
span: Span,
273-
name: Atom<'a>,
274-
symbol_id: SymbolId,
275-
flags: ReferenceFlags,
276-
) -> IdentifierReference<'a> {
277-
let reference_id = self.create_bound_reference(symbol_id, flags);
278-
IdentifierReference::new_with_reference_id(span, name, Some(reference_id))
279-
}
280-
281269
/// Create an unbound reference
282270
pub fn create_unbound_reference(
283271
&mut self,
@@ -290,17 +278,6 @@ impl TraverseScoping {
290278
reference_id
291279
}
292280

293-
/// Create an unbound `IdentifierReference`
294-
pub fn create_unbound_reference_id<'a>(
295-
&mut self,
296-
span: Span,
297-
name: Atom<'a>,
298-
flags: ReferenceFlags,
299-
) -> IdentifierReference<'a> {
300-
let reference_id = self.create_unbound_reference(name.to_compact_str(), flags);
301-
IdentifierReference::new_with_reference_id(span, name, Some(reference_id))
302-
}
303-
304281
/// Create a reference optionally bound to a `SymbolId`.
305282
///
306283
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference`
@@ -318,24 +295,6 @@ impl TraverseScoping {
318295
}
319296
}
320297

321-
/// Create an `IdentifierReference` optionally bound to a `SymbolId`.
322-
///
323-
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference_id`
324-
/// or `TraverseCtx::create_unbound_reference_id`.
325-
pub fn create_reference_id<'a>(
326-
&mut self,
327-
span: Span,
328-
name: Atom<'a>,
329-
symbol_id: Option<SymbolId>,
330-
flags: ReferenceFlags,
331-
) -> IdentifierReference<'a> {
332-
if let Some(symbol_id) = symbol_id {
333-
self.create_bound_reference_id(span, name, symbol_id, flags)
334-
} else {
335-
self.create_unbound_reference_id(span, name, flags)
336-
}
337-
}
338-
339298
/// Create reference in current scope, looking up binding for `name`
340299
pub fn create_reference_in_current_scope(
341300
&mut self,
@@ -365,24 +324,6 @@ impl TraverseScoping {
365324
self.delete_reference(ident.reference_id().unwrap(), &ident.name);
366325
}
367326

368-
/// Clone `IdentifierReference` based on the original reference's `SymbolId` and name.
369-
///
370-
/// This method makes a lookup of the `SymbolId` for the reference. If you need to create multiple
371-
/// `IdentifierReference`s for the same binding, it is better to look up the `SymbolId` only once,
372-
/// and generate `IdentifierReference`s with `TraverseScoping::create_reference_id`.
373-
pub fn clone_identifier_reference<'a>(
374-
&mut self,
375-
ident: &IdentifierReference<'a>,
376-
flags: ReferenceFlags,
377-
) -> IdentifierReference<'a> {
378-
let reference =
379-
self.symbols().get_reference(ident.reference_id.get().unwrap_or_else(|| {
380-
unreachable!("IdentifierReference must have a reference_id");
381-
}));
382-
let symbol_id = reference.symbol_id();
383-
self.create_reference_id(ident.span, ident.name.clone(), symbol_id, flags)
384-
}
385-
386327
/// Determine whether evaluating the specific input `node` is a consequenceless reference.
387328
///
388329
/// I.E evaluating it won't result in potentially arbitrary code from being ran. The following are

0 commit comments

Comments
 (0)