Sha256: c7023b16034e820906942c8a28fe3daa5ab2939cb1995c98ae7a80c4528ecd8b

Contents?: true

Size: 1.84 KB

Versions: 41

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # This cop 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

41 entries across 41 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.29.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.29.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.28.2 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.28.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.28.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.27.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.26.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.26.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.25.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.25.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.24.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.24.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.23.0 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.22.3 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.22.2 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
rubocop-1.22.1 lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb