Sha256: 0bf8413214fc2c7c5c87076a83eac018256e92847249e0d6d03aff24e5f7d871

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

# typed: strict
# frozen_string_literal: true

module Tapioca
  module Gem
    module Listeners
      class SorbetTypeVariables < Base
        extend T::Sig

        include Runtime::Reflection

        private

        sig { override.params(event: ScopeNodeAdded).void }
        def on_scope(event)
          constant = event.constant
          node = event.node

          compile_type_variable_declarations(node, constant)

          sclass = RBI::SingletonClass.new
          compile_type_variable_declarations(sclass, singleton_class_of(constant))
          node << sclass if sclass.nodes.length > 1
        end

        sig { params(tree: RBI::Tree, constant: Module).void }
        def compile_type_variable_declarations(tree, constant)
          # Try to find the type variables defined on this constant, bail if we can't
          type_variables = Runtime::GenericTypeRegistry.lookup_type_variables(constant)
          return unless type_variables

          # Map each type variable to its string representation.
          #
          # Each entry of `type_variables` maps a Module to a String,
          # and the order they are inserted into the hash is the order they should be
          # defined in the source code.
          type_variable_declarations = type_variables.map do |type_variable|
            type_variable_name = type_variable.name
            next unless type_variable_name

            tree << RBI::TypeMember.new(type_variable_name, type_variable.serialize)
          end

          return if type_variable_declarations.empty?

          tree << RBI::Extend.new("T::Generic")
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
tapioca-0.8.3 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.8.2 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.7.3 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.8.1 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.8.0 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.7.2 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.7.1 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.7.0 lib/tapioca/gem/listeners/sorbet_type_variables.rb