feat(node): Add AST traversal utilities and expose Node API#1224
Open
marlon-costa-dc wants to merge 1 commit intomozilla:masterfrom
Open
feat(node): Add AST traversal utilities and expose Node API#1224marlon-costa-dc wants to merge 1 commit intomozilla:masterfrom
marlon-costa-dc wants to merge 1 commit intomozilla:masterfrom
Conversation
3 tasks
aa07ca1 to
ceb99b0
Compare
ceb99b0 to
167bcda
Compare
Collaborator
|
This CI error should be fixed in another PR, so not blocking for these contributions |
Luni-4
requested changes
Feb 25, 2026
Comment on lines
+441
to
+464
| if let Some(declarator) = node.child_by_field_name("declarator") | ||
| && let Some(fd) = declarator.first_occurrence(|id| { | ||
| Cpp::FunctionDeclarator == id | ||
| || Cpp::FunctionDeclarator2 == id | ||
| || Cpp::FunctionDeclarator3 == id | ||
| }) && let Some(first) = fd.child(0) | ||
| { | ||
| match first.kind_id().into() { | ||
| Cpp::TypeIdentifier | ||
| | Cpp::Identifier | ||
| | Cpp::FieldIdentifier | ||
| | Cpp::DestructorName | ||
| | Cpp::OperatorName | ||
| | Cpp::QualifiedIdentifier | ||
| | Cpp::QualifiedIdentifier2 | ||
| | Cpp::QualifiedIdentifier3 | ||
| | Cpp::QualifiedIdentifier4 | ||
| | Cpp::TemplateFunction | ||
| | Cpp::TemplateMethod => { | ||
| let code = &code[first.start_byte()..first.end_byte()]; | ||
| return std::str::from_utf8(code).ok(); | ||
| } | ||
| _ => {} | ||
| }) | ||
| && let Some(first) = fd.child(0) | ||
| { | ||
| match first.kind_id().into() { | ||
| Cpp::TypeIdentifier | ||
| | Cpp::Identifier | ||
| | Cpp::FieldIdentifier | ||
| | Cpp::DestructorName | ||
| | Cpp::OperatorName | ||
| | Cpp::QualifiedIdentifier | ||
| | Cpp::QualifiedIdentifier2 | ||
| | Cpp::QualifiedIdentifier3 | ||
| | Cpp::QualifiedIdentifier4 | ||
| | Cpp::TemplateFunction | ||
| | Cpp::TemplateMethod => { | ||
| let code = &code[first.start_byte()..first.end_byte()]; | ||
| return std::str::from_utf8(code).ok(); | ||
| } | ||
| _ => {} |
Collaborator
There was a problem hiding this comment.
Can these changes be moved in another PR?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds utility methods for AST traversal to the
Nodestruct andSearchtrait:has_ancestor(pred)— Check if any ancestor node matches a predicate (non-recursive, explicit stack)traverse_children(token_list)— Navigate multi-level child paths for finding deeply nested nodesall_occurrences(pred)— Find all descendant nodes matching a predicate (depth-first, extendsSearchtrait)asttoolsmodule — Reusable AST traversal utilities (exported aspub mod)pubvisibility — ExposeNodemethods (kind_id,parent,start_row, etc.) for downstream crate usageif letwith&&Motivation
These methods support more complex AST analysis patterns needed by downstream crates that use
rust-code-analysisas a dependency:Implementation Notes
has_ancestoruses a generic closure (Fn(&Node) -> bool) for flexible predicatesall_occurrencesusesfn(u16) -> boolto match existingfirst_occurrencesignatureReplaces
Replaces #1213 (closed due to messy branch history). Clean single-commit version.
Test Plan
cargo checkpassescargo test --workspace)