-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Display correction candidate if an incorrect cop name is given #5300
Display correction candidate if an incorrect cop name is given #5300
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice improvement!
...Mmm, It seems travis's bundler is a bad mood. |
You should be good with a rebase. |
Maybe we cannot memorize correctly cop names. if an incorrect cop name is given with `--only` or `--except` options on CLI option, it just only raises exception. I think it's not friendly. So I've added its error message to correction candidate. Example: ``` $ bundle exec rubocop --only Lint/Li Inspecting 1081 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/Li. Did you mean? Lint/LiteralAsCondition, Lint/LiteralInInterpolation ... ```
1717621
to
aaa815d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥇
BTW, the message should also include candidates that are found with some similarity algorithm (e.g. Levenshtein distance). |
Yeah, that could be a nice future improvement. |
Follow up of rubocop#5300. This commit suppresses backtrace when the following case. ## Before ```console % rubocop --only Lint/RedundantWithI Inspecting 1081 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundantWithI. Did you mean? Lint/RedundantWithIndex /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/options.rb:200:in `block in validate_cop_list' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/options.rb:195:in `each' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/options.rb:195:in `validate_cop_list' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:275:in `block in mobilized_cop_classes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:274:in `each' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:274:in `mobilized_cop_classes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:257:in `inspect_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:205:in `block in do_inspection_loop' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:237:in `block in iterate_until_no_changes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:230:in `loop' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:230:in `iterate_until_no_changes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:201:in `do_inspection_loop' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:111:in `block in file_offenses' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:121:in `file_offense_cache' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:109:in `file_offenses' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:100:in `process_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:78:in `block in each_inspected_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:75:in `each' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:75:in `reduce' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:75:in `each_inspected_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:67:in `inspect_files' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:39:in `run' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/cli.rb:143:in `execute_runner' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/cli.rb:71:in `execute_runners' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/cli.rb:35:in `run' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/bin/rubocop:13:in `block in <top (required)>' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/2.4.0/benchmark.rb:308:in `realtime' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/bin/rubocop:12:in `<top (required)>' /Users/koic/.rbenv/versions/2.4.3/bin/rubocop:23:in `load' /Users/koic/.rbenv/versions/2.4.3/bin/rubocop:23:in `<main>' ``` ## After ```console % rubocop --only Lint/RedundantWithI Inspecting 1081 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundantWithI. Did you mean? Lint/RedundantWithIndex ``` As a general RuboCop users, it is undesirable to have the correct candidate cop name buried in the backtrace.
Follow up of #5300. This commit suppresses backtrace when the following case. ## Before ```console % rubocop --only Lint/RedundantWithI Inspecting 1081 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundantWithI. Did you mean? Lint/RedundantWithIndex /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/options.rb:200:in `block in validate_cop_list' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/options.rb:195:in `each' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/options.rb:195:in `validate_cop_list' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:275:in `block in mobilized_cop_classes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:274:in `each' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:274:in `mobilized_cop_classes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:257:in `inspect_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:205:in `block in do_inspection_loop' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:237:in `block in iterate_until_no_changes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:230:in `loop' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:230:in `iterate_until_no_changes' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:201:in `do_inspection_loop' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:111:in `block in file_offenses' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:121:in `file_offense_cache' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:109:in `file_offenses' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:100:in `process_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:78:in `block in each_inspected_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:75:in `each' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:75:in `reduce' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:75:in `each_inspected_file' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:67:in `inspect_files' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/runner.rb:39:in `run' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/cli.rb:143:in `execute_runner' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/cli.rb:71:in `execute_runners' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/lib/rubocop/cli.rb:35:in `run' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/bin/rubocop:13:in `block in <top (required)>' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/2.4.0/benchmark.rb:308:in `realtime' /Users/koic/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/rubocop-0.52.0/bin/rubocop:12:in `<top (required)>' /Users/koic/.rbenv/versions/2.4.3/bin/rubocop:23:in `load' /Users/koic/.rbenv/versions/2.4.3/bin/rubocop:23:in `<main>' ``` ## After ```console % rubocop --only Lint/RedundantWithI Inspecting 1081 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundantWithI. Did you mean? Lint/RedundantWithIndex ``` As a general RuboCop users, it is undesirable to have the correct candidate cop name buried in the backtrace.
Follow up of rubocop#5300 (comment). This commit changed the search approach of the correct cop candidates from prefix-match by regular expression to `StringUtil.similarity`. ## Before When there is no prefix-match, candidates are not displayed. ```console % rubocop --only Lint/RedundanWithI Inspecting 1083 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundanWithI. ``` ## After When there is no prefix-match, candidate is displayed. ```console % rubocop --only Lint/RedundanWithI Inspecting 1083 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundanWithI. Did you mean? Lint/RedundantWithIndex ``` Cop candidates different from user's expectations may be displayed, however I think that there is no problem in cop names of similarity to typing miss. ```console % rubocop --only Lint/RedundanWithO Inspecting 1083 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundanWithO. Did you mean? Lint/RandOne, Lint/RedundantWithObject ``` ## Other Information Initially I thought about using the module of did_you_mean which is a standard library of Ruby 2.3 or higher. However, since the following code does not work unless it is Ruby 2.4 or higher, I decided to use `StringUtil.similarity`. ```ruby DidYouMean::SpellChecker.new(dictionary: cop_names).correct(name) ``` Also, since it displays multiple cop candidates, this commit did not use `NameSimilarity#find_similar_name`.
Follow up of #5300 (comment). This commit changed the search approach of the correct cop candidates from prefix-match by regular expression to `StringUtil.similarity`. ## Before When there is no prefix-match, candidates are not displayed. ```console % rubocop --only Lint/RedundanWithI Inspecting 1083 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundanWithI. ``` ## After When there is no prefix-match, candidate is displayed. ```console % rubocop --only Lint/RedundanWithI Inspecting 1083 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundanWithI. Did you mean? Lint/RedundantWithIndex ``` Cop candidates different from user's expectations may be displayed, however I think that there is no problem in cop names of similarity to typing miss. ```console % rubocop --only Lint/RedundanWithO Inspecting 1083 files 0 files inspected, no offenses detected Unrecognized cop or department: Lint/RedundanWithO. Did you mean? Lint/RandOne, Lint/RedundantWithObject ``` ## Other Information Initially I thought about using the module of did_you_mean which is a standard library of Ruby 2.3 or higher. However, since the following code does not work unless it is Ruby 2.4 or higher, I decided to use `StringUtil.similarity`. ```ruby DidYouMean::SpellChecker.new(dictionary: cop_names).correct(name) ``` Also, since it displays multiple cop candidates, this commit did not use `NameSimilarity#find_similar_name`.
Maybe we cannot memorize correctly cop names.
if an incorrect cop name is given with
--only
or--except
options on CLI option,it just only raises exception. I think it's not friendly.
So I've added its error message to correction candidate.
Example:
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
rake default
orrake parallel
. It executes all tests and RuboCop for itself, and generates the documentation.