Skip to content

Commit 303c037

Browse files
authored
Merge pull request #1283 from johnny-miyake/fix_empty_line_separation
[Fix #1282] Fix autocorrect for EmptyLineSeparation
2 parents 02d6e30 + 671b19d commit 303c037

File tree

7 files changed

+654
-16
lines changed

7 files changed

+654
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Changelog
22

33
## Master (Unreleased)
4+
45
* Fix incorrect path suggested by `RSpec/FilePath` cop when second argument contains spaces. ([@tejasbubane][])
6+
* Fix autocorrect for EmptyLineSeparation. ([@johnny-miyake][])
57

68
## 2.11.1 (2022-05-18)
79

@@ -695,3 +697,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
695697
[@t3h2mas]: https://github.com/t3h2mas
696698
[@M-Yamashita01]: https://github.com/M-Yamashita01
697699
[@luke-hill]: https://github.com/luke-hill
700+
[@johnny-miyake]: https://github.com/johnny-miyake

lib/rubocop/cop/rspec/mixin/empty_line_separation.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ module RuboCop
44
module Cop
55
module RSpec
66
# Helps determine the offending location if there is not an empty line
7-
# following the node. Allows comments to follow directly after.
7+
# following the node. Allows comments to follow directly after
8+
# in the following cases.
9+
# - `rubocop:enable` directive
10+
# - followed by empty line(s)
811
module EmptyLineSeparation
912
include FinalEndLocation
1013
include RangeHelp
@@ -21,13 +24,19 @@ def missing_separating_line_offense(node)
2124
end
2225

2326
def missing_separating_line(node)
24-
line = final_end_location(node).line
27+
line = final_end_line = final_end_location(node).line
2528

26-
line += 1 while comment_line?(processed_source[line])
29+
while comment_line?(processed_source[line])
30+
line += 1
31+
comment = processed_source.comment_at_line(line)
32+
if DirectiveComment.new(comment).enabled?
33+
enable_directive_line = line
34+
end
35+
end
2736

2837
return if processed_source[line].blank?
2938

30-
yield offending_loc(line)
39+
yield offending_loc(enable_directive_line || final_end_line)
3140
end
3241

3342
def offending_loc(last_line)

spec/rubocop/cop/rspec/empty_line_after_example_group_spec.rb

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,150 @@
116116
end
117117
RUBY
118118
end
119+
120+
it 'does not register an offense for a comment followed by an empty line' do
121+
expect_no_offenses(<<-RUBY)
122+
RSpec.describe Foo do
123+
describe 'bar' do
124+
end
125+
# comment
126+
127+
describe 'baz' do
128+
end
129+
end
130+
RUBY
131+
end
132+
133+
it 'flags a missing empty line before a comment' do
134+
expect_offense(<<-RUBY)
135+
RSpec.describe Foo do
136+
describe 'bar' do
137+
end
138+
^^^ Add an empty line after `describe`.
139+
# comment
140+
describe 'baz' do
141+
end
142+
end
143+
RUBY
144+
145+
expect_correction(<<-RUBY)
146+
RSpec.describe Foo do
147+
describe 'bar' do
148+
end
149+
150+
# comment
151+
describe 'baz' do
152+
end
153+
end
154+
RUBY
155+
end
156+
157+
it 'flags a missing empty line before a multiline comment' do
158+
expect_offense(<<-RUBY)
159+
RSpec.describe Foo do
160+
describe 'bar' do
161+
end
162+
^^^ Add an empty line after `describe`.
163+
# multiline comment
164+
# multiline comment
165+
describe 'baz' do
166+
end
167+
end
168+
RUBY
169+
170+
expect_correction(<<-RUBY)
171+
RSpec.describe Foo do
172+
describe 'bar' do
173+
end
174+
175+
# multiline comment
176+
# multiline comment
177+
describe 'baz' do
178+
end
179+
end
180+
RUBY
181+
end
182+
183+
it 'flags a missing empty line after a `rubocop:enable` directive' do
184+
expect_offense(<<-RUBY)
185+
RSpec.describe Foo do
186+
# rubocop:disable RSpec/Foo
187+
describe 'bar' do
188+
end
189+
# rubocop:enable RSpec/Foo
190+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `describe`.
191+
describe 'baz' do
192+
end
193+
end
194+
RUBY
195+
196+
expect_correction(<<-RUBY)
197+
RSpec.describe Foo do
198+
# rubocop:disable RSpec/Foo
199+
describe 'bar' do
200+
end
201+
# rubocop:enable RSpec/Foo
202+
203+
describe 'baz' do
204+
end
205+
end
206+
RUBY
207+
end
208+
209+
it 'flags a missing empty line before a `rubocop:disable` directive' do
210+
expect_offense(<<-RUBY)
211+
RSpec.describe Foo do
212+
describe 'bar' do
213+
end
214+
^^^ Add an empty line after `describe`.
215+
# rubocop:disable RSpec/Foo
216+
describe 'baz' do
217+
end
218+
# rubocop:enable RSpec/Foo
219+
end
220+
RUBY
221+
222+
expect_correction(<<-RUBY)
223+
RSpec.describe Foo do
224+
describe 'bar' do
225+
end
226+
227+
# rubocop:disable RSpec/Foo
228+
describe 'baz' do
229+
end
230+
# rubocop:enable RSpec/Foo
231+
end
232+
RUBY
233+
end
234+
235+
it 'flags a missing empty line after a `rubocop:enable` directive '\
236+
'when it is followed by a `rubocop:disable` directive' do
237+
expect_offense(<<-RUBY)
238+
RSpec.describe Foo do
239+
# rubocop:disable RSpec/Foo
240+
describe 'bar' do
241+
end
242+
# rubocop:enable RSpec/Foo
243+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `describe`.
244+
# rubocop:disable RSpec/Foo
245+
describe 'baz' do
246+
end
247+
# rubocop:enable RSpec/Foo
248+
end
249+
RUBY
250+
251+
expect_correction(<<-RUBY)
252+
RSpec.describe Foo do
253+
# rubocop:disable RSpec/Foo
254+
describe 'bar' do
255+
end
256+
# rubocop:enable RSpec/Foo
257+
258+
# rubocop:disable RSpec/Foo
259+
describe 'baz' do
260+
end
261+
# rubocop:enable RSpec/Foo
262+
end
263+
RUBY
264+
end
119265
end

spec/rubocop/cop/rspec/empty_line_after_example_spec.rb

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,152 @@
101101
RUBY
102102
end
103103

104+
it 'does not register an offense for a comment followed by an empty line' do
105+
expect_no_offenses(<<-RUBY)
106+
RSpec.describe Foo do
107+
it 'does this' do
108+
end
109+
# comment
110+
111+
it 'does that' do
112+
end
113+
end
114+
RUBY
115+
end
116+
117+
it 'flags a missing empty line before a comment' do
118+
expect_offense(<<-RUBY)
119+
RSpec.describe Foo do
120+
it 'does this' do
121+
end
122+
^^^ Add an empty line after `it`.
123+
# comment
124+
it 'does that' do
125+
end
126+
end
127+
RUBY
128+
129+
expect_correction(<<-RUBY)
130+
RSpec.describe Foo do
131+
it 'does this' do
132+
end
133+
134+
# comment
135+
it 'does that' do
136+
end
137+
end
138+
RUBY
139+
end
140+
141+
it 'flags a missing empty line before a multiline comment' do
142+
expect_offense(<<-RUBY)
143+
RSpec.describe Foo do
144+
it 'does this' do
145+
end
146+
^^^ Add an empty line after `it`.
147+
# multiline comment
148+
# multiline comment
149+
it 'does that' do
150+
end
151+
end
152+
RUBY
153+
154+
expect_correction(<<-RUBY)
155+
RSpec.describe Foo do
156+
it 'does this' do
157+
end
158+
159+
# multiline comment
160+
# multiline comment
161+
it 'does that' do
162+
end
163+
end
164+
RUBY
165+
end
166+
167+
it 'flags a missing empty line after a `rubocop:enable` directive' do
168+
expect_offense(<<-RUBY)
169+
RSpec.describe Foo do
170+
# rubocop:disable RSpec/Foo
171+
it 'does this' do
172+
end
173+
# rubocop:enable RSpec/Foo
174+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `it`.
175+
it 'does that' do
176+
end
177+
end
178+
RUBY
179+
180+
expect_correction(<<-RUBY)
181+
RSpec.describe Foo do
182+
# rubocop:disable RSpec/Foo
183+
it 'does this' do
184+
end
185+
# rubocop:enable RSpec/Foo
186+
187+
it 'does that' do
188+
end
189+
end
190+
RUBY
191+
end
192+
193+
it 'flags a missing empty line before a `rubocop:disable` directive' do
194+
expect_offense(<<-RUBY)
195+
RSpec.describe Foo do
196+
it 'does this' do
197+
end
198+
^^^ Add an empty line after `it`.
199+
# rubocop:disable RSpec/Foo
200+
it 'does that' do
201+
end
202+
# rubocop:enable RSpec/Foo
203+
end
204+
RUBY
205+
206+
expect_correction(<<-RUBY)
207+
RSpec.describe Foo do
208+
it 'does this' do
209+
end
210+
211+
# rubocop:disable RSpec/Foo
212+
it 'does that' do
213+
end
214+
# rubocop:enable RSpec/Foo
215+
end
216+
RUBY
217+
end
218+
219+
it 'flags a missing empty line after a `rubocop:enable` directive '\
220+
'when it is followed by a `rubocop:disable` directive' do
221+
expect_offense(<<-RUBY)
222+
RSpec.describe Foo do
223+
# rubocop:disable RSpec/Foo
224+
it 'does this' do
225+
end
226+
# rubocop:enable RSpec/Foo
227+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `it`.
228+
# rubocop:disable RSpec/Foo
229+
it 'does that' do
230+
end
231+
# rubocop:enable RSpec/Foo
232+
end
233+
RUBY
234+
235+
expect_correction(<<-RUBY)
236+
RSpec.describe Foo do
237+
# rubocop:disable RSpec/Foo
238+
it 'does this' do
239+
end
240+
# rubocop:enable RSpec/Foo
241+
242+
# rubocop:disable RSpec/Foo
243+
it 'does that' do
244+
end
245+
# rubocop:enable RSpec/Foo
246+
end
247+
RUBY
248+
end
249+
104250
context 'when AllowConsecutiveOneLiners is false' do
105251
let(:cop_config) { { 'AllowConsecutiveOneLiners' => false } }
106252

0 commit comments

Comments
 (0)