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 collapse
- VOID_CONTEXT_METHODS =
%i[each tap].freeze
Constants included from MethodIdentifierPredicates
MethodIdentifierPredicates::ENUMERATOR_METHODS, MethodIdentifierPredicates::OPERATOR_METHODS
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::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::REFERENCES, Node::SHORTHAND_ASSIGNMENTS, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES
Instance Method Summary collapse
-
#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.
-
#keywords? ⇒ Boolean
Checks whether the ‘block` literal is delimited by `do`-`end` keywords.
-
#lambda? ⇒ Boolean
Checks whether this ‘block` literal belongs to a lambda.
-
#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?, #enumerator_method?, #method?, #negation_method?, #operator_method?, #predicate_method?, #prefix_bang?, #prefix_not?, #self_receiver?
Methods inherited from Node
#ancestors, #argument?, #assignment?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #child_nodes, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #guard_clause?, #immutable_literal?, #initialize, #keyword?, #last_line, #line_count, #literal?, #mutable_literal?, #node_parts, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent_module_name, #parenthesized_call?, #pure?, #range_type?, #receiver, #reference?, #shorthand_asgn?, #sibling_index, #source, #source_length, #source_range, #special_keyword?, #truthy_literal?, #updated, #value_used?, #variable?
Methods included from NodePattern::Macros
#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first
Methods included from Sexp
Constructor Details
This class inherits a constructor from RuboCop::AST::Node
Instance Method Details
#arguments ⇒ Array<Node>
The arguments of this block.
26 27 28 29 30 31 32 |
# File 'lib/rubocop/ast/node/block_node.rb', line 26 def arguments if numblock_type? [] # Numbered parameters have no block arguments. else node_parts[1] end end |
#arguments? ⇒ Boolean
Checks whether this block takes any arguments.
51 52 53 |
# File 'lib/rubocop/ast/node/block_node.rb', line 51 def arguments? !arguments.empty? end |
#body ⇒ Node?
The body of this block.
37 38 39 |
# File 'lib/rubocop/ast/node/block_node.rb', line 37 def body node_parts[2] end |
#braces? ⇒ Boolean
Checks whether the ‘block` literal is delimited by curly braces.
58 59 60 |
# File 'lib/rubocop/ast/node/block_node.rb', line 58 def braces? loc.end&.is?('}') end |
#closing_delimiter ⇒ String
The closing delimiter for this ‘block` literal.
86 87 88 |
# File 'lib/rubocop/ast/node/block_node.rb', line 86 def closing_delimiter delimiters.last end |
#delimiters ⇒ Array<String>
The delimiters for this ‘block` literal.
72 73 74 |
# File 'lib/rubocop/ast/node/block_node.rb', line 72 def delimiters [loc.begin.source, loc.end.source].freeze end |
#keywords? ⇒ Boolean
Checks whether the ‘block` literal is delimited by `do`-`end` keywords.
65 66 67 |
# File 'lib/rubocop/ast/node/block_node.rb', line 65 def keywords? loc.end&.is?('end') end |
#lambda? ⇒ Boolean
Checks whether this ‘block` literal belongs to a lambda.
109 110 111 |
# File 'lib/rubocop/ast/node/block_node.rb', line 109 def lambda? send_node.method?(:lambda) end |
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
44 45 46 |
# File 'lib/rubocop/ast/node/block_node.rb', line 44 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.
102 103 104 |
# File 'lib/rubocop/ast/node/block_node.rb', line 102 def multiline? !single_line? end |
#opening_delimiter ⇒ String
The opening delimiter for this ‘block` literal.
79 80 81 |
# File 'lib/rubocop/ast/node/block_node.rb', line 79 def opening_delimiter delimiters.first end |
#send_node ⇒ SendNode
The ‘send` node associated with this block.
19 20 21 |
# File 'lib/rubocop/ast/node/block_node.rb', line 19 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.
94 95 96 |
# File 'lib/rubocop/ast/node/block_node.rb', line 94 def single_line? loc.begin.line == loc.end.line end |
#void_context? ⇒ Boolean
Checks whether this node body is a void context.
116 117 118 |
# File 'lib/rubocop/ast/node/block_node.rb', line 116 def void_context? VOID_CONTEXT_METHODS.include?(method_name) end |