Sha256: ad9094754da3a92629537a515e5d74ff00242e52dccc40f861e9cc359e3a25df

Contents?: true

Size: 1.99 KB

Versions: 44

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

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

44 entries across 44 versions & 6 rubygems

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