Skip to content

Commit

Permalink
Add "did you mean" message when failing due to invalid cops in config…
Browse files Browse the repository at this point in the history
…uration.
  • Loading branch information
dvandersluis committed Dec 5, 2020
1 parent 620d4cc commit a54011d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
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

0 comments on commit a54011d

Please sign in to comment.