Skip to content

Commit 2ea1fad

Browse files
authored
Merge pull request #1011 from rubocop-hq/fix-leading-subject
Fix subject nesting detection in RSpec/LeadingSubject
2 parents 5719f8f + fefb978 commit 2ea1fad

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

89
## 1.43.1 (2020-08-17)
910

lib/rubocop/cop/rspec/leading_subject.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ def check_previous_nodes(node)
5454
private
5555

5656
def offending_node(node)
57-
node.parent.each_child_node.find do |sibling|
57+
parent(node).each_child_node.find do |sibling|
5858
break if sibling.equal?(node)
5959

6060
yield sibling if offending?(sibling)
6161
end
6262
end
6363

64+
def parent(node)
65+
node.each_ancestor(:block).first.body
66+
end
67+
6468
def autocorrect(corrector, node, sibling)
6569
RuboCop::RSpec::Corrector::MoveNode.new(
6670
node, corrector, processed_source

spec/rubocop/cop/rspec/leading_subject_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,16 @@ def helper_method
269269
end
270270
RUBY
271271
end
272+
273+
it 'ignores subject nested inside a block' do
274+
expect_no_offenses(<<-RUBY)
275+
RSpec.describe User do
276+
let(:foo) { 'bar' }
277+
278+
it_behaves_like 'a good citizen' do
279+
subject { described_class.new }
280+
end
281+
end
282+
RUBY
283+
end
272284
end

0 commit comments

Comments
 (0)