Class: Prism::AssocNode

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

Overview

Represents a hash key/value pair.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, operator_loc, location) ⇒ AssocNode

def initialize: (key: Node, value: Node?, operator_loc: Location?, location: Location) -> void



747
748
749
750
751
752
# File 'lib/prism/node.rb', line 747

def initialize(key, value, operator_loc, location)
  @key = key
  @value = value
  @operator_loc = operator_loc
  @location = location
end

Instance Attribute Details

#keyObject (readonly)

attr_reader key: Node



738
739
740
# File 'lib/prism/node.rb', line 738

def key
  @key
end

#operator_locObject (readonly)

attr_reader operator_loc: Location?



744
745
746
# File 'lib/prism/node.rb', line 744

def operator_loc
  @operator_loc
end

#valueObject (readonly)

attr_reader value: Node?



741
742
743
# File 'lib/prism/node.rb', line 741

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



755
756
757
# File 'lib/prism/node.rb', line 755

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

#child_nodesObject Also known as: deconstruct

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



760
761
762
# File 'lib/prism/node.rb', line 760

def child_nodes
  [key, value]
end

#comment_targetsObject

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



773
774
775
# File 'lib/prism/node.rb', line 773

def comment_targets
  [key, *value, *operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



765
766
767
768
769
770
# File 'lib/prism/node.rb', line 765

def compact_child_nodes
  compact = []
  compact << key
  compact << value if value
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> AssocNode



778
779
780
781
782
783
784
785
# File 'lib/prism/node.rb', line 778

def copy(**params)
  AssocNode.new(
    params.fetch(:key) { key },
    params.fetch(:value) { value },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



791
792
793
# File 'lib/prism/node.rb', line 791

def deconstruct_keys(keys)
  { key: key, value: value, operator_loc: operator_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



800
801
802
803
804
805
806
807
808
809
810
811
812
# File 'lib/prism/node.rb', line 800

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── key:\n"
  inspector << inspector.child_node(key, "")
  if (value = self.value).nil?
    inspector << "├── value: ∅\n"
  else
    inspector << "├── value:\n"
    inspector << value.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "└── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector.to_str
end

#operatorObject

def operator: () -> String?



796
797
798
# File 'lib/prism/node.rb', line 796

def operator
  operator_loc&.slice
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



828
829
830
# File 'lib/prism/node.rb', line 828

def type
  :assoc_node
end