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

Class Method 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



855
856
857
858
859
860
# File 'lib/prism/node.rb', line 855

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



846
847
848
# File 'lib/prism/node.rb', line 846

def key
  @key
end

#operator_locObject (readonly)

attr_reader operator_loc: Location?



852
853
854
# File 'lib/prism/node.rb', line 852

def operator_loc
  @operator_loc
end

#valueObject (readonly)

attr_reader value: Node?



849
850
851
# File 'lib/prism/node.rb', line 849

def value
  @value
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



947
948
949
# File 'lib/prism/node.rb', line 947

def self.type
  :assoc_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



863
864
865
# File 'lib/prism/node.rb', line 863

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

#child_nodesObject Also known as: deconstruct

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



868
869
870
# File 'lib/prism/node.rb', line 868

def child_nodes
  [key, value]
end

#comment_targetsObject

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



881
882
883
# File 'lib/prism/node.rb', line 881

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



873
874
875
876
877
878
# File 'lib/prism/node.rb', line 873

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

#copy(**params) ⇒ Object

def copy: (**params) -> AssocNode



886
887
888
889
890
891
892
893
# File 'lib/prism/node.rb', line 886

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]



899
900
901
# File 'lib/prism/node.rb', line 899

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

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'lib/prism/node.rb', line 909

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?



904
905
906
# File 'lib/prism/node.rb', line 904

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



937
938
939
# File 'lib/prism/node.rb', line 937

def type
  :assoc_node
end