File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -116,6 +116,23 @@ module Prism
116
116
result
117
117
end
118
118
119
+ # Returns the first node that matches the given block when visited in a
120
+ # depth-first search. This is useful for finding a node that matches a
121
+ # particular condition.
122
+ #
123
+ # node.breadth_first_search { |node| node.node_id == node_id }
124
+ #
125
+ def breadth_first_search(&block)
126
+ queue = [self] #: Array[Prism::node]
127
+
128
+ while (node = queue.shift)
129
+ return node if yield node
130
+ queue.concat(node.compact_child_nodes)
131
+ end
132
+
133
+ nil
134
+ end
135
+
119
136
# Returns a list of the fields that exist for this node class. Fields
120
137
# describe the structure of the node. This kind of reflection is useful for
121
138
# things like recursively visiting each node _and_ field in the tree.
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../test_helper"
4
+
5
+ module Prism
6
+ class BreadthFirstSearchTest < TestCase
7
+ def test_breadth_first_search
8
+ result = Prism . parse ( "[1 + 2, 2]" )
9
+ found =
10
+ result . value . breadth_first_search do |node |
11
+ node . is_a? ( IntegerNode ) && node . value == 2
12
+ end
13
+
14
+ refute_nil found
15
+ assert_equal 8 , found . start_offset
16
+ end
17
+ end
18
+ end
You can’t perform that action at this time.
0 commit comments