TraverseCtx

Struct TraverseCtx 

Source
pub struct TraverseCtx<'a, State> {
    pub state: State,
    pub ancestry: TraverseAncestry<'a>,
    pub scoping: TraverseScoping<'a>,
    pub ast: AstBuilder<'a>,
}
Expand description

AST traversal utilities Traverse context.

Passed to all AST visitor functions.

Provides ability to:

§Namespaced APIs

All APIs are provided via 2 routes:

  1. Directly on TraverseCtx.
  2. Via “namespaces”.
DirectNamespaced
ctx.parent()ctx.ancestry.parent()
ctx.current_scope_id()ctx.scoping.current_scope_id()
ctx.alloc(thing)ctx.ast.alloc(thing)

Purpose of the “namespaces” is to support if you want to mutate scope tree or symbol table while holding an &Ancestor, or AST nodes obtained from an &Ancestor.

For example, this will not compile because it attempts to borrow ctx immutably and mutably at same time:

use oxc_ast::ast::*;
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

struct MyTransform;
impl<'a> Traverse<'a> for MyTransform {
    fn enter_unary_expression(&mut self, unary_expr: &mut UnaryExpression<'a>, ctx: &mut TraverseCtx<'a>) {
        // `right` is ultimately borrowed from `ctx`
        let right = match ctx.parent() {
            Ancestor::BinaryExpressionLeft(bin_expr) => bin_expr.right(),
            _ => return,
        };

        // Won't compile! `ctx.scopes_mut()` attempts to mut borrow `ctx`
        // while it's already borrowed by `right`.
        let scope_tree_mut = ctx.scopes_mut();

        // Use `right` later on
        dbg!(right);
    }
}

You can fix this by using the “namespaced” methods instead. This works because you can borrow ctx.ancestry and ctx.scoping simultaneously:

use oxc_ast::ast::*;
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

struct MyTransform;
impl<'a> Traverse<'a, ()> for MyTransform {
    fn enter_unary_expression(&mut self, unary_expr: &mut UnaryExpression<'a>, ctx: &mut TraverseCtx<'a, ()>) {
        let right = match ctx.ancestry.parent() {
            Ancestor::BinaryExpressionLeft(bin_expr) => bin_expr.right(),
            _ => return,
        };

        let scoping_mut = ctx.scoping.scoping_mut();

        dbg!(right);
    }
}

Fields§

§state: State§ancestry: TraverseAncestry<'a>§scoping: TraverseScoping<'a>§ast: AstBuilder<'a>

Implementations§

Source§

impl<'a, State> TraverseCtx<'a, State>

Source

pub fn alloc<T>(&self, node: T) -> Box<'a, T>

Allocate a node in the arena.

Returns a Box<'a, T>.

Shortcut for ctx.ast.alloc.

Source

pub fn parent<'t>(&'t self) -> Ancestor<'a, 't>

Get parent of current node.

Shortcut for ctx.ancestry.parent.

Source

pub fn ancestor<'t>(&'t self, level: usize) -> Ancestor<'a, 't>

Get ancestor of current node.

level is number of levels above parent. ancestor(0) is equivalent to parent() (but better to use parent() as it’s more efficient).

If level is out of bounds (above Program), returns Ancestor::None.

Shortcut for ctx.ancestry.ancestor.

Source

pub fn ancestors<'t>(&'t self) -> impl Iterator<Item = Ancestor<'a, 't>>

Get iterator over ancestors, starting with parent and working up.

Last Ancestor returned will be Program. Ancestor::None is not included in iteration.

Shortcut for ctx.ancestry.ancestors.

Source

pub fn ancestors_depth(&self) -> usize

Get depth in the AST.

Count includes current node. i.e. in Program, depth is 1.

Shortcut for self.ancestry.ancestors_depth.

Source

pub fn current_scope_id(&self) -> ScopeId

Get current scope ID.

Shortcut for ctx.scoping.current_scope_id.

Source

pub fn current_hoist_scope_id(&self) -> ScopeId

