Class: RuboCop::AST::NodePattern::Node

Inherits:
Parser::AST::Node
  • Object
show all
Extended by:
Forwardable
Includes:
Descendence
Defined in:
lib/rubocop/ast/node_pattern/node.rb

Overview

Base class for AST Nodes of a ‘NodePattern`

Defined Under Namespace

Modules: ForbidInSeqHead Classes: AnyOrder, Capture, Predicate, Repetition, Rest, Sequence, Subsequence, Union

Constant Summary collapse

FunctionCall =
Predicate
MAP =

Registry

Hash.new(Node).merge!(
  sequence: Sequence,
  repetition: Repetition,
  rest: Rest,
  capture: Capture,
  predicate: Predicate,
  any_order: AnyOrder,
  function_call: FunctionCall,
  subsequence: Subsequence,
  union: Union
).freeze

Instance Method Summary collapse

Methods included from Descendence

#child_nodes, #descendants, #each_child_node, #each_descendant, #each_node

Instance Method Details

#arityInteger, Range

Note: ‘arity.end` may be `Float::INFINITY`

Returns:

  • (Integer, Range)

    An Integer for fixed length terms, otherwise a Range.



29
30
31
# File 'lib/rubocop/ast/node_pattern/node.rb', line 29

def arity
  1
end

#arity_rangeRange

Returns arity as a Range.

Returns:

  • (Range)

    arity as a Range



69
70
71
72
# 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?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rubocop/ast/node_pattern/node.rb', line 23

def capture?
  false
end

#childNode

Returns most nodes have only one child.

Returns:

  • (Node)

    most nodes have only one child



48
49
50
# File 'lib/rubocop/ast/node_pattern/node.rb', line 48

def child
  children[0]
end

#children_nodesArray<Node>

Returns:



43
44
45
# File 'lib/rubocop/ast/node_pattern/node.rb', line 43

def children_nodes
  children.grep(Node)
end

#in_sequence_headArray<Node>?

Returns replace node with result, or ‘nil` if no change requested.

Returns:

  • (Array<Node>, nil)

    replace node with result, or ‘nil` if no change requested.



34
35
36
# File 'lib/rubocop/ast/node_pattern/node.rb', line 34

def in_sequence_head
  nil
end

#matches_within_set?Boolean

that matches within a Set (e.g. ‘42`, `:sym` but not `/regexp/`)

Returns:

  • (Boolean)

    returns true for nodes having a Ruby literal equivalent



64
65
66
# File 'lib/rubocop/ast/node_pattern/node.rb', line 64

def matches_within_set?
  MATCHES_WITHIN_SET.include?(type)
end

#nb_capturesInteger

Returns nb of captures of that node and its descendants.

Returns:

  • (Integer)

    nb of captures of that node and its descendants



53
54
55
# File 'lib/rubocop/ast/node_pattern/node.rb', line 53

def nb_captures
  children_nodes.sum(&:nb_captures)
end

#rest?Boolean

To be overridden by subclasses

Returns:

  • (Boolean)


19
20
21
# File 'lib/rubocop/ast/node_pattern/node.rb', line 19

def rest?
  false
end

#source_rangeObject



78
79
80
# File 'lib/rubocop/ast/node_pattern/node.rb', line 78

def source_range
  loc.expression
end

#variadic?Boolean

Returns whether it matches a variable number of elements

Returns:

  • (Boolean)

    returns whether it matches a variable number of elements



58
59
60
# File 'lib/rubocop/ast/node_pattern/node.rb', line 58

def variadic?
  arity.is_a?(Range)
end

#with(type: @type, children: @children, location: @location) ⇒ Object



74
75
76
# 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