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.



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

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

  all_names.merge(env.class_decls.keys)
  all_names.merge(env.interface_decls.keys)
  all_names.merge(env.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

Instance Method Details

#has_name?(full_name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/rbs/resolver/type_name_resolver.rb', line 50

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

#resolve(type_name, context:) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rbs/resolver/type_name_resolver.rb', line 25

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

  try_cache([type_name, context]) do
    resolve_in(type_name, context)
  end
end

#resolve_in(type_name, context) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbs/resolver/type_name_resolver.rb', line 35

def resolve_in(type_name, context)
  if context
    parent, child = context
    case child
    when false
      resolve_in(type_name, parent)
    when TypeName
      name = type_name.with_prefix(child.to_namespace)
      has_name?(name) || resolve_in(type_name, parent)
    end
  else
    has_name?(type_name.absolute!)
  end
end

#try_cache(query) ⇒ Object



18
19
20
21
22
23
# File 'lib/rbs/resolver/type_name_resolver.rb', line 18

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