class RuboCop::Cop::Naming::BinaryOperatorParameterName

Makes sure that certain binary operator methods have their sole parameter named `other`.

@example

# bad
def +(amount); end

# good
def +(other); end

Constants

EXCLUDED
MSG
OP_LIKE_METHODS

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/naming/binary_operator_parameter_name.rb, line 29
def on_def(node)
  op_method_candidate?(node) do |name, arg|
    add_offense(arg, message: format(MSG, opr: name)) do |corrector|
      corrector.replace(arg, 'other')
      node.each_descendant(:lvar, :lvasgn) do |lvar|
        lvar_location = lvar.loc.name
        next unless lvar_location.source == arg.source

        corrector.replace(lvar_location, 'other')
      end
    end
  end
end

Private Instance Methods

op_method?(name) click to toggle source
# File lib/rubocop/cop/naming/binary_operator_parameter_name.rb, line 45
def op_method?(name)
  return false if EXCLUDED.include?(name)

  !/\A[[:word:]]/.match?(name) || OP_LIKE_METHODS.include?(name)
end