Skip to content

Commit 0451790

Browse files
committed
Fix a false positive for RSpec/DescribedClass when SkipBlocks is true and numblocks are used
1 parent d4000f8 commit 0451790

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
- Fix a false positive for `RSpec/DescribedClass` when `SkipBlocks` is true and numblocks are used. ([@earlopain])
6+
57
## 3.5.0 (2025-02-16)
68

79
- Don't let `RSpec/PredicateMatcher` replace `respond_to?` with two arguments with the RSpec `respond_to` matcher. ([@bquorning])

Diff for: 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?

Diff for: spec/rubocop/cop/rspec/described_class_spec.rb

+22-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,35 @@
1010
expect_offense(<<~RUBY)
1111
describe MyClass do
1212
controller(ApplicationController) do
13-
bar = MyClass
13+
self.bar = MyClass
14+
end
15+
16+
before do
17+
MyClass
18+
^^^^^^^ Use `described_class` instead of `MyClass`.
19+
20+
Foo.custom_block do
21+
MyClass
22+
end
23+
end
24+
end
25+
RUBY
26+
end
27+
28+
it 'ignores offenses within non-rspec numblocks' do
29+
expect_offense(<<~RUBY)
30+
describe MyClass do
31+
controller(ApplicationController) do
32+
do_some_work(_1)
33+
self.bar = MyClass
1434
end
1535
1636
before do
1737
MyClass
1838
^^^^^^^ Use `described_class` instead of `MyClass`.
1939
2040
Foo.custom_block do
41+
do_some_work(_1)
2142
MyClass
2243
end
2344
end

0 commit comments

Comments
 (0)