Class: RuboCop::AST::BlockNode
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Node ,
NodePattern::Macros ,
Parser::AST::Node
|
|
Instance Chain:
|
|
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
-
IT_BLOCK_ARGUMENT =
private
# File 'lib/rubocop/ast/node/block_node.rb', line 14[ArgNode.new(:arg, [:it])].freeze
-
VOID_CONTEXT_METHODS =
private
# File 'lib/rubocop/ast/node/block_node.rb', line 16%i[each tap].freeze
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 |
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
-
#arguments ⇒ Array<Node>
readonly
The arguments of this block.
-
#arguments? ⇒ Boolean
readonly
Checks whether this block takes any arguments.
-
#braces? ⇒ Boolean
readonly
Checks whether the
block
literal is delimited by curly braces. -
#keywords? ⇒ Boolean
readonly
Checks whether the
block
literal is delimited bydo
-end
keywords. -
#lambda? ⇒ Boolean
readonly
Checks whether this
block
literal belongs to a lambda. -
#multiline? ⇒ Boolean
readonly
Checks whether this is a multiline block.
-
#single_line? ⇒ Boolean
readonly
Checks whether this is a single line block.
-
#void_context? ⇒ Boolean
readonly
Checks whether this node body is a void context.
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 |
#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 |
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? |
|
||
#multiline? | Predicates. |
||
#mutable_literal?, #numeric_type?, #operator_keyword?, | |||
#parent | Returns the parent node, or |
||
#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 |
||
#variable?, #begin_value_used?, #case_if_value_used?, #for_value_used?, #while_until_value_used? |
Instance Method Summary
-
#argument_list ⇒ Array<Node>
Returns a collection of all descendants of this node that are argument type nodes.
-
#body ⇒ Node?
The body of this block.
-
#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.
-
#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.
-
#opening_delimiter ⇒ String
The opening delimiter for this
block
literal. -
#send_node ⇒ SendNode
The
send
node associated with this block. - #numbered_arguments private
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 |
||
#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 |
|
||
#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 |
||
#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
Constructor Details
This class inherits a constructor from RuboCop::AST::Node
Instance Attribute Details
#arguments ⇒ Array
<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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# File 'lib/rubocop/ast/node/block_node.rb', line 154
def void_context? VOID_CONTEXT_METHODS.include?(method_name) end
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.
# 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
#body ⇒ Node?
The body of this block.
# File 'lib/rubocop/ast/node/block_node.rb', line 75
def body node_parts[2] end
#closing_delimiter ⇒ String
The closing delimiter for this block
literal.
# 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.
# 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
.
# File 'lib/rubocop/ast/node/block_node.rb', line 31
def first_argument arguments[0] end
#last_argument ⇒ Node?
A shorthand for getting the last argument of this block.
Equivalent to arguments.last
.
# 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.
# File 'lib/rubocop/ast/node/block_node.rb', line 82
def method_name send_node.method_name end
#numbered_arguments (private)
[ GitHub ]
#opening_delimiter ⇒ String
The opening delimiter for this block
literal.
# 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.
# File 'lib/rubocop/ast/node/block_node.rb', line 22
def send_node node_parts[0] end