Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subject nesting detection in RSpec/LeadingSubject #1011

Merged
merged 1 commit into from Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

* Add `RSpec/RepeatedIncludeExample` cop. ([@biinari][])
* Fix `RSpec/FilePath` when checking a file with a shared example. ([@pirj][])
* Fix subject nesting detection in `RSpec/LeadingSubject`. ([@pirj][])

## 1.43.1 (2020-08-17)

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rspec/leading_subject.rb
Expand Up @@ -54,13 +54,17 @@ def check_previous_nodes(node)
private

def offending_node(node)
node.parent.each_child_node.find do |sibling|
parent(node).each_child_node.find do |sibling|
break if sibling.equal?(node)

yield sibling if offending?(sibling)
end
end

def parent(node)
node.each_ancestor(:block).first.body
Copy link
Collaborator

@bquorning bquorning Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pirj Could you help me understand how this fixed the bug? (Extra points for adding the explanation to the commit message ❤️)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Described what was wrong here: 87562ac

By picking the nearest outer block body, we make sure we only pick real siblings inside that block.

end

def autocorrect(corrector, node, sibling)
RuboCop::RSpec::Corrector::MoveNode.new(
node, corrector, processed_source
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/rspec/leading_subject_spec.rb
Expand Up @@ -269,4 +269,16 @@ def helper_method
end
RUBY
end

it 'ignores subject nested inside a block' do
expect_no_offenses(<<-RUBY)
RSpec.describe User do
let(:foo) { 'bar' }

it_behaves_like 'a good citizen' do
subject { described_class.new }
end
end
RUBY
end
end