Module: RuboCop::AST::MethodDispatchNode
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
NodePattern::Macros
|
|
Instance Chain:
|
|
Defined in: | lib/rubocop/ast/node/mixin/method_dispatch_node.rb |
Overview
Common functionality for nodes that are a kind of method dispatch:
send
, csend
, super
, zsuper
, yield
, defined?
,
and (modern only): index
, indexasgn
, lambda
Constant Summary
-
ARITHMETIC_OPERATORS =
private
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 12%i[+ - * / % **].freeze
-
SPECIAL_MODIFIERS =
private
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 14%w[private protected].freeze
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
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
-
#access_modifier? ⇒ Boolean
readonly
Checks whether the dispatched method is an access modifier.
-
#arithmetic_operation? ⇒ Boolean
readonly
Checks whether this node is an arithmetic operation.
-
#assignment?
readonly
Alias for #setter_method?.
-
#bare_access_modifier? ⇒ Boolean
readonly
Checks whether the dispatched method is a bare access modifier that affects all methods defined after the macro.
-
#binary_operation? ⇒ Boolean
readonly
Checks whether this is a binary operation.
-
#block_literal? ⇒ Boolean
readonly
Whether this method dispatch has an explicit block.
-
#const_receiver? ⇒ Boolean
readonly
Checks whether the explicit receiver of this method dispatch is a
const
node. -
#dot? ⇒ Boolean
readonly
Checks whether the dispatched method uses a dot to connect the receiver and the method name.
-
#double_colon? ⇒ Boolean
readonly
Checks whether the dispatched method uses a double colon to connect the receiver and the method name.
-
#implicit_call? ⇒ Boolean
readonly
Checks whether the method dispatch is the implicit form of
#call
, e.g. -
#lambda? ⇒ Boolean
readonly
Checks whether this is a lambda.
-
#lambda_literal? ⇒ Boolean
readonly
Checks whether this is a lambda literal (stabby lambda.).
-
#macro? ⇒ Boolean
readonly
Checks whether the dispatched method is a macro method.
-
#non_bare_access_modifier? ⇒ Boolean
readonly
Checks whether the dispatched method is a non-bare access modifier that affects only the method it receives.
-
#safe_navigation? ⇒ Boolean
readonly
Checks whether the dispatched method uses a safe navigation operator to connect the receiver and the method name.
-
#self_receiver? ⇒ Boolean
readonly
Checks whether the explicit receiver of this method dispatch is
self
. -
#setter_method? ⇒ Boolean
(also: #assignment?)
readonly
Checks whether the dispatched method is a setter method.
-
#special_modifier? ⇒ Boolean
readonly
Checks whether the dispatched method is a bare
private
orprotected
access modifier that affects all methods defined after the macro. -
#unary_operation? ⇒ Boolean
readonly
Checks whether this is a unary operation.
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 |
Instance Method Summary
-
#block_node ⇒ BlockNode?
The
block
,numblock
, oritblock
node associated with this method dispatch, if any. -
#command?(name) ⇒ Boolean
Checks whether the name of the dispatched method matches the argument and has an implicit receiver.
-
#def_modifier(node = self) ⇒ Node | nil
Checks if this node is part of a chain of
def
ordefs
modifiers. -
#def_modifier?(node = self) ⇒ Boolean
Checks if this node is part of a chain of
def
ordefs
modifiers. -
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
-
#receiver ⇒ Node?
The receiving node of the method dispatch.
-
#selector ⇒ Parser::Source::Range
The source range for the method name or keyword that dispatches this call.
- #adjacent_def_modifier?(node = self) private
- #bare_access_modifier_declaration?(node = self) private
- #in_macro_scope?(node = self) private
- #non_bare_access_modifier_declaration?(node = self) private
MethodIdentifierPredicates
- Included
#method? | Checks whether the method name matches the argument. |
Instance Attribute Details
#access_modifier? ⇒ Boolean
(readonly)
Checks whether the dispatched method is an access modifier.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 64
def access_modifier? || end
#arithmetic_operation? ⇒ Boolean
(readonly)
Checks whether this node is an arithmetic operation
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 175
def arithmetic_operation? ARITHMETIC_OPERATORS.include?(method_name) end
#assignment? (readonly)
Alias for #setter_method?.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 110
alias assignment? setter_method?
#bare_access_modifier? ⇒ Boolean
(readonly)
Checks whether the dispatched method is a bare access modifier that affects all methods defined after the macro.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 73
def macro? && end
#binary_operation? ⇒ Boolean
(readonly)
Checks whether this is a binary operation.
#block_literal? ⇒ Boolean
(readonly)
Whether this method dispatch has an explicit block.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 167
def block_literal? parent&.any_block_type? && eql?(parent.send_node) end
#const_receiver? ⇒ Boolean
(readonly)
Checks whether the explicit receiver of this method dispatch is a
const
node.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 152
def const_receiver? receiver&.const_type? end
#dot? ⇒ Boolean
(readonly)
Checks whether the dispatched method uses a dot to connect the receiver and the method name.
This is useful for comparison operators, which can be called either
with or without a dot, i.e. foo == bar
or foo.== bar
.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 119
def dot? loc_is?(:dot, '.') end
#double_colon? ⇒ Boolean
(readonly)
Checks whether the dispatched method uses a double colon to connect the receiver and the method name.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 127
def double_colon? loc_is?(:dot, '::') end
#implicit_call? ⇒ Boolean
(readonly)
Checks whether the method dispatch is the implicit form of #call
,
e.g. foo.(bar)
.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 160
def implicit_call? method?(:call) && !selector end
#lambda? ⇒ Boolean
(readonly)
Checks whether this is a lambda. Some versions of parser parses non-literal lambdas as a method send.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 212
def lambda? block_literal? && command?(:lambda) end
#lambda_literal? ⇒ Boolean
(readonly)
Checks whether this is a lambda literal (stabby lambda.)
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 223
def lambda_literal? loc.expression.source == '->' && block_literal? end
#macro? ⇒ Boolean
(readonly)
This does not include DSLs that use nested blocks, like RSpec
Checks whether the dispatched method is a macro method. A macro method is defined as a method that sits in a class, module, or block body and has an implicit receiver.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 57
def macro? !receiver && in_macro_scope? end
#non_bare_access_modifier? ⇒ Boolean
(readonly)
Checks whether the dispatched method is a non-bare access modifier that affects only the method it receives.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 82
def macro? && end
#self_receiver? ⇒ Boolean
(readonly)
Checks whether the explicit receiver of this method dispatch is
self
.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 143
def self_receiver? receiver&.self_type? end
#setter_method? ⇒ Boolean
(readonly)
Also known as: #assignment?
Checks whether the dispatched method is a setter method.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 107
def setter_method? loc?(:operator) end
#special_modifier? ⇒ Boolean
(readonly)
Checks whether the dispatched method is a bare private
or protected
access modifier that affects all methods defined after the macro.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 91
def special_modifier? && SPECIAL_MODIFIERS.include?(source) end
#unary_operation? ⇒ Boolean
(readonly)
Checks whether this is a unary operation.
Instance Method Details
#adjacent_def_modifier?(node = self) (private)
[ GitHub ]# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 272
def_node_matcher :adjacent_def_modifier?, <<~PATTERN (send nil? _ (any_def ...)) PATTERN
#bare_access_modifier_declaration?(node = self) (private)
[ GitHub ]# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 277
def_node_matcher :, <<~PATTERN (send nil? {:public :protected :private :module_function}) PATTERN
#block_node ⇒ BlockNode?
The block
, numblock
, or itblock
node associated with this method dispatch, if any.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 46
def block_node parent if block_literal? end
#command?(name) ⇒ Boolean
Checks whether the name of the dispatched method matches the argument and has an implicit receiver.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 100
def command?(name) !receiver && method?(name) end
#def_modifier(node = self) ⇒ Node | nil
Checks if this node is part of a chain of def
or defs
modifiers.
or nil
if it isn’t a def modifier
#def_modifier?(node = self) ⇒ Boolean
Checks if this node is part of a chain of def
or defs
modifiers.
See also #def_modifier that returns the node or nil
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 187
def def_modifier?(node = self) !!def_modifier(node) end
#in_macro_scope?(node = self) (private)
[ GitHub ]# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 256
def_node_matcher :in_macro_scope?, <<~PATTERN { root? # Either a root node, ^{ # or the parent is... sclass class module class_constructor? # a class-like node [ { # or some "wrapper" kwbegin begin any_block (if _condition <%0 _>) # note: we're excluding the condition of `if` nodes } #in_macro_scope? # that is itself in a macro scope ] } } PATTERN
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 27
def method_name node_parts[1] end
#non_bare_access_modifier_declaration?(node = self) (private)
[ GitHub ]# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 282
def_node_matcher :, <<~PATTERN (send nil? {:public :protected :private :module_function} _+) PATTERN
#receiver ⇒ Node?
The receiving node of the method dispatch.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 20
def receiver node_parts[0] end
#selector ⇒ Parser::Source::Range
The source range for the method name or keyword that dispatches this call.
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 34
def selector if loc.respond_to? :keyword loc.keyword else loc.selector end end