Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ module Prism
end

# Returns the first node that matches the given block when visited in a
# depth-first search. This is useful for finding a node that matches a
# breadth-first search. This is useful for finding a node that matches a
# particular condition.
#
# node.breadth_first_search { |node| node.node_id == node_id }
Expand All @@ -215,6 +215,26 @@ module Prism

nil
end
alias find breadth_first_search
Copy link
Member

@eregon eregon Jan 27, 2026

Choose a reason for hiding this comment

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

Same thing here, find might be better/faster for/with DFS, especially for the example shown with node_id.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I thought about that too. But I figured we can cross that bridge when we get to it if we really need to add a DFS variant.


# Returns all of the nodes that match the given block when visited in a
# breadth-first search. This is useful for finding all nodes that match a
Comment on lines +220 to +221
Copy link
Member

Choose a reason for hiding this comment

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

For finding all nodes matching something, maybe a depth-first search would be faster?
I'm unsure if the order matters much in such cases, but maybe?

I'm thinking aliasing to find_all might be suboptimal if there is a DFS variant at some point.

# particular condition.
#
# node.breadth_first_search_all { |node| node.is_a?(Prism::CallNode) }
#
def breadth_first_search_all(&block)
queue = [self] #: Array[Prism::node]
results = [] #: Array[Prism::node]

while (node = queue.shift)
results << node if yield node
queue.concat(node.compact_child_nodes)
end

results
end
alias find_all breadth_first_search_all

# Returns a list of the fields that exist for this node class. Fields
# describe the structure of the node. This kind of reflection is useful for
Expand Down
3 changes: 3 additions & 0 deletions templates/rbi/prism/node.rbi.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class Prism::Node
sig { params(block: T.proc.params(node: Prism::Node).returns(T::Boolean)).returns(T.nilable(Prism::Node)) }
def breadth_first_search(&block); end

sig { params(block: T.proc.params(node: Prism::Node).returns(T::Boolean)).returns(T::Array[Prism::Node]) }
def breadth_first_search_all(&block); end

sig { abstract.params(visitor: Prism::Visitor).returns(T.untyped) }
def accept(visitor); end

Expand Down
3 changes: 3 additions & 0 deletions templates/sig/prism/node.rbs.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ module Prism
def to_dot: () -> String
def tunnel: (Integer line, Integer column) -> Array[Prism::node]
def breadth_first_search: () { (Prism::node) -> bool } -> Prism::node?
alias find breadth_first_search
def breadth_first_search_all: () { (Prism::node) -> bool } -> Array[Prism::node]
alias find_all breadth_first_search_all
def newline!: (Array[untyped]) -> void

def save: (_Repository repository) -> void
Expand Down
11 changes: 11 additions & 0 deletions test/prism/result/breadth_first_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ def test_breadth_first_search
refute_nil found
assert_equal 8, found.start_offset
end

def test_breadth_first_search_all
result = Prism.parse("[1 + 2, 2]")
found_nodes =
result.value.breadth_first_search_all do |node|
node.is_a?(IntegerNode)
end

assert_equal 3, found_nodes.size
assert_equal 8, found_nodes[0].start_offset
end
end
end