module RuboCop::AST::MethodDispatchNode
Common functionality for nodes that are a kind of method dispatch: `send`, `csend`, `super`, `zsuper`, `yield`, `defined?`, and (modern only): `index`, `indexasgn`, `lambda`
Constants
- ARITHMETIC_OPERATORS
- SPECIAL_MODIFIERS
Public Instance Methods
Checks whether the dispatched method is an access modifier.
@return [Boolean] whether the dispatched method is an access modifier
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 53 def access_modifier? bare_access_modifier? || non_bare_access_modifier? end
Checks whether this node is an arithmetic operation
@return [Boolean] whether the dispatched method is an arithmetic
operation
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 164 def arithmetic_operation? ARITHMETIC_OPERATORS.include?(method_name) end
Checks whether the dispatched method is a bare access modifier that affects all methods defined after the macro.
@return [Boolean] whether the dispatched method is a bare
access modifier
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 62 def bare_access_modifier? macro? && bare_access_modifier_declaration? end
Checks whether this is a binary operation.
@example
foo + bar
@return [Bookean] whether this method is a binary operation
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 237 def binary_operation? return false unless loc.selector operator_method? && loc.expression.begin_pos != loc.selector.begin_pos end
Whether this method dispatch has an explicit block.
@return [Boolean] whether the dispatched method has a block
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 156 def block_literal? parent&.block_type? && eql?(parent.send_node) end
The `block` node associated with this method dispatch, if any.
@return [BlockNode, nil] the `block` node associated with this method
call or `nil`
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 35 def block_node parent if block_literal? end
Checks whether the name of the dispatched method matches the argument and has an implicit receiver.
@param [Symbol, String] name the method name to check for @return [Boolean] whether the method name matches the argument
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 89 def command?(name) !receiver && method?(name) end
Checks whether the explicit receiver of this method dispatch is a `const` node.
@return [Boolean] whether the receiver of this method dispatch
is a `const` node
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 141 def const_receiver? receiver&.const_type? end
Checks if this node is part of a chain of `def` or `defs` modifiers.
@example
private def foo; end
@return [Node | nil] returns the `def|defs` node this is a modifier for, or `nil` if it isn't a def modifier
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 188 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
Checks if this node is part of a chain of `def` or `defs` modifiers.
@example
private def foo; end
@return wether the `def|defs` node is a modifier or not. See also `def_modifier` that returns the node or `nil`
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 176 def def_modifier?(node = self) !!def_modifier(node) end
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`.
@return [Boolean] whether the method was called with a connecting dot
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 108 def dot? loc.respond_to?(:dot) && loc.dot && loc.dot.is?('.') end
Checks whether the dispatched method uses a double colon to connect the receiver and the method name.
@return [Boolean] whether the method was called with a connecting dot
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 116 def double_colon? loc.respond_to?(:dot) && loc.dot && loc.dot.is?('::') end
Checks whether the method dispatch is the implicit form of `#call`, e.g. `foo.(bar)`.
@return [Boolean] whether the method is the implicit form of `#call`
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 149 def implicit_call? method?(:call) && !loc.selector end
Checks whether this is a lambda. Some versions of parser parses non-literal lambdas as a method send.
@return [Boolean] whether this method is a lambda
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 202 def lambda? block_literal? && command?(:lambda) end
Checks whether this is a lambda literal (stabby lambda.)
@example
-> (foo) { bar }
@return [Boolean] whether this method is a lambda literal
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 213 def lambda_literal? block_literal? && loc.expression && loc.expression.source == '->' end
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.
@note This does not include DSLs that use nested blocks, like RSpec
@return [Boolean] whether the dispatched method is a macro method
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 46 def macro? !receiver && in_macro_scope? end
The name of the dispatched method as a symbol.
@return [Symbol] the name of the dispatched method
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 27 def method_name node_parts[1] end
Checks whether the dispatched method is a non-bare access modifier that affects only the method it receives.
@return [Boolean] whether the dispatched method is a non-bare
access modifier
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 71 def non_bare_access_modifier? macro? && non_bare_access_modifier_declaration? end
The receiving node of the method dispatch.
@return [Node, nil] the receiver of the dispatched method or `nil`
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 20 def receiver node_parts[0] end
Checks whether the explicit receiver of this method dispatch is `self`.
@return [Boolean] whether the receiver of this method dispatch is `self`
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 132 def self_receiver? receiver&.self_type? end
Checks whether the dispatched method is a setter method.
@return [Boolean] whether the dispatched method is a setter
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 96 def setter_method? loc.respond_to?(:operator) && loc.operator end
Checks whether the dispatched method is a bare `private` or `protected` access modifier that affects all methods defined after the macro.
@return [Boolean] whether the dispatched method is a bare
`private` or `protected` access modifier
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 80 def special_modifier? bare_access_modifier? && SPECIAL_MODIFIERS.include?(source) end
Checks whether this is a unary operation.
@example
-foo
@return [Boolean] whether this method is a unary operation
# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 224 def unary_operation? return false unless loc.selector operator_method? && loc.expression.begin_pos == loc.selector.begin_pos end