Get current var hoisting scope ID.

Shortcut for ctx.scoping.current_hoist_scope_id.

Source

pub fn current_block_scope_id(&self) -> ScopeId

Get current block scope ID.

Shortcut for ctx.scoping.current_block_scope_id.

Source

pub fn current_scope_flags(&self) -> ScopeFlags

Get current scope flags.

Shortcut for ctx.scoping.current_scope_flags.

Source

pub fn scoping(&self) -> &Scoping

Get scopes tree.

Shortcut for ctx.scoping.scopes.

Source

pub fn scoping_mut(&mut self) -> &mut Scoping

Get mutable scopes tree.

Shortcut for ctx.scoping.scopes_mut.

Source

pub fn ancestor_scopes(&self) -> impl Iterator<Item = ScopeId>

Get iterator over scopes, starting with current scope and working up.

This is a shortcut for ctx.scoping.parent_scopes.

Source

pub fn create_child_scope( &mut self, parent_id: ScopeId, flags: ScopeFlags, ) -> ScopeId

Create new scope as child of provided scope.

flags provided are amended to inherit from parent scope’s flags.

This is a shortcut for ctx.scoping.create_child_scope.

Source

pub fn create_child_scope_of_current(&mut self, flags: ScopeFlags) -> ScopeId

Create new scope as child of current scope.

flags provided are amended to inherit from parent scope’s flags.

This is a shortcut for ctx.scoping.create_child_scope_of_current.

Source

pub fn insert_scope_below_statement( &mut self, stmt: &Statement<'_>, flags: ScopeFlags, ) -> ScopeId

Insert a scope into scope tree below a statement.

Statement must be in current scope. New scope is created as child of current scope. All child scopes of the statement are reassigned to be children of the new scope.

flags provided are amended to inherit from parent scope’s flags.

This is a shortcut for ctx.scoping.insert_scope_below_statement.

Source

pub fn insert_scope_below_statement_from_scope_id( &mut self, stmt: &Statement<'_>, scope_id: ScopeId, flags: ScopeFlags, ) -> ScopeId

Insert a scope into scope tree below a statement.

Statement must be in provided scope. New scope is created as child of the provided scope. All child scopes of the statement are reassigned to be children of the new scope.

flags provided are amended to inherit from parent scope’s flags.

This is a shortcut for ctx.scoping.insert_scope_below_statement_from_scope_id.

Source

pub fn insert_scope_below_expression( &mut self, expr: &Expression<'_>, flags: ScopeFlags, ) -> ScopeId

Insert a scope into scope tree below an expression.

Expression must be in current scope. New scope is created as child of current scope. All child scopes of the expression are reassigned to be children of the new scope.

flags provided are amended to inherit from parent scope’s flags.

This is a shortcut for ctx.scoping.insert_scope_below_expression.

Source

pub fn insert_scope_below_statements( &mut self, stmts: &Vec<'_, Statement<'_>>, flags: ScopeFlags, ) -> ScopeId

Insert a scope into scope tree below a Vec of statements.

Statements must be in current scope. New scope is created as child of current scope. All child scopes of the statement are reassigned to be children of the new scope.

flags provided are amended to inherit from parent scope’s flags.

This is a shortcut for ctx.scoping.insert_scope_below_statements.

Source

pub fn insert_scope_between( &mut self, parent_id: ScopeId, child_id: ScopeId, flags: ScopeFlags, ) -> ScopeId

Insert a scope between a parent and a child scope.

For example, given the following scopes

parentScope1: {
    childScope: { }
    childScope2: { }
}

and calling this function with parentScope1 and childScope, the resulting scopes will be:

parentScope1: {
    newScope: {
        childScope: { }
    }
    childScope2: { }
}

This is a shortcut for ctx.scoping.insert_scope_between.

Source

pub fn remove_scope_for_expression( &mut self, scope_id: ScopeId, expr: &Expression<'_>, )

Remove scope for an expression from the scope chain.

Delete the scope and set parent of its child scopes to its parent scope. e.g.:

  • Starting scopes parentage A -> B, B -> C, B -> D.
  • Remove scope B from chain.
  • End result: scopes A -> C, A -> D.

Use this when removing an expression which owns a scope, without removing its children. For example when unwrapping (() => foo)() to just foo. foo here could be an expression which itself contains scopes.

This is a shortcut for ctx.scoping.remove_scope_for_expression.

Source

pub fn generate_binding( &mut self, name: Atom<'a>, scope_id: ScopeId, flags: SymbolFlags, ) -> BoundIdentifier<'a>

Generate binding.

Creates a symbol with the provided name and flags and adds it to the specified scope.

This is a shortcut for ctx.scoping.generate_binding.

Source

pub fn generate_binding_in_current_scope( &mut self, name: Atom<'a>, flags: SymbolFlags, ) -> BoundIdentifier<'a>

Generate binding in current scope.

Creates a symbol with the provided name and flags and adds it to the current scope.

This is a shortcut for ctx.scoping.generate_binding_in_current_scope.

Source

pub fn generate_uid_name(&mut self, name: &str) -> Atom<'a>

Generate UID var name.

Finds a unique variable name which does clash with any other variables used in the program.

See TraverseScoping::generate_uid_name for important information on how UIDs are generated. There are some potential “gotchas”.

This is a shortcut for ctx.scoping.generate_uid_name.

Source

pub fn generate_uid( &mut self, name: &str, scope_id: ScopeId, flags: SymbolFlags, ) -> BoundIdentifier<'a>

Generate UID.

See also comments on TraverseScoping::generate_uid_name for important information on how UIDs are generated. There are some potential “gotchas”.

Source

pub fn generate_uid_in_current_scope( &mut self, name: &str, flags: SymbolFlags, ) -> BoundIdentifier<'a>

Generate UID in current scope.

See also comments on TraverseScoping::generate_uid_name for important information on how UIDs are generated. There are some potential “gotchas”.

Source

pub fn generate_uid_in_root_scope( &mut self, name: &str, flags: SymbolFlags, ) -> BoundIdentifier<'a>

Generate UID in root scope.

See also comments on TraverseScoping::generate_uid_name for important information on how UIDs are generated. There are some potential “gotchas”.

Source

pub fn generate_uid_based_on_node<N>( &mut self, node: &N, scope_id: ScopeId, flags: SymbolFlags, ) -> BoundIdentifier<'a>
where N: GatherNodeParts<'a>,

Generate UID based on node.

Recursively gathers the identifying names of a node, and joins them with $.

Based on Babel’s scope.generateUidBasedOnNode logic. https://github.com/babel/babel/blob/419644f27c5c59deb19e71aaabd417a3bc5483ca/packages/babel-traverse/src/scope/index.ts#L543

Source

pub fn generate_uid_in_current_scope_based_on_node<N>( &mut self, node: &N, flags: SymbolFlags, ) -> BoundIdentifier<'a>
where N: GatherNodeParts<'a>,

Generate UID in current scope based on node.

See also comments on TraverseScoping::generate_uid_name for important information on how UIDs are generated. There are some potential “gotchas”.

Source

pub fn generate_uid_in_current_hoist_scope( &mut self, name: &str, ) -> BoundIdentifier<'a>

Generate UID in current hoist scope.

See also comments on TraverseScoping::generate_uid_name for important information on how UIDs are generated. There are some potential “gotchas”.

Source

pub fn generate_uid_in_current_hoist_scope_based_on_node<N>( &mut self, node: &N, ) -> BoundIdentifier<'a>
where N: GatherNodeParts<'a>,

Generate UID in current hoist scope based on node.

See also comments on TraverseScoping::generate_uid_name for important information on how UIDs are generated. There are some potential “gotchas”.

Source

pub fn create_bound_reference( &mut self, symbol_id: SymbolId, flags: ReferenceFlags, ) -> ReferenceId

Create a reference bound to a SymbolId.

This is a shortcut for ctx.scoping.create_bound_reference.

Source

pub fn create_bound_ident_reference( &mut self, span: Span, name: Atom<'a>, symbol_id: SymbolId, flags: ReferenceFlags, ) -> IdentifierReference<'a>

Create an IdentifierReference bound to a SymbolId.

Source

pub fn create_bound_ident_expr( &mut self, span: Span, name: Atom<'a>, symbol_id: SymbolId, flags: ReferenceFlags, ) -> Expression<'a>

Create an Expression::Identifier bound to a SymbolId.

Source

pub fn create_unbound_reference( &mut self, name: &str, flags: ReferenceFlags, ) -> ReferenceId

Create an unbound reference.

This is a shortcut for ctx.scoping.create_unbound_reference.

Source

pub fn create_unbound_ident_reference( &mut self, span: Span, name: Atom<'a>, flags: ReferenceFlags, ) -> IdentifierReference<'a>

Create an unbound IdentifierReference.

Source

pub fn create_unbound_ident_expr( &mut self, span: Span, name: Atom<'a>, flags: ReferenceFlags, ) -> Expression<'a>

Create an unbound Expression::Identifier.

Source

pub fn create_reference( &mut self, name: &str, symbol_id: Option<SymbolId>, flags: ReferenceFlags, ) -> ReferenceId

Create a reference optionally bound to a SymbolId.

If you know if there’s a SymbolId or not, prefer TraverseCtx::create_bound_reference or TraverseCtx::create_unbound_reference.

This is a shortcut for ctx.scoping.create_reference.

Source

pub fn create_ident_reference( &mut self, span: Span, name: Atom<'a>, symbol_id: Option<SymbolId>, flags: ReferenceFlags, ) -> IdentifierReference<'a>

Create an IdentifierReference optionally bound to a SymbolId.

If you know if there’s a SymbolId or not, prefer TraverseCtx::create_bound_ident_reference or TraverseCtx::create_unbound_ident_reference.

Source

pub fn create_ident_expr( &mut self, span: Span, name: Atom<'a>, symbol_id: Option<SymbolId>, flags: ReferenceFlags, ) -> Expression<'a>

Create an Expression::Identifier optionally bound to a SymbolId.

If you know if there’s a SymbolId or not, prefer TraverseCtx::create_bound_ident_expr or TraverseCtx::create_unbound_ident_expr.

Source

pub fn create_reference_in_current_scope( &mut self, name: &str, flags: ReferenceFlags, ) -> ReferenceId

Create reference in current scope, looking up binding for name,

This is a shortcut for ctx.scoping.create_reference_in_current_scope.

Source

pub fn delete_reference(&mut self, reference_id: ReferenceId, name: &str)

Delete a reference.

Provided name must match reference_id.

This is a shortcut for ctx.scoping.delete_reference.

Source

pub fn delete_reference_for_identifier( &mut self, ident: &IdentifierReference<'_>, )

Delete reference for an IdentifierReference.

This is a shortcut for ctx.scoping.delete_reference_for_identifier.

Auto Trait Implementations§

§

impl<'a, State> Freeze for TraverseCtx<'a, State>
where State: Freeze,

§

impl<'a, State> !RefUnwindSafe for TraverseCtx<'a, State>

§

impl<'a, State> !Send for TraverseCtx<'a, State>

§

impl<'a, State> !Sync for TraverseCtx<'a, State>

§

impl<'a, State> Unpin for TraverseCtx<'a, State>
where State: Unpin,

§

impl<'a, State> !UnwindSafe for TraverseCtx<'a, State>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'a, T> FromIn<'a, T> for T

Source§

fn from_in(t: T, _: &'a Allocator) -> T

Converts to this type from the input type within the given allocator.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<'a, T, U> IntoIn<'a, U> for T
where U: FromIn<'a, T>,

Source§

fn into_in(self, allocator: &'a Allocator) -> U

Converts this type into the (usually inferred) input type within the given allocator.
Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.