File: empty_line_after_hook.rb

package info (click to toggle)
ruby-rubocop-rspec 2.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 1,892 kB
  • sloc: ruby: 22,283; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks if there is an empty line after hook blocks.
      #
      # `AllowConsecutiveOneLiners` configures whether adjacent
      # one-line definitions are considered an offense.
      #
      # @example
      #   # bad
      #   before { do_something }
      #   it { does_something }
      #
      #   # bad
      #   after { do_something }
      #   it { does_something }
      #
      #   # bad
      #   around { |test| test.run }
      #   it { does_something }
      #
      #   # good
      #   after { do_something }
      #
      #   it { does_something }
      #
      #   # fair - it's ok to have non-separated one-liners hooks
      #   around { |test| test.run }
      #   after { do_something }
      #
      #   it { does_something }
      #
      # @example with AllowConsecutiveOneLiners configuration
      #   # rubocop.yml
      #   # RSpec/EmptyLineAfterHook:
      #   #   AllowConsecutiveOneLiners: false
      #
      #   # bad
      #   around { |test| test.run }
      #   after { do_something }
      #
      #   it { does_something }
      #
      #   # good
      #   around { |test| test.run }
      #
      #   after { do_something }
      #
      #   it { does_something }
      #
      class EmptyLineAfterHook < Base
        extend AutoCorrector
        include ConfigurableEnforcedStyle
        include EmptyLineSeparation

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

        def on_block(node)
          return unless hook?(node)
          return if cop_config['AllowConsecutiveOneLiners'] &&
            chained_single_line_hooks?(node)

          missing_separating_line_offense(node) do |method|
            format(MSG, hook: method)
          end
        end

        alias on_numblock on_block

        private

        def chained_single_line_hooks?(node)
          next_node = node.right_sibling

          hook?(next_node) && node.single_line? && next_node.single_line?
        end
      end
    end
  end
end