Skip to content

Commit

Permalink
Fix an error for RSpec/ScatteredSetup when one of the hooks is an e…
Browse files Browse the repository at this point in the history
…mpty block
  • Loading branch information
Earlopain committed Jun 9, 2024
1 parent 06fd3a0 commit e92b770
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Support `AutoCorrect: contextual` option for LSP. ([@ydah])
- Enable all pending cops. ([@bquorning])
- Add new `RSpec/MissingExpectationTargetMethod` cop. ([@krororo])
- Fix an error for `RSpec/ScatteredSetup` when one of the hooks is an empty block. ([@earlopain])

## 2.29.2 (2024-05-02)

Expand Down Expand Up @@ -894,6 +895,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@drowze]: https://github.com/Drowze
[@dswij]: https://github.com/dswij
[@dvandersluis]: https://github.com/dvandersluis
[@earlopain]: https://github.com/earlopain
[@edgibbs]: https://github.com/edgibbs
[@eikes]: https://github.com/eikes
[@eitoball]: https://github.com/eitoball
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/scattered_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def autocorrect(corrector, first_occurrence, occurrence)
return if first_occurrence == occurrence || !first_occurrence.body

corrector.insert_after(first_occurrence.body,
"\n#{occurrence.body.source}")
"\n#{occurrence.body&.source}")
corrector.remove(range_by_whole_lines(occurrence.source_range,
include_final_newline: true))
end
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/rspec/scattered_setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,22 @@
end
RUBY
end

it 'flags hooks when one of the hooks is an empty block' do
expect_offense(<<~RUBY)
describe Foo do
before { do_something }
^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 3).
before { }
^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 2).
end
RUBY

expect_correction(<<~RUBY)
describe Foo do
before { do_something
}
end
RUBY
end
end

0 comments on commit e92b770

Please sign in to comment.