class Prism::ParametersNode

Represents the list of parameters on a method, block, or lambda definition.

def a(b, c, d)
      ^^^^^^^
end

Attributes

block[R]

attr_reader block: BlockParameterNode?

keywords[R]

attr_reader keywords: Array[RequiredKeywordParameterNode | OptionalKeywordParameterNode]

optionals[R]

attr_reader optionals: Array

posts[R]

attr_reader posts: Array[RequiredParameterNode | MultiTargetNode | KeywordRestParameterNode | NoKeywordsParameterNode | ForwardingParameterNode]

requireds[R]

attr_reader requireds: Array[RequiredParameterNode | MultiTargetNode]

rest[R]

attr_reader rest: RestParameterNode | ImplicitRestNode | nil

Public Class Methods

new(source, requireds, optionals, rest, posts, keywords, keyword_rest, block, location) click to toggle source

def initialize: (Array[RequiredParameterNode | MultiTargetNode] requireds, Array optionals, RestParameterNode | ImplicitRestNode | nil rest, Array[RequiredParameterNode | MultiTargetNode | KeywordRestParameterNode | NoKeywordsParameterNode | ForwardingParameterNode] posts, Array[RequiredKeywordParameterNode | OptionalKeywordParameterNode] keywords, KeywordRestParameterNode | ForwardingParameterNode | NoKeywordsParameterNode | nil keyword_rest, BlockParameterNode? block, Location location) -> void

# File prism/node.rb, line 14442
def initialize(source, requireds, optionals, rest, posts, keywords, keyword_rest, block, location)
  @source = source
  @location = location
  @requireds = requireds
  @optionals = optionals
  @rest = rest
  @posts = posts
  @keywords = keywords
  @keyword_rest = keyword_rest
  @block = block
end
type() click to toggle source

Similar to type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol

# File prism/node.rb, line 14545
def self.type
  :parameters_node
end

Public Instance Methods

===(other) click to toggle source

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.

# File prism/node.rb, line 14551
def ===(other)
  other.is_a?(ParametersNode) &&
    (requireds.length == other.requireds.length) &&
    requireds.zip(other.requireds).all? { |left, right| left === right } &&
    (optionals.length == other.optionals.length) &&
    optionals.zip(other.optionals).all? { |left, right| left === right } &&
    (rest === other.rest) &&
    (posts.length == other.posts.length) &&
    posts.zip(other.posts).all? { |left, right| left === right } &&
    (keywords.length == other.keywords.length) &&
    keywords.zip(other.keywords).all? { |left, right| left === right } &&
    (keyword_rest === other.keyword_rest) &&
    (block === other.block)
end
accept(visitor) click to toggle source

def accept: (Visitor visitor) -> void

# File prism/node.rb, line 14455
def accept(visitor)
  visitor.visit_parameters_node(self)
end
child_nodes() click to toggle source

def child_nodes: () -> Array[nil | Node]

# File prism/node.rb, line 14460
def child_nodes
  [*requireds, *optionals, rest, *posts, *keywords, keyword_rest, block]
end
Also aliased as: deconstruct
comment_targets() click to toggle source

def comment_targets: () -> Array[Node | Location]

# File prism/node.rb, line 14478
def comment_targets
  [*requireds, *optionals, *rest, *posts, *keywords, *keyword_rest, *block] #: Array[Prism::node | Location]
end
compact_child_nodes() click to toggle source

def compact_child_nodes: () -> Array

# File prism/node.rb, line 14465
def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact.concat(requireds)
  compact.concat(optionals)
  compact << rest if rest
  compact.concat(posts)
  compact.concat(keywords)
  compact << keyword_rest if keyword_rest
  compact << block if block
  compact
end
copy(requireds: self.requireds, optionals: self.optionals, rest: self.rest, posts: self.posts, keywords: self.keywords, keyword_rest: self.keyword_rest, block: self.block, location: self.location) click to toggle source

def copy: (?requireds: Array[RequiredParameterNode | MultiTargetNode], ?optionals: Array, ?rest: RestParameterNode | ImplicitRestNode | nil, ?posts: Array[RequiredParameterNode | MultiTargetNode | KeywordRestParameterNode | NoKeywordsParameterNode | ForwardingParameterNode], ?keywords: Array[RequiredKeywordParameterNode | OptionalKeywordParameterNode], ?keyword_rest: KeywordRestParameterNode | ForwardingParameterNode | NoKeywordsParameterNode | nil, ?block: BlockParameterNode?, ?location: Location) -> ParametersNode

# File prism/node.rb, line 14483
def copy(requireds: self.requireds, optionals: self.optionals, rest: self.rest, posts: self.posts, keywords: self.keywords, keyword_rest: self.keyword_rest, block: self.block, location: self.location)
  ParametersNode.new(source, requireds, optionals, rest, posts, keywords, keyword_rest, block, location)
end
deconstruct()

def deconstruct: () -> Array[nil | Node]

Alias for: child_nodes
deconstruct_keys(keys) click to toggle source

def deconstruct_keys: (Array keys) -> { requireds: Array[RequiredParameterNode | MultiTargetNode], optionals: Array, rest: RestParameterNode | ImplicitRestNode | nil, posts: Array[RequiredParameterNode | MultiTargetNode | KeywordRestParameterNode | NoKeywordsParameterNode | ForwardingParameterNode], keywords: Array[RequiredKeywordParameterNode | OptionalKeywordParameterNode], keyword_rest: KeywordRestParameterNode | ForwardingParameterNode | NoKeywordsParameterNode | nil, block: BlockParameterNode?, location: Location }

# File prism/node.rb, line 14491
def deconstruct_keys(keys)
  { requireds: requireds, optionals: optionals, rest: rest, posts: posts, keywords: keywords, keyword_rest: keyword_rest, block: block, location: location }
end
inspect() click to toggle source

def inspect -> String

# File prism/node.rb, line 14517
def inspect
  InspectVisitor.compose(self)
end
signature() click to toggle source

Mirrors the Method#parameters method.

# File prism/node_ext.rb, line 238
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|
    if param.is_a?(MultiTargetNode)
      names << [:req]
    elsif param.is_a?(NoKeywordsParameterNode)
      # 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
type() click to toggle source

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol

# File prism/node.rb, line 14535
def type
  :parameters_node
end