Sha256: 8b60f2837f7c0f0cf0f893d86b31cd16287dc007a92d5364042c2f4819df213f

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module RBS
  module Resolver
    class TypeNameResolver
      attr_reader :all_names
      attr_reader :cache

      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

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

      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

      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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rbs-3.1.1 lib/rbs/resolver/type_name_resolver.rb
rbs-3.1.0 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.4 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.3 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.2 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.1 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.0 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.0.dev.3 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.0.dev.2 lib/rbs/resolver/type_name_resolver.rb