Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "did you mean" message when failing due to invalid cops in configuration #9171

Merged
merged 1 commit into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/change_add_did_you_mean_message_when_warning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9171](https://github.com/rubocop-hq/rubocop/pull/9171): Add "did you mean" message when failing due to invalid cops in configuration. ([@dvandersluis][])
13 changes: 10 additions & 3 deletions lib/rubocop/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@ def alert_about_unrecognized_cops(invalid_cop_names)
# to do so than to pass the value around to various methods.
next if name == 'inherit_mode'

unknown_cops << "unrecognized cop #{name} found in " \
"#{smart_loaded_path}"
suggestions = NameSimilarity.find_similar_names(name, Cop::Registry.global.map(&:cop_name))
suggestion = "Did you mean `#{suggestions.join('`, `')}`?" if suggestions.any?

message = <<~MESSAGE.rstrip
unrecognized cop #{name} found in #{smart_loaded_path}
#{suggestion}
MESSAGE

unknown_cops << message
end
raise ValidationError, unknown_cops.join(', ') if unknown_cops.any?
raise ValidationError, unknown_cops.join("\n") if unknown_cops.any?
end

def validate_syntax_cop
Expand Down
17 changes: 13 additions & 4 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1445,16 +1445,25 @@ def meow_at_4am?
create_file('example/example1.rb', '#' * 90)

create_file('example/.rubocop.yml', <<~YAML)
Style/LyneLenth:
Layout/LyneLenth:
Enabled: true
Max: 100
Lint/LiteralInCondition:
Enabled: true
Style/AlignHash:
Enabled: true
YAML

expect(cli.run(%w[--format simple example])).to eq(2)
expect($stderr.string)
.to eq(['Error: unrecognized cop Style/LyneLenth found in ' \
'example/.rubocop.yml',
''].join("\n"))
.to eq(<<~OUTPUT)
Error: unrecognized cop Layout/LyneLenth found in example/.rubocop.yml
Did you mean `Layout/LineLength`?
unrecognized cop Lint/LiteralInCondition found in example/.rubocop.yml
Did you mean `Lint/LiteralAsCondition`?
unrecognized cop Style/AlignHash found in example/.rubocop.yml
Did you mean `Style/Alias`, `Style/OptionHash`?
OUTPUT
end

it 'prints a warning for an unrecognized configuration parameter' do
Expand Down