module RuboCop::AST::MethodDispatchNode

Common functionality for nodes that are a kind of method dispatch: `send`, `csend`, `super`, `zsuper`, `yield`, `defined?`

Constants

ARITHMETIC_OPERATORS
SPECIAL_MODIFIERS

Public Instance Methods

access_modifier?() click to toggle source

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 57
def access_modifier?
  bare_access_modifier? || non_bare_access_modifier?
end
arguments() click to toggle source

An array containing the arguments of the dispatched method.

@return [Array<Node>] the arguments of the dispatched method

# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 31
def arguments
  node_parts[2..-1]
end
arithmetic_operation?() click to toggle source

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 160
def arithmetic_operation?
  ARITHMETIC_OPERATORS.include?(method_name)
end
assignment?()
Alias for: setter_method?
bare_access_modifier?() click to toggle source

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 66
def bare_access_modifier?
  macro? && bare_access_modifier_declaration?
end
binary_operation?() click to toggle source

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 215
def binary_operation?
  return false unless loc.selector

  operator_method? && loc.expression.begin_pos != loc.selector.begin_pos
end
block_literal?() click to toggle source

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 152
def block_literal?
  parent&.block_type? && eql?(parent.send_node)
end
block_node() click to toggle source

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 39
def block_node
  parent if block_literal?
end
command?(name) click to toggle source

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 93
def command?(name)
  !receiver && method?(name)
end
const_receiver?() click to toggle source

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 137
def const_receiver?
  receiver&.const_type?
end
def_modifier?() click to toggle source

Checks if this node is part of a chain of `def` modifiers.

@example

private def foo; end

@return [Boolean] whether the dispatched method is a `def` modifier

# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 171
def def_modifier?
  send_type? &&
    [self, *each_descendant(:send)].any?(&:adjacent_def_modifier?)
end
dot?() click to toggle source

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 112
def dot?
  loc.respond_to?(:dot) && loc.dot && loc.dot.is?('.')
end
double_colon?() click to toggle source

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 120
def double_colon?
  loc.respond_to?(:dot) && loc.dot && loc.dot.is?('::')
end
implicit_call?() click to toggle source

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 145
def implicit_call?
  method?(:call) && !loc.selector
end
lambda?() click to toggle source

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 180
def lambda?
  block_literal? && command?(:lambda)
end
lambda_literal?() click to toggle source

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 191
def lambda_literal?
  block_literal? && loc.expression && loc.expression.source == '->'
end
macro?() click to toggle source

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 50
def macro?
  !receiver && macro_scope?
end
method_name() click to toggle source

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 24
def method_name
  node_parts[1]
end
non_bare_access_modifier?() click to toggle source

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 75
def non_bare_access_modifier?
  macro? && non_bare_access_modifier_declaration?
end
receiver() click to toggle source

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 17
def receiver
  node_parts[0]
end
self_receiver?() click to toggle source

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 128
def self_receiver?
  receiver&.self_type?
end
setter_method?() click to toggle source

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 100
def setter_method?
  loc.respond_to?(:operator) && loc.operator
end
Also aliased as: assignment?
special_modifier?() click to toggle source

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 84
def special_modifier?
  bare_access_modifier? && SPECIAL_MODIFIERS.include?(source)
end
unary_operation?() click to toggle source

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 202
def unary_operation?
  return false unless loc.selector

  operator_method? && loc.expression.begin_pos == loc.selector.begin_pos
end

Private Instance Methods

macro_kwbegin_wrapper?(parent) click to toggle source

Check if a node's parent is a kwbegin wrapper within a macro scope

@param parent [Node] parent of the node being checked

@return [Boolean] true if the parent is a kwbegin in a macro scope

# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 235
def macro_kwbegin_wrapper?(parent)
  parent.kwbegin_type? && macro_scope?(parent)
end
root_node?(node) click to toggle source

Check if a node does not have a parent

@param node [Node]

@return [Boolean] if the parent is nil

# File lib/rubocop/ast/node/mixin/method_dispatch_node.rb, line 244
def root_node?(node)
  node.parent.nil?
end