Module: RuboCop::AST::MethodDispatchNode
- Extended by:
- NodePattern::Macros
- Includes:
- MethodIdentifierPredicates
- Included in:
- DefinedNode, IndexNode, IndexasgnNode, LambdaNode, SendNode, SuperNode, YieldNode
- 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`
Instance Method Summary collapse
-
#access_modifier? ⇒ Boolean
Checks whether the dispatched method is an access modifier.
-
#arithmetic_operation? ⇒ Boolean
Checks whether this node is an arithmetic operation.
-
#bare_access_modifier? ⇒ Boolean
Checks whether the dispatched method is a bare access modifier that affects all methods defined after the macro.
-
#binary_operation? ⇒ Boolean
Checks whether this is a binary operation.
-
#block_literal? ⇒ Boolean
Whether this method dispatch has an explicit block.
-
#block_node ⇒ BlockNode?
The ‘block` or `numblock` 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.
-
#const_receiver? ⇒ Boolean
Checks whether the explicit receiver of this method dispatch is a ‘const` node.
-
#def_modifier(node = self) ⇒ Node | nil
Checks if this node is part of a chain of ‘def` or `defs` modifiers.
-
#def_modifier?(node = self) ⇒ Boolean
Checks if this node is part of a chain of ‘def` or `defs` modifiers.
-
#dot? ⇒ Boolean
Checks whether the dispatched method uses a dot to connect the receiver and the method name.
-
#double_colon? ⇒ Boolean
Checks whether the dispatched method uses a double colon to connect the receiver and the method name.
-
#implicit_call? ⇒ Boolean
Checks whether the method dispatch is the implicit form of ‘#call`, e.g.
-
#lambda? ⇒ Boolean
Checks whether this is a lambda.
-
#lambda_literal? ⇒ Boolean
Checks whether this is a lambda literal (stabby lambda.).
-
#macro? ⇒ Boolean
Checks whether the dispatched method is a macro method.
-
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
-
#non_bare_access_modifier? ⇒ Boolean
Checks whether the dispatched method is a non-bare access modifier that affects only the method it receives.
-
#receiver ⇒ Node?
The receiving node of the method dispatch.
-
#safe_navigation? ⇒ Boolean
Checks whether the dispatched method uses a safe navigation operator to connect the receiver and the method name.
-
#selector ⇒ Parser::Source::Range
The source range for the method name or keyword that dispatches this call.
-
#self_receiver? ⇒ Boolean
Checks whether the explicit receiver of this method dispatch is ‘self`.
-
#setter_method? ⇒ Boolean
(also: #assignment?)
Checks whether the dispatched method is a setter method.
-
#special_modifier? ⇒ Boolean
Checks whether the dispatched method is a bare ‘private` or `protected` access modifier that affects all methods defined after the macro.
-
#unary_operation? ⇒ Boolean
Checks whether this is a unary operation.
Methods included from NodePattern::Macros
def_node_matcher, def_node_search
Methods included from MethodIdentifierPredicates
#assignment_method?, #bang_method?, #camel_case_method?, #comparison_method?, #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?
Instance Method Details
#access_modifier? ⇒ Boolean
Checks whether the dispatched method is an access modifier.
64 65 66 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 64 def access_modifier? || end |
#arithmetic_operation? ⇒ Boolean
Checks whether this node is an arithmetic operation
175 176 177 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 175 def arithmetic_operation? ARITHMETIC_OPERATORS.include?(method_name) end |
#bare_access_modifier? ⇒ Boolean
Checks whether the dispatched method is a bare access modifier that affects all methods defined after the macro.
73 74 75 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 73 def macro? && end |
#binary_operation? ⇒ Boolean
Checks whether this is a binary operation.
248 249 250 251 252 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 248 def binary_operation? return false unless selector operator_method? && loc.expression.begin_pos != selector.begin_pos end |
#block_literal? ⇒ Boolean
Whether this method dispatch has an explicit block.
167 168 169 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 167 def block_literal? (parent&.block_type? || parent&.numblock_type?) && eql?(parent.send_node) end |
#block_node ⇒ BlockNode?
The ‘block` or `numblock` node associated with this method dispatch, if any.
46 47 48 |
# 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.
100 101 102 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 100 def command?(name) !receiver && method?(name) end |
#const_receiver? ⇒ Boolean
Checks whether the explicit receiver of this method dispatch is a ‘const` node.
152 153 154 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 152 def const_receiver? receiver&.const_type? 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
199 200 201 202 203 204 205 206 207 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 199 def def_modifier(node = self) arg = node.children[2] return unless node.send_type? && node.receiver.nil? && arg.is_a?(::AST::Node) return arg if arg.def_type? || arg.defs_type? def_modifier(arg) end |
#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`
187 188 189 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 187 def def_modifier?(node = self) !!def_modifier(node) end |
#dot? ⇒ Boolean
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`.
119 120 121 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 119 def dot? loc.respond_to?(:dot) && loc.dot&.is?('.') end |
#double_colon? ⇒ Boolean
Checks whether the dispatched method uses a double colon to connect the receiver and the method name.
127 128 129 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 127 def double_colon? loc.respond_to?(:dot) && loc.dot&.is?('::') end |
#implicit_call? ⇒ Boolean
Checks whether the method dispatch is the implicit form of ‘#call`, e.g. `foo.(bar)`.
160 161 162 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 160 def implicit_call? method?(:call) && !selector end |
#lambda? ⇒ Boolean
Checks whether this is a lambda. Some versions of parser parses non-literal lambdas as a method send.
213 214 215 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 213 def lambda? block_literal? && command?(:lambda) end |
#lambda_literal? ⇒ Boolean
Checks whether this is a lambda literal (stabby lambda.)
224 225 226 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 224 def lambda_literal? loc.expression.source == '->' && block_literal? end |
#macro? ⇒ Boolean
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.
57 58 59 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 57 def macro? !receiver && in_macro_scope? end |
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
27 28 29 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 27 def method_name node_parts[1] end |
#non_bare_access_modifier? ⇒ Boolean
Checks whether the dispatched method is a non-bare access modifier that affects only the method it receives.
82 83 84 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 82 def macro? && end |
#receiver ⇒ Node?
The receiving node of the method dispatch.
20 21 22 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 20 def receiver node_parts[0] end |
#safe_navigation? ⇒ Boolean
Checks whether the dispatched method uses a safe navigation operator to connect the receiver and the method name.
135 136 137 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 135 def loc.respond_to?(:dot) && loc.dot&.is?('&.') end |
#selector ⇒ Parser::Source::Range
The source range for the method name or keyword that dispatches this call.
34 35 36 37 38 39 40 |
# 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 |
#self_receiver? ⇒ Boolean
Checks whether the explicit receiver of this method dispatch is ‘self`.
143 144 145 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 143 def self_receiver? receiver&.self_type? end |
#setter_method? ⇒ Boolean Also known as: assignment?
Checks whether the dispatched method is a setter method.
107 108 109 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 107 def setter_method? loc.respond_to?(:operator) && loc.operator end |
#special_modifier? ⇒ Boolean
Checks whether the dispatched method is a bare ‘private` or `protected` access modifier that affects all methods defined after the macro.
91 92 93 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 91 def special_modifier? && SPECIAL_MODIFIERS.include?(source) end |
#unary_operation? ⇒ Boolean
Checks whether this is a unary operation.
235 236 237 238 239 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 235 def unary_operation? return false unless selector operator_method? && loc.expression.begin_pos == selector.begin_pos end |