Class: Prism::ConstantPathNode

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

Overview

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

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

Explore code sample

Defined Under Namespace

Classes: DynamicPartsInConstantPathError, ErrorRecoveryNodesInConstantPathError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, parent, name, delimiter_loc, name_loc) ⇒ ConstantPathNode

Initialize a new ConstantPathNode node.

: (Source source, Integer node_id, Location location, Integer flags, Prism::node? parent, Symbol? name, Location delimiter_loc, Location name_loc) -> void

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • parent (Prism::node, nil)
  • name (Symbol, nil)
  • delimiter_loc (Location)
  • name_loc (Location)


9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
# File 'lib/prism/node.rb', line 9203

def initialize(source, node_id, location, flags, parent, name, delimiter_loc, name_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @parent = parent
  @name = name
  @delimiter_loc = delimiter_loc
  @name_loc = name_loc
end

Class Method Details

.type ⇒ :constant_path_node

See Node.type.

: () -> :constant_path_node

Returns:

  • (:constant_path_node)


9297
9298
9299
# File 'lib/prism/node.rb', line 9297

def self.type
  :constant_path_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


9420
9421
9422
9423
9424
9425
9426
# File 'lib/prism/node.rb', line 9420

def ===(other) # :nodoc:
  other.is_a?(ConstantPathNode) &&
    (parent === other.parent) &&
    (name === other.name) &&
    (delimiter_loc.nil? == other.delimiter_loc.nil?) &&
    (name_loc.nil? == other.name_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


9228
9229
9230
# File 'lib/prism/node.rb', line 9228

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


9235
9236
9237
# File 'lib/prism/node.rb', line 9235

def child_nodes
  [parent]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


9252
9253
9254
9255
9256
# File 'lib/prism/node.rb', line 9252

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  if (parent = self.parent); compact << parent; end
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, parent: self.parent, name: self.name, delimiter_loc: self.delimiter_loc, name_loc: self.name_loc) ⇒ ConstantPathNode

:call-seq:

copy(**fields) -> ConstantPathNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?parent: Prism::node?, ?name: Symbol?, ?delimiter_loc: Location, ?name_loc: Location) -> ConstantPathNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • parent: (Prism::node, nil) (defaults to: self.parent)
  • name: (Symbol, nil) (defaults to: self.name)
  • delimiter_loc: (Location) (defaults to: self.delimiter_loc)
  • name_loc: (Location) (defaults to: self.name_loc)

Returns:



9264
9265
9266
# File 'lib/prism/node.rb', line 9264

def copy(node_id: self.node_id, location: self.location, flags: self.flags, parent: self.parent, name: self.name, delimiter_loc: self.delimiter_loc, name_loc: self.name_loc)
  ConstantPathNode.new(source, node_id, location, flags, parent, name, delimiter_loc, name_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
# File 'lib/prism/node.rb', line 9271

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location parent name delimiter_loc name_loc delimiter]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :parent                  then deconstructed[:parent] = self.parent
    when :name                    then deconstructed[:name] = self.name
    when :delimiter_loc           then deconstructed[:delimiter_loc] = self.delimiter_loc
    when :name_loc                then deconstructed[:name_loc] = self.name_loc
    when :delimiter               then deconstructed[:delimiter] = self.delimiter
    end
  end
end

#delimiter ⇒ String

:call-seq:

delimiter -> String

Slice the location of delimiter_loc from the source.

: () -> String

Returns:

  • (String)


9413
9414
9415
# File 'lib/prism/node.rb', line 9413

def delimiter
  delimiter_loc.slice
end

#delimiter_loc ⇒ Location

:category: Locations :call-seq:

delimiter_loc -> Location

The Location of the :: delimiter.

::Foo
^^

One::Two
   ^^

Explore code sample

: () -> Location

Returns:



9358
9359
9360
9361
9362
# File 'lib/prism/node.rb', line 9358

def delimiter_loc
  location = @delimiter_loc
  return location if location.is_a?(Location)
  @delimiter_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


9243
9244
9245
9246
9247
# File 'lib/prism/node.rb', line 9243

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  if (parent = self.parent); yield parent; end
end

#full_name ⇒ String

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

: () -> String

Returns:

  • (String)


257
258
259
# File 'lib/prism/node_ext.rb', line 257

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

#full_name_parts ⇒ Array[Symbol]

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

: () -> Array

Returns:

  • (Array[Symbol])


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/prism/node_ext.rb', line 233

def full_name_parts
  parts = [] #: Array[Symbol]
  current = self #: node?

  while current.is_a?(ConstantPathNode)
    name = current.name
    if name.nil?
      raise ErrorRecoveryNodesInConstantPathError, "Constant path contains error recovery nodes. Cannot compute full name"
    end

    parts.unshift(name)
    current = current.parent
  end

  if !current.is_a?(ConstantReadNode) && !current.nil?
    raise DynamicPartsInConstantPathError, "Constant path contains dynamic parts. Cannot compute full name"
  end

  parts.unshift(current&.name || :"")
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


9302
9303
9304
# File 'lib/prism/node.rb', line 9302

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#name ⇒ Symbol?

:call-seq:

name -> Symbol | nil

The name of the constant being accessed. This could be nil in the event of a syntax error.

: () -> Symbol?

Returns:

  • (Symbol, nil)


9337
9338
9339
# File 'lib/prism/node.rb', line 9337

def name
  @name
end

#name_loc ⇒ Location

:category: Locations :call-seq:

name_loc -> Location

The Location of the name of the constant.

::Foo
  ^^^

One::Two
     ^^^

Explore code sample

: () -> Location

Returns:



9390
9391
9392
9393
9394
# File 'lib/prism/node.rb', line 9390

def name_loc
  location = @name_loc
  return location if location.is_a?(Location)
  @name_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#parent ⇒ Prism::node?

:call-seq:

parent -> Node | nil

The left-hand node of the path, if present. It can be nil or any non-void expression. It will be nil when the constant lookup is at the root of the module tree.

Foo::Bar
^^^

self::Test
^^^^

a.b::C
^^^

Explore code sample

: () -> Prism::node?

Returns:

  • (Prism::node, nil)


9327
9328
9329
# File 'lib/prism/node.rb', line 9327

def parent
  @parent
end

#save_delimiter_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the delimiter_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



9369
9370
9371
# File 'lib/prism/node.rb', line 9369

def save_delimiter_loc(repository)
  repository.enter(node_id, :delimiter_loc)
end

#save_name_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the name_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



9401
9402
9403
# File 'lib/prism/node.rb', line 9401

def save_name_loc(repository)
  repository.enter(node_id, :name_loc)
end

#type ⇒ :constant_path_node

See Node#type.

: () -> :constant_path_node

Returns:

  • (:constant_path_node)


9290
9291
9292
# File 'lib/prism/node.rb', line 9290

def type
  :constant_path_node
end