Sha256: 12e50dc08b44e0a175340bb1a77dc2c953efd7464874ec755749385dba18af84

Contents?: true

Size: 1.39 KB

Versions: 14

Compression:

Stored size: 1.39 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 < Cop
        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 investigate(_processed_source)
          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

14 entries across 14 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.72.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.71.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.70.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
rubocop-0.69.0 lib/rubocop/cop/internal_affairs/useless_message_assertion.rb