Class: Prism::ConstantPathNode

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

Overview

Represents accessing a constant through a path of ‘::` operators.

Foo::Bar
^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



3799
3800
3801
3802
3803
3804
# File 'lib/prism/node.rb', line 3799

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



3793
3794
3795
# File 'lib/prism/node.rb', line 3793

def child
  @child
end

#delimiter_locObject (readonly)

attr_reader delimiter_loc: Location



3796
3797
3798
# File 'lib/prism/node.rb', line 3796

def delimiter_loc
  @delimiter_loc
end

#parentObject (readonly)

attr_reader parent: Node?



3790
3791
3792
# File 'lib/prism/node.rb', line 3790

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



3807
3808
3809
# File 'lib/prism/node.rb', line 3807

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

#child_nodesObject Also known as: deconstruct

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



3812
3813
3814
# File 'lib/prism/node.rb', line 3812

def child_nodes
  [parent, child]
end

#comment_targetsObject

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



3825
3826
3827
# File 'lib/prism/node.rb', line 3825

def comment_targets
  [*parent, child, delimiter_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



3817
3818
3819
3820
3821
3822
# File 'lib/prism/node.rb', line 3817

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

#copy(**params) ⇒ Object

def copy: (**params) -> ConstantPathNode



3830
3831
3832
3833
3834
3835
3836
3837
# File 'lib/prism/node.rb', line 3830

def copy(**params)
  ConstantPathNode.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]



3843
3844
3845
# File 'lib/prism/node.rb', line 3843

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

#delimiterObject

def delimiter: () -> String



3848
3849
3850
# File 'lib/prism/node.rb', line 3848

def delimiter
  delimiter_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
# File 'lib/prism/node.rb', line 3852

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



3880
3881
3882
# File 'lib/prism/node.rb', line 3880

def type
  :constant_path_node
end