Skip to content

Commit ff444c2

Browse files
authored
Merge pull request #1982 from rubocop/improve-cwc
Change `RSpec/ContextWording` cop to always report an offense when both `Prefixes` and `AllowedPatterns` are empty
2 parents 7592cec + 5a75070 commit ff444c2

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
- Change `RSpec/ContextWording` cop to always report an offense when both `Prefixes` and `AllowedPatterns` are empty. ([@ydah])
6+
57
## 3.1.0 (2024-10-01)
68

79
- Add `RSpec/StringAsInstanceDoubleConstant` to check for and correct strings used as instance_doubles. ([@corsonknowles])

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ the configuration to meet project needs. Other acceptable prefixes may
720720
include `if`, `unless`, `for`, `before`, `after`, or `during`.
721721
They may consist of multiple words if desired.
722722
723+
If both `Prefixes` and `AllowedPatterns` are empty, this cop will always
724+
report an offense. So you need to set at least one of them.
725+
723726
This cop can be customized allowed context description pattern
724727
with `AllowedPatterns`. By default, there are no checking by pattern.
725728

lib/rubocop/cop/rspec/context_wording.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ module RSpec
1212
#
1313
# @see http://www.betterspecs.org/#contexts
1414
#
15+
# If both `Prefixes` and `AllowedPatterns` are empty, this cop will always
16+
# report an offense. So you need to set at least one of them.
17+
#
1518
# @example `Prefixes` configuration
1619
# # .rubocop.yml
1720
# # RSpec/ContextWording:
@@ -58,7 +61,9 @@ module RSpec
5861
class ContextWording < Base
5962
include AllowedPattern
6063

61-
MSG = 'Context description should match %<patterns>s.'
64+
MSG_MATCH = 'Context description should match %<patterns>s.'
65+
MSG_ALWAYS = 'Current settings will always report an offense. Please ' \
66+
'add allowed words to `Prefixes` or `AllowedPatterns`.'
6267

6368
# @!method context_wording(node)
6469
def_node_matcher :context_wording, <<~PATTERN
@@ -67,8 +72,7 @@ class ContextWording < Base
6772

6873
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
6974
context_wording(node) do |context|
70-
if bad_pattern?(context)
71-
message = format(MSG, patterns: expect_patterns)
75+
unless matches_allowed_pattern?(description(context))
7276
add_offense(context, message: message)
7377
end
7478
end
@@ -84,12 +88,6 @@ def prefix_regexes
8488
@prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ }
8589
end
8690

87-
def bad_pattern?(node)
88-
return false if allowed_patterns.empty?
89-
90-
!matches_allowed_pattern?(description(node))
91-
end
92-
9391
def description(context)
9492
if context.xstr_type?
9593
context.value.value
@@ -98,6 +96,14 @@ def description(context)
9896
end
9997
end
10098

99+
def message
100+
if allowed_patterns.empty?
101+
MSG_ALWAYS
102+
else
103+
format(MSG_MATCH, patterns: expect_patterns)
104+
end
105+
end
106+
101107
def expect_patterns
102108
inspected = allowed_patterns.map do |pattern|
103109
pattern.inspect.gsub(/\A"|"\z/, '/')

spec/rubocop/cop/rspec/context_wording_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@
220220
{ 'Prefixes' => [], 'AllowedPatterns' => [] }
221221
end
222222

223-
it 'skips any description' do
224-
expect_no_offenses(<<~RUBY)
225-
context 'arbitrary text' do
223+
it 'always registers an offense' do
224+
expect_offense(<<~RUBY)
225+
context 'this is an incorrect context' do
226+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Current settings will always report an offense. Please add allowed words to `Prefixes` or `AllowedPatterns`.
226227
end
227228
RUBY
228229
end

0 commit comments

Comments
 (0)