Sha256: c8a493e6eb8b6600165f78e1d6a99873df5bf9fcc8ddb114e3980c16d285460d

Contents?: true

Size: 1.38 KB

Versions: 20

Compression:

Stored size: 1.38 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`.'

        def_node_search :described_class_msg, <<~PATTERN
          (const (send nil? :described_class) :MSG)
        PATTERN

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

        def on_new_investigation
          assertions_using_described_class_msg.each do |node|
            add_offense(node)
          end
        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?(&method(:rspec_expectation_on_msg?))
          end
        end

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

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.3.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.3.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.2.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.1.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-1.0.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.93.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.93.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.92.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.91.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.91.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.90.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.89.1 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.89.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb