Sha256: ff4881d9d629851b3f543041543cc0f91add341f04034ccba290bfd5b0e03f29

Contents?: true

Size: 1.98 KB

Versions: 182

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks whether `expect_offense` and correction expectation methods
      # (i.e. `expect_correction` and `expect_no_corrections`) are separated by empty line.
      #
      # @example
      #   # bad
      #   it 'registers and corrects an offense' do
      #     expect_offense(<<~RUBY)
      #       bad_method
      #       ^^^^^^^^^^ Use `good_method`.
      #     RUBY
      #     expect_correction(<<~RUBY)
      #       good_method
      #     RUBY
      #   end
      #
      #   # good
      #   it 'registers and corrects an offense' do
      #     expect_offense(<<~RUBY)
      #       bad_method
      #       ^^^^^^^^^^ Use `good_method`.
      #     RUBY
      #
      #     expect_correction(<<~RUBY)
      #       good_method
      #     RUBY
      #   end
      #
      class EmptyLineBetweenExpectOffenseAndCorrection < Base
        extend AutoCorrector

        MSG = 'Add empty line between `expect_offense` and `%<expect_correction>s`.'
        RESTRICT_ON_SEND = %i[expect_offense].freeze
        CORRECTION_EXPECTATION_METHODS = %i[expect_correction expect_no_corrections].freeze

        def on_send(node)
          return unless (next_sibling = node.right_sibling) && next_sibling.send_type?

          method_name = next_sibling.method_name
          return unless CORRECTION_EXPECTATION_METHODS.include?(method_name)

          range = offense_range(node)
          return unless range.last_line + 1 == next_sibling.loc.line

          add_offense(range, message: format(MSG, expect_correction: method_name)) do |corrector|
            corrector.insert_after(range, "\n")
          end
        end

        private

        def offense_range(node)
          first_argument = node.first_argument

          if first_argument.respond_to?(:heredoc?) && first_argument.heredoc?
            first_argument.loc.heredoc_end
          else
            node
          end
        end
      end
    end
  end
end

Version data entries

182 entries across 175 versions & 17 rubygems

Version Path
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.62.1 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.62.0 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.61.0 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
rubocop-1.60.2 lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb