refactor(transformer): improve comments for BoundIdentifier helper (#4895)

Add more comments for `BindingIdentifier` helper, and correct comments which were inaccurate.
This commit is contained in:
overlookmotel 2024-08-14 11:52:25 +00:00
parent 93ae1c78b8
commit 117dff2c47

View file

@ -9,7 +9,33 @@ use oxc_syntax::{
}; };
use oxc_traverse::TraverseCtx; use oxc_traverse::TraverseCtx;
/// Store for a created binding identifier /// Info about a binding, from which one can create a `BindingIdentifier` or `IdentifierReference`s.
///
/// Typical usage:
///
/// ```rs
/// // Generate a UID for a top-level var
/// let binding = BoundIdentifier::new_root_uid("foo", SymbolFlags::FunctionScopedVariable, ctx);
///
/// // Generate an `IdentifierReference`s and insert them into AST
/// some_node.id = binding.create_read_reference(ctx);
/// some_other_node.id = binding.create_read_reference(ctx);
///
/// // Store details of the binding for later
/// self.foo_binding = binding;
///
/// // Later on in `exit_program`
/// let id = binding.create_binding_identifier();
/// // Insert `var <id> = something;` into `program.body`
/// ```
///
/// Notes:
///
/// * `BoundIdentifier` is smaller than `BindingIdentifier`, so takes less memory when you store
/// it for later use.
/// * `BoundIdentifier` is `Clone` (unlike `BindingIdentifier`).
/// * `BoundIdentifier` re-uses the same `Atom` for all `BindingIdentifier` / `IdentifierReference`s
/// created from it.
#[derive(Clone)] #[derive(Clone)]
pub struct BoundIdentifier<'a> { pub struct BoundIdentifier<'a> {
pub name: Atom<'a>, pub name: Atom<'a>,
@ -17,7 +43,7 @@ pub struct BoundIdentifier<'a> {
} }
impl<'a> BoundIdentifier<'a> { impl<'a> BoundIdentifier<'a> {
/// Create `BoundIdentifier` for new binding /// Create `BoundIdentifier` for new binding in specified scope
pub fn new_uid( pub fn new_uid(
name: &str, name: &str,
scope_id: ScopeId, scope_id: ScopeId,
@ -35,14 +61,12 @@ impl<'a> BoundIdentifier<'a> {
Self::new_uid(name, scope_id, flags, ctx) Self::new_uid(name, scope_id, flags, ctx)
} }
/// Create `IdentifierReference` referencing this binding which is read from /// Create `IdentifierReference` referencing this binding, which is read from, with dummy `Span`
/// in current scope
pub fn create_read_reference(&self, ctx: &mut TraverseCtx<'a>) -> IdentifierReference<'a> { pub fn create_read_reference(&self, ctx: &mut TraverseCtx<'a>) -> IdentifierReference<'a> {
self.create_spanned_read_reference(SPAN, ctx) self.create_spanned_read_reference(SPAN, ctx)
} }
/// Create `IdentifierReference` referencing this binding which is read from /// Create `IdentifierReference` referencing this binding, which is read from, with specified `Span`
/// in current scope
pub fn create_spanned_read_reference( pub fn create_spanned_read_reference(
&self, &self,
span: Span, span: Span,