Sha256: ae103995da1b8b09561ce9adacce3e748947f51ee74d5e2e879a0d21315c3614

Contents?: true

Size: 923 Bytes

Versions: 17

Compression:

Stored size: 923 Bytes

Contents

# encoding: utf-8

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

        OP_LIKE_METHODS = [:eql?, :equal?]

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

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

        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

17 entries across 17 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.35.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.34.2 lib/rubocop/cop/style/op_method.rb
rubocop-0.34.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.34.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.33.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.32.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.32.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.31.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.30.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.30.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.29.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.29.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.28.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.27.1 lib/rubocop/cop/style/op_method.rb
rubocop-0.27.0 lib/rubocop/cop/style/op_method.rb
rubocop-0.26.1 lib/rubocop/cop/style/op_method.rb