Sha256: 9c58911bbdd7556844489f2525319cd3c8c3285315fb5136ee81202100279bcb

Contents?: true

Size: 1.77 KB

Versions: 19

Compression:

Stored size: 1.77 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

        sig { override.params(event: NodeAdded).returns(T::Boolean) }
        def ignore?(event)
          event.is_a?(Tapioca::Gem::ForeignScopeNodeAdded)
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
tapioca-0.11.6 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.11.5 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.11.4 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.11.3 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.11.2 lib/tapioca/gem/listeners/sorbet_type_variables.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/tapioca-0.11.1/lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.11.1 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.11.0 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.10.5 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.10.4 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.10.3 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.10.2 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.10.1 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.10.0 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.9.4 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.9.3 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.9.2 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.9.1 lib/tapioca/gem/listeners/sorbet_type_variables.rb
tapioca-0.9.0 lib/tapioca/gem/listeners/sorbet_type_variables.rb