Sha256: d45954ffb89be8a22567df63112cc574825a144a1e60ee77b0a0b6b1f13aba18

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces the use of `refute_operator(expected, :<, actual)` over `refute(expected < actual)`.
      #
      # @example
      #
      #   # bad
      #   refute(expected < actual)
      #
      #   # good
      #   refute_operator(expected, :<, actual)
      #
      class RefuteOperator < Base
        extend AutoCorrector

        MSG = 'Prefer using `refute_operator(%<new_arguments>s)`.'
        RESTRICT_ON_SEND = %i[refute].freeze

        def on_send(node)
          first_argument = node.first_argument
          return unless first_argument.respond_to?(:operator_method?) && first_argument.operator_method?

          new_arguments = build_new_arguments(node)

          add_offense(node, message: format(MSG, new_arguments: new_arguments)) do |corrector|
            corrector.replace(node.loc.selector, 'refute_operator')

            corrector.replace(range_of_arguments(node), new_arguments)
          end
        end

        private

        def build_new_arguments(node)
          lhs, op, rhs = *node.first_argument
          new_arguments = "#{lhs.source}, :#{op}, #{rhs.source}"

          if node.arguments.count == 2
            new_arguments << ", #{node.last_argument.source}"
          else
            new_arguments
          end
        end

        def range_of_arguments(node)
          node.first_argument.source_range.begin.join(node.last_argument.source_range.end)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/refute_operator.rb