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

Instance Method Details

#child_nodesArray<Node>

Returns an array of child nodes. This is a shorthand for ‘node.each_child_node.to_a`.

Returns:

  • (Array<Node>)

    an array of child nodes



38
39
40
41
42
43
# 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

#descendantsArray<Node>

Returns an array of descendant nodes. This is a shorthand for ‘node.each_descendant.to_a`.

Returns:

  • (Array<Node>)

    an array of descendant nodes



72
73
74
# File 'lib/rubocop/ast/node/mixin/descendence.rb', line 72

def descendants
  each_descendant.to_a
end

#each_child_nodeself, 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.

Overloads:

  • #each_child_nodeself, Enumerator

    Yield all nodes.

  • #each_child_node(type, ...) ⇒ self, Enumerator

    Yield only nodes matching any of the types.

    Parameters:

    • type (Symbol)

      a node type

Yield Parameters:

  • node (Node)

    each child node

Returns:

  • (self)

    if a block is given

  • (Enumerator)

    if no block is given



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_descendantself, 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.

Overloads:

  • #each_descendantself, Enumerator

    Yield all nodes.

  • #each_descendant(type) ⇒ self, Enumerator

    Yield only nodes matching the type.

    Parameters:

    • type (Symbol)

      a node type

  • #each_descendant(type_a, type_b, ...) ⇒ self, Enumerator

    Yield only nodes matching any of the types.

    Parameters:

    • type_a (Symbol)

      a node type

    • type_b (Symbol)

      a node type

Yield Parameters:

  • node (Node)

    each descendant node

Returns:

  • (self)

    if a block is given

  • (Enumerator)

    if no block is given



60
61
62
63
64
65
66
# 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_nodeself, 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.

Overloads:

  • #each_nodeself, Enumerator

    Yield all nodes.

  • #each_node(type) ⇒ self, Enumerator

    Yield only nodes matching the type.

    Parameters:

    • type (Symbol)

      a node type

  • #each_node(type_a, type_b, ...) ⇒ self, Enumerator

    Yield only nodes matching any of the types.

    Parameters:

    • type_a (Symbol)

      a node type

    • type_b (Symbol)

      a node type

Yield Parameters:

  • node (Node)

    each node

Returns:

  • (self)

    if a block is given

  • (Enumerator)

    if no block is given



95
96
97
98
99
100
101
102
103
# 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? || types.include?(type)

  visit_descendants(types, &block)

  self
end