Class: Prism::HashNode

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

Overview

Represents a hash literal.

{ a => b }
^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, opening_loc, elements, closing_loc) ⇒ HashNode

Initialize a new HashNode node.



8762
8763
8764
8765
8766
8767
8768
8769
8770
# File 'lib/prism/node.rb', line 8762

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

Instance Attribute Details

#elementsObject (readonly)

The elements of the hash. These can be either ‘AssocNode`s or `AssocSplatNode`s.

{ a: b }
  ^^^^

{ **foo }
  ^^^^^


8835
8836
8837
# File 'lib/prism/node.rb', line 8835

def elements
  @elements
end

Class Method Details

.typeObject

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



8874
8875
8876
# File 'lib/prism/node.rb', line 8874

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



8880
8881
8882
8883
8884
8885
8886
# File 'lib/prism/node.rb', line 8880

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

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



8773
8774
8775
# File 'lib/prism/node.rb', line 8773

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



8778
8779
8780
# File 'lib/prism/node.rb', line 8778

def child_nodes
  [*elements]
end

#closingObject

def closing: () -> String



8859
8860
8861
# File 'lib/prism/node.rb', line 8859

def closing
  closing_loc.slice
end

#closing_locObject

The location of the closing brace.

{ a => b }
         ^


8841
8842
8843
8844
8845
# File 'lib/prism/node.rb', line 8841

def closing_loc
  location = @closing_loc
  return location if location.is_a?(Location)
  @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#comment_targetsObject

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



8795
8796
8797
# File 'lib/prism/node.rb', line 8795

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8790
8791
8792
# File 'lib/prism/node.rb', line 8790

def compact_child_nodes
  [*elements]
end

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

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?opening_loc: Location, ?elements: Array[AssocNode | AssocSplatNode], ?closing_loc: Location) -> HashNode



8800
8801
8802
# File 'lib/prism/node.rb', line 8800

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

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, opening_loc: Location, elements: Array[AssocNode | AssocSplatNode], closing_loc: Location }



8808
8809
8810
# File 'lib/prism/node.rb', line 8808

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

#each_child_nodeObject

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



8783
8784
8785
8786
8787
# File 'lib/prism/node.rb', line 8783

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  elements.each { |node| yield node }
end

#inspectObject

def inspect -> String



8864
8865
8866
# File 'lib/prism/node.rb', line 8864

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String



8854
8855
8856
# File 'lib/prism/node.rb', line 8854

def opening
  opening_loc.slice
end

#opening_locObject

The location of the opening brace.

{ a => b }
^


8816
8817
8818
8819
8820
# File 'lib/prism/node.rb', line 8816

def opening_loc
  location = @opening_loc
  return location if location.is_a?(Location)
  @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_closing_loc(repository) ⇒ Object

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



8849
8850
8851
# File 'lib/prism/node.rb', line 8849

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc)
end

#save_opening_loc(repository) ⇒ Object

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



8824
8825
8826
# File 'lib/prism/node.rb', line 8824

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc)
end

#typeObject

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



8869
8870
8871
# File 'lib/prism/node.rb', line 8869

def type
  :hash_node
end