+85
-2
lines changedFilter options
+85
-2
lines changed Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
+
* [#14232](https://github.com/rubocop/rubocop/issues/14232): Add `AllowedPatterns` and `AllowBangMethods` configuration to `Naming/PredicateMethod`. ([@dvandersluis][])
Original file line number Diff line number Diff line change
@@ -3063,13 +3063,16 @@ Naming/PredicateMethod:
3063
3063
Description: 'Checks that predicate methods end with `?` and non-predicate methods do not.'
3064
3064
Enabled: pending
3065
3065
VersionAdded: '1.76'
3066
+
VersionChanged: '<<next>>'
3066
3067
# In `aggressive` mode, the cop will register an offense for predicate methods that
3067
3068
# may return a non-boolean value.
3068
3069
# In `conservative` mode, the cop will *not* register an offense for predicate methods
3069
3070
# that may return a non-boolean value.
3070
3071
Mode: conservative
3071
3072
AllowedMethods:
3072
3073
- call
3074
+
AllowedPatterns: []
3075
+
AllowBangMethods: false
3073
3076
3074
3077
Naming/PredicatePrefix:
3075
3078
Description: 'Predicate method names should not be prefixed and end with a `?`.'
Original file line number Diff line number Diff line change
@@ -22,7 +22,11 @@ module Naming
22
22
#
23
23
# The cop also has `AllowedMethods` configuration in order to prevent the cop from
24
24
# registering an offense from a method name that does not confirm to the naming
25
-
# guidelines. By default, `call` is allowed.
25
+
# guidelines. By default, `call` is allowed. The cop also has `AllowedPatterns`
26
+
# configuration to allow method names by regular expression.
27
+
#
28
+
# The cop can furthermore be configured to allow all bang methods (method names
29
+
# ending with `!`), with `AllowBangMethods: true` (default false).
26
30
#
27
31
# @example Mode: conservative (default)
28
32
# # bad
@@ -73,8 +77,21 @@ module Naming
73
77
# true
74
78
# end
75
79
#
80
+
# @example AllowBangMethods: false (default)
81
+
# # bad
82
+
# def save!
83
+
# true
84
+
# end
85
+
#
86
+
# @example AllowBangMethods: true
87
+
# # good
88
+
# def save!
89
+
# true
90
+
# end
91
+
#
76
92
class PredicateMethod < Base
77
93
include AllowedMethods
94
+
include AllowedPattern
78
95
79
96
MSG_PREDICATE = 'Predicate method names should end with `?`.'
80
97
MSG_NON_PREDICATE = 'Non-predicate method names should not end with `?`.'
@@ -97,6 +114,8 @@ def on_def(node)
97
114
98
115
def allowed?(node)
99
116
allowed_method?(node.method_name) ||
117
+
matches_allowed_pattern?(node.method_name) ||
118
+
allowed_bang_method?(node) ||
100
119
node.operator_method? ||
101
120
node.body.nil?
102
121
end
@@ -210,6 +229,16 @@ def extract_conditional_branches(node)
210
229
def conservative?
211
230
cop_config.fetch('Mode', :conservative).to_sym == :conservative
212
231
end
232
+
233
+
def allowed_bang_method?(node)
234
+
return false unless allow_bang_methods?
235
+
236
+
node.bang_method?
237
+
end
238
+
239
+
def allow_bang_methods?
240
+
cop_config.fetch('AllowBangMethods', false)
241
+
end
213
242
end
214
243
end
215
244
end
Original file line number Diff line number Diff line change
@@ -2,10 +2,14 @@
2
2
3
3
RSpec.describe RuboCop::Cop::Naming::PredicateMethod, :config do
4
4
let(:allowed_methods) { [] }
5
+
let(:allowed_patterns) { [] }
6
+
let(:allow_bang_methods) { false }
5
7
let(:cop_config) do
6
8
{
7
9
'Mode' => mode,
8
-
'AllowedMethods' => allowed_methods
10
+
'AllowedMethods' => allowed_methods,
11
+
'AllowedPatterns' => allowed_patterns,
12
+
'AllowBangMethods' => allow_bang_methods
9
13
}
10
14
end
11
15
@@ -513,6 +517,52 @@ def on_defined?(node)
513
517
RUBY
514
518
end
515
519
end
520
+
521
+
context 'with AllowedPatterns' do
522
+
let(:allowed_patterns) { %w[\Afoo] }
523
+
524
+
it 'does not register an offense for a method name that matches the pattern' do
525
+
expect_no_offenses(<<~RUBY)
526
+
def foo?
527
+
'foo'
528
+
end
529
+
RUBY
530
+
end
531
+
532
+
it 'registers an offense for a method name that does not match the pattern' do
533
+
expect_offense(<<~RUBY)
534
+
def barfoo?
535
+
^^^^^^^ Non-predicate method names should not end with `?`.
536
+
'bar'
537
+
end
538
+
RUBY
539
+
end
540
+
end
541
+
542
+
context 'with AllowBangMethods: true' do
543
+
let(:allow_bang_methods) { true }
544
+
545
+
it 'does not register an offense for a bang method that returns a boolean' do
546
+
expect_no_offenses(<<~RUBY)
547
+
def save!
548
+
true
549
+
end
550
+
RUBY
551
+
end
552
+
end
553
+
554
+
context 'with AllowBangMethods: false' do
555
+
let(:allow_bang_methods) { false }
556
+
557
+
it 'registers an offense for a bang method that returns a boolean' do
558
+
expect_offense(<<~RUBY)
559
+
def save!
560
+
^^^^^ Predicate method names should end with `?`.
561
+
true
562
+
end
563
+
RUBY
564
+
end
565
+
end
516
566
end
517
567
518
568
context 'with Mode: conservative' do
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4