Module: RuboCop::AST::MethodDispatchNode
- Extended by:
- NodePattern::Macros
- Includes:
- MethodIdentifierPredicates
- 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?`
Constant Summary collapse
- ARITHMETIC_OPERATORS =
%i[+ - * / % **].freeze
Constants included from MethodIdentifierPredicates
RuboCop::AST::MethodIdentifierPredicates::ENUMERATOR_METHODS, RuboCop::AST::MethodIdentifierPredicates::OPERATOR_METHODS
Instance Method Summary collapse
-
#access_modifier? ⇒ Boolean
Checks whether the dispatched method is an access modifier.
-
#arguments ⇒ Array<Node>
An array containing the arguments of the dispatched method.
-
#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? ⇒ Bookean
Checks whether this is a binary operation.
-
#block_literal? ⇒ Boolean
Whether this method dispatch has an explicit block.
-
#block_node ⇒ BlockNode?
The ‘block` 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? ⇒ Boolean
Checks if this node is part of a chain of ‘def` 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.
-
#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.
-
#unary_operation? ⇒ Boolean
Checks whether this is a unary operation.
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 MethodIdentifierPredicates
#assignment_method?, #bang_method?, #camel_case_method?, #comparison_method?, #enumerator_method?, #method?, #negation_method?, #operator_method?, #predicate_method?, #prefix_bang?, #prefix_not?
Instance Method Details
#access_modifier? ⇒ Boolean
Checks whether the dispatched method is an access modifier.
56 57 58 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 56 def access_modifier? || end |
#arguments ⇒ Array<Node>
An array containing the arguments of the dispatched method.
30 31 32 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 30 def arguments node_parts[2..-1] end |
#arithmetic_operation? ⇒ Boolean
Checks whether this node is an arithmetic operation
150 151 152 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 150 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.
65 66 67 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 65 def macro? && end |
#binary_operation? ⇒ Bookean
Checks whether this is a binary operation.
205 206 207 208 209 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 205 def binary_operation? return false unless loc.selector operator_method? && loc.expression.begin_pos != loc.selector.begin_pos end |
#block_literal? ⇒ Boolean
Whether this method dispatch has an explicit block.
142 143 144 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 142 def block_literal? parent&.block_type? && eql?(parent.send_node) end |
#block_node ⇒ BlockNode?
The ‘block` node associated with this method dispatch, if any.
38 39 40 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 38 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.
83 84 85 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 83 def command?(name) !receiver && method?(name) end |
#const_receiver? ⇒ Boolean
Checks whether the explicit receiver of this method dispatch is a ‘const` node.
127 128 129 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 127 def const_receiver? receiver&.const_type? end |
#def_modifier? ⇒ Boolean
Checks if this node is part of a chain of ‘def` modifiers.
161 162 163 164 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 161 def def_modifier? send_type? && [self, *each_descendant(:send)].any?(&:adjacent_def_modifier?) 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`.
102 103 104 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 102 def dot? loc.respond_to?(:dot) && loc.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.
110 111 112 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 110 def double_colon? loc.respond_to?(:dot) && loc.dot && loc.dot.is?('::') end |
#implicit_call? ⇒ Boolean
Checks whether the method dispatch is the implicit form of ‘#call`, e.g. `foo.(bar)`.
135 136 137 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 135 def implicit_call? method?(:call) && !loc.selector end |
#lambda? ⇒ Boolean
Checks whether this is a lambda. Some versions of parser parses non-literal lambdas as a method send.
170 171 172 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 170 def lambda? block_literal? && command?(:lambda) end |
#lambda_literal? ⇒ Boolean
Checks whether this is a lambda literal (stabby lambda.)
181 182 183 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 181 def lambda_literal? block_literal? && loc.expression && loc.expression.source == '->' 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.
49 50 51 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 49 def macro? !receiver && macro_scope? end |
#method_name ⇒ Symbol
The name of the dispatched method as a symbol.
23 24 25 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 23 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.
74 75 76 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 74 def macro? && end |
#receiver ⇒ Node?
The receiving node of the method dispatch.
16 17 18 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 16 def receiver node_parts[0] end |
#self_receiver? ⇒ Boolean
Checks whether the explicit receiver of this method dispatch is ‘self`.
118 119 120 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 118 def self_receiver? receiver&.self_type? end |
#setter_method? ⇒ Boolean Also known as: assignment?
Checks whether the dispatched method is a setter method.
90 91 92 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 90 def setter_method? loc.respond_to?(:operator) && loc.operator end |
#unary_operation? ⇒ Boolean
Checks whether this is a unary operation.
192 193 194 195 196 |
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 192 def unary_operation? return false unless loc.selector operator_method? && loc.expression.begin_pos == loc.selector.begin_pos end |