Class: ReferenceExtractor::Internal::ConstNodeInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/reference_extractor/internal/const_node_inspector.rb

Overview

Extracts a constant name from an AST node of type :const

Instance Method Summary collapse

Instance Method Details

#constant_name_from_node(node, ancestors:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/reference_extractor/internal/const_node_inspector.rb', line 7

def constant_name_from_node(node, ancestors:)
  return nil unless NodeHelpers.constant?(node)

  parent = ancestors.first

  # Only process the root `const` node for namespaced constant references. For example, in the
  # reference `Spam::Eggs::Thing`, we only process the const node associated with `Spam`.
  return nil unless root_constant?(parent)

  if parent && constant_in_module_or_class_definition?(node, parent: parent)
    fully_qualify_constant(ancestors)
  else
    begin
      NodeHelpers.constant_name(node)
    rescue NodeHelpers::TypeError
      nil
    end
  end
end