class TypeProf::Signature

Public Instance Methods

screen_name(iseq, scratch) click to toggle source
# File typeprof-0.21.3/lib/typeprof/type.rb, line 853
def screen_name(iseq, scratch)
  fargs_str = "("
  sig_help = {}
  add_farg = -> farg, name, help: false, key: sig_help.size do
    name = "`#{ name }`" if RBS::Parser::KEYWORDS.key?(name.to_s)
    name = "noname" if name.is_a?(Integer) || name == :"*"
    fargs_str << ", " if fargs_str != "("
    i = fargs_str.size
    fargs_str << (Config.current.options[:show_parameter_names] && name ? "#{ farg } #{ name }" : farg)
    sig_help[key] = (i...fargs_str.size)
  end

  @lead_tys.zip(iseq ? iseq.locals : []) do |ty, name|
    add_farg.call(ty.screen_name(scratch), name, help: true)
  end

  @opt_tys&.zip(iseq ? iseq.locals[@lead_tys.size, @opt_tys.size] : []) do |ty, name|
    add_farg.call("?" + ty.screen_name(scratch), name, help: true)
  end

  if @rest_ty
    if iseq
      rest_index = iseq.fargs_format[:rest_start]
      name = rest_index ? iseq.locals[rest_index] : nil
    end
    add_farg.call("*" + @rest_ty.screen_name(scratch), name)
  end

  if iseq
    post_start = iseq.fargs_format[:post_start]
    names = post_start ? iseq.locals[post_start, @post_tys.size] : []
  end
  @post_tys&.zip(names || []) do |ty, name|
    add_farg.call(ty.screen_name(scratch), name)
  end

  @kw_tys&.each do |req, sym, ty|
    opt = req ? "" : "?"
    add_farg.call("#{ opt }#{ sym }: #{ ty.screen_name(scratch) }", nil, help: true, key: sym)
  end

  if @kw_rest_ty
    all_val_ty = Type.bot
    @kw_rest_ty.each_child_global do |ty|
      if ty == Type.any
        val_ty = ty
      else
        # ty is a Type::Hash
        _key_ty, val_ty = ty.elems.squash
      end
      all_val_ty = all_val_ty.union(val_ty)
    end
    add_farg.call("**" + all_val_ty.screen_name(scratch), nil)
  end

  fargs_str << ")"

  fargs_str = "" if fargs_str == "()"

  # Dirty Hack: Stop the iteration at most once!
  # I'll remove this hack if RBS removes the limitation of nesting blocks
  return fargs_str, sig_help if caller_locations.any? {|frame| frame.label == "show_block_signature" }

  optional = false
  blks = []
  @blk_ty.each_child_global do |ty|
    if ty.is_a?(Type::Proc)
      blks << ty
    else
      # XXX: how should we handle types other than Type.nil
      optional = true
    end
  end
  if blks != []
    fargs_str << " " if fargs_str != ""
    fargs_str << "?" if optional
    fargs_str << scratch.show_block_signature(blks)
  end

  return fargs_str, sig_help
end