Sha256: a35de5b560843b24239eb60a74d457e55fb69d3228e631a1dad41788a5a74810

Contents?: true

Size: 1.77 KB

Versions: 176

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
# typed: strict

module T::NonForcingConstants
  # NOTE: This method is documented on the RBI in Sorbet's payload, so that it
  # shows up in the hover/completion documentation via LSP.
  T::Sig::WithoutRuntime.sig {params(val: BasicObject, klass: String).returns(T::Boolean)}
  def self.non_forcing_is_a?(val, klass)
    method_name = "T::NonForcingConstants.non_forcing_is_a?"
    if klass.empty?
      raise ArgumentError.new("The string given to `#{method_name}` must not be empty")
    end

    current_klass = T.let(nil, T.nilable(Module))
    current_prefix = T.let(nil, T.nilable(String))

    parts = klass.split('::')
    parts.each do |part|
      if current_klass.nil?
        # First iteration
        if part != ""
          raise ArgumentError.new("The string given to `#{method_name}` must be an absolute constant reference that starts with `::`")
        end

        current_klass = Object
        current_prefix = ''

        # if this had a :: prefix, then there's no more loading to
        # do---skip to the next one
        next
      end

      if current_klass.autoload?(part)
        # There's an autoload registered for that constant, which means it's not
        # yet loaded. `value` can't be an instance of something not yet loaded.
        return false
      end

      # Sorbet guarantees that the string is an absolutely resolved name.
      search_inheritance_chain = false
      if !current_klass.const_defined?(part, search_inheritance_chain)
        return false
      end

      current_klass = current_klass.const_get(part)
      current_prefix = "#{current_prefix}::#{part}"

      if !Module.===(current_klass)
        raise ArgumentError.new("#{current_prefix} is not a class or module")
      end
    end

    current_klass.===(val)
  end
end

Version data entries

176 entries across 176 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11834 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11829 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11826 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11823 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11820 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11813 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11812 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11810 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11805 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11802 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11801 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11798 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11796 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11789 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11787 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11784 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11781 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11780 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11778 lib/types/non_forcing_constants.rb
sorbet-runtime-0.5.11770 lib/types/non_forcing_constants.rb