A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://ruby-syntax-tree.github.io/syntax_tree/SyntaxTree/Params.html below:

class SyntaxTree::Params - RDoc Documentation

  1. SyntaxTree::
  2. Params
class SyntaxTree::Params

Params represents defining parameters on a method or lambda.

def method(param) end
Attributes
nil | BlockArg

the optional block parameter

Array[ [ Label, nil | Node

]] any keyword parameters and their

optional default values

Array[ [ Ident, Node

]] any optional parameters and their default

values

Array[ Ident | MLHSParen ]

any positional parameters that exist after a

rest parameter
Public 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 = []
end
Public 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
end

Source

def accept(visitor)
  visitor.visit_params(self)
end

Source

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

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

Source

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

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

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

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