Skip to content

Commit bf6c177

Browse files
committed
Add new for AllowAdjacentOneLineHooks option for Rspec/EmptyLineAfterHook
1 parent a8b16a6 commit bf6c177

File tree

5 files changed

+490
-228
lines changed

5 files changed

+490
-228
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
* Add new `AllowConsecutiveOneLiners` (default true) option for `Rspec/EmptyLineAfterHook` cop. ([@ngouy][])
6+
57
## 2.12.1 (2022-07-03)
68

79
* Fix a false positive for `RSpec/Capybara/SpecificMatcher`. ([@ydah][])

config/default.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,10 @@ RSpec/EmptyLineAfterHook:
315315
Description: Checks if there is an empty line after hook blocks.
316316
Enabled: true
317317
VersionAdded: '1.27'
318+
VersionChanged: '2.13'
318319
StyleGuide: https://rspec.rubystyle.guide/#empty-line-after-let
319320
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterHook
321+
AllowConsecutiveOneLiners: true
320322

321323
RSpec/EmptyLineAfterSubject:
322324
Description: Checks if there is an empty line after subject block.

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,11 +1123,14 @@ it { does_something }
11231123
| Yes
11241124
| Yes
11251125
| 1.27
1126-
| -
1126+
| 2.13
11271127
|===
11281128

11291129
Checks if there is an empty line after hook blocks.
11301130

1131+
`AllowConsecutiveOneLiners` configures whether adjacent
1132+
one-line definitions are considered an offense.
1133+
11311134
=== Examples
11321135

11331136
[source,ruby]
@@ -1145,21 +1148,49 @@ around { |test| test.run }
11451148
it { does_something }
11461149
11471150
# good
1148-
before { do_something }
1151+
after { do_something }
11491152
11501153
it { does_something }
11511154
1152-
# good
1155+
# fair - it's ok to have non-separated one-liners hooks
1156+
around { |test| test.run }
1157+
after { do_something }
1158+
1159+
it { does_something }
1160+
----
1161+
1162+
==== with AllowConsecutiveOneLiners configuration
1163+
1164+
[source,ruby]
1165+
----
1166+
# rubocop.yml
1167+
# RSpec/EmptyLineAfterHook:
1168+
# AllowConsecutiveOneLiners: false
1169+
1170+
# bad
1171+
around { |test| test.run }
11531172
after { do_something }
11541173
11551174
it { does_something }
11561175
11571176
# good
11581177
around { |test| test.run }
11591178
1179+
after { do_something }
1180+
11601181
it { does_something }
11611182
----
11621183

1184+
=== Configurable attributes
1185+
1186+
|===
1187+
| Name | Default value | Configurable values
1188+
1189+
| AllowConsecutiveOneLiners
1190+
| `true`
1191+
| Boolean
1192+
|===
1193+
11631194
=== References
11641195

11651196
* https://rspec.rubystyle.guide/#empty-line-after-let

lib/rubocop/cop/rspec/empty_line_after_hook.rb

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module Cop
55
module RSpec
66
# Checks if there is an empty line after hook blocks.
77
#
8+
# `AllowConsecutiveOneLiners` configures whether adjacent
9+
# one-line definitions are considered an offense.
10+
#
811
# @example
912
# # bad
1013
# before { do_something }
@@ -19,33 +22,57 @@ module RSpec
1922
# it { does_something }
2023
#
2124
# # good
22-
# before { do_something }
25+
# after { do_something }
2326
#
2427
# it { does_something }
2528
#
26-
# # good
29+
# # fair - it's ok to have non-separated one-liners hooks
30+
# around { |test| test.run }
2731
# after { do_something }
2832
#
2933
# it { does_something }
3034
#
31-
# # good
35+
# @example with AllowConsecutiveOneLiners configuration
36+
# # rubocop.yml
37+
# # RSpec/EmptyLineAfterHook:
38+
# # AllowConsecutiveOneLiners: false
39+
#
40+
# # bad
3241
# around { |test| test.run }
42+
# after { do_something }
3343
#
3444
# it { does_something }
3545
#
46+
# # good
47+
# around { |test| test.run }
48+
#
49+
# after { do_something }
50+
#
51+
# it { does_something }
3652
class EmptyLineAfterHook < Base
3753
extend AutoCorrector
54+
include ConfigurableEnforcedStyle
3855
include EmptyLineSeparation
3956

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

4259
def on_block(node)
4360
return unless hook?(node)
61+
return if cop_config['AllowConsecutiveOneLiners'] &&
62+
chained_single_line_hooks?(node)
4463

4564
missing_separating_line_offense(node) do |method|
4665
format(MSG, hook: method)
4766
end
4867
end
68+
69+
private
70+
71+
def chained_single_line_hooks?(node)
72+
next_node = node.right_sibling
73+
74+
hook?(next_node) && node.single_line? && next_node.single_line?
75+
end
4976
end
5077
end
5178
end

0 commit comments

Comments
 (0)