class Prism::ParametersNode

Public Instance Methods

signature ()

Mirrors the Method#parameters method.

# File lib/prism/node_ext.rb, line 269
def signature
  names = [] #: Array[[Symbol, Symbol] | [Symbol]]

  requireds.each do |param|
    names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
  end

  optionals.each { |param| names << [:opt, param.name] }

  if rest && rest.is_a?(RestParameterNode)
    names << [:rest, rest.name || :*]
  end

  posts.each do |param|
    case param
    when MultiTargetNode
      names << [:req]
    when NoKeywordsParameterNode, KeywordRestParameterNode, ForwardingParameterNode
      # Invalid syntax, e.g. "def f(**nil, ...)" moves the NoKeywordsParameterNode to posts
      raise "Invalid syntax"
    else
      names << [:req, param.name]
    end
  end

  # Regardless of the order in which the keywords were defined, the required
  # keywords always come first followed by the optional keywords.
  keyopt = [] #: Array[OptionalKeywordParameterNode]
  keywords.each do |param|
    if param.is_a?(OptionalKeywordParameterNode)
      keyopt << param
    else
      names << [:keyreq, param.name]
    end
  end

  keyopt.each { |param| names << [:key, param.name] }

  case keyword_rest
  when ForwardingParameterNode
    names.concat([[:rest, :*], [:keyrest, :**], [:block, :&]])
  when KeywordRestParameterNode
    names << [:keyrest, keyword_rest.name || :**]
  when NoKeywordsParameterNode
    names << [:nokey]
  end

  names << [:block, block.name || :&] if block
  names
end