module RuboCop::AST::Descendence

Common functionality for primitive literal nodes: `sym`, `str`, `int`, `float`, …

Public Instance Methods

child_nodes() click to toggle source

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

@return [Array<Node>] an array of child nodes

# File lib/rubocop/ast/node/mixin/descendence.rb, line 38
def child_nodes
  each_child_node.to_a
end
descendants() click to toggle source

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

@return [Array<Node>] an array of descendant nodes

# File lib/rubocop/ast/node/mixin/descendence.rb, line 69
def descendants
  each_descendant.to_a
end
each_child_node(*types) { |child| ... } click to toggle source

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.

@overload each_child_node

Yield all nodes.

@overload each_child_node(type, …)

Yield only nodes matching any of the types.
@param [Symbol] type a node type

@yieldparam [Node] node each child node @return [self] if a block is given @return [Enumerator] if no block is given

# 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(*types, &block) click to toggle source

Calls the given block for each descendant node with depth first order. If no block is given, an `Enumerator` is returned.

@overload each_descendant

Yield all nodes.

@overload each_descendant(type)

Yield only nodes matching the type.
@param [Symbol] type a node type

@overload each_descendant(type_a, type_b, …)

Yield only nodes matching any of the types.
@param [Symbol] type_a a node type
@param [Symbol] type_b a node type

@yieldparam [Node] node each descendant node @return [self] if a block is given @return [Enumerator] if no block is given

# 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(*types) { |self| ... } click to toggle source

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.

@overload each_node

Yield all nodes.

@overload each_node(type)

Yield only nodes matching the type.
@param [Symbol] type a node type

@overload each_node(type_a, type_b, …)

Yield only nodes matching any of the types.
@param [Symbol] type_a a node type
@param [Symbol] type_b a node type

@yieldparam [Node] node each node @return [self] if a block is given @return [Enumerator] if no block is given

# 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

Protected Instance Methods

visit_descendants(types) { |child| ... } click to toggle source
# File lib/rubocop/ast/node/mixin/descendence.rb, line 104
def visit_descendants(types, &block)
  each_child_node do |child|
    yield child if types.empty? || types.include?(child.type)
    child.visit_descendants(types, &block)
  end
end