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
17
# 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.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

Instance Method Details

#has_name?(full_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#resolve(type_name, context:) ⇒ Object



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

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



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

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



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

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