Skip to content

Commit

Permalink
Fix RSpec/NamedSubject when block has no body
Browse files Browse the repository at this point in the history
  • Loading branch information
splattael committed Jul 7, 2023
1 parent 1eeaaad commit 674a20a
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 @@ -7,6 +7,7 @@
- Update the message output of `RSpec/ExpectActual` to include the word 'value'. ([@corydiamand])
- Fix a false negative for `RSpec/Pending` when `it` without body. ([@ydah])
- Add new `RSpec/ReceiveMessages` cop. ([@ydah])
- Fix `RSpec/NamedSubject` when block has no body. ([@splattael])

## 2.22.0 (2023-05-06)

Expand Down Expand Up @@ -866,6 +867,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 674a20a

Please sign in to comment.