[ruby/prism] Add Node#breadth_first_search
https://github.com/ruby/prism/commit/1ffb141199
This commit is contained in:
parent
aca42a2478
commit
32090e2b8d
@ -116,6 +116,23 @@ module Prism
|
|||||||
result
|
result
|
||||||
end
|
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
|
||||||
|
# particular condition.
|
||||||
|
#
|
||||||
|
# node.breadth_first_search { |node| node.node_id == node_id }
|
||||||
|
#
|
||||||
|
def breadth_first_search(&block)
|
||||||
|
queue = [self] #: Array[Prism::node]
|
||||||
|
|
||||||
|
while (node = queue.shift)
|
||||||
|
return node if yield node
|
||||||
|
queue.concat(node.compact_child_nodes)
|
||||||
|
end
|
||||||
|
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
# Returns a list of the fields that exist for this node class. Fields
|
# 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
|
# describe the structure of the node. This kind of reflection is useful for
|
||||||
# things like recursively visiting each node _and_ field in the tree.
|
# things like recursively visiting each node _and_ field in the tree.
|
||||||
|
18
test/prism/result/breadth_first_search_test.rb
Normal file
18
test/prism/result/breadth_first_search_test.rb
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "../test_helper"
|
||||||
|
|
||||||
|
module Prism
|
||||||
|
class BreadthFirstSearchTest < TestCase
|
||||||
|
def test_breadth_first_search
|
||||||
|
result = Prism.parse("[1 + 2, 2]")
|
||||||
|
found =
|
||||||
|
result.value.breadth_first_search do |node|
|
||||||
|
node.is_a?(IntegerNode) && node.value == 2
|
||||||
|
end
|
||||||
|
|
||||||
|
refute_nil found
|
||||||
|
assert_equal 8, found.start_offset
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user