class RuboCop::AST::BlockNode

A node extension for `block` nodes. This will be used in place of a plain node when the builder constructs the AST, making its methods available to all `send` nodes within RuboCop.

A `block` node is essentially a method send with a block. Parser nests the `send` node inside the `block` node.

Constants

VOID_CONTEXT_METHODS

Public Instance Methods

arguments() click to toggle source

The arguments of this block.

@return [Array<Node>]

# File lib/rubocop/ast/node/block_node.rb, line 23
def arguments
  node_parts[1]
end
arguments?() click to toggle source

Checks whether this block takes any arguments.

@return [Boolean] whether this `block` node takes any arguments

# File lib/rubocop/ast/node/block_node.rb, line 37
def arguments?
  !arguments.empty?
end
body() click to toggle source

The body of this block.

@return [Node, nil] the body of the `block` node or `nil`

# File lib/rubocop/ast/node/block_node.rb, line 30
def body
  node_parts[2]
end
braces?() click to toggle source

Checks whether the `block` literal is delimited by curly braces.

@return [Boolean] whether the `block` literal is enclosed in braces

# File lib/rubocop/ast/node/block_node.rb, line 44
def braces?
  loc.end && loc.end.is?('}')
end
closing_delimiter() click to toggle source

The closing delimiter for this `block` literal.

@return [String] the closing delimiter for the `block` literal

# File lib/rubocop/ast/node/block_node.rb, line 72
def closing_delimiter
  delimiters.last
end
delimiters() click to toggle source

The delimiters for this `block` literal.

@return [Array<String>] the delimiters for the `block` literal

# File lib/rubocop/ast/node/block_node.rb, line 58
def delimiters
  [loc.begin.source, loc.end.source].freeze
end
keywords?() click to toggle source

Checks whether the `block` literal is delimited by `do`-`end` keywords.

@return [Boolean] whether the `block` literal is enclosed in `do`-`end`

# File lib/rubocop/ast/node/block_node.rb, line 51
def keywords?
  loc.end && loc.end.is?('end')
end
lambda?() click to toggle source

Checks whether this `block` literal belongs to a lambda.

@return [Boolean] whether the `block` literal belongs to a lambda

# File lib/rubocop/ast/node/block_node.rb, line 95
def lambda?
  send_node.method?(:lambda)
end
multiline?() click to toggle source

Checks whether this is a multiline block. This is overridden here because the general version in `Node` does not work for `block` nodes.

@return [Boolean] whether the `block` literal is on a several lines

# File lib/rubocop/ast/node/block_node.rb, line 88
def multiline?
  !single_line?
end
node_parts() click to toggle source

Custom destructuring method. This can be used to normalize destructuring for different variations of the node.

@return [Array] the different parts of the `block` node

# File lib/rubocop/ast/node/block_node.rb, line 110
def node_parts
  to_a
end
opening_delimiter() click to toggle source

The opening delimiter for this `block` literal.

@return [String] the opening delimiter for the `block` literal

# File lib/rubocop/ast/node/block_node.rb, line 65
def opening_delimiter
  delimiters.first
end
send_node() click to toggle source

The `send` node associated with this block.

@return [SendNode] the `send` node associated with the `block` node

# File lib/rubocop/ast/node/block_node.rb, line 16
def send_node
  node_parts[0]
end
single_line?() click to toggle source

Checks whether this is a single line block. This is overridden here because the general version in `Node` does not work for `block` nodes.

@return [Boolean] whether the `block` literal is on a single line

# File lib/rubocop/ast/node/block_node.rb, line 80
def single_line?
  loc.begin.line == loc.end.line
end
void_context?() click to toggle source

Checks whether this node body is a void context.

@return [Boolean] whether the `block` node body is a void context

# File lib/rubocop/ast/node/block_node.rb, line 102
def void_context?
  VOID_CONTEXT_METHODS.include?(send_node.method_name)
end