Skip to content

Conversation

@marlon-costa-dc
Copy link
Contributor

Summary

This PR adds utility methods to the Node struct and Search trait for improved AST traversal capabilities:

  • has_ancestor() - Check if any ancestor node matches a predicate
  • all_occurrences() - Find all descendant nodes matching a predicate (extends Search trait)

Origin

These methods were developed to support more complex AST analysis patterns, particularly for:

  • Checking node context by examining ancestors (e.g., "is this node inside a class?")
  • Collecting all nodes of a certain type for batch processing

What it does

has_ancestor<F: Fn(&Node) -> bool>(&self, pred: F) -> bool

Traverses up the tree from the current node's parent to the root, returning true if any ancestor satisfies the given predicate.

// Example: Check if node is inside a function
let in_function = node.has_ancestor(|n| n.kind_id() == FunctionDeclaration);

all_occurrences(&self, pred: fn(u16) -> bool) -> Vec<Node<'a>>

Starting from the current node, finds all descendant nodes (including self) that match the predicate. Uses depth-first traversal.

// Example: Find all function calls in a subtree
let calls = node.all_occurrences(|id| id == CallExpression);

Implementation decisions

  1. has_ancestor uses a generic closure (F: Fn(&Node) -> bool) instead of fn(u16) -> bool to allow more flexible predicates that can inspect the full node, not just the kind_id.

  2. all_occurrences uses fn(u16) -> bool to match the existing first_occurrence signature in the Search trait, maintaining API consistency.

  3. Both methods are non-recursive using explicit stacks to avoid stack overflow on deeply nested ASTs.

Test plan

  • Code compiles without errors
  • Existing tests pass
  • New methods are ready for use by downstream implementations

🤖 Generated with Claude Code

- Add has_ancestor() for checking if any ancestor matches a predicate
- Add all_occurrences() to Search trait for finding all matching nodes
Copilot AI review requested due to automatic review settings January 20, 2026 16:53
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds two utility methods for AST traversal: has_ancestor() for checking if any ancestor node matches a predicate, and all_occurrences() for finding all descendant nodes matching a predicate. These methods support more complex AST analysis patterns by enabling upward and comprehensive downward tree traversal.

Changes:

  • Added has_ancestor() method to the Node struct using a generic closure for flexible ancestor checking
  • Added all_occurrences() method to the Search trait for collecting all matching descendant nodes
  • Both methods use non-recursive implementations with explicit stacks to avoid stack overflow on deep ASTs

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/traits.rs Added all_occurrences signature to the Search trait interface
src/node.rs Implemented has_ancestor on Node and all_occurrences in the Search trait implementation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

marlon-costa-dc and others added 2 commits January 20, 2026 14:23
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant