Sha256: c042fb4da408914ad45eb7ce23a77fb3e1be1997e4723c7cee01111ad34e61f6

Contents?: true

Size: 1023 Bytes

Versions: 10

Compression:

Stored size: 1023 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop makes sure that certain operator methods have their sole
      # parameter named `other`.
      class OpMethod < Cop
        MSG = 'When defining the `%s` operator, ' \
              'name its argument `other`.'.freeze

        OP_LIKE_METHODS = [:eql?, :equal?].freeze

        BLACKLISTED = [:+@, :-@, :[], :[]=, :<<].freeze

        TARGET_ARGS = [s(:args, s(:arg, :other)),
                       s(:args, s(:arg, :_other))].freeze

        def on_def(node)
          name, args, _body = *node
          return unless op_method?(name) &&
                        args.children.size == 1 &&
                        !TARGET_ARGS.include?(args)

          add_offense(args.children[0], :expression, format(MSG, name))
        end

        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

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-0.41.2 lib/rubocop/cop/style/op_method.rb
rubocop-0.41.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.41.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.40.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.39.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.38.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.37.2 lib/rubocop/cop/style/op_method.rb
rubocop-0.37.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.37.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.36.0 lib/rubocop/cop/style/op_method.rb