class SyntaxTree::Params

Params represents defining parameters on a method or lambda.

def method(param) end

Attributes

block[R]
nil | BlockArg

the optional block parameter

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

keyword_rest[R]
nil | :nil | ArgsForward | KwRestParam

the optional keyword rest

parameter

keywords[R]
Array[ [ Label, nil | Node

]] any keyword parameters and their

optional default values

optionals[R]
Array[ [ Ident, Node

]] any optional parameters and their default

values

posts[R]
Array[ Ident | MLHSParen ]

any positional parameters that exist after a

rest parameter
requireds[R]
Array[ Ident | MLHSParen ]

any required parameters

rest[R]
nil | ArgsForward | ExcessedComma | RestParam

the optional rest

parameter

Public Class Methods

new( location:, requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil ) click to toggle source
# File lib/syntax_tree/node.rb, line 8298
def initialize(
  location:,
  requireds: [],
  optionals: [],
  rest: nil,
  posts: [],
  keywords: [],
  keyword_rest: nil,
  block: nil
)
  @requireds = requireds
  @optionals = optionals
  @rest = rest
  @posts = posts
  @keywords = keywords
  @keyword_rest = keyword_rest
  @block = block
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 8428
def ===(other)
  other.is_a?(Params) && ArrayMatch.call(requireds, other.requireds) &&
    optionals.length == other.optionals.length &&
    optionals
      .zip(other.optionals)
      .all? { |left, right| ArrayMatch.call(left, right) } &&
    rest === other.rest && ArrayMatch.call(posts, other.posts) &&
    keywords.length == other.keywords.length &&
    keywords
      .zip(other.keywords)
      .all? { |left, right| ArrayMatch.call(left, right) } &&
    keyword_rest === other.keyword_rest && block === other.block
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 8328
def accept(visitor)
  visitor.visit_params(self)
end
arity() click to toggle source

Returns a range representing the possible number of arguments accepted by this params node not including the block. For example:

def foo(a, b = 1, c:, d: 2, &block)
  ...
end

has arity 2..4.

# File lib/syntax_tree/node.rb, line 8451
def arity
  optional_keywords = keywords.count { |_label, value| value }

  lower_bound =
    requireds.length + posts.length + keywords.length - optional_keywords

  upper_bound =
    if keyword_rest.nil? && rest.nil?
      lower_bound + optionals.length + optional_keywords
    end

  lower_bound..upper_bound
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 8332
def child_nodes
  keyword_rest = self.keyword_rest

  [
    *requireds,
    *optionals.flatten(1),
    rest,
    *posts,
    *keywords.flatten(1),
    (keyword_rest if keyword_rest != :nil),
    block
  ]
end
Also aliased as: deconstruct
copy( location: nil, requireds: nil, optionals: nil, rest: nil, posts: nil, keywords: nil, keyword_rest: nil, block: nil ) click to toggle source
# File lib/syntax_tree/node.rb, line 8346
def copy(
  location: nil,
  requireds: nil,
  optionals: nil,
  rest: nil,
  posts: nil,
  keywords: nil,
  keyword_rest: nil,
  block: nil
)
  node =
    Params.new(
      location: location || self.location,
      requireds: requireds || self.requireds,
      optionals: optionals || self.optionals,
      rest: rest || self.rest,
      posts: posts || self.posts,
      keywords: keywords || self.keywords,
      keyword_rest: keyword_rest || self.keyword_rest,
      block: block || self.block
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 8374
def deconstruct_keys(_keys)
  {
    location: location,
    requireds: requireds,
    optionals: optionals,
    rest: rest,
    posts: posts,
    keywords: keywords,
    keyword_rest: keyword_rest,
    block: block,
    comments: comments
  }
end
empty?() click to toggle source

Params nodes are the most complicated in the tree. Occasionally you want to know if they are “empty”, which means not having any parameters declared. This logic accesses every kind of parameter and determines if it’s missing.

# File lib/syntax_tree/node.rb, line 8323
def empty?
  requireds.empty? && optionals.empty? && !rest && posts.empty? &&
    keywords.empty? && !keyword_rest && !block
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 8388
def format(q)
  rest = self.rest
  keyword_rest = self.keyword_rest

  parts = [
    *requireds,
    *optionals.map { |(name, value)| OptionalFormatter.new(name, value) }
  ]

  parts << rest if rest && !rest.is_a?(ExcessedComma)
  parts.concat(posts)
  parts.concat(
    keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
  )

  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
  parts << block if block

  if parts.empty?
    q.nest(0) { format_contents(q, parts) }
    return
  end

  if q.parent.is_a?(DefNode)
    q.nest(0) do
      q.text("(")
      q.group do
        q.indent do
          q.breakable_empty
          format_contents(q, parts)
        end
        q.breakable_empty
      end
      q.text(")")
    end
  else
    q.nest(0) { format_contents(q, parts) }
  end
end