Class: Prism::HashNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/parse_result/newlines.rbs,
ext/prism/api_node.c

Overview

Represents a hash literal.

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

Explore code sample

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.

: (Source source, Integer node_id, Location location, Integer flags, Location opening_loc, Array[AssocNode | AssocSplatNode] elements, Location closing_loc) -> void

Parameters:



15080
15081
15082
15083
15084
15085
15086
15087
15088
# File 'lib/prism/node.rb', line 15080

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

Class Method Details

.type ⇒ :hash_node

See Node.type.

: () -> :hash_node

Returns:

  • (:hash_node)


15171
15172
15173
# File 'lib/prism/node.rb', line 15171

def self.type
  :hash_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


15285
15286
15287
15288
15289
15290
15291
# File 'lib/prism/node.rb', line 15285

def ===(other) # :nodoc:
  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

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


15104
15105
15106
# File 'lib/prism/node.rb', line 15104

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


15111
15112
15113
# File 'lib/prism/node.rb', line 15111

def child_nodes
  [*elements]
end

#closing ⇒ String

:call-seq:

closing -> String

Slice the location of closing_loc from the source.

: () -> String

Returns:

  • (String)


15278
15279
15280
# File 'lib/prism/node.rb', line 15278

def closing
  closing_loc.slice
end

#closing_loc ⇒ Location

:category: Locations :call-seq:

closing_loc -> Location

The Location of the closing brace.

{ a => b }
         ^

Explore code sample

: () -> Location

Returns:



15245
15246
15247
15248
15249
# File 'lib/prism/node.rb', line 15245

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

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


15128
15129
15130
# File 'lib/prism/node.rb', line 15128

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) ⇒ HashNode

:call-seq:

copy(**fields) -> HashNode

Creates a copy of self with the given fields, using self as the template.

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

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • opening_loc: (Location) (defaults to: self.opening_loc)
  • elements: (Array[AssocNode | AssocSplatNode]) (defaults to: self.elements)
  • closing_loc: (Location) (defaults to: self.closing_loc)

Returns:



15138
15139
15140
# File 'lib/prism/node.rb', line 15138

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) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
# File 'lib/prism/node.rb', line 15145

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location opening_loc elements closing_loc opening closing]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :elements                then deconstructed[:elements] = self.elements
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :opening                 then deconstructed[:opening] = self.opening
    when :closing                 then deconstructed[:closing] = self.closing
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


15119
15120
15121
15122
15123
# File 'lib/prism/node.rb', line 15119

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  elements.each { |node| yield node }
end

#elements ⇒ Array[AssocNode | AssocSplatNode]

:call-seq:

elements -> Array[AssocNode | AssocSplatNode]

The elements of the hash. These can be either AssocNodes or AssocSplatNodes.

{ a: b }
  ^^^^

{ **foo }
  ^^^^^

Explore code sample

: () -> Array[AssocNode | AssocSplatNode]

Returns:



15227
15228
15229
# File 'lib/prism/node.rb', line 15227

def elements
  @elements
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


15176
15177
15178
# File 'lib/prism/node.rb', line 15176

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


370
371
372
373
374
375
376
377
# File 'lib/prism/parse_result/newlines.rb', line 370

def newline_flag!(lines) # :nodoc:
  first = elements.first
  if first && !static_literal?
    first.newline_flag!(lines)
  else
    super
  end
end

#opening ⇒ String

:call-seq:

opening -> String

Slice the location of opening_loc from the source.

: () -> String

Returns:

  • (String)


15268
15269
15270
# File 'lib/prism/node.rb', line 15268

def opening
  opening_loc.slice
end

#opening_loc ⇒ Location

:category: Locations :call-seq:

opening_loc -> Location

The Location of the opening brace.

{ a => b }
^

Explore code sample

: () -> Location

Returns:



15196
15197
15198
15199
15200
# File 'lib/prism/node.rb', line 15196

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) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



15256
15257
15258
# File 'lib/prism/node.rb', line 15256

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

#save_opening_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



15207
15208
15209
# File 'lib/prism/node.rb', line 15207

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

#type ⇒ :hash_node

See Node#type.

: () -> :hash_node

Returns:

  • (:hash_node)


15164
15165
15166
# File 'lib/prism/node.rb', line 15164

def type
  :hash_node
end