Skip to content

Commit

Permalink
Fix a false positive for RSpec/DescribedClassModuleWrapping when RS…
Browse files Browse the repository at this point in the history
…pec.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.
  • Loading branch information
ydah committed Apr 18, 2023
1 parent 0610b73 commit 6b52c86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/rspec/described_class_module_wrapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions spec/rubocop/cop/rspec/described_class_module_wrapping_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 6b52c86

Please sign in to comment.