Skip to content

Commit 38fd7bd

Browse files
authored
Merge pull request #1912 from Earlopain/error-scattered-setup
Fix an error for `RSpec/ScatteredSetup` when one of the hooks is an empty block
2 parents ddf763b + e92b770 commit 38fd7bd

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Support `AutoCorrect: contextual` option for LSP. ([@ydah])
1111
- Enable all pending cops. ([@bquorning])
1212
- Add new `RSpec/MissingExpectationTargetMethod` cop. ([@krororo])
13+
- Fix an error for `RSpec/ScatteredSetup` when one of the hooks is an empty block. ([@earlopain])
1314

1415
## 2.29.2 (2024-05-02)
1516

@@ -894,6 +895,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
894895
[@drowze]: https://github.com/Drowze
895896
[@dswij]: https://github.com/dswij
896897
[@dvandersluis]: https://github.com/dvandersluis
898+
[@earlopain]: https://github.com/earlopain
897899
[@edgibbs]: https://github.com/edgibbs
898900
[@eikes]: https://github.com/eikes
899901
[@eitoball]: https://github.com/eitoball

lib/rubocop/cop/rspec/scattered_setup.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def autocorrect(corrector, first_occurrence, occurrence)
7676
return if first_occurrence == occurrence || !first_occurrence.body
7777

7878
corrector.insert_after(first_occurrence.body,
79-
"\n#{occurrence.body.source}")
79+
"\n#{occurrence.body&.source}")
8080
corrector.remove(range_by_whole_lines(occurrence.source_range,
8181
include_final_newline: true))
8282
end

spec/rubocop/cop/rspec/scattered_setup_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,22 @@
150150
end
151151
RUBY
152152
end
153+
154+
it 'flags hooks when one of the hooks is an empty block' do
155+
expect_offense(<<~RUBY)
156+
describe Foo do
157+
before { do_something }
158+
^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 3).
159+
before { }
160+
^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 2).
161+
end
162+
RUBY
163+
164+
expect_correction(<<~RUBY)
165+
describe Foo do
166+
before { do_something
167+
}
168+
end
169+
RUBY
170+
end
153171
end

0 commit comments

Comments
 (0)