Sha256: 76cac96ff0f1f50da9cf5d9c7a7bdbbf08df91ce94d02f3af7a27e03d364715d

Contents?: true

Size: 1.24 KB

Versions: 12

Compression:

Stored size: 1.24 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.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

12 entries across 12 versions & 2 rubygems

Version Path
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rbs-2.8.4/lib/rbs/resolver/type_name_resolver.rb
rbs-2.8.4 lib/rbs/resolver/type_name_resolver.rb
rbs-2.8.3 lib/rbs/resolver/type_name_resolver.rb
rbs-3.0.0.dev.1 lib/rbs/resolver/type_name_resolver.rb
rbs-2.8.2 lib/rbs/resolver/type_name_resolver.rb
rbs-2.8.1 lib/rbs/resolver/type_name_resolver.rb
rbs-2.8.0 lib/rbs/resolver/type_name_resolver.rb
rbs-2.8.0.pre.1 lib/rbs/resolver/type_name_resolver.rb
rbs-2.7.0 lib/rbs/resolver/type_name_resolver.rb
rbs-2.7.0.pre.3 lib/rbs/resolver/type_name_resolver.rb
rbs-2.7.0.pre.2 lib/rbs/resolver/type_name_resolver.rb
rbs-2.7.0.pre.1 lib/rbs/resolver/type_name_resolver.rb