Class: ReferenceExtractor::Internal::ParsedConstantDefinitions

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

Overview

A collection of constant definitions parsed from an Abstract Syntax Tree (AST).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_node:) ⇒ ParsedConstantDefinitions

Returns a new instance of ParsedConstantDefinitions.



24
25
26
27
28
# File 'lib/reference_extractor/internal/parsed_constant_definitions.rb', line 24

def initialize(root_node:)
  @local_definitions = {}

  collect_local_definitions_from_root(root_node) if root_node
end

Class Method Details

.reference_qualifications(constant_name, namespace_path:) ⇒ Object

What fully qualified constants can this constant refer to in this context?



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/reference_extractor/internal/parsed_constant_definitions.rb', line 11

def reference_qualifications(constant_name, namespace_path:)
  return [constant_name] if constant_name.start_with?("::")

  resolved_constant_name = "::#{constant_name}"

  possible_namespaces = namespace_path.each_with_object([""]) do |current, acc|
    acc << "#{acc.last}::#{current}" if current
  end

  possible_namespaces.map { |namespace| namespace + resolved_constant_name }
end

Instance Method Details

#local_reference?(constant_name, location: nil, namespace_path: []) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/reference_extractor/internal/parsed_constant_definitions.rb', line 30

def local_reference?(constant_name, location: nil, namespace_path: [])
  qualifications = self.class.reference_qualifications(constant_name, namespace_path: namespace_path)

  qualifications.any? do |name|
    @local_definitions[name] &&
      @local_definitions[name] != location
  end
end