Sha256: 84ba99d42213138bda5223630efa76b76495cc44cbb4d72d59fe9efc0272c37d

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module RaaP
  class MethodType
    attr_reader :rbs

    def initialize(method, type_params_decl: [], type_args: [], self_type: nil, instance_type: nil, class_type: nil)
      rbs =
        case method
        when ""
          raise ArgumentError, "method type is empty"
        when String
          ::RBS::Parser.parse_method_type(method, require_eof: true) or raise
        when ::RBS::MethodType
          method
        else
          raise "bad method #{method}"
        end
      ts = TypeSubstitution.new(type_params_decl + rbs.type_params, type_args)

      @rbs = ts.method_type_sub(rbs, self_type:, instance_type:, class_type:)
      @fun_type = FunctionType.new(@rbs.type)
    end

    def pick_arguments(size: 10, eval: true)
      args, kwargs = @fun_type.pick_arguments(size: size, eval:)
      block = pick_block(size: size, eval:)

      [args, kwargs, block]
    end

    def pick_block(size: 10, eval: true)
      block = @rbs.block
      return nil if block.nil?
      return nil if (block.required == false) && [true, false].sample

      Proc.new { Type.new(block.type.return_type).pick(size:, eval:) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
raap-0.1.0 lib/raap/method_type.rb