Class: Txray::ClientIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/txray/client_index.rb

Constant Summary collapse

RETURN_WRITES =
[
  Prism::InstanceVariableOrWriteNode, Prism::InstanceVariableWriteNode,
  Prism::LocalVariableOrWriteNode, Prism::LocalVariableWriteNode,
  Prism::ConstantOrWriteNode, Prism::ConstantWriteNode
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(classifier) ⇒ ClientIndex

Returns a new instance of ClientIndex.



11
12
13
14
15
16
17
18
# File 'lib/txray/client_index.rb', line 11

def initialize(classifier)
  @classifier = classifier
  @locals = {}
  @ivars = {}
  @constants = {}
  @methods = {}
  @delegations = {}
end

Instance Method Details

#delegated_kind(namespace, name, depth = 0) ⇒ Object



44
45
46
47
48
49
# File 'lib/txray/client_index.rb', line 44

def delegated_kind(namespace, name, depth = 0)
  target = @delegations.dig(namespace, name)
  return nil if target.nil? || depth > 4

  @methods.dig(namespace, target) || delegated_kind(namespace, target, depth + 1)
end

#index(source) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/txray/client_index.rb', line 20

def index(source)
  Namespaces.each(source.root) do |namespace, node|
    case node
    when Prism::LocalVariableWriteNode then bind(@locals, source.path, node.name, node.value)
    when Prism::InstanceVariableWriteNode, Prism::InstanceVariableOrWriteNode
      bind(@ivars, namespace, node.name, node.value)
    when Prism::ConstantWriteNode then bind_constant(namespace, node)
    when Prism::DefNode then bind_method(namespace, node)
    when Prism::CallNode then bind_delegation(namespace, node)
    end
  end
end

#kind_of(node, context, depth = 0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/txray/client_index.rb', line 33

def kind_of(node, context, depth = 0)
  return nil if node.nil? || depth > 4

  case node
  when Prism::LocalVariableReadNode then @locals.dig(context.path, node.name)
  when Prism::InstanceVariableReadNode then @ivars.dig(context.namespace, node.name)
  when Prism::ConstantReadNode, Prism::ConstantPathNode then constant_kind(node, context.namespace)
  when Prism::CallNode then call_kind(node, context, depth)
  end
end