Module: RuboCop::AST::Descendence
Overview
Common functionality for primitive literal nodes: sym
, str
,
int
, float
, …
Instance Method Summary
-
#child_nodes ⇒ Array<Node>
Returns an array of child nodes.
-
#descendants ⇒ Array<Node>
Returns an array of descendant nodes.
-
#each_child_node ⇒ self, Enumerator
Calls the given block for each child node.
-
#each_descendant ⇒ self, Enumerator
Calls the given block for each descendant node with depth first order.
-
#each_node ⇒ self, Enumerator
Calls the given block for the receiver and each descendant node in depth-first order.
Instance Method Details
#child_nodes ⇒ Array
<Node>
Returns an array of child nodes.
This is a shorthand for node.each_child_node.to_a
.
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 38
def child_nodes # Iterate child nodes directly to avoid allocating an Enumerator. nodes = [] each_child_node { |node| nodes << node } nodes end
#descendants ⇒ Array
<Node>
Returns an array of descendant nodes.
This is a shorthand for node.each_descendant.to_a
.
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 72
def descendants each_descendant.to_a end
#each_child_node ⇒ self
, Enumerator
#each_child_node(type, ...) ⇒ self
, Enumerator
self
, Enumerator
#each_child_node(type, ...) ⇒ self
, Enumerator
Calls the given block for each child node.
If no block is given, an Enumerator
is returned.
Note that this is different from node.children.each { |child| … }
which yields all children including non-node elements.
#each_descendant ⇒ self
, Enumerator
#each_descendant(type) ⇒ self
, Enumerator
#each_descendant(type_a, type_b, ...) ⇒ self
, Enumerator
self
, Enumerator
#each_descendant(type) ⇒ self
, Enumerator
#each_descendant(type_a, type_b, ...) ⇒ self
, Enumerator
Calls the given block for each descendant node with depth first order.
If no block is given, an Enumerator
is returned.
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 60
def each_descendant(*types, &block) return to_enum(__method__, *types) unless block visit_descendants(types, &block) self end
#each_node ⇒ self
, Enumerator
#each_node(type) ⇒ self
, Enumerator
#each_node(type_a, type_b, ...) ⇒ self
, Enumerator
self
, Enumerator
#each_node(type) ⇒ self
, Enumerator
#each_node(type_a, type_b, ...) ⇒ self
, Enumerator
Calls the given block for the receiver and each descendant node in
depth-first order.
If no block is given, an Enumerator
is returned.
This method would be useful when you treat the receiver node as the root of a tree and want to iterate over all nodes in the tree.
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 95
def each_node(*types, &block) return to_enum(__method__, *types) unless block yield self if types.empty? || type?(*types) visit_descendants(types, &block) self end