File tree Expand file tree Collapse file tree 6 files changed +75
-0
lines changed Expand file tree Collapse file tree 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}
255255#
256256RSpec/StringAsInstanceDoubleConstant :
257257 Enabled : true
258+ RSpec/RedundantContext :
259+ Enabled : true
Original file line number Diff line number Diff line change 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 ] )
Original file line number Diff line number Diff 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+
797803RSpec/RedundantPredicateMatcher :
798804 Description : Checks for redundant predicate matcher.
799805 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 8181require_relative 'rspec/receive_messages'
8282require_relative 'rspec/receive_never'
8383require_relative 'rspec/redundant_around'
84+ require_relative 'rspec/redundant_context'
8485require_relative 'rspec/redundant_predicate_matcher'
8586require_relative 'rspec/remove_const'
8687require_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