Class: Prism::FindPatternNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents a find pattern in pattern matching.

foo in *bar, baz, *qux
       ^^^^^^^^^^^^^^^

foo in [*bar, baz, *qux]
       ^^^^^^^^^^^^^^^^^

foo in Foo(*bar, baz, *qux)
       ^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, constant, left, requireds, right, opening_loc, closing_loc) ⇒ FindPatternNode

Initialize a new FindPatternNode node.



5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
# File 'lib/prism/node.rb', line 5967

def initialize(source, node_id, location, flags, constant, left, requireds, right, opening_loc, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @constant = constant
  @left = left
  @requireds = requireds
  @right = right
  @opening_loc = opening_loc
  @closing_loc = closing_loc
end

Instance Attribute Details

#constantObject (readonly)

attr_reader constant: ConstantReadNode | ConstantPathNode | nil



6019
6020
6021
# File 'lib/prism/node.rb', line 6019

def constant
  @constant
end

#leftObject (readonly)

attr_reader left: SplatNode



6022
6023
6024
# File 'lib/prism/node.rb', line 6022

def left
  @left
end

#requiredsObject (readonly)

attr_reader requireds: Array



6025
6026
6027
# File 'lib/prism/node.rb', line 6025

def requireds
  @requireds
end

#rightObject (readonly)

attr_reader right: SplatNode | MissingNode



6028
6029
6030
# File 'lib/prism/node.rb', line 6028

def right
  @right
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



6077
6078
6079
# File 'lib/prism/node.rb', line 6077

def self.type
  :find_pattern_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
# File 'lib/prism/node.rb', line 6083

def ===(other)
  other.is_a?(FindPatternNode) &&
    (constant === other.constant) &&
    (left === other.left) &&
    (requireds.length == other.requireds.length) &&
    requireds.zip(other.requireds).all? { |left, right| left === right } &&
    (right === other.right) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



5981
5982
5983
# File 'lib/prism/node.rb', line 5981

def accept(visitor)
  visitor.visit_find_pattern_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



5986
5987
5988
# File 'lib/prism/node.rb', line 5986

def child_nodes
  [constant, left, *requireds, right]
end

#closingObject

def closing: () -> String?



6062
6063
6064
# File 'lib/prism/node.rb', line 6062

def closing
  closing_loc&.slice
end

#closing_locObject

attr_reader closing_loc: Location?



6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
# File 'lib/prism/node.rb', line 6044

def closing_loc
  location = @closing_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



6001
6002
6003
# File 'lib/prism/node.rb', line 6001

def comment_targets
  [*constant, left, *requireds, right, *opening_loc, *closing_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5991
5992
5993
5994
5995
5996
5997
5998
# File 'lib/prism/node.rb', line 5991

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << constant if constant
  compact << left
  compact.concat(requireds)
  compact << right
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, left: self.left, requireds: self.requireds, right: self.right, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?constant: ConstantReadNode | ConstantPathNode | nil, ?left: SplatNode, ?requireds: Array, ?right: SplatNode | MissingNode, ?opening_loc: Location?, ?closing_loc: Location?) -> FindPatternNode



6006
6007
6008
# File 'lib/prism/node.rb', line 6006

def copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, left: self.left, requireds: self.requireds, right: self.right, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  FindPatternNode.new(source, node_id, location, flags, constant, left, requireds, right, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, constant: ConstantReadNode | ConstantPathNode | nil, left: SplatNode, requireds: Array, right: SplatNode | MissingNode, opening_loc: Location?, closing_loc: Location? }



6014
6015
6016
# File 'lib/prism/node.rb', line 6014

def deconstruct_keys(keys)
  { node_id: node_id, location: location, constant: constant, left: left, requireds: requireds, right: right, opening_loc: opening_loc, closing_loc: closing_loc }
end

#inspectObject

def inspect -> String



6067
6068
6069
# File 'lib/prism/node.rb', line 6067

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



6057
6058
6059
# File 'lib/prism/node.rb', line 6057

def opening
  opening_loc&.slice
end

#opening_locObject

attr_reader opening_loc: Location?



6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
# File 'lib/prism/node.rb', line 6031

def opening_loc
  location = @opening_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



6072
6073
6074
# File 'lib/prism/node.rb', line 6072

def type
  :find_pattern_node
end