Class: Prism::HashPatternNode

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

Overview

Represents a hash pattern in pattern matching.

foo => { a: 1, b: 2 }
       ^^^^^^^^^^^^^^

foo => { a: 1, b: 2, **c }
       ^^^^^^^^^^^^^^^^^^^

foo => Bar[a: 1, b: 2]
       ^^^^^^^^^^^^^^^

foo in { a: 1, b: 2 }
       ^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, constant, elements, rest, opening_loc, closing_loc) ⇒ HashPatternNode

Initialize a new HashPatternNode node.



8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
# File 'lib/prism/node.rb', line 8904

def initialize(source, node_id, location, flags, constant, elements, rest, opening_loc, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @constant = constant
  @elements = elements
  @rest = rest
  @opening_loc = opening_loc
  @closing_loc = closing_loc
end

Instance Attribute Details

#constantObject (readonly)

Represents the optional constant preceding the Hash.

foo => Bar[a: 1, b: 2]
     ^^^

foo => Bar::Baz[a: 1, b: 2]
     ^^^^^^^^


8969
8970
8971
# File 'lib/prism/node.rb', line 8969

def constant
  @constant
end

#elementsObject (readonly)

Represents the explicit named hash keys and values.

foo => { a: 1, b:, ** }
         ^^^^^^^^


8975
8976
8977
# File 'lib/prism/node.rb', line 8975

def elements
  @elements
end

#restObject (readonly)

Represents the rest of the Hash keys and values. This can be named, unnamed, or explicitly forbidden via ‘**nil`, this last one results in a NoKeywordsParameterNode.

foo => { a: 1, b:, **c }
                   ^^^

foo => { a: 1, b:, ** }
                   ^^

foo => { a: 1, b:, **nil }
                   ^^^^^


8987
8988
8989
# File 'lib/prism/node.rb', line 8987

def rest
  @rest
end

Class Method Details

.typeObject

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



9060
9061
9062
# File 'lib/prism/node.rb', line 9060

def self.type
  :hash_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.



9066
9067
9068
9069
9070
9071
9072
9073
9074
# File 'lib/prism/node.rb', line 9066

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

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



8917
8918
8919
# File 'lib/prism/node.rb', line 8917

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



8922
8923
8924
# File 'lib/prism/node.rb', line 8922

def child_nodes
  [constant, *elements, rest]
end

#closingObject

def closing: () -> String?



9045
9046
9047
# File 'lib/prism/node.rb', line 9045

def closing
  closing_loc&.slice
end

#closing_locObject

The location of the closing brace.

foo => { a: 1 }
              ^

foo => Bar[a: 1]
               ^


9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
# File 'lib/prism/node.rb', line 9021

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]



8945
8946
8947
# File 'lib/prism/node.rb', line 8945

def comment_targets
  [*constant, *elements, *rest, *opening_loc, *closing_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8936
8937
8938
8939
8940
8941
8942
# File 'lib/prism/node.rb', line 8936

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << constant if constant
  compact.concat(elements)
  compact << rest if rest
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, elements: self.elements, rest: self.rest, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?constant: ConstantPathNode | ConstantReadNode | nil, ?elements: Array, ?rest: AssocSplatNode | NoKeywordsParameterNode | nil, ?opening_loc: Location?, ?closing_loc: Location?) -> HashPatternNode



8950
8951
8952
# File 'lib/prism/node.rb', line 8950

def copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, elements: self.elements, rest: self.rest, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  HashPatternNode.new(source, node_id, location, flags, constant, elements, rest, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, constant: ConstantPathNode | ConstantReadNode | nil, elements: Array, rest: AssocSplatNode | NoKeywordsParameterNode | nil, opening_loc: Location?, closing_loc: Location? }



8958
8959
8960
# File 'lib/prism/node.rb', line 8958

def deconstruct_keys(keys)
  { node_id: node_id, location: location, constant: constant, elements: elements, rest: rest, opening_loc: opening_loc, closing_loc: closing_loc }
end

#each_child_node {|constant| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



8927
8928
8929
8930
8931
8932
8933
# File 'lib/prism/node.rb', line 8927

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield constant if constant
  elements.each { |node| yield node }
  yield rest if rest
end

#inspectObject

def inspect -> String



9050
9051
9052
# File 'lib/prism/node.rb', line 9050

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



9040
9041
9042
# File 'lib/prism/node.rb', line 9040

def opening
  opening_loc&.slice
end

#opening_locObject

The location of the opening brace.

foo => { a: 1 }
       ^

foo => Bar[a: 1]
          ^


8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
# File 'lib/prism/node.rb', line 8996

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

#save_closing_loc(repository) ⇒ Object

Save the closing_loc location using the given saved source so that it can be retrieved later.



9035
9036
9037
# File 'lib/prism/node.rb', line 9035

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
end

#save_opening_loc(repository) ⇒ Object

Save the opening_loc location using the given saved source so that it can be retrieved later.



9010
9011
9012
# File 'lib/prism/node.rb', line 9010

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc) unless @opening_loc.nil?
end

#typeObject

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



9055
9056
9057
# File 'lib/prism/node.rb', line 9055

def type
  :hash_pattern_node
end