class SyntaxTree::Params
Params
represents defining parameters on a method or lambda.
def method(param) end
Attributes
- nil | :nil |
ArgsForward
|KwRestParam
-
the optional keyword rest
parameter
- nil |
ArgsForward
|ExcessedComma
|RestParam
-
the optional rest
parameter
Public Class Methods
Source
# File lib/syntax_tree/node.rb, line 8313 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
Source
# File lib/syntax_tree/node.rb, line 8443 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
Source
# File lib/syntax_tree/node.rb, line 8343 def accept(visitor) visitor.visit_params(self) end
Source
# File lib/syntax_tree/node.rb, line 8466 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
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.
Source
# File lib/syntax_tree/node.rb, line 8347 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
Source
# File lib/syntax_tree/node.rb, line 8361 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
Source
# File lib/syntax_tree/node.rb, line 8389 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
Source
# File lib/syntax_tree/node.rb, line 8338 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end
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.
Source
# File lib/syntax_tree/node.rb, line 8403 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