Skip to content

Commit 4d58956

Browse files
committed
[Fix #2001] Add new RedundantContext cop
To check context with single example. Closes #2001
1 parent 162d91a commit 4d58956

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

.rubocop.yml

+2
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,5 @@ Style/YAMLFileRead: {Enabled: true}
255255
#
256256
RSpec/StringAsInstanceDoubleConstant:
257257
Enabled: true
258+
RSpec/RedundantContext:
259+
Enabled: true

CHANGELOG.md

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

33
## Master (Unreleased)
44

5+
- Add new `RSpec/RedundantContext` cop. ([@tejasbubane])
6+
57
## 3.3.0 (2024-12-12)
68

79
- Deprecate `top_level_group?` method from `TopLevelGroup` mixin as all of its callers were intentionally removed from `Rubocop/RSpec`. ([@corsonknowles])

config/default.yml

+6
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,12 @@ RSpec/RedundantAround:
794794
VersionAdded: '2.19'
795795
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantAround
796796

797+
RSpec/RedundantContext:
798+
Description: Detect redundant `context` hook.
799+
Enabled: pending
800+
VersionAdded: "<<next>>"
801+
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantContext
802+
797803
RSpec/RedundantPredicateMatcher:
798804
Description: Checks for redundant predicate matcher.
799805
Enabled: true
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module RSpec
6+
# Detect redundant `context` hook.
7+
#
8+
# @example
9+
# # bad
10+
# context 'when condition' do
11+
# it 'tests something' do
12+
# end
13+
# end
14+
#
15+
# # good
16+
# it 'tests something when condition' do
17+
# end
18+
#
19+
class RedundantContext < Base
20+
MSG = 'Redundant context with single example.'
21+
22+
# @!method redundant_context?(node)
23+
def_node_matcher :redundant_context?, <<~PATTERN
24+
(block
25+
(send #rspec? :context _)
26+
_
27+
(block (send _ :it ...) ...))
28+
PATTERN
29+
30+
def on_block(node)
31+
return unless redundant_context?(node)
32+
33+
add_offense(node)
34+
end
35+
alias on_numblock on_block
36+
end
37+
end
38+
end
39+
end

lib/rubocop/cop/rspec_cops.rb

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
require_relative 'rspec/receive_messages'
8282
require_relative 'rspec/receive_never'
8383
require_relative 'rspec/redundant_around'
84+
require_relative 'rspec/redundant_context'
8485
require_relative 'rspec/redundant_predicate_matcher'
8586
require_relative 'rspec/remove_const'
8687
require_relative 'rspec/repeated_description'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::RSpec::RedundantContext do
4+
it 'registers an offense when single example inside context' do
5+
expect_offense(<<~RUBY)
6+
context 'when condition' do
7+
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant context with single example.
8+
it 'does something' do
9+
end
10+
end
11+
RUBY
12+
end
13+
14+
it 'does not register offense when multiple examples inside context' do
15+
expect_no_offenses(<<~RUBY)
16+
context 'when condition' do
17+
it 'does something' do
18+
end
19+
20+
it 'does something else' do
21+
end
22+
end
23+
RUBY
24+
end
25+
end

0 commit comments

Comments
 (0)