Sha256: 4916a48586e4545f0f823e340ce35b39076fbe0189f807dc615209037b94977b

Contents?: true

Size: 1.84 KB

Versions: 192

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks for redundant `subject(:cop) { described_class.new }`.
      #
      # @example
      #   # bad
      #   RSpec.describe RuboCop::Cop::Department::Foo do
      #     subject(:cop) { described_class.new(config) }
      #   end
      #
      #   # good
      #   RSpec.describe RuboCop::Cop::Department::Foo, :config do
      #   end
      #
      class RedundantDescribedClassAsSubject < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Remove the redundant `subject`%<additional_message>s.'

        # @!method described_class_subject?(node)
        def_node_matcher :described_class_subject?, <<~PATTERN
          (block
            (send nil? :subject
              (sym :cop))
            (args)
            (send
              (send nil? :described_class) :new
              $...))
        PATTERN

        def on_block(node)
          return unless (described_class_arguments = described_class_subject?(node))
          return if described_class_arguments.count >= 2

          describe = find_describe_method_node(node)

          unless (exist_config = describe.last_argument.source == ':config')
            additional_message = ' and specify `:config` in `describe`'
          end

          message = format(MSG, additional_message: additional_message)

          add_offense(node, message: message) do |corrector|
            corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))

            corrector.insert_after(describe.last_argument, ', :config') unless exist_config
          end
        end

        private

        def find_describe_method_node(block_node)
          block_node.ancestors.find { |node| node.block_type? && node.method?(:describe) }.send_node
        end
      end
    end
  end
end

Version data entries

192 entries across 185 versions & 18 rubygems

Version Path
rubocop-1.71.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.70.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.69.2 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.69.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.69.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb