Sha256: e67cc935604e3ab46149532d5ca962f94109b138ce02b84e76f2879b00c309c7

Contents?: true

Size: 1.43 KB

Versions: 192

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Naming
      # Makes sure that certain binary operator methods have their
      # sole  parameter named `other`.
      #
      # @example
      #
      #   # bad
      #   def +(amount); end
      #
      #   # good
      #   def +(other); end
      class BinaryOperatorParameterName < Base
        extend AutoCorrector

        MSG = 'When defining the `%<opr>s` operator, name its argument `other`.'

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

        # @!method op_method_candidate?(node)
        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)) 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

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

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

Version data entries

192 entries across 185 versions & 18 rubygems

Version Path
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.59 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.58 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.57 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.56 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/naming/binary_operator_parameter_name.rb