Class: RubyDetective::AST::Nodes::ConstantReferenceNode

Inherits:
GenericNode
  • Object
show all
Defined in:
lib/ruby_detective/ast/nodes/constant_reference_node.rb

Constant Summary collapse

CONSTANT_NAME_INDEX =
1
NESTED_CONSTANT_INDEX =

Recursively builds the constant path by traversing it’s children, that way we can compose a path composed of multiple namespaces, for example: Foo::Bar::Batz as [:Foo, :Bar, :Batz].

0

Instance Attribute Summary

Attributes inherited from GenericNode

#ast_node, #children, #file_path, #parent_node

Instance Method Summary collapse

Methods inherited from GenericNode

#absolute_path_sign_node?, #class_declaration_node?, #constant_reference_node?, #declared_namespace, #first_line, #generic_node?, #initialize, #last_line, #module_declaration_node?, #namespace, #query, #raw_children, #short_namespace, #value_node?

Constructor Details

This class inherits a constructor from RubyDetective::AST::Nodes::GenericNode

Instance Method Details

#constant_nameObject



6
7
8
# File 'lib/ruby_detective/ast/nodes/constant_reference_node.rb', line 6

def constant_name
  children[CONSTANT_NAME_INDEX].value
end

#constant_pathObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_detective/ast/nodes/constant_reference_node.rb', line 21

def constant_path
  nested_constant = children[NESTED_CONSTANT_INDEX]

  if nested_constant.constant_reference_node?
    nested_constant.constant_path + [constant_name]
  elsif nested_constant.absolute_path_sign_node?
    # This is used to signify that the constant path was forced to start
    # from the root, for example: "::Foo::Bar"
    [:"::"] + [constant_name]
  else
    [constant_name]
  end
end

#top_level_constant?Boolean

A top level constant is for example the “Bar” from “Foo::Bar”. We access it by checking if the parent is another constant, if it is it means the constant is a nested one and not top level.

Returns:

  • (Boolean)


13
14
15
# File 'lib/ruby_detective/ast/nodes/constant_reference_node.rb', line 13

def top_level_constant?
  !parent_node.constant_reference_node?
end

#typeObject



35
36
37
# File 'lib/ruby_detective/ast/nodes/constant_reference_node.rb', line 35

def type
  :constant
end