Class: RuboCop::AST::BlockNode
- Includes:
- MethodIdentifierPredicates
- 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::ASSIGNMENTS, Node::BASIC_CONDITIONALS, Node::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::CONDITIONALS, Node::EQUALS_ASSIGNMENTS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::LOOP_TYPES, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::POST_CONDITION_LOOP_TYPES, Node::REFERENCES, Node::SHORTHAND_ASSIGNMENTS, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES
Instance Method Summary collapse
-
#argument_list ⇒ Array<Node>
Returns a collection of all descendants of this node that are argument type nodes.
-
#arguments ⇒ Array<Node>
The arguments of this block.
-
#arguments? ⇒ Boolean
Checks whether this block takes any arguments.
-
#body ⇒ Node?
The body of this block.
-
#braces? ⇒ Boolean
Checks whether the ‘block` literal is delimited by curly braces.
-
#closing_delimiter ⇒ String
The closing delimiter for this ‘block` literal.
-
#delimiters ⇒ Array<String>
The delimiters for this ‘block` literal.
-
#first_argument ⇒ Node?
A shorthand for getting the first argument of this block.
-
#keywords? ⇒ Boolean
Checks whether the ‘block` literal is delimited by `do`-`end` keywords.
-
#lambda? ⇒ Boolean
Checks whether this ‘block` literal belongs to a lambda.
-
#last_argument ⇒ Node?
A shorthand for getting the last argument of this block.
-
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
-
#multiline? ⇒ Boolean
Checks whether this is a multiline block.
-
#opening_delimiter ⇒ String
The opening delimiter for this ‘block` literal.
-
#send_node ⇒ SendNode
The ‘send` node associated with this block.
-
#single_line? ⇒ Boolean
Checks whether this is a single line block.
-
#void_context? ⇒ Boolean
Checks whether this node body is a void context.
Methods included from MethodIdentifierPredicates
#assignment_method?, #bang_method?, #camel_case_method?, #comparison_method?, #const_receiver?, #enumerable_method?, #enumerator_method?, #method?, #negation_method?, #nonmutating_array_method?, #nonmutating_binary_operator_method?, #nonmutating_hash_method?, #nonmutating_operator_method?, #nonmutating_string_method?, #nonmutating_unary_operator_method?, #operator_method?, #predicate_method?, #prefix_bang?, #prefix_not?, #self_receiver?
Methods inherited from Node
#ancestors, #any_block_type?, #argument?, #argument_type?, #assignment?, #assignment_or_similar?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #class_constructor?, #class_definition?, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #each_ancestor, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #global_const?, #guard_clause?, #immutable_literal?, #initialize, #keyword?, #lambda_or_proc?, #last_line, #left_sibling, #left_siblings, #line_count, #literal?, #loc?, #loc_is?, #loop_keyword?, #match_guard_clause?, #module_definition?, #mutable_literal?, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent?, #parent_module_name, #parenthesized_call?, #post_condition_loop?, #proc?, #pure?, #range_type?, #receiver, #recursive_basic_literal?, #recursive_literal?, #reference?, #right_sibling, #right_siblings, #root?, #send_type?, #shorthand_asgn?, #sibling_index, #source, #source_length, #source_range, #special_keyword?, #str_content, #struct_constructor?, #truthy_literal?, #type?, #updated, #value_used?, #variable?
Methods included from NodePattern::Macros
#def_node_matcher, #def_node_search
Methods included from Descendence
#child_nodes, #descendants, #each_child_node, #each_descendant, #each_node
Methods included from Sexp
Constructor Details
This class inherits a constructor from RuboCop::AST::Node
Instance Method Details
#argument_list ⇒ Array<Node>
Returns a collection of all descendants of this node that are argument type nodes. See ‘ArgsNode#argument_list` for details.
62 63 64 65 66 67 68 69 70 |
# 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 |
#arguments ⇒ Array<Node>
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.
50 51 52 53 54 55 56 |
# 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
Checks whether this block takes any arguments.
89 90 91 |
# File 'lib/rubocop/ast/node/block_node.rb', line 89 def arguments? !arguments.empty? end |
#body ⇒ Node?
The body of this block.
75 76 77 |
# File 'lib/rubocop/ast/node/block_node.rb', line 75 def body node_parts[2] end |
#braces? ⇒ Boolean
Checks whether the ‘block` literal is delimited by curly braces.
96 97 98 |
# File 'lib/rubocop/ast/node/block_node.rb', line 96 def braces? loc.end.is?('}') end |
#closing_delimiter ⇒ String
The closing delimiter for this ‘block` literal.
124 125 126 |
# File 'lib/rubocop/ast/node/block_node.rb', line 124 def closing_delimiter delimiters.last end |
#delimiters ⇒ Array<String>
The delimiters for this ‘block` literal.
110 111 112 |
# File 'lib/rubocop/ast/node/block_node.rb', line 110 def delimiters [loc.begin.source, loc.end.source].freeze end |
#first_argument ⇒ Node?
A shorthand for getting the first argument of this block. Equivalent to ‘arguments.first`.
31 32 33 |
# File 'lib/rubocop/ast/node/block_node.rb', line 31 def first_argument arguments[0] end |
#keywords? ⇒ Boolean
Checks whether the ‘block` literal is delimited by `do`-`end` keywords.
103 104 105 |
# File 'lib/rubocop/ast/node/block_node.rb', line 103 def keywords? loc.end.is?('end') end |
#lambda? ⇒ Boolean
Checks whether this ‘block` literal belongs to a lambda.
147 148 149 |
# File 'lib/rubocop/ast/node/block_node.rb', line 147 def lambda? send_node.method?(:lambda) end |
#last_argument ⇒ Node?
A shorthand for getting the last argument of this block. Equivalent to ‘arguments.last`.
40 41 42 |
# File 'lib/rubocop/ast/node/block_node.rb', line 40 def last_argument arguments[-1] end |
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
82 83 84 |
# File 'lib/rubocop/ast/node/block_node.rb', line 82 def method_name send_node.method_name 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.
140 141 142 |
# File 'lib/rubocop/ast/node/block_node.rb', line 140 def multiline? !single_line? end |
#opening_delimiter ⇒ String
The opening delimiter for this ‘block` literal.
117 118 119 |
# File 'lib/rubocop/ast/node/block_node.rb', line 117 def opening_delimiter delimiters.first end |
#send_node ⇒ SendNode
The ‘send` node associated with this block.
22 23 24 |
# File 'lib/rubocop/ast/node/block_node.rb', line 22 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.
132 133 134 |
# File 'lib/rubocop/ast/node/block_node.rb', line 132 def single_line? loc.begin.line == loc.end.line end |
#void_context? ⇒ Boolean
Checks whether this node body is a void context.
154 155 156 |
# File 'lib/rubocop/ast/node/block_node.rb', line 154 def void_context? VOID_CONTEXT_METHODS.include?(method_name) end |