Sha256: c267f15f0fbee1218a558908fc03ce1e663fa3e10e7689712ea6902c5ded43d2

Contents?: true

Size: 1.06 KB

Versions: 13

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

        OP_LIKE_METHODS = %i[eql? equal?].freeze
        BLACKLISTED = %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 BLACKLISTED.include?(name)
          name !~ /\A\w/ || OP_LIKE_METHODS.include?(name)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
rubocop-0.58.2 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.58.1 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.58.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.57.2 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.57.1 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.57.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.56.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.55.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.54.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.53.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.52.1 lib/rubocop/cop/naming/binary_operator_parameter_name.rb
rubocop-0.52.0 lib/rubocop/cop/naming/binary_operator_parameter_name.rb