Skip to content

Commit 35e07d5

Browse files
authored
Merge pull request #1574 from ydah/described-class-module-wrapping
Fix a false positive for `RSpec/DescribedClassModuleWrapping` when RSpec.describe numblock is nested within a module
2 parents 0610b73 + 6b52c86 commit 35e07d5

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
- Add support `be_status` style for `RSpec/Rails/HttpStatus`. ([@ydah])
6+
- Fix a false positive for `RSpec/DescribedClassModuleWrapping` when RSpec.describe numblock is nested within a module. ([@ydah])
67
- Add new `RSpec/IndexedLet` cop. ([@dmitrytsepelev])
78
- Fix order of expected and actual in correction for `RSpec/Rails/MinitestAssertions` ([@mvz])
89
- Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` inside `&&`, `||` and `:?` when `omit_parentheses` is on ([@dmitrytsepelev])

lib/rubocop/cop/rspec/described_class_module_wrapping.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ module RSpec
2222
class DescribedClassModuleWrapping < Base
2323
MSG = 'Avoid opening modules and defining specs within them.'
2424

25-
# @!method find_rspec_blocks(node)
26-
def_node_search :find_rspec_blocks, <<~PATTERN
27-
(block (send #explicit_rspec? #ExampleGroups.all ...) ...)
25+
# @!method include_rspec_blocks?(node)
26+
def_node_search :include_rspec_blocks?, <<~PATTERN
27+
({block numblock} (send #explicit_rspec? #ExampleGroups.all ...) ...)
2828
PATTERN
2929

3030
def on_module(node)
31-
find_rspec_blocks(node) do
32-
add_offense(node)
33-
end
31+
return unless include_rspec_blocks?(node)
32+
33+
add_offense(node)
3434
end
3535
end
3636
end

spec/rubocop/cop/rspec/described_class_module_wrapping_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
RSpec.describe RuboCop::Cop::RSpec::DescribedClassModuleWrapping do
3+
RSpec.describe RuboCop::Cop::RSpec::DescribedClassModuleWrapping, :ruby27 do
44
it 'allows a describe block in the outermost scope' do
55
expect_no_offenses(<<-RUBY)
66
RSpec.describe MyClass do
@@ -9,7 +9,8 @@
99
RUBY
1010
end
1111

12-
it 'registers an offense when RSpec.describe is nested within a module' do
12+
it 'registers an offense when RSpec.describe block is nested ' \
13+
'within a module' do
1314
expect_offense(<<-RUBY)
1415
module MyModule
1516
^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them.
@@ -21,7 +22,21 @@ module MyModule
2122
RUBY
2223
end
2324

24-
it 'registers an offense when RSpec.describe is nested within two modules' do
25+
it 'registers an offense when RSpec.describe numblock is nested ' \
26+
'within a module' do
27+
expect_offense(<<-RUBY)
28+
module MyModule
29+
^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them.
30+
RSpec.describe MyClass do
31+
_1
32+
subject { "MyClass" }
33+
end
34+
end
35+
RUBY
36+
end
37+
38+
it 'registers an offense when RSpec.describe block is nested ' \
39+
'within two modules' do
2540
expect_offense(<<-RUBY)
2641
module MyFirstModule
2742
^^^^^^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them.
@@ -36,7 +51,7 @@ module MySecondModule
3651
RUBY
3752
end
3853

39-
it 'allows a module that does not contain RSpec.describe' do
54+
it 'allows a module that does not contain RSpec.describe block' do
4055
expect_no_offenses(<<-RUBY)
4156
module MyModule
4257
def some_method

0 commit comments

Comments
 (0)