class RBS::Resolver::TypeNameResolver

Attributes

all_names[R]
cache[R]
env[R]

Public Class Methods

new(env) click to toggle source
# File rbs-3.4.4/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

Public Instance Methods

has_name?(full_name) click to toggle source
# File rbs-3.4.4/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) click to toggle source
# File rbs-3.4.4/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:) click to toggle source
# File rbs-3.4.4/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) click to toggle source
# File rbs-3.4.4/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) { || ... } click to toggle source
# File rbs-3.4.4/lib/rbs/resolver/type_name_resolver.rb, line 21
def try_cache(query)
  cache.fetch(query) do
    result = yield
    cache[query] = result
  end
end