Skip to content

Commit 0227e6e

Browse files
authored
Merge pull request #1316 from r7kamura/feature/empty-example-group
Add autocorrect support for `RSpec/EmptyExampleGroup`
2 parents ca36f7e + 1f4b02c commit 0227e6e

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
* Add new `AllowConsecutiveOneLiners` (default true) option for `Rspec/EmptyLineAfterHook` cop. ([@ngouy][])
6+
* Add autocorrect support for `RSpec/EmptyExampleGroup`. ([@r7kamura][])
67

78
## 2.12.1 (2022-07-03)
89

config/default.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,9 @@ RSpec/Dialect:
279279
RSpec/EmptyExampleGroup:
280280
Description: Checks if an example group does not include any tests.
281281
Enabled: true
282+
SafeAutoCorrect: false
282283
VersionAdded: '1.7'
283-
VersionChanged: '2.0'
284+
VersionChanged: '2.13'
284285
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyExampleGroup
285286

286287
RSpec/EmptyHook:

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,9 @@ end
879879

880880
| Enabled
881881
| Yes
882-
| No
882+
| Yes (Unsafe)
883883
| 1.7
884-
| 2.0
884+
| 2.13
885885
|===
886886

887887
Checks if an example group does not include any tests.

lib/rubocop/cop/rspec/empty_example_group.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ module RSpec
3636
# pending 'will add tests later'
3737
# end
3838
class EmptyExampleGroup < Base
39+
extend AutoCorrector
40+
41+
include RangeHelp
42+
3943
MSG = 'Empty example group detected.'
4044

4145
# @!method example_group_body(node)
@@ -135,7 +139,11 @@ def on_block(node)
135139
return if node.each_ancestor(:block).any? { |block| example?(block) }
136140

137141
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
139147
end
140148
end
141149

@@ -163,6 +171,13 @@ def conditionals_with_examples?(body)
163171
def examples_in_branches?(condition_node)
164172
condition_node.branches.any? { |branch| examples?(branch) }
165173
end
174+
175+
def removed_range(node)
176+
range_by_whole_lines(
177+
node.location.expression,
178+
include_final_newline: true
179+
)
180+
end
166181
end
167182
end
168183
end

spec/rubocop/cop/rspec/empty_example_group_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919
it { should be_true }
2020
end
2121
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
2235
end
2336

2437
it 'flags an empty top level describe' do
@@ -27,6 +40,9 @@
2740
^^^^^^^^^^^^ Empty example group detected.
2841
end
2942
RUBY
43+
44+
expect_correction(<<~RUBY)
45+
RUBY
3046
end
3147

3248
it 'flags example group with examples defined in hooks' do

0 commit comments

Comments
 (0)