File tree Expand file tree Collapse file tree 5 files changed +37
-4
lines changed Expand file tree Collapse file tree 5 files changed +37
-4
lines changed Original file line number Diff line number Diff line change 3
3
## Master (Unreleased)
4
4
5
5
* Add new ` AllowConsecutiveOneLiners ` (default true) option for ` Rspec/EmptyLineAfterHook ` cop. ([ @ngouy ] [ ] )
6
+ * Add autocorrect support for ` RSpec/EmptyExampleGroup ` . ([ @r7kamura ] [ ] )
6
7
7
8
## 2.12.1 (2022-07-03)
8
9
Original file line number Diff line number Diff line change @@ -279,8 +279,9 @@ RSpec/Dialect:
279
279
RSpec/EmptyExampleGroup :
280
280
Description : Checks if an example group does not include any tests.
281
281
Enabled : true
282
+ SafeAutoCorrect : false
282
283
VersionAdded : ' 1.7'
283
- VersionChanged : ' 2.0 '
284
+ VersionChanged : ' 2.13 '
284
285
Reference : https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyExampleGroup
285
286
286
287
RSpec/EmptyHook :
Original file line number Diff line number Diff line change 879
879
880
880
| Enabled
881
881
| Yes
882
- | No
882
+ | Yes (Unsafe)
883
883
| 1.7
884
- | 2.0
884
+ | 2.13
885
885
|===
886
886
887
887
Checks if an example group does not include any tests.
Original file line number Diff line number Diff line change @@ -36,6 +36,10 @@ module RSpec
36
36
# pending 'will add tests later'
37
37
# end
38
38
class EmptyExampleGroup < Base
39
+ extend AutoCorrector
40
+
41
+ include RangeHelp
42
+
39
43
MSG = 'Empty example group detected.'
40
44
41
45
# @!method example_group_body(node)
@@ -135,7 +139,11 @@ def on_block(node)
135
139
return if node . each_ancestor ( :block ) . any? { |block | example? ( block ) }
136
140
137
141
example_group_body ( node ) do |body |
138
- add_offense ( node . send_node ) if offensive? ( body )
142
+ next unless offensive? ( body )
143
+
144
+ add_offense ( node . send_node ) do |corrector |
145
+ corrector . remove ( removed_range ( node ) )
146
+ end
139
147
end
140
148
end
141
149
@@ -163,6 +171,13 @@ def conditionals_with_examples?(body)
163
171
def examples_in_branches? ( condition_node )
164
172
condition_node . branches . any? { |branch | examples? ( branch ) }
165
173
end
174
+
175
+ def removed_range ( node )
176
+ range_by_whole_lines (
177
+ node . location . expression ,
178
+ include_final_newline : true
179
+ )
180
+ end
166
181
end
167
182
end
168
183
end
Original file line number Diff line number Diff line change 19
19
it { should be_true }
20
20
end
21
21
RUBY
22
+
23
+ expect_correction ( <<~RUBY )
24
+ describe Foo do
25
+
26
+ describe '#thingy?' do
27
+ specify do
28
+ expect(whatever.thingy?).to be(true)
29
+ end
30
+ end
31
+
32
+ it { should be_true }
33
+ end
34
+ RUBY
22
35
end
23
36
24
37
it 'flags an empty top level describe' do
27
40
^^^^^^^^^^^^ Empty example group detected.
28
41
end
29
42
RUBY
43
+
44
+ expect_correction ( <<~RUBY )
45
+ RUBY
30
46
end
31
47
32
48
it 'flags example group with examples defined in hooks' do
You can’t perform that action at this time.
0 commit comments