From 6b52c8609c820d5c7c841a35885c455ab40537d0 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Tue, 18 Apr 2023 14:26:28 +0900 Subject: [PATCH] Fix a false positive for `RSpec/DescribedClassModuleWrapping` when RSpec.describe numblock is nested within a module This PR is fix a false positive for `RSpec/DescribedClassModuleWrapping` when RSpec.describe numblock is nested within a module. --- CHANGELOG.md | 1 + .../rspec/described_class_module_wrapping.rb | 12 +++++----- .../described_class_module_wrapping_spec.rb | 23 +++++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54d5f5644..02e701b41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Master (Unreleased) - Add support `be_status` style for `RSpec/Rails/HttpStatus`. ([@ydah]) +- Fix a false positive for `RSpec/DescribedClassModuleWrapping` when RSpec.describe numblock is nested within a module. ([@ydah]) - Add new `RSpec/IndexedLet` cop. ([@dmitrytsepelev]) - Fix order of expected and actual in correction for `RSpec/Rails/MinitestAssertions` ([@mvz]) - Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` inside `&&`, `||` and `:?` when `omit_parentheses` is on ([@dmitrytsepelev]) diff --git a/lib/rubocop/cop/rspec/described_class_module_wrapping.rb b/lib/rubocop/cop/rspec/described_class_module_wrapping.rb index f35a484da..263bec673 100644 --- a/lib/rubocop/cop/rspec/described_class_module_wrapping.rb +++ b/lib/rubocop/cop/rspec/described_class_module_wrapping.rb @@ -22,15 +22,15 @@ module RSpec class DescribedClassModuleWrapping < Base MSG = 'Avoid opening modules and defining specs within them.' - # @!method find_rspec_blocks(node) - def_node_search :find_rspec_blocks, <<~PATTERN - (block (send #explicit_rspec? #ExampleGroups.all ...) ...) + # @!method include_rspec_blocks?(node) + def_node_search :include_rspec_blocks?, <<~PATTERN + ({block numblock} (send #explicit_rspec? #ExampleGroups.all ...) ...) PATTERN def on_module(node) - find_rspec_blocks(node) do - add_offense(node) - end + return unless include_rspec_blocks?(node) + + add_offense(node) end end end diff --git a/spec/rubocop/cop/rspec/described_class_module_wrapping_spec.rb b/spec/rubocop/cop/rspec/described_class_module_wrapping_spec.rb index dc1e33535..583b2a002 100644 --- a/spec/rubocop/cop/rspec/described_class_module_wrapping_spec.rb +++ b/spec/rubocop/cop/rspec/described_class_module_wrapping_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe RuboCop::Cop::RSpec::DescribedClassModuleWrapping do +RSpec.describe RuboCop::Cop::RSpec::DescribedClassModuleWrapping, :ruby27 do it 'allows a describe block in the outermost scope' do expect_no_offenses(<<-RUBY) RSpec.describe MyClass do @@ -9,7 +9,8 @@ RUBY end - it 'registers an offense when RSpec.describe is nested within a module' do + it 'registers an offense when RSpec.describe block is nested ' \ + 'within a module' do expect_offense(<<-RUBY) module MyModule ^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them. @@ -21,7 +22,21 @@ module MyModule RUBY end - it 'registers an offense when RSpec.describe is nested within two modules' do + it 'registers an offense when RSpec.describe numblock is nested ' \ + 'within a module' do + expect_offense(<<-RUBY) + module MyModule + ^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them. + RSpec.describe MyClass do + _1 + subject { "MyClass" } + end + end + RUBY + end + + it 'registers an offense when RSpec.describe block is nested ' \ + 'within two modules' do expect_offense(<<-RUBY) module MyFirstModule ^^^^^^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them. @@ -36,7 +51,7 @@ module MySecondModule RUBY end - it 'allows a module that does not contain RSpec.describe' do + it 'allows a module that does not contain RSpec.describe block' do expect_no_offenses(<<-RUBY) module MyModule def some_method