Skip to content

Commit 1be4c09

Browse files
authored
Merge pull request #961 from rubocop-hq/extract-to-blank-line-separation
Extract some shared logic to BlankLineSeparation
2 parents b948759 + 51d593a commit 1be4c09

9 files changed

+40
-47
lines changed

lib/rubocop-rspec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
require_relative 'rubocop/rspec/align_let_brace'
2424
require_relative 'rubocop/rspec/factory_bot'
2525
require_relative 'rubocop/rspec/final_end_location'
26-
require_relative 'rubocop/rspec/blank_line_separation'
26+
require_relative 'rubocop/rspec/empty_line_separation'
2727
require_relative 'rubocop/rspec/corrector/move_node'
2828

2929
RuboCop::RSpec::Inject.defaults!

lib/rubocop/cop/rspec/empty_line_after_example.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,16 @@ module RSpec
4343
#
4444
class EmptyLineAfterExample < Cop
4545
extend AutoCorrector
46-
include RuboCop::RSpec::BlankLineSeparation
46+
include RuboCop::RSpec::EmptyLineSeparation
4747

4848
MSG = 'Add an empty line after `%<example>s`.'
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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@ module RSpec
2525
#
2626
class EmptyLineAfterExampleGroup < Cop
2727
extend AutoCorrector
28-
include RuboCop::RSpec::BlankLineSeparation
28+
include RuboCop::RSpec::EmptyLineSeparation
2929

3030
MSG = 'Add an empty line after `%<example_group>s`.'
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: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@ module RSpec
1818
# it { does_something }
1919
class EmptyLineAfterFinalLet < Cop
2020
extend AutoCorrector
21-
include RuboCop::RSpec::BlankLineSeparation
21+
include RuboCop::RSpec::EmptyLineSeparation
2222

23-
MSG = 'Add an empty line after the last `let` block.'
23+
MSG = 'Add an empty line after the last `%<let>s`.'
2424

2525
def on_block(node)
2626
return unless example_group_with_body?(node)
2727

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-
add_offense(location) do |corrector|
35-
corrector.insert_after(location.end, "\n")
36-
end
32+
missing_separating_line_offense(latest_let) do |method|
33+
format(MSG, let: method)
3734
end
3835
end
3936
end

lib/rubocop/cop/rspec/empty_line_after_hook.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@ module RSpec
3535
#
3636
class EmptyLineAfterHook < Cop
3737
extend AutoCorrector
38-
include RuboCop::RSpec::BlankLineSeparation
38+
include RuboCop::RSpec::EmptyLineSeparation
3939

4040
MSG = 'Add an empty line after `%<hook>s`.'
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: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@ module RSpec
1616
# let(:foo) { bar }
1717
class EmptyLineAfterSubject < Cop
1818
extend AutoCorrector
19-
include RuboCop::RSpec::BlankLineSeparation
19+
include RuboCop::RSpec::EmptyLineSeparation
2020

21-
MSG = 'Add empty line after `subject`.'
21+
MSG = 'Add an empty line after `%<subject>s`.'
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-
add_offense(location) do |corrector|
29-
corrector.insert_after(location.end, "\n")
30-
end
26+
missing_separating_line_offense(node) do |method|
27+
format(MSG, subject: method)
3128
end
3229
end
3330

lib/rubocop/rspec/blank_line_separation.rb renamed to lib/rubocop/rspec/empty_line_separation.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22

33
module RuboCop
44
module RSpec
5-
# Helps determine the offending location if there is not a blank line
5+
# Helps determine the offending location if there is not an empty line
66
# following the node. Allows comments to follow directly after.
7-
module BlankLineSeparation
7+
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

spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
RSpec.describe User do
99
let(:a) { a }
1010
let(:b) { b }
11-
^^^^^^^^^^^^^ Add an empty line after the last `let` block.
11+
^^^^^^^^^^^^^ Add an empty line after the last `let`.
1212
it { expect(a).to eq(b) }
1313
end
1414
RUBY
@@ -30,7 +30,7 @@
3030
let!(:b) do
3131
b
3232
end
33-
^^^ Add an empty line after the last `let` block.
33+
^^^ Add an empty line after the last `let!`.
3434
it { expect(a).to eq(b) }
3535
end
3636
RUBY
@@ -52,7 +52,7 @@
5252
RSpec.describe User do
5353
let(:a) { a }
5454
let(:user, &args[:build_user])
55-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
55+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let`.
5656
it { expect(a).to eq(b) }
5757
end
5858
RUBY
@@ -96,7 +96,7 @@
9696
let(:a) { a }
9797
let(:b) { b }
9898
# end of setup
99-
^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
99+
^^^^^^^^^^^^^^ Add an empty line after the last `let`.
100100
it { expect(a).to eq(b) }
101101
end
102102
RUBY
@@ -119,7 +119,7 @@
119119
let(:b) { b }
120120
# a multiline comment marking
121121
# the end of setup
122-
^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
122+
^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let`.
123123
it { expect(a).to eq(b) }
124124
end
125125
RUBY
@@ -144,7 +144,7 @@
144144
subject { described_class }
145145
146146
let!(:b) { b }
147-
^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
147+
^^^^^^^^^^^^^^ Add an empty line after the last `let!`.
148148
it { expect(a).to eq(b) }
149149
end
150150
RUBY
@@ -236,7 +236,7 @@
236236
hello
237237
world
238238
BAR
239-
^^^ Add an empty line after the last `let` block.
239+
^^^ Add an empty line after the last `let`.
240240
it 'has tricky syntax' do
241241
expect(foo).to eql(" hello\n world\n")
242242
end

spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
expect_offense(<<-RUBY)
88
RSpec.describe User do
99
subject { described_class.new }
10-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line after `subject`.
10+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `subject`.
1111
let(:params) { foo }
1212
end
1313
RUBY
@@ -25,7 +25,7 @@
2525
expect_offense(<<-RUBY)
2626
RSpec.describe User do
2727
subject! { described_class.new }
28-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line after `subject`.
28+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `subject!`.
2929
let(:params) { foo }
3030
end
3131
RUBY

0 commit comments

Comments
 (0)