Skip to content

Commit

Permalink
Add a validate_config for Naming/PredicateName
Browse files Browse the repository at this point in the history
This will help make sure that users config makes sense and achieves the
behavior they intend it to, even if they don't read the docs.

This also eases the burden of the docs to mention this case so loudly.
  • Loading branch information
maxjacobson authored and bbatsov committed Jul 31, 2024
1 parent 68bd9da commit 2443641
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13068](https://github.com/rubocop/rubocop/pull/13068): Add config validation to `Naming/PredicateName` to check that all `ForbiddenPrefixes` are being checked. ([@maxjacobson][])
10 changes: 10 additions & 0 deletions lib/rubocop/cop/naming/predicate_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ def on_def(node)
end
alias on_defs on_def

def validate_config
forbidden_prefixes.each do |forbidden_prefix|
next if predicate_prefixes.include?(forbidden_prefix)

raise ValidationError, <<~MSG.chomp
The `Naming/PredicateName` cop is misconfigured. Prefix #{forbidden_prefix} must be included in NamePrefix because it is included in ForbiddenPrefixes.
MSG
end
end

private

def allowed_method_name?(method_name, prefix)
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,27 @@ def meow_at_4am?
end
end

context 'when Naming/PredicateName has predicate prefix forbidden which is not listed in NamePrefix' do
it 'fails with an error message' do
create_file('example.rb', 'puts 1')
create_file('.rubocop.yml', <<~YAML)
Naming/PredicateName:
NamePrefix:
- is_
- has_
ForbiddenPrefixes:
- is_
- has_
- seems_to_be_
YAML
expect(cli.run(['example.rb'])).to eq(2)
expect($stderr.string.strip).to eq(
'Error: The `Naming/PredicateName` cop is misconfigured. Prefix seems_to_be_ must be ' \
'included in NamePrefix because it is included in ForbiddenPrefixes.'
)
end
end

it 'allows the default configuration file as the -c argument' do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
Expand Down

0 comments on commit 2443641

Please sign in to comment.