Class: Prism::ConstantPathTargetNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.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

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



4089
4090
4091
4092
4093
4094
# File 'lib/prism/node.rb', line 4089

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



4083
4084
4085
# File 'lib/prism/node.rb', line 4083

def child
  @child
end

#delimiter_locObject (readonly)

attr_reader delimiter_loc: Location



4086
4087
4088
# File 'lib/prism/node.rb', line 4086

def delimiter_loc
  @delimiter_loc
end

#parentObject (readonly)

attr_reader parent: Node?



4080
4081
4082
# File 'lib/prism/node.rb', line 4080

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



4097
4098
4099
# File 'lib/prism/node.rb', line 4097

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

#child_nodesObject Also known as: deconstruct

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



4102
4103
4104
# File 'lib/prism/node.rb', line 4102

def child_nodes
  [parent, child]
end

#comment_targetsObject

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



4115
4116
4117
# File 'lib/prism/node.rb', line 4115

def comment_targets
  [*parent, child, delimiter_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



4107
4108
4109
4110
4111
4112
# File 'lib/prism/node.rb', line 4107

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

#copy(**params) ⇒ Object

def copy: (**params) -> ConstantPathTargetNode



4120
4121
4122
4123
4124
4125
4126
4127
# File 'lib/prism/node.rb', line 4120

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]



4133
4134
4135
# File 'lib/prism/node.rb', line 4133

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

#delimiterObject

def delimiter: () -> String



4138
4139
4140
# File 'lib/prism/node.rb', line 4138

def delimiter
  delimiter_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
# File 'lib/prism/node.rb', line 4142

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



4170
4171
4172
# File 'lib/prism/node.rb', line 4170

def type
  :constant_path_target_node
end