/
empty_line_after_example.rb
85 lines (75 loc) · 2.01 KB
/
empty_line_after_example.rb
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
83
84
85
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks if there is an empty line after example blocks.
#
# @example
# # bad
# RSpec.describe Foo do
# it 'does this' do
# end
# it 'does that' do
# end
# end
#
# # good
# RSpec.describe Foo do
# it 'does this' do
# end
#
# it 'does that' do
# end
# end
#
# # fair - it's ok to have non-separated one-liners
# RSpec.describe Foo do
# it { one }
# it { two }
# end
#
# @example with AllowConsecutiveOneLiners configuration
#
# # rubocop.yml
# # RSpec/EmptyLineAfterExample:
# # AllowConsecutiveOneLiners: false
#
# # bad
# RSpec.describe Foo do
# it { one }
# it { two }
# end
#
class EmptyLineAfterExample < Cop
extend AutoCorrector
include EmptyLineSeparation
MSG = 'Add an empty line after `%<example>s`.'
def on_block(node)
return unless example?(node)
return if allowed_one_liner?(node)
missing_separating_line_offense(node) do |method|
format(MSG, example: method)
end
end
def allowed_one_liner?(node)
consecutive_one_liner?(node) && allow_consecutive_one_liners?
end
def allow_consecutive_one_liners?
cop_config['AllowConsecutiveOneLiners']
end
def consecutive_one_liner?(node)
node.line_count == 1 && next_one_line_example?(node)
end
def next_one_line_example?(node)
next_sibling = next_sibling(node)
return unless next_sibling
return unless example?(next_sibling)
next_sibling.line_count == 1
end
def next_sibling(node)
node.parent.children[node.sibling_index + 1]
end
end
end
end
end