Skip to content

Commit

Permalink
Merge pull request #1668 from splattael/named-subject-block-without-body
Browse files Browse the repository at this point in the history
Fix `RSpec/NamedSubject` when block has no body
  • Loading branch information
pirj authored Jul 17, 2023
2 parents 3a2cd96 + 678aceb commit 003e208
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix a false negative for `RSpec/Pending` when `it` without body. ([@ydah])
- Add new `RSpec/ReceiveMessages` cop. ([@ydah])
- Add `AllowedIdentifiers` and `AllowedPatterns` configuration option to `RSpec/IndexedLet`. ([@ydah])
- Fix `RSpec/NamedSubject` when block has no body. ([@splattael])

## 2.22.0 (2023-05-06)

Expand Down Expand Up @@ -867,6 +868,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@seanpdoyle]: https://github.com/seanpdoyle
[@sl4vr]: https://github.com/sl4vr
[@smcgivern]: https://github.com/smcgivern
[@splattael]: https://github.com/splattael
[@stephannv]: https://github.com/stephannv
[@t3h2mas]: https://github.com/t3h2mas
[@tdeo]: https://github.com/tdeo
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/named_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def nearest_subject(node)
end

def find_subject(block_node)
block_node.body.child_nodes.find { |send_node| subject?(send_node) }
block_node.body&.child_nodes&.find { |send_node| subject?(send_node) }
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/rspec/named_subject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ def foo
end
RUBY
end

it 'ignores subject when block has no body' do
expect_no_offenses(<<-RUBY)
it "is a User" do
subject.each do
# empty body
end
end
RUBY
end
end

context 'when IgnoreSharedExamples is false' do
Expand Down

0 comments on commit 003e208

Please sign in to comment.