Class: ReferenceExtractor::Internal::AstReferenceExtractor

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

Overview

Extracts a possible constant reference from a given AST node.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant_name_inspectors:, context_provider:, root_path:) ⇒ AstReferenceExtractor

Returns a new instance of AstReferenceExtractor.



34
35
36
37
38
39
40
41
42
# File 'lib/reference_extractor/internal/ast_reference_extractor.rb', line 34

def initialize(
  constant_name_inspectors:,
  context_provider:,
  root_path:
)
  @constant_name_inspectors = constant_name_inspectors
  @context_provider = context_provider
  @root_path = root_path
end

Class Method Details

.get_fully_qualified_references_from(unresolved_references, context_provider) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/reference_extractor/internal/ast_reference_extractor.rb', line 8

def get_fully_qualified_references_from(unresolved_references, context_provider)
  fully_qualified_references = []

  unresolved_references.each do |unresolved_reference|
    constant =
      context_provider.context_for(
        unresolved_reference.constant_name,
        current_namespace_path: unresolved_reference.namespace_path
      )

    next if constant.nil?

    # Ignore references that resolve to the same file they originate from.
    next if constant.location.to_s == unresolved_reference.relative_path.to_s

    fully_qualified_references << Reference.new(
      relative_path: unresolved_reference.relative_path,
      constant: constant,
      source_location: unresolved_reference.source_location
    )
  end

  fully_qualified_references
end

Instance Method Details

#extract_references(root_node, relative_path:) ⇒ Object

Extract and resolve all references from the AST in one step



45
46
47
48
49
50
51
52
53
54
# File 'lib/reference_extractor/internal/ast_reference_extractor.rb', line 45

def extract_references(root_node, relative_path:)
  local_constant_definitions = ParsedConstantDefinitions.new(root_node: root_node)
  unresolved_references = collect_references(
    root_node,
    ancestors: [],
    relative_path: relative_path,
    local_constant_definitions: local_constant_definitions
  )
  self.class.get_fully_qualified_references_from(unresolved_references, @context_provider)
end

#reference_from_node(node, ancestors:, relative_path:, local_constant_definitions:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/reference_extractor/internal/ast_reference_extractor.rb', line 56

def reference_from_node(node, ancestors:, relative_path:, local_constant_definitions:)
  constant_name = nil

  @constant_name_inspectors.each do |inspector|
    constant_name = inspector.constant_name_from_node(node, ancestors:)

    break if constant_name
  end

  if constant_name
    reference_from_constant(
      constant_name,
      node:,
      ancestors:,
      relative_path:,
      local_constant_definitions:
    )
  end
end