@@ -187,7 +187,7 @@ module Prism
187187 while (node = queue.shift)
188188 result < < node
189189
190- node.compact_child_nodes.each do |child_node|
190+ node.each_child_node do |child_node|
191191 child_location = child_node.location
192192
193193 start_line = child_location.start_line
@@ -259,6 +259,13 @@ module Prism
259259
260260 alias deconstruct child_nodes
261261
262+ # With a block given, yields each child node. Without a block, returns
263+ # an enumerator that contains each child node. Excludes any `nil`s in
264+ # the place of optional nodes that were not present.
265+ def each_child_node
266+ raise NoMethodError, "undefined method `each_child_node' for #{inspect}"
267+ end
268+
262269 # Returns an array of child nodes, excluding any `nil`s in the place of
263270 # optional nodes that were not present.
264271 def compact_child_nodes
@@ -335,6 +342,22 @@ module Prism
335342 } . compact . join ( ", " ) %> ]
336343 end
337344
345+ # def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator[Prism::node]
346+ def each_child_node
347+ return to_enum(:each_child_node) unless block_given?
348+
349+ <%- node . fields . each do |field | -%>
350+ <%- case field -%>
351+ <%- when Prism ::Template ::NodeField -%>
352+ yield <%= field . name %>
353+ <%- when Prism ::Template ::OptionalNodeField -%>
354+ yield <%= field . name %> if <%= field . name %>
355+ <%- when Prism ::Template ::NodeListField -%>
356+ <%= field . name %> .each {|node| yield node }
357+ <%- end -%>
358+ <%- end -%>
359+ end
360+
338361 # def compact_child_nodes: () -> Array[Node]
339362 def compact_child_nodes
340363 <%- if node . fields . any? { |field | field . is_a? ( Prism ::Template ::OptionalNodeField ) } -%>
0 commit comments