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

Inherits:
Parser::AST::Node
  • Object
show all
Extended by:
SimpleForwardable
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 SimpleForwardable

def_delegators

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.



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

def arity
  1
end

#arity_rangeRange

Returns arity as a Range.

Returns:

  • (Range)

    arity as a Range



68
69
70
71
# File 'lib/rubocop/ast/node_pattern/node.rb', line 68

def arity_range
  a = arity
  a.is_a?(Range) ? a : INT_TO_RANGE[a]
end

#capture?Boolean

Returns:

  • (Boolean)


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

def capture?
  false
end

#childNode

Returns most nodes have only one child.

Returns:

  • (Node)

    most nodes have only one child



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

def child
  children[0]
end

#children_nodesArray<Node>

Returns:



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

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.



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

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



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

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



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

def nb_captures
  children_nodes.sum(&:nb_captures)
end

#rest?Boolean

To be overridden by subclasses

Returns:

  • (Boolean)


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

def rest?
  false
end

#source_rangeObject



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

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



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

def variadic?
  arity.is_a?(Range)
end

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



73
74
75
# File 'lib/rubocop/ast/node_pattern/node.rb', line 73

def with(type: @type, children: @children, location: @location)
  self.class.new(type, children, { location: location })
end