Skip to content

Commit 0ba0917

Browse files
committed
Fix a false positive for RSpec/DescribedClass when SkipBlocks is true and numblocks are used
1 parent 436a4d4 commit 0ba0917

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add new `RSpec/IncludeExamples` cop to enforce using `it_behaves_like` over `include_examples`. ([@dvandersluis])
88
- Change `RSpec/ScatteredSetup` to allow `around` hooks to be scattered. ([@ydah])
99
- Fix an error `RSpec/ChangeByZero` cop when without expect block. ([@lee266])
10+
- Fix a false positive for `RSpec/DescribedClass` when `SkipBlocks` is true and numblocks are used. ([@earlopain])
1011

1112
## 3.5.0 (2025-02-16)
1213

lib/rubocop/cop/rspec/described_class.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ def scope_change?(node)
153153
end
154154

155155
def skippable_block?(node)
156-
node.block_type? && !rspec_block?(node) && cop_config['SkipBlocks']
156+
return false unless cop_config['SkipBlocks']
157+
158+
node.any_block_type? && !rspec_block?(node)
157159
end
158160

159161
def only_static_constants?

spec/rubocop/cop/rspec/described_class_spec.rb

+23-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
expect_offense(<<~RUBY)
1111
describe MyClass do
1212
controller(ApplicationController) do
13-
bar = MyClass
13+
self.bar = MyClass
1414
end
1515
1616
before do
@@ -27,7 +27,7 @@
2727
expect_correction(<<~RUBY)
2828
describe MyClass do
2929
controller(ApplicationController) do
30-
bar = MyClass
30+
self.bar = MyClass
3131
end
3232
3333
before do
@@ -40,6 +40,27 @@
4040
end
4141
RUBY
4242
end
43+
44+
it 'ignores offenses within non-rspec numblocks' do
45+
expect_offense(<<~RUBY)
46+
describe MyClass do
47+
controller(ApplicationController) do
48+
do_some_work(_1)
49+
self.bar = MyClass
50+
end
51+
52+
before do
53+
MyClass
54+
^^^^^^^ Use `described_class` instead of `MyClass`.
55+
56+
Foo.custom_block do
57+
do_some_work(_1)
58+
MyClass
59+
end
60+
end
61+
end
62+
RUBY
63+
end
4364
end
4465

4566
context 'when SkipBlocks is `false`' do

0 commit comments

Comments
 (0)