Sha256: 242eadd3b38fc0dd02d1e4d071b87d9825b1bdf05e6c429a140a2e92f047fce5

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Naming
      # This cop makes sure that certain binary operator methods have their
      # sole  parameter named `other`.
      #
      # @example
      #
      #   # bad
      #   def +(amount); end
      #
      #   # good
      #   def +(other); end
      class BinaryOperatorParameterName < Cop
        MSG = 'When defining the `%<opr>s` operator, ' \
              'name its argument `other`.'

        OP_LIKE_METHODS = %i[eql? equal?].freeze
        EXCLUDED = %i[+@ -@ [] []= << === `].freeze

        def_node_matcher :op_method_candidate?, <<~PATTERN
          (def [#op_method? $_] (args $(arg [!:other !:_other])) _)
        PATTERN

        def on_def(node)
          op_method_candidate?(node) do |name, arg|
            add_offense(arg, message: format(MSG, opr: name))
          end
        end

        private

        def op_method?(name)
          return false if EXCLUDED.include?(name)

          !/\A\w/.match?(name) || OP_LIKE_METHODS.include?(name)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
rubocop-0.88.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.87.1 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.87.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb