Class: RuboCop::AST::BlockNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rubocop/ast/node/block_node.rb

Overview

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.

Constant Summary

Constants inherited from Node

Node::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::REFERENCES, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES

Instance Method Summary collapse

Methods inherited from Node

#ancestors, #argument?, #asgn_method_call?, #basic_literal?, #binary_operation?, #chained?, #child_nodes, #complete!, #complete?, #const_name, def_matcher, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #falsey_literal?, #immutable_literal?, #initialize, #keyword?, #keyword_bang?, #keyword_not?, #literal?, #mutable_literal?, #numeric_type?, #operator_keyword?, #parent, #parent_module_name, #pure?, #receiver, #reference?, #sibling_index, #source, #source_range, #special_keyword?, #truthy_literal?, #unary_operation?, #updated, #value_used?, #variable?

Methods included from Sexp

#s

Constructor Details

This class inherits a constructor from RuboCop::AST::Node

Instance Method Details

#argumentsArray<Node>

The arguments of this block.

Returns:



22
23
24
# File 'lib/rubocop/ast/node/block_node.rb', line 22

def arguments
  node_parts[1]
end

#arguments?Boolean

Checks whether this block takes any arguments.

Returns:

  • (Boolean)

    whether this ‘block` node takes any arguments



36
37
38
# File 'lib/rubocop/ast/node/block_node.rb', line 36

def arguments?
  !arguments.empty?
end

#bodyNode?

The body of this block.

Returns:

  • (Node, nil)

    the body of the ‘block` node or `nil`



29
30
31
# File 'lib/rubocop/ast/node/block_node.rb', line 29

def body
  node_parts[2]
end

#braces?Boolean

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

Returns:

  • (Boolean)

    whether the ‘block` literal is enclosed in braces



43
44
45
# File 'lib/rubocop/ast/node/block_node.rb', line 43

def braces?
  loc.end && loc.end.is?('}')
end

#closing_delimiterString

The closing delimiter for this ‘block` literal.

Returns:

  • (String)

    the closing delimiter for the ‘block` literal



71
72
73
# File 'lib/rubocop/ast/node/block_node.rb', line 71

def closing_delimiter
  delimiters.last
end

#delimitersArray<String>

The delimiters for this ‘block` literal.

Returns:

  • (Array<String>)

    the delimiters for the ‘block` literal



57
58
59
# File 'lib/rubocop/ast/node/block_node.rb', line 57

def delimiters
  [loc.begin.source, loc.end.source].freeze
end

#keywords?Boolean

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

Returns:

  • (Boolean)

    whether the ‘block` literal is enclosed in `do`-`end`



50
51
52
# File 'lib/rubocop/ast/node/block_node.rb', line 50

def keywords?
  loc.end && loc.end.is?('end')
end

#lambda?Boolean

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

Returns:

  • (Boolean)

    whether the ‘block` literal belongs to a lambda



94
95
96
# File 'lib/rubocop/ast/node/block_node.rb', line 94

def lambda?
  send_node.method?(:lambda)
end

#multiline?Boolean

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

Returns:

  • (Boolean)

    whether the ‘block` literal is on a several lines



87
88
89
# File 'lib/rubocop/ast/node/block_node.rb', line 87

def multiline?
  !single_line?
end

#node_partsArray

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

Returns:

  • (Array)

    the different parts of the ‘block` node



102
103
104
# File 'lib/rubocop/ast/node/block_node.rb', line 102

def node_parts
  to_a
end

#opening_delimiterString

The opening delimiter for this ‘block` literal.

Returns:

  • (String)

    the opening delimiter for the ‘block` literal



64
65
66
# File 'lib/rubocop/ast/node/block_node.rb', line 64

def opening_delimiter
  delimiters.first
end

#send_nodeSendNode

The ‘send` node associated with this block.

Returns:

  • (SendNode)

    the ‘send` node associated with the `block` node



15
16
17
# File 'lib/rubocop/ast/node/block_node.rb', line 15

def send_node
  node_parts[0]
end

#single_line?Boolean

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

Returns:

  • (Boolean)

    whether the ‘block` literal is on a single line



79
80
81
# File 'lib/rubocop/ast/node/block_node.rb', line 79

def single_line?
  loc.begin.line == loc.end.line
end