Skip to content

Commit 51d593a

Browse files
committed
Extract some shared logic to BlankLineSeparation
1 parent ee6da00 commit 51d593a

File tree

6 files changed

+21
-30
lines changed

6 files changed

+21
-30
lines changed

lib/rubocop/cop/rspec/empty_line_after_example.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,10 @@ class EmptyLineAfterExample < Cop
4949

5050
def on_block(node)
5151
return unless example?(node)
52-
return if last_child?(node)
5352
return if allowed_one_liner?(node)
5453

55-
missing_separating_line(node) do |location|
56-
msg = format(MSG, example: node.method_name)
57-
add_offense(location, message: msg) do |corrector|
58-
corrector.insert_after(location.end, "\n")
59-
end
54+
missing_separating_line_offense(node) do |method|
55+
format(MSG, example: method)
6056
end
6157
end
6258

lib/rubocop/cop/rspec/empty_line_after_example_group.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@ class EmptyLineAfterExampleGroup < Cop
3131

3232
def on_block(node)
3333
return unless example_group?(node)
34-
return if last_child?(node)
3534

36-
missing_separating_line(node) do |location|
37-
msg = format(MSG, example_group: node.method_name)
38-
add_offense(location, message: msg) do |corrector|
39-
corrector.insert_after(location.end, "\n")
40-
end
35+
missing_separating_line_offense(node) do |method|
36+
format(MSG, example_group: method)
4137
end
4238
end
4339
end

lib/rubocop/cop/rspec/empty_line_after_final_let.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ def on_block(node)
2828
latest_let = node.body.child_nodes.select { |child| let?(child) }.last
2929

3030
return if latest_let.nil?
31-
return if last_child?(latest_let)
3231

33-
missing_separating_line(latest_let) do |location|
34-
msg = format(MSG, let: latest_let.method_name)
35-
add_offense(location, message: msg) do |corrector|
36-
corrector.insert_after(location.end, "\n")
37-
end
32+
missing_separating_line_offense(latest_let) do |method|
33+
format(MSG, let: method)
3834
end
3935
end
4036
end

lib/rubocop/cop/rspec/empty_line_after_hook.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ class EmptyLineAfterHook < Cop
4141

4242
def on_block(node)
4343
return unless hook?(node)
44-
return if last_child?(node)
4544

46-
missing_separating_line(node) do |location|
47-
msg = format(MSG, hook: node.method_name)
48-
add_offense(location, message: msg) do |corrector|
49-
corrector.insert_after(location.end, "\n")
50-
end
45+
missing_separating_line_offense(node) do |method|
46+
format(MSG, hook: method)
5147
end
5248
end
5349
end

lib/rubocop/cop/rspec/empty_line_after_subject.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ class EmptyLineAfterSubject < Cop
2222

2323
def on_block(node)
2424
return unless subject?(node) && !in_spec_block?(node)
25-
return if last_child?(node)
2625

27-
missing_separating_line(node) do |location|
28-
msg = format(MSG, subject: node.method_name)
29-
add_offense(location, message: msg) do |corrector|
30-
corrector.insert_after(location.end, "\n")
31-
end
26+
missing_separating_line_offense(node) do |method|
27+
format(MSG, subject: method)
3228
end
3329
end
3430

lib/rubocop/rspec/empty_line_separation.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ module EmptyLineSeparation
88
include FinalEndLocation
99
include RuboCop::Cop::RangeHelp
1010

11+
def missing_separating_line_offense(node)
12+
return if last_child?(node)
13+
14+
missing_separating_line(node) do |location|
15+
msg = yield(node.method_name)
16+
add_offense(location, message: msg) do |corrector|
17+
corrector.insert_after(location.end, "\n")
18+
end
19+
end
20+
end
21+
1122
def missing_separating_line(node)
1223
line = final_end_location(node).line
1324

0 commit comments

Comments
 (0)