summaryrefslogtreecommitdiff
path: root/rubocop/cop/rspec/empty_line_after_shared_example.rb
blob: 5d09565bd5a091749d556e93f6330a474b096004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

require 'rubocop/rspec/final_end_location'
require 'rubocop/rspec/blank_line_separation'
require 'rubocop/rspec/language'

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 < RuboCop::Cop::Cop
        include RuboCop::RSpec::BlankLineSeparation
        include RuboCop::RSpec::Language

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

        def_node_matcher :shared_examples,
                         (SharedGroups::ALL + Includes::ALL).block_pattern

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

            missing_separating_line(node) do |location|
              add_offense(node,
                          location: location,
                          message: format(MSG, example: node.method_name))
            end
          end
        end
      end
    end
  end
end