Method: RBS::Prototype::RBI#parse_params

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

#parse_params(args_node, args, method_type, variables:) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/rbs/prototype/rbi.rb', line 320

def parse_params(args_node, args, method_type, variables:)
  vars = (node_to_hash(each_arg(args).to_a[0]) || {}).transform_values {|value| type_of(value, variables: variables) }

  required_positionals = []
  optional_positionals = []
  rest_positionals = nil
  trailing_positionals = []
  required_keywords = {}
  optional_keywords = {}
  rest_keywords = nil

  var_names = args_node.children[0]
  pre_num, _pre_init, opt, _first_post, post_num, _post_init, rest, kw, kwrest, block = args_node.children[1].children

  pre_num.times.each do |i|
    name = var_names[i]
    type = vars[name] || Types::Bases::Any.new(location: nil)
    required_positionals << Types::Function::Param.new(type: type, name: name)
  end

  index = pre_num
  while opt
    name = var_names[index]
    if (type = vars[name])
      optional_positionals << Types::Function::Param.new(type: type, name: name)
    end
    index += 1
    opt = opt.children[1]
  end

  if rest
    name = var_names[index]
    if (type = vars[name])
      rest_positionals = Types::Function::Param.new(type: type, name: name)
    end
    index += 1
  end

  post_num.times do |i|
    name = var_names[i+index]
    if (type = vars[name])
      trailing_positionals << Types::Function::Param.new(type: type, name: name)
    end
    index += 1
  end

  while kw
    name, value = kw.children[0].children
    if (type = vars[name])
      if value
        optional_keywords[name] = Types::Function::Param.new(type: type, name: name)
      else
        required_keywords[name] = Types::Function::Param.new(type: type, name: name)
      end
    end

    kw = kw.children[1]
  end

  if kwrest
    name = kwrest.children[0]
    if (type = vars[name])
      rest_keywords = Types::Function::Param.new(type: type, name: name)
    end
  end

  method_block = nil
  if block
    if (type = vars[block])
      if type.is_a?(Types::Proc)
        method_block = MethodType::Block.new(required: true, type: type.type)
      elsif type.is_a?(Types::Bases::Any)
        method_block = MethodType::Block.new(
          required: true,
          type: Types::Function.empty(Types::Bases::Any.new(location: nil))
        )
      # Handle an optional block like `T.nilable(T.proc.void)`.
      elsif type.is_a?(Types::Optional) && type.type.is_a?(Types::Proc)
        method_block = MethodType::Block.new(required: false, type: type.type.type)
      else
        STDERR.puts "Unexpected block type: #{type}"
        PP.pp args_node, STDERR
        method_block = MethodType::Block.new(
          required: true,
          type: Types::Function.empty(Types::Bases::Any.new(location: nil))
        )
      end
    end
  end

  method_type.update(
    type: method_type.type.update(
      required_positionals: required_positionals,
      optional_positionals: optional_positionals,
      rest_positionals: rest_positionals,
      trailing_positionals: trailing_positionals,
      required_keywords: required_keywords,
      optional_keywords: optional_keywords,
      rest_keywords: rest_keywords
    ),
    block: method_block
  )
end