File tree 6 files changed +75
-0
lines changed
6 files changed +75
-0
lines changed Original file line number Diff line number Diff line change @@ -255,3 +255,5 @@ Style/YAMLFileRead: {Enabled: true}
255
255
#
256
256
RSpec/StringAsInstanceDoubleConstant :
257
257
Enabled : true
258
+ RSpec/RedundantContext :
259
+ Enabled : true
Original file line number Diff line number Diff line change 2
2
3
3
## Master (Unreleased)
4
4
5
+ - Add new ` RSpec/RedundantContext ` cop. ([ @tejasbubane ] )
6
+
5
7
## 3.3.0 (2024-12-12)
6
8
7
9
- Deprecate ` top_level_group? ` method from ` TopLevelGroup ` mixin as all of its callers were intentionally removed from ` Rubocop/RSpec ` . ([ @corsonknowles ] )
Original file line number Diff line number Diff line change @@ -794,6 +794,12 @@ RSpec/RedundantAround:
794
794
VersionAdded : ' 2.19'
795
795
Reference : https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantAround
796
796
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
+
797
803
RSpec/RedundantPredicateMatcher :
798
804
Description : Checks for redundant predicate matcher.
799
805
Enabled : true
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 81
81
require_relative 'rspec/receive_messages'
82
82
require_relative 'rspec/receive_never'
83
83
require_relative 'rspec/redundant_around'
84
+ require_relative 'rspec/redundant_context'
84
85
require_relative 'rspec/redundant_predicate_matcher'
85
86
require_relative 'rspec/remove_const'
86
87
require_relative 'rspec/repeated_description'
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments