Sha256: eafd4c9e9483de94b695cefc89e95f52fdc70fde77769c8dd7341e4cd37ddb6f

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # This cop enforces to use `refute_empty` instead of
      # using `refute(object.empty?)`.
      #
      # @example
      #   # bad
      #   refute(object.empty?)
      #   refute(object.empty?, 'the message')
      #
      #   # good
      #   refute_empty(object)
      #   refute_empty(object, 'the message')
      #
      class RefuteEmpty < Cop
        include ArgumentRangeHelper

        MSG = 'Prefer using `refute_empty(%<arguments>s)` over ' \
              '`refute(%<receiver>s)`.'

        def_node_matcher :refute_with_empty, <<~PATTERN
          (send nil? :refute $(send $_ :empty?) $...)
        PATTERN

        def on_send(node)
          refute_with_empty(node) do |first_receiver_arg, object, rest_receiver_arg|
            message = rest_receiver_arg.first

            arguments = [object.source, message&.source].compact.join(', ')
            receiver = [first_receiver_arg.source, message&.source].compact.join(', ')

            offense_message = format(MSG, arguments: arguments, receiver: receiver)
            add_offense(node, message: offense_message)
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            refute_with_empty(node) do |_, actual_arg|
              corrector.replace(node.loc.selector, 'refute_empty')
              corrector.replace(first_argument_range(node), actual_arg.source)
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-minitest-0.6.2 lib/rubocop/cop/minitest/refute_empty.rb
rubocop-minitest-0.6.1 lib/rubocop/cop/minitest/refute_empty.rb
rubocop-minitest-0.6.0 lib/rubocop/cop/minitest/refute_empty.rb
rubocop-minitest-0.5.1 lib/rubocop/cop/minitest/refute_empty.rb