Sha256: 416ba7b6968db5d28ddca183165a4754e3c7881028190154a7bbbff67a9f799b

Contents?: true

Size: 1.93 KB

Versions: 41

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

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

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_let_rubocop_config_new.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.29.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.29.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.28.2 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.28.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.28.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.27.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.26.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.26.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.25.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.25.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.24.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.24.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.23.0 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.22.3 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.22.2 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
rubocop-1.22.1 lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb