Sha256: cc5e7e81acddc479d5a8d6f083f2a5b59ce76eb0b66611ba9428a1791a6e8016

Contents?: true

Size: 1.92 KB

Versions: 30

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks that `let` is `RuboCop::Config.new` with no arguments.
      #
      # @example
      #   # bad
      #   RSpec.describe RuboCop::Cop::Department::Foo, :config do
      #     let(:config) { RuboCop::Config.new }
      #   end
      #
      #   # good
      #   RSpec.describe RuboCop::Cop::Department::Foo, :config do
      #   end
      #
      #   RSpec.describe RuboCop::Cop::Department::Foo, :config do
      #     let(:config) { RuboCop::Config.new(argument) }
      #   end
      #
      class RedundantLetRuboCopConfigNew < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%<additional_message>s.'

        # @!method let_rubocop_config_new?(node)
        def_node_matcher :let_rubocop_config_new?, <<~PATTERN
          (block
            (send nil? :let
              (sym :config))
            (args)
            (send
              (const
                (const nil? :RuboCop) :Config) :new))
        PATTERN

        def on_block(node)
          return unless let_rubocop_config_new?(node)

          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

30 entries across 26 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.43.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.42.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.41.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.41.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.40.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.39.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.38.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.37.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.37.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.36.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.35.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb