123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::BlockNode

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Node, NodePattern::Macros, Parser::AST::Node
Instance Chain:
self, MethodIdentifierPredicates, Node, Descendence, Sexp, Parser::AST::Node
Inherits: RuboCop::AST::Node
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

Node - Inherited

ASSIGNMENTS, BASIC_CONDITIONALS, BASIC_LITERALS, COMPARISON_OPERATORS, COMPOSITE_LITERALS, CONDITIONALS, EMPTY_CHILDREN, EMPTY_PROPERTIES, EQUALS_ASSIGNMENTS, FALSEY_LITERALS, GROUP_FOR_TYPE, IMMUTABLE_LITERALS, KEYWORDS, LITERALS, LITERAL_RECURSIVE_METHODS, LITERAL_RECURSIVE_TYPES, LOOP_TYPES, MUTABLE_LITERALS, OPERATOR_KEYWORDS, POST_CONDITION_LOOP_TYPES, REFERENCES, SHORTHAND_ASSIGNMENTS, SPECIAL_KEYWORDS, TRUTHY_LITERALS, VARIABLES

MethodIdentifierPredicates - Included

ENUMERABLE_METHODS, ENUMERATOR_METHODS, NONMUTATING_ARRAY_METHODS, NONMUTATING_BINARY_OPERATOR_METHODS, NONMUTATING_HASH_METHODS, NONMUTATING_OPERATOR_METHODS, NONMUTATING_STRING_METHODS, NONMUTATING_UNARY_OPERATOR_METHODS, OPERATOR_METHODS

Class Method Summary

Node - Inherited

.new,
.def_recursive_literal_predicate

Define a recursive_? predicate method for the given node kind.

NodePattern::Macros - Extended

def_node_matcher

Define a method which applies a pattern to an AST node.

def_node_search

Define a method which recurses over the descendants of an AST node, checking whether any of them match the provided pattern.

Instance Attribute Summary

MethodIdentifierPredicates - Included

#assignment_method?

Checks whether the method is an assignment method.

#bang_method?

Checks whether the method is a bang method.

#camel_case_method?

Checks whether the method is a camel case method, e.g.

#comparison_method?

Checks whether the method is a comparison method.

#const_receiver?

Checks whether the explicit receiver of node is a const node.

#enumerable_method?

Checks whether the method is an Enumerable method.

#enumerator_method?

Checks whether the method is an enumerator method.

#negation_method?

Checks whether this is a negation method, i.e.

#nonmutating_array_method?

Checks whether the method is a nonmutating Array method.

#nonmutating_binary_operator_method?

Checks whether the method is a nonmutating binary operator method.

#nonmutating_hash_method?

Checks whether the method is a nonmutating Hash method.

#nonmutating_operator_method?

Checks whether the method is a nonmutating operator method.

#nonmutating_string_method?

Checks whether the method is a nonmutating String method.

#nonmutating_unary_operator_method?

Checks whether the method is a nonmutating unary operator method.

#operator_method?

Checks whether the method is an operator method.

#predicate_method?

Checks whether the method is a predicate method.

#prefix_bang?

Checks whether this is a prefix bang method, e.g.

#prefix_not?

Checks whether this is a prefix not method, e.g.

#self_receiver?

Checks whether the explicit receiver of this node is self.

Node - Inherited

#any_block_type?, #any_def_type?, #any_match_pattern_type?, #argument?, #argument_type?, #assignment?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #complete?, #conditional?, #empty_source?, #equals_asgn?, #falsey_literal?, #guard_clause?, #immutable_literal?, #keyword?, #literal?,
#loop_keyword?
Note
loop { } is a normal method call and thus not a loop keyword.
#multiline?

Predicates.

#mutable_literal?, #numeric_type?, #operator_keyword?,
#parent

Returns the parent node, or nil if the receiver is a root node.

#parent?, #parenthesized_call?, #post_condition_loop?,
#pure?

Some expressions are evaluated for their value, some for their side effects, and some for both.

#range_type?, #reference?, #root?,
#send_type?

Most nodes are of 'send' type, so this method is defined separately to make this check as fast as possible.

#shorthand_asgn?, #single_line?, #special_keyword?, #truthy_literal?,
#value_used?

Some expressions are evaluated for their value, some for their side effects, and some for both If we know that an expression is useful only for its side effects, that means we can transform it in ways which preserve the side effects, but change the return value So, does the return value of this node matter? If we changed it to (…​; nil), might that affect anything?

#variable?, #begin_value_used?, #case_if_value_used?, #for_value_used?, #while_until_value_used?

Instance Method Summary

MethodIdentifierPredicates - Included

#method?

Checks whether the method name matches the argument.

Node - Inherited

#ancestors

Returns an array of ancestor nodes.

#assignment_or_similar?

Some cops treat the shovel operator as a kind of assignment.

#class_constructor?, #class_definition?, #complete!, #const_name, #defined_module, #defined_module_name,
#each_ancestor

Calls the given block for each ancestor node from parent to root.

#first_line, #global_const?, #lambda?, #lambda_or_proc?, #last_line,
#left_sibling

Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.

#left_siblings

Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.

#line_count,
#loc?

Shortcut to safely check if a location is present.

#loc_is?

Shortcut to safely test a particular location, even if this location does not exist or is nil.

#match_guard_clause?, #module_definition?,
#node_parts

Common destructuring method.

#nonempty_line_count,
#parent_module_name

Searching the AST.

#proc?, #receiver, #recursive_basic_literal?, #recursive_literal?,
#right_sibling

Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.

#right_siblings

Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.

#sibling_index

Returns the index of the receiver node in its siblings.

#source
Note
Some rare nodes may have no source, like s(:args) in foo {}.
#source_length, #source_range, #str_content, #struct_constructor?,
#type?

Determine if the node is one of several node types in a single query Allows specific single node types, as well as "grouped" types (e.g.

#updated

Override Node#updated so that AST::Processor does not try to mutate our ASTs.

#defined_module0, #new_class_or_module_block?, #parent_module_name_for_block, #parent_module_name_for_sclass, #parent_module_name_part, #visit_ancestors

Descendence - Included

#child_nodes

Returns an array of child nodes.

#descendants

Returns an array of descendant nodes.

#each_child_node

Calls the given block for each child node.

#each_descendant

Calls the given block for each descendant node with depth first order.

#each_node

Calls the given block for the receiver and each descendant node in depth-first order.

Sexp - Included

#s

Creates a Node with type type and children children.

Constructor Details

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

Instance Attribute Details

#argumentsArray<Node> (readonly)

The arguments of this block. Note that if the block has destructured arguments, arguments will return a mlhs node, whereas #argument_list will return only actual argument nodes.

[ GitHub ]

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

def arguments
  if block_type?
    node_parts[1]
  else
    [].freeze # Numblocks and itblocks have no explicit block arguments.
  end
end

#arguments?Boolean (readonly)

Checks whether this block takes any arguments.

Returns:

  • (Boolean)

    whether this block node takes any arguments

[ GitHub ]

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

def arguments?
  !arguments.empty?
end

#braces?Boolean (readonly)

Checks whether the block literal is delimited by curly braces.

Returns:

  • (Boolean)

    whether the block literal is enclosed in braces

[ GitHub ]

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

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

#keywords?Boolean (readonly)

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

Returns:

  • (Boolean)

    whether the block literal is enclosed in do-end

[ GitHub ]

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

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

#lambda?Boolean (readonly)

Checks whether this block literal belongs to a lambda.

Returns:

  • (Boolean)

    whether the block literal belongs to a lambda

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 147

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

#multiline?Boolean (readonly)

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

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 140

def multiline?
  !single_line?
end

#single_line?Boolean (readonly)

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

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 132

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

#void_context?Boolean (readonly)

Checks whether this node body is a void context.

Returns:

  • (Boolean)

    whether the block node body is a void context

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 154

def void_context?
  VOID_CONTEXT_METHODS.include?(method_name)
end

Instance Method Details

#argument_listArray<Node>

Returns a collection of all descendants of this node that are argument type nodes. See ArgsNode#argument_list for details.

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 62

def argument_list
  if numblock_type?
    numbered_arguments
  elsif itblock_type?
    IT_BLOCK_ARGUMENT
  else
    arguments.argument_list
  end
end

#bodyNode?

The body of this block.

Returns:

  • (Node, nil)

    the body of the block node or nil

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 75

def body
  node_parts[2]
end

#closing_delimiterString

The closing delimiter for this block literal.

Returns:

  • (String)

    the closing delimiter for the block literal

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 124

def closing_delimiter
  delimiters.last
end

#delimitersArray<String>

The delimiters for this block literal.

Returns:

  • (Array<String>)

    the delimiters for the block literal

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 110

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

#first_argumentNode?

A shorthand for getting the first argument of this block. Equivalent to arguments.first.

Returns:

  • (Node, nil)

    the first argument of this block, or nil if there are no arguments

[ GitHub ]

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

def first_argument
  arguments[0]
end

#last_argumentNode?

A shorthand for getting the last argument of this block. Equivalent to arguments.last.

Returns:

  • (Node, nil)

    the last argument of this block, or nil if there are no arguments

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 40

def last_argument
  arguments[-1]
end

#method_nameSymbol

The name of the dispatched method as a symbol.

Returns:

  • (Symbol)

    the name of the dispatched method

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 82

def method_name
  send_node.method_name
end

#numbered_arguments (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 160

def numbered_arguments
  max_param = children[1]
  1.upto(max_param).map do |i|
    ArgNode.new(:arg, [:"_#{i}"])
  end.freeze
end

#opening_delimiterString

The opening delimiter for this block literal.

Returns:

  • (String)

    the opening delimiter for the block literal

[ GitHub ]

  
# File 'lib/rubocop/ast/node/block_node.rb', line 117

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

[ GitHub ]

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

def send_node
  node_parts[0]
end