class SyntaxTree::BlockVar

BlockVar represents the parameters being declared for a block. Effectively this node is everything contained within the pipes. This includes all of the various parameter types, as well as block-local variable declarations.

method do |positional, optional = value, keyword:, █ local|
end

Constants

SEPARATOR

We’ll keep a single instance of this separator around for all block vars to cut down on allocations.

Attributes

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

locals[R]
Array[ Ident ]

the list of block-local variable declarations

params[R]
Params

the parameters being declared with the block

Public Class Methods

new(params:, locals:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 2151
def initialize(params:, locals:, location:)
  @params = params
  @locals = locals
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 2209
def ===(other)
  other.is_a?(BlockVar) && params === other.params &&
    ArrayMatch.call(locals, other.locals)
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 2158
def accept(visitor)
  visitor.visit_block_var(self)
end
arg0?() click to toggle source

When a single required parameter is declared for a block, it gets automatically expanded if the values being yielded into it are an array.

# File lib/syntax_tree/node.rb, line 2216
def arg0?
  params.requireds.length == 1 && params.optionals.empty? &&
    params.rest.nil? && params.posts.empty? && params.keywords.empty? &&
    params.keyword_rest.nil? && params.block.nil?
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 2162
def child_nodes
  [params, *locals]
end
Also aliased as: deconstruct
copy(params: nil, locals: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 2166
def copy(params: nil, locals: nil, location: nil)
  node =
    BlockVar.new(
      params: params || self.params,
      locals: locals || self.locals,
      location: location || self.location
    )

  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 2180
def deconstruct_keys(_keys)
  { params: params, locals: locals, location: location, comments: comments }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 2196
def format(q)
  q.text("|")
  q.group do
    q.remove_breaks(q.format(params))

    if locals.any?
      q.text("; ")
      q.seplist(locals, SEPARATOR) { |local| q.format(local) }
    end
  end
  q.text("|")
end