class RuboCop::AST::NodePattern::Node

Base class for AST Nodes of a `NodePattern`

Constants

INT_TO_RANGE
MATCHES_WITHIN_SET

Public Instance Methods

arity() click to toggle source

@return [Integer, Range] An Integer for fixed length terms, otherwise a Range. Note: `arity.end` may be `Float::INFINITY`

# File lib/rubocop/ast/node_pattern/node.rb, line 29
def arity
  1
end
arity_range() click to toggle source

@return [Range] arity as a Range

# File lib/rubocop/ast/node_pattern/node.rb, line 69
def arity_range
  a = arity
  a.is_a?(Range) ? a : INT_TO_RANGE[a]
end
capture?() click to toggle source
# File lib/rubocop/ast/node_pattern/node.rb, line 23
def capture?
  false
end
child() click to toggle source

@return [Node] most nodes have only one child

# File lib/rubocop/ast/node_pattern/node.rb, line 48
def child
  children[0]
end
children_nodes() click to toggle source

@return [Array<Node>]

# File lib/rubocop/ast/node_pattern/node.rb, line 43
def children_nodes
  children.grep(Node)
end
in_sequence_head() click to toggle source

@return [Array<Node>, nil] replace node with result, or `nil` if no change requested.

# File lib/rubocop/ast/node_pattern/node.rb, line 34
def in_sequence_head
  nil
end
matches_within_set?() click to toggle source

@return [Boolean] returns true for nodes having a Ruby literal equivalent that matches within a Set (e.g. `42`, `:sym` but not `/regexp/`)

# File lib/rubocop/ast/node_pattern/node.rb, line 64
def matches_within_set?
  MATCHES_WITHIN_SET.include?(type)
end
nb_captures() click to toggle source

@return [Integer] nb of captures of that node and its descendants

# File lib/rubocop/ast/node_pattern/node.rb, line 53
def nb_captures
  children_nodes.sum(&:nb_captures)
end
rest?() click to toggle source

To be overridden by subclasses

# File lib/rubocop/ast/node_pattern/node.rb, line 19
def rest?
  false
end
variadic?() click to toggle source

@return [Boolean] returns whether it matches a variable number of elements

# File lib/rubocop/ast/node_pattern/node.rb, line 58
def variadic?
  arity.is_a?(Range)
end
with(type: @type, children: @children, location: @location) click to toggle source
# File lib/rubocop/ast/node_pattern/node.rb, line 74
def with(type: @type, children: @children, location: @location)
  self.class.new(type, children, { location: location })
end