diff --git a/CHANGELOG.md b/CHANGELOG.md index 8929d0465..c27d17e16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Master (Unreleased) +## 1.43.2 (2020-08-25) + +* 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) * Fix `RSpec/FilePath` when checking a file defining e.g. an empty class. ([@bquorning][]) diff --git a/lib/rubocop/cop/rspec/file_path.rb b/lib/rubocop/cop/rspec/file_path.rb index c67c73faf..544095dce 100644 --- a/lib/rubocop/cop/rspec/file_path.rb +++ b/lib/rubocop/cop/rspec/file_path.rb @@ -69,7 +69,7 @@ class FilePath < Base def_node_search :routing_metadata?, '(pair (sym :type) (sym :routing))' - def on_top_level_group(node) + def on_top_level_example_group(node) return unless top_level_groups.one? const_described(node) do |send_node, described_class, arguments| diff --git a/lib/rubocop/cop/rspec/leading_subject.rb b/lib/rubocop/cop/rspec/leading_subject.rb index 1789c55b7..16ece6d24 100644 --- a/lib/rubocop/cop/rspec/leading_subject.rb +++ b/lib/rubocop/cop/rspec/leading_subject.rb @@ -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 + end + def autocorrect(corrector, node, sibling) RuboCop::RSpec::Corrector::MoveNode.new( node, corrector, processed_source diff --git a/lib/rubocop/rspec/top_level_group.rb b/lib/rubocop/rspec/top_level_group.rb index d8bc9bbda..cfe56ea95 100644 --- a/lib/rubocop/rspec/top_level_group.rb +++ b/lib/rubocop/rspec/top_level_group.rb @@ -16,7 +16,7 @@ def on_new_investigation return unless root_node top_level_groups.each do |node| - example_group?(node, &method(:on_top_level_example_group)) + on_top_level_example_group(node) if example_group?(node) on_top_level_group(node) end end @@ -29,9 +29,9 @@ def top_level_groups private # Dummy methods to be overridden in the consumer - def on_top_level_example_group; end + def on_top_level_example_group(_node); end - def on_top_level_group; end + def on_top_level_group(_node); end def top_level_group?(node) top_level_groups.include?(node) diff --git a/lib/rubocop/rspec/version.rb b/lib/rubocop/rspec/version.rb index 5996b9bce..f82daf9dd 100644 --- a/lib/rubocop/rspec/version.rb +++ b/lib/rubocop/rspec/version.rb @@ -4,7 +4,7 @@ module RuboCop module RSpec # Version information for the RSpec RuboCop plugin. module Version - STRING = '1.43.1' + STRING = '1.43.2' end end end diff --git a/spec/rubocop/cop/rspec/file_path_spec.rb b/spec/rubocop/cop/rspec/file_path_spec.rb index 97fe04799..fc44613b7 100644 --- a/spec/rubocop/cop/rspec/file_path_spec.rb +++ b/spec/rubocop/cop/rspec/file_path_spec.rb @@ -64,6 +64,12 @@ RUBY end + it 'ignores shared examples' do + expect_no_offenses(<<-RUBY, 'user.rb') + shared_examples_for 'foo' do; end + RUBY + end + it 'skips specs that do not describe a class / method' do expect_no_offenses(<<-RUBY, 'some/class/spec.rb') describe 'Test something' do; end diff --git a/spec/rubocop/cop/rspec/leading_subject_spec.rb b/spec/rubocop/cop/rspec/leading_subject_spec.rb index 9bc7d6982..c359bc2d0 100644 --- a/spec/rubocop/cop/rspec/leading_subject_spec.rb +++ b/spec/rubocop/cop/rspec/leading_subject_spec.rb @@ -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