Sha256: 37d134900094d90347e247b816da0848bab664d0981a1b981b7779d80f891fd6

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'rubocop-rspec'
require_relative 'base'

module Rubocop
  module Cop
    module RSpec
      # Checks if there is an empty line after shared example blocks.
      #
      # @example
      #   # bad
      #   RSpec.describe Foo do
      #     it_behaves_like 'do this first'
      #     it_behaves_like 'does this' do
      #     end
      #     it_behaves_like 'does that' do
      #     end
      #     it_behaves_like 'do some more'
      #   end
      #
      #   # good
      #   RSpec.describe Foo do
      #     it_behaves_like 'do this first'
      #     it_behaves_like 'does this' do
      #     end
      #
      #     it_behaves_like 'does that' do
      #     end
      #
      #     it_behaves_like 'do some more'
      #   end
      #
      #   # fair - it's ok to have non-separated without blocks
      #   RSpec.describe Foo do
      #     it_behaves_like 'do this first'
      #     it_behaves_like 'does this'
      #   end
      #
      class EmptyLineAfterSharedExample < Base
        extend RuboCop::Cop::AutoCorrector
        include RuboCop::Cop::RSpec::EmptyLineSeparation

        MSG = 'Add an empty line after `%<example>s` block.'

        # @!method shared_examples(node)
        def_node_matcher :shared_examples, <<~PATTERN
          {
            (block (send #rspec? #SharedGroups.all ...) ...)
            {
              (block (send nil? #Includes.all ...) ...)
              (send nil? #Includes.all ...)
            }
          }
        PATTERN

        def on_block(node)
          shared_examples(node) do
            break if last_child?(node)

            missing_separating_line_offense(node) do |method|
              format(MSG, example: method)
            end
          end
        end
        alias_method :on_numblock, :on_block
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gitlab-styles-13.0.1 lib/rubocop/cop/rspec/empty_line_after_shared_example.rb
gitlab-styles-13.0.0 lib/rubocop/cop/rspec/empty_line_after_shared_example.rb
gitlab-styles-11.0.0 lib/rubocop/cop/rspec/empty_line_after_shared_example.rb
gitlab-styles-10.1.0 lib/rubocop/cop/rspec/empty_line_after_shared_example.rb