mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(traverse): allow TraverseCtx::find_ancestor closure to return AST node (#3236)
Add a lifetime to `TraverseCtx::find_ancestor` to allow the closure to return an AST node.
This commit is contained in:
parent
4e20b04acc
commit
a23ba71689
1 changed files with 26 additions and 2 deletions
|
|
@ -81,9 +81,33 @@ impl<'a> TraverseCtx<'a> {
|
|||
/// * `FinderRet::Found(value)` to stop walking and return `Some(value)`.
|
||||
/// * `FinderRet::Stop` to stop walking and return `None`.
|
||||
/// * `FinderRet::Continue` to continue walking up.
|
||||
pub fn find_ancestor<F, O>(&self, finder: F) -> Option<O>
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use oxc_ast::ast::ThisExpression;
|
||||
/// use oxc_traverse::{Ancestor, FinderRet, Traverse, TraverseCtx};
|
||||
///
|
||||
/// struct MyTraverse;
|
||||
/// impl<'a> Traverse<'a> for MyTraverse {
|
||||
/// fn enter_this_expression(&mut self, this_expr: &mut ThisExpression, ctx: &TraverseCtx<'a>) {
|
||||
/// // Get name of function where `this` is bound
|
||||
/// let fn_id = ctx.find_ancestor(|ancestor| {
|
||||
/// match ancestor {
|
||||
/// Ancestor::FunctionBody(func) => FinderRet::Found(func.id()),
|
||||
/// Ancestor::FunctionParams(func) => FinderRet::Found(func.id()),
|
||||
/// _ => FinderRet::Continue
|
||||
/// }
|
||||
/// });
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
//
|
||||
// `'c` lifetime on `&'c self` and `&'c Ancestor` passed into the closure
|
||||
// allows an `Ancestor` or AST node to be returned from the closure.
|
||||
pub fn find_ancestor<'c, F, O>(&'c self, finder: F) -> Option<O>
|
||||
where
|
||||
F: Fn(&Ancestor<'a>) -> FinderRet<O>,
|
||||
F: Fn(&'c Ancestor<'a>) -> FinderRet<O>,
|
||||
{
|
||||
for ancestor in self.stack.iter().rev() {
|
||||
match finder(ancestor) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue