Class: Prism::ConstantPathTargetNode

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

Overview

Represents writing to a constant path in a context that doesn’t have an explicit value.

Foo::Foo, Bar::Bar = baz
^^^^^^^^  ^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, child, delimiter_loc, location) ⇒ ConstantPathTargetNode

def initialize: (parent: Node?, child: Node, delimiter_loc: Location, location: Location) -> void



4742
4743
4744
4745
4746
4747
# File 'lib/prism/node.rb', line 4742

def initialize(parent, child, delimiter_loc, location)
  @parent = parent
  @child = child
  @delimiter_loc = delimiter_loc
  @location = location
end

Instance Attribute Details

#childObject (readonly)

attr_reader child: Node



4736
4737
4738
# File 'lib/prism/node.rb', line 4736

def child
  @child
end

#delimiter_locObject (readonly)

attr_reader delimiter_loc: Location



4739
4740
4741
# File 'lib/prism/node.rb', line 4739

def delimiter_loc
  @delimiter_loc
end

#parentObject (readonly)

attr_reader parent: Node?



4733
4734
4735
# File 'lib/prism/node.rb', line 4733

def parent
  @parent
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



4834
4835
4836
# File 'lib/prism/node.rb', line 4834

def self.type
  :constant_path_target_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



4750
4751
4752
# File 'lib/prism/node.rb', line 4750

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

#child_nodesObject Also known as: deconstruct

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



4755
4756
4757
# File 'lib/prism/node.rb', line 4755

def child_nodes
  [parent, child]
end

#comment_targetsObject

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



4768
4769
4770
# File 'lib/prism/node.rb', line 4768

def comment_targets
  [*parent, child, delimiter_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



4760
4761
4762
4763
4764
4765
# File 'lib/prism/node.rb', line 4760

def compact_child_nodes
  compact = []
  compact << parent if parent
  compact << child
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> ConstantPathTargetNode



4773
4774
4775
4776
4777
4778
4779
4780
# File 'lib/prism/node.rb', line 4773

def copy(**params)
  ConstantPathTargetNode.new(
    params.fetch(:parent) { parent },
    params.fetch(:child) { child },
    params.fetch(:delimiter_loc) { delimiter_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



4786
4787
4788
# File 'lib/prism/node.rb', line 4786

def deconstruct_keys(keys)
  { parent: parent, child: child, delimiter_loc: delimiter_loc, location: location }
end

#delimiterObject

def delimiter: () -> String



4791
4792
4793
# File 'lib/prism/node.rb', line 4791

def delimiter
  delimiter_loc.slice
end

#full_nameObject

Returns the full name of this constant path. For example: “Foo::Bar”



142
143
144
# File 'lib/prism/node_ext.rb', line 142

def full_name
  full_name_parts.join("::")
end

#full_name_partsObject

Returns the list of parts for the full name of this constant path. For example: [:Foo, :Bar]



137
138
139
# File 'lib/prism/node_ext.rb', line 137

def full_name_parts
  (parent&.full_name_parts || [:""]).push(child.name)
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
# File 'lib/prism/node.rb', line 4796

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (parent = self.parent).nil?
    inspector << "├── parent: ∅\n"
  else
    inspector << "├── parent:\n"
    inspector << parent.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── child:\n"
  inspector << inspector.child_node(child, "")
  inspector << "└── delimiter_loc: #{inspector.location(delimiter_loc)}\n"
  inspector.to_str
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



4824
4825
4826
# File 'lib/prism/node.rb', line 4824

def type
  :constant_path_target_node
end