Sha256: 6cae9c4a1b839f41233411dd2d9f3fff958fe3d959859279d308e17cad8b6b12

Contents?: true

Size: 1.51 KB

Versions: 114

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks that cops are not tested using `described_class::MSG`.
      #
      # @example
      #
      #     # bad
      #     expect(cop.messages).to eq([described_class::MSG])
      #
      #     # good
      #     expect(cop.messages).to eq(['Do not write bad code like that.'])
      #
      class UselessMessageAssertion < Base
        MSG = 'Do not specify cop behavior using `described_class::MSG`.'

        # @!method described_class_msg(node)
        def_node_search :described_class_msg, <<~PATTERN
          (const (send nil? :described_class) :MSG)
        PATTERN

        # @!method rspec_expectation_on_msg?(node)
        def_node_matcher :rspec_expectation_on_msg?, <<~PATTERN
          (send (send nil? :expect #contains_described_class_msg?) :to ...)
        PATTERN

        def on_new_investigation
          return if processed_source.blank?

          assertions_using_described_class_msg.each { |node| add_offense(node) }
        end

        private

        def contains_described_class_msg?(node)
          described_class_msg(node).any?
        end

        def assertions_using_described_class_msg
          described_class_msg(processed_source.ast).reject do |node|
            node.ancestors.any? { |ancestor| rspec_expectation_on_msg?(ancestor) }
          end
        end

        # Only process spec files
        def relevant_file?(file)
          file.end_with?('_spec.rb')
        end
      end
    end
  end
end

Version data entries

114 entries across 114 versions & 9 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.62.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.62.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.61.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.60.2 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb