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.



7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
# File 'lib/prism/node.rb', line 7348

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



7398
7399
7400
# File 'lib/prism/node.rb', line 7398

def constant
  @constant
end

#elementsObject (readonly)

attr_reader elements: Array



7401
7402
7403
# File 'lib/prism/node.rb', line 7401

def elements
  @elements
end

#restObject (readonly)

attr_reader rest: AssocSplatNode | NoKeywordsParameterNode | nil



7404
7405
7406
# File 'lib/prism/node.rb', line 7404

def rest
  @rest
end

Class Method Details

.typeObject

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



7453
7454
7455
# File 'lib/prism/node.rb', line 7453

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.



7459
7460
7461
7462
7463
7464
7465
7466
7467
# File 'lib/prism/node.rb', line 7459

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



7361
7362
7363
# File 'lib/prism/node.rb', line 7361

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

#child_nodesObject Also known as: deconstruct

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



7366
7367
7368
# File 'lib/prism/node.rb', line 7366

def child_nodes
  [constant, *elements, rest]
end

#closingObject

def closing: () -> String?



7438
7439
7440
# File 'lib/prism/node.rb', line 7438

def closing
  closing_loc&.slice
end

#closing_locObject

attr_reader closing_loc: Location?



7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
# File 'lib/prism/node.rb', line 7420

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]



7380
7381
7382
# File 'lib/prism/node.rb', line 7380

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



7371
7372
7373
7374
7375
7376
7377
# File 'lib/prism/node.rb', line 7371

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



7385
7386
7387
# File 'lib/prism/node.rb', line 7385

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



7393
7394
7395
# File 'lib/prism/node.rb', line 7393

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



7443
7444
7445
# File 'lib/prism/node.rb', line 7443

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



7433
7434
7435
# File 'lib/prism/node.rb', line 7433

def opening
  opening_loc&.slice
end

#opening_locObject

attr_reader opening_loc: Location?



7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
# File 'lib/prism/node.rb', line 7407

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

#typeObject

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



7448
7449
7450
# File 'lib/prism/node.rb', line 7448

def type
  :hash_pattern_node
end