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.



8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
# File 'lib/prism/node.rb', line 8373

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


8429
8430
8431
# File 'lib/prism/node.rb', line 8429

def constant
  @constant
end

#elementsObject (readonly)

Represents the explicit named hash keys and values.

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


8435
8436
8437
# File 'lib/prism/node.rb', line 8435

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


8447
8448
8449
# File 'lib/prism/node.rb', line 8447

def rest
  @rest
end

Class Method Details

.typeObject

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



8520
8521
8522
# File 'lib/prism/node.rb', line 8520

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.



8526
8527
8528
8529
8530
8531
8532
8533
8534
# File 'lib/prism/node.rb', line 8526

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



8386
8387
8388
# File 'lib/prism/node.rb', line 8386

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



8391
8392
8393
# File 'lib/prism/node.rb', line 8391

def child_nodes
  [constant, *elements, rest]
end

#closingObject

def closing: () -> String?



8505
8506
8507
# File 'lib/prism/node.rb', line 8505

def closing
  closing_loc&.slice
end

#closing_locObject

The location of the closing brace.

foo => { a: 1 }
              ^

foo => Bar[a: 1]
               ^


8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
# File 'lib/prism/node.rb', line 8481

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]



8405
8406
8407
# File 'lib/prism/node.rb', line 8405

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8396
8397
8398
8399
8400
8401
8402
# File 'lib/prism/node.rb', line 8396

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



8410
8411
8412
# File 'lib/prism/node.rb', line 8410

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



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

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



8510
8511
8512
# File 'lib/prism/node.rb', line 8510

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



8500
8501
8502
# File 'lib/prism/node.rb', line 8500

def opening
  opening_loc&.slice
end

#opening_locObject

The location of the opening brace.

foo => { a: 1 }
       ^

foo => Bar[a: 1]
          ^


8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
# File 'lib/prism/node.rb', line 8456

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.



8495
8496
8497
# File 'lib/prism/node.rb', line 8495

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.



8470
8471
8472
# File 'lib/prism/node.rb', line 8470

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



8515
8516
8517
# File 'lib/prism/node.rb', line 8515

def type
  :hash_pattern_node
end