Class: RuboCop::AST::NodePattern::Node
- Inherits:
-
Parser::AST::Node
- Object
- Parser::AST::Node
- RuboCop::AST::NodePattern::Node
- Extended by:
- SimpleForwardable
- Includes:
- Descendence
- Defined in:
- lib/rubocop/ast/node_pattern/node.rb
Overview
Base class for AST Nodes of a ‘NodePattern`
Direct Known Subclasses
AnyOrder, Capture, Predicate, Repetition, Rest, Sequence, Subsequence, Union
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
-
#arity ⇒ Integer, Range
Note: ‘arity.end` may be `Float::INFINITY`.
-
#arity_range ⇒ Range
Arity as a Range.
- #capture? ⇒ Boolean
-
#child ⇒ Node
Most nodes have only one child.
- #children_nodes ⇒ Array<Node>
-
#in_sequence_head ⇒ Array<Node>?
Replace node with result, or ‘nil` if no change requested.
-
#matches_within_set? ⇒ Boolean
that matches within a Set (e.g. ‘42`, `:sym` but not `/regexp/`).
-
#nb_captures ⇒ Integer
Nb of captures of that node and its descendants.
-
#rest? ⇒ Boolean
To be overridden by subclasses.
- #source_range ⇒ Object
-
#variadic? ⇒ Boolean
Returns whether it matches a variable number of elements.
- #with(type: @type, children: @children, location: @location) ⇒ Object
Methods included from SimpleForwardable
Methods included from Descendence
#child_nodes, #descendants, #each_child_node, #each_descendant, #each_node
Instance Method Details
#arity ⇒ Integer, Range
Note: ‘arity.end` may be `Float::INFINITY`
28 29 30 |
# File 'lib/rubocop/ast/node_pattern/node.rb', line 28 def arity 1 end |
#arity_range ⇒ Range
Returns 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
22 23 24 |
# File 'lib/rubocop/ast/node_pattern/node.rb', line 22 def capture? false end |
#child ⇒ Node
Returns 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_nodes ⇒ Array<Node>
42 43 44 |
# File 'lib/rubocop/ast/node_pattern/node.rb', line 42 def children_nodes children.grep(Node) end |
#in_sequence_head ⇒ Array<Node>?
Returns 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/`)
63 64 65 |
# File 'lib/rubocop/ast/node_pattern/node.rb', line 63 def matches_within_set? MATCHES_WITHIN_SET.include?(type) end |
#nb_captures ⇒ Integer
Returns 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
18 19 20 |
# File 'lib/rubocop/ast/node_pattern/node.rb', line 18 def rest? false end |
#source_range ⇒ Object
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
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 |