Method: RBS::Prototype::Runtime#method_type

Defined in:
lib/rbs/prototype/runtime.rb

#method_type(method) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/rbs/prototype/runtime.rb', line 171

def method_type(method)
  untyped = Types::Bases::Any.new(location: nil)

  required_positionals = [] #: Array[Types::Function::Param]
  optional_positionals = [] #: Array[Types::Function::Param]
  rest = nil #: Types::Function::Param?
  trailing_positionals = [] #: Array[Types::Function::Param]
  required_keywords = {} #: Hash[Symbol, Types::Function::Param]
  optional_keywords = {} #: Hash[Symbol, Types::Function::Param]
  rest_keywords = nil #: Types::Function::Param?

  requireds = required_positionals

  block = nil #: Types::Block?

  method.parameters.each do |(kind, name)|
    case kind
    when :req
      requireds << Types::Function::Param.new(name: name, type: untyped)
    when :opt
      requireds = trailing_positionals
      optional_positionals << Types::Function::Param.new(name: name, type: untyped)
    when :rest
      requireds = trailing_positionals
      name = nil if name == :* # For `def f(...) end` syntax
      rest = Types::Function::Param.new(name: name, type: untyped)
    when :keyreq
      name or raise
      required_keywords[name] = Types::Function::Param.new(name: nil, type: untyped)
    when :key
      name or raise
      optional_keywords[name] = Types::Function::Param.new(name: nil, type: untyped)
    when :keyrest
      rest_keywords = Types::Function::Param.new(name: nil, type: untyped)
    when :block
      block = Types::Block.new(
        type: Types::Function.empty(untyped).update(rest_positionals: Types::Function::Param.new(name: nil, type: untyped)),
        required: true,
        self_type: nil
      )
    end
  end

  block ||= block_from_ast_of(method)

  return_type = if method.name == :initialize
                  Types::Bases::Void.new(location: nil)
                else
                  untyped
                end
  method_type = Types::Function.new(
    required_positionals: required_positionals,
    optional_positionals: optional_positionals,
    rest_positionals: rest,
    trailing_positionals: trailing_positionals,
    required_keywords: required_keywords,
    optional_keywords: optional_keywords,
    rest_keywords: rest_keywords,
    return_type: return_type,
  )

  MethodType.new(
    location: nil,
    type_params: [],
    type: method_type,
    block: block
  )
end