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`

Constant Summary collapse

ARITHMETIC_OPERATORS =
%i[+ - * / % **].freeze
SPECIAL_MODIFIERS =
%w[private protected].freeze

Constants included from MethodIdentifierPredicates

RuboCop::AST::MethodIdentifierPredicates::ENUMERABLE_METHODS, RuboCop::AST::MethodIdentifierPredicates::ENUMERATOR_METHODS, RuboCop::AST::MethodIdentifierPredicates::NONMUTATING_ARRAY_METHODS, RuboCop::AST::MethodIdentifierPredicates::NONMUTATING_BINARY_OPERATOR_METHODS, RuboCop::AST::MethodIdentifierPredicates::NONMUTATING_HASH_METHODS, RuboCop::AST::MethodIdentifierPredicates::NONMUTATING_OPERATOR_METHODS, RuboCop::AST::MethodIdentifierPredicates::NONMUTATING_STRING_METHODS, RuboCop::AST::MethodIdentifierPredicates::NONMUTATING_UNARY_OPERATOR_METHODS, RuboCop::AST::MethodIdentifierPredicates::OPERATOR_METHODS

Instance Method Summary collapse

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.

Returns:

  • (Boolean)

    whether the dispatched method is an access modifier



51
52
53
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 51

def access_modifier?
  bare_access_modifier? || non_bare_access_modifier?
end

#arithmetic_operation?Boolean

Checks whether this node is an arithmetic operation

Returns:

  • (Boolean)

    whether the dispatched method is an arithmetic operation



162
163
164
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 162

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.

Returns:

  • (Boolean)

    whether the dispatched method is a bare access modifier



60
61
62
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 60

def bare_access_modifier?
  macro? && bare_access_modifier_declaration?
end

#binary_operation?Bookean

Checks whether this is a binary operation.

Examples:


foo + bar

Returns:

  • (Bookean)

    whether this method is a binary operation



217
218
219
220
221
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 217

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.

Returns:

  • (Boolean)

    whether the dispatched method has a block



154
155
156
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 154

def block_literal?
  parent&.block_type? && eql?(parent.send_node)
end

#block_nodeBlockNode?

The ‘block` node associated with this method dispatch, if any.

Returns:

  • (BlockNode, nil)

    the ‘block` node associated with this method call or `nil`



33
34
35
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 33

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.

Parameters:

  • name (Symbol, String)

    the method name to check for

Returns:

  • (Boolean)

    whether the method name matches the argument



87
88
89
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 87

def command?(name)
  !receiver && method?(name)
end

#const_receiver?Boolean

Checks whether the explicit receiver of this method dispatch is a ‘const` node.

Returns:

  • (Boolean)

    whether the receiver of this method dispatch is a ‘const` node



139
140
141
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 139

def const_receiver?
  receiver&.const_type?
end

#def_modifier?Boolean

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

Examples:


private def foo; end

Returns:

  • (Boolean)

    whether the dispatched method is a ‘def` modifier



173
174
175
176
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 173

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`.

Returns:

  • (Boolean)

    whether the method was called with a connecting dot



106
107
108
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 106

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.

Returns:

  • (Boolean)

    whether the method was called with a connecting dot



114
115
116
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 114

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)`.

Returns:

  • (Boolean)

    whether the method is the implicit form of ‘#call`



147
148
149
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 147

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.

Returns:

  • (Boolean)

    whether this method is a lambda



182
183
184
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 182

def lambda?
  block_literal? && command?(:lambda)
end

#lambda_literal?Boolean

Checks whether this is a lambda literal (stabby lambda.)

Examples:


-> (foo) { bar }

Returns:

  • (Boolean)

    whether this method is a lambda literal



193
194
195
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 193

def lambda_literal?
  block_literal? && loc.expression && loc.expression.source == '->'
end

#macro?Boolean

Note:

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.

Returns:

  • (Boolean)

    whether the dispatched method is a macro method



44
45
46
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 44

def macro?
  !receiver && macro_scope?
end

#method_nameSymbol

The name of the dispatched method as a symbol.

Returns:

  • (Symbol)

    the name of the dispatched method



25
26
27
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 25

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.

Returns:

  • (Boolean)

    whether the dispatched method is a non-bare access modifier



69
70
71
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 69

def non_bare_access_modifier?
  macro? && non_bare_access_modifier_declaration?
end

#receiverNode?

The receiving node of the method dispatch.

Returns:

  • (Node, nil)

    the receiver of the dispatched method or ‘nil`



18
19
20
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 18

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.

Returns:

  • (Boolean)

    whether the method was called with a connecting dot



122
123
124
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 122

def safe_navigation?
  loc.respond_to?(:dot) && loc.dot && loc.dot.is?('&.')
end

#self_receiver?Boolean

Checks whether the explicit receiver of this method dispatch is ‘self`.

Returns:

  • (Boolean)

    whether the receiver of this method dispatch is ‘self`



130
131
132
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 130

def self_receiver?
  receiver&.self_type?
end

#setter_method?Boolean Also known as: assignment?

Checks whether the dispatched method is a setter method.

Returns:

  • (Boolean)

    whether the dispatched method is a setter



94
95
96
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 94

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.

Returns:

  • (Boolean)

    whether the dispatched method is a bare ‘private` or `protected` access modifier



78
79
80
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 78

def special_modifier?
  bare_access_modifier? && SPECIAL_MODIFIERS.include?(source)
end

#unary_operation?Boolean

Checks whether this is a unary operation.

Examples:


-foo

Returns:

  • (Boolean)

    whether this method is a unary operation



204
205
206
207
208
# File 'lib/rubocop/ast/node/mixin/method_dispatch_node.rb', line 204

def unary_operation?
  return false unless loc.selector

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