Module: RuboCop::AST::Descendence
- Included in:
- Node, NodePattern::Node
- Defined in:
- lib/rubocop/ast/node/mixin/descendence.rb
Overview
Common functionality for primitive literal nodes: ‘sym`, `str`, `int`, `float`, …
Instance Method Summary collapse
-
#child_nodes ⇒ Array<Node>
Returns an array of child nodes.
-
#descendants ⇒ Array<Node>
Returns an array of descendant nodes.
-
#each_child_node(*types) {|node| ... } ⇒ self, Enumerator
Calls the given block for each child node.
-
#each_descendant(*types) {|node| ... } ⇒ self, Enumerator
Calls the given block for each descendant node with depth first order.
-
#each_node(*types) {|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`.
38 39 40 |
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 38 def child_nodes each_child_node.to_a end |
#descendants ⇒ Array<Node>
Returns an array of descendant nodes. This is a shorthand for ‘node.each_descendant.to_a`.
69 70 71 |
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 69 def descendants each_descendant.to_a end |
#each_child_node ⇒ 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.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 22 def each_child_node(*types) return to_enum(__method__, *types) unless block_given? children.each do |child| next unless child.is_a?(::AST::Node) yield child if types.empty? || types.include?(child.type) end self end |
#each_descendant ⇒ 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.
57 58 59 60 61 62 63 |
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 57 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
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.
92 93 94 95 96 97 98 99 100 |
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 92 def each_node(*types, &block) return to_enum(__method__, *types) unless block yield self if types.empty? || types.include?(type) visit_descendants(types, &block) self end |