Params
represents defining parameters on a method or lambda.
def method(param) endAttributes
BlockArg
the optional block parameter
Label
, nil | Node
]] any keyword parameters and their
optional default values
Ident
, Node
]] any optional parameters and their default
values
Ident
| MLHSParen
]
any positional parameters that exist after a
rest parameterPublic Class Methods Source
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 = [] endPublic Instance Methods Source
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 endSource
def accept(visitor) visitor.visit_params(self) endSource
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.
Sourcedef child_nodes keyword_rest = self.keyword_rest [ *requireds, *optionals.flatten(1), rest, *posts, *keywords.flatten(1), (keyword_rest if keyword_rest != :nil), block ] endSource
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 endSource
def deconstruct_keys(_keys) { location: location, requireds: requireds, optionals: optionals, rest: rest, posts: posts, keywords: keywords, keyword_rest: keyword_rest, block: block, comments: comments } endSource
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.
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4