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 }
       ^^^^^^^^^^^^^^^^^^^

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.



8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
# File 'lib/prism/node.rb', line 8301

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)

attr_reader constant: ConstantReadNode | ConstantPathNode | nil



8351
8352
8353
# File 'lib/prism/node.rb', line 8351

def constant
  @constant
end

#elementsObject (readonly)

attr_reader elements: Array



8354
8355
8356
# File 'lib/prism/node.rb', line 8354

def elements
  @elements
end

#restObject (readonly)

attr_reader rest: AssocSplatNode | NoKeywordsParameterNode | nil



8357
8358
8359
# File 'lib/prism/node.rb', line 8357

def rest
  @rest
end

Class Method Details

.typeObject

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



8418
8419
8420
# File 'lib/prism/node.rb', line 8418

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.



8424
8425
8426
8427
8428
8429
8430
8431
8432
# File 'lib/prism/node.rb', line 8424

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



8314
8315
8316
# File 'lib/prism/node.rb', line 8314

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

#child_nodesObject Also known as: deconstruct

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



8319
8320
8321
# File 'lib/prism/node.rb', line 8319

def child_nodes
  [constant, *elements, rest]
end

#closingObject

def closing: () -> String?



8403
8404
8405
# File 'lib/prism/node.rb', line 8403

def closing
  closing_loc&.slice
end

#closing_locObject

attr_reader closing_loc: Location?



8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
# File 'lib/prism/node.rb', line 8379

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]



8333
8334
8335
# File 'lib/prism/node.rb', line 8333

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8324
8325
8326
8327
8328
8329
8330
# File 'lib/prism/node.rb', line 8324

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: ConstantReadNode | ConstantPathNode | nil, ?elements: Array, ?rest: AssocSplatNode | NoKeywordsParameterNode | nil, ?opening_loc: Location?, ?closing_loc: Location?) -> HashPatternNode



8338
8339
8340
# File 'lib/prism/node.rb', line 8338

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: ConstantReadNode | ConstantPathNode | nil, elements: Array, rest: AssocSplatNode | NoKeywordsParameterNode | nil, opening_loc: Location?, closing_loc: Location? }



8346
8347
8348
# File 'lib/prism/node.rb', line 8346

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

#inspectObject

def inspect -> String



8408
8409
8410
# File 'lib/prism/node.rb', line 8408

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



8398
8399
8400
# File 'lib/prism/node.rb', line 8398

def opening
  opening_loc&.slice
end

#opening_locObject

attr_reader opening_loc: Location?



8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
# File 'lib/prism/node.rb', line 8360

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.



8393
8394
8395
# File 'lib/prism/node.rb', line 8393

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.



8374
8375
8376
# File 'lib/prism/node.rb', line 8374

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`.



8413
8414
8415
# File 'lib/prism/node.rb', line 8413

def type
  :hash_pattern_node
end