Class: RBS::Resolver::TypeNameResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/resolver/type_name_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TypeNameResolver

Returns a new instance of TypeNameResolver.



10
11
12
13
14
15
16
17
18
19
# File 'lib/rbs/resolver/type_name_resolver.rb', line 10

def initialize(env)
  @all_names = Set[]
  @cache = {}
  @env = env

  all_names.merge(env.class_decls.keys)
  all_names.merge(env.interface_decls.keys)
  all_names.merge(env.type_alias_decls.keys)
  all_names.merge(env.class_alias_decls.keys)
end

Instance Attribute Details

#all_namesObject (readonly)

Returns the value of attribute all_names.



6
7
8
# File 'lib/rbs/resolver/type_name_resolver.rb', line 6

def all_names
  @all_names
end

#cacheObject (readonly)

Returns the value of attribute cache.



7
8
9
# File 'lib/rbs/resolver/type_name_resolver.rb', line 7

def cache
  @cache
end

#envObject (readonly)

Returns the value of attribute env.



8
9
10
# File 'lib/rbs/resolver/type_name_resolver.rb', line 8

def env
  @env
end

Instance Method Details

#has_name?(full_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/rbs/resolver/type_name_resolver.rb', line 84

def has_name?(full_name)
  if all_names.include?(full_name)
    full_name
  end
end

#partition(type_name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbs/resolver/type_name_resolver.rb', line 51

def partition(type_name)
  if type_name.namespace.empty?
    head = type_name.name
    tail = nil
  else
    head, *tail = type_name.namespace.path

    head or raise

    tail = TypeName.new(
      name: type_name.name,
      namespace: Namespace.new(absolute: false, path: tail)
    )
  end

  [head, tail]
end

#resolve(type_name, context:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbs/resolver/type_name_resolver.rb', line 28

def resolve(type_name, context:)
  if type_name.absolute?
    return type_name
  end

  try_cache([type_name, context]) do
    head, tail = partition(type_name)

    head = resolve_in(head, context)

    if head
      if tail
        absolute_name = tail.with_prefix(head.to_namespace)
        if env.normalize_type_name?(absolute_name)
          absolute_name
        end
      else
        head
      end
    end
  end
end

#resolve_in(head, context) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbs/resolver/type_name_resolver.rb', line 69

def resolve_in(head, context)
  if context
    parent, child = context
    case child
    when false
      resolve_in(head, parent)
    when TypeName
      name = TypeName.new(name: head, namespace: child.to_namespace)
      has_name?(name) || resolve_in(head, parent)
    end
  else
    has_name?(TypeName.new(name: head, namespace: Namespace.root))
  end
end

#try_cache(query) ⇒ Object



21
22
23
24
25
26
# File 'lib/rbs/resolver/type_name_resolver.rb', line 21

def try_cache(query)
  cache.fetch(query) do
    result = yield
    cache[query] = result
  end
end