Sha256: c51218f780087768943d009631118a8341ad9e8ca4c902b5260891e1be86de77

Contents?: true

Size: 1.53 KB

Versions: 30

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Common functionality for `AssertNil` and `RefuteNil` cops.
      # @api private
      module NilAssertionHandleable
        MSG = 'Prefer using `%<assertion_type>s_nil(%<preferred_args>s)`.'

        private

        def register_offense(node, actual, message)
          message = build_message(node, actual, message)

          add_offense(node, message: message) do |corrector|
            autocorrect(corrector, node, actual)
          end
        end

        def build_message(node, actual, message)
          message = message.first
          message_source = message&.source

          preferred_args = [actual.source, message_source].compact

          format(
            MSG,
            assertion_type: assertion_type,
            preferred_args: preferred_args.join(', '),
            method: node.method_name
          )
        end

        def autocorrect(corrector, node, actual)
          corrector.replace(node.loc.selector, :"#{assertion_type}_nil")
          if comparison_or_predicate_assertion_method?(node)
            corrector.replace(first_and_second_arguments_range(node), actual.source)
          else
            corrector.remove(node.first_argument.loc.dot)
            corrector.remove(node.first_argument.loc.selector)
          end
        end

        def comparison_or_predicate_assertion_method?(node)
          node.method?(:"#{assertion_type}_equal") || node.method?(:"#{assertion_type}_predicate")
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
rubocop-minitest-0.26.0 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.25.1 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.25.0 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.24.0 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.23.2 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.23.1 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.23.0 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.22.2 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.22.1 lib/rubocop/cop/mixin/nil_assertion_handleable.rb
rubocop-minitest-0.22.0 lib/rubocop/cop/mixin/nil_assertion_handleable.rb