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

Use did_you_mean instead of jaro_winkler #7931

Merged
merged 4 commits into from May 10, 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.md
Expand Up @@ -41,6 +41,7 @@
* [#7390](https://github.com/rubocop-hq/rubocop/issues/7390): **(Breaking)** Enabling a cop overrides disabling its department. ([@jonas054][])
* [#7936](https://github.com/rubocop-hq/rubocop/issues/7936): Mark `Lint/BooleanSymbol` as unsafe. ([@laurmurclar][])
* [#7948](https://github.com/rubocop-hq/rubocop/pull/7948): Mark unsafe for `Style/OptionalArguments`. ([@koic][])
* [#7931](https://github.com/rubocop-hq/rubocop/pull/7931): Remove dependency on the `jaro_winkler` gem, instead depending on `did_you_mean`. This may be a breaking change for RuboCop libraries calling `NameSimilarity#find_similar_name`. ([@bquorning][])

## 0.82.0 (2020-04-16)

Expand Down
1 change: 0 additions & 1 deletion lib/rubocop.rb
Expand Up @@ -14,7 +14,6 @@
require_relative 'rubocop/path_util'
require_relative 'rubocop/file_finder'
require_relative 'rubocop/platform'
require_relative 'rubocop/string_util'
require_relative 'rubocop/name_similarity'
require_relative 'rubocop/node_pattern'
require_relative 'rubocop/string_interpreter'
Expand Down
7 changes: 1 addition & 6 deletions lib/rubocop/cop/lint/redundant_cop_disable_directive.rb
Expand Up @@ -26,7 +26,6 @@ module Lint
# # good
# x += 1
class RedundantCopDisableDirective < Cop
include NameSimilarity
include RangeHelp

COP_NAME = 'Lint/RedundantCopDisableDirective'
Expand Down Expand Up @@ -235,7 +234,7 @@ def describe(cop)
elsif all_cop_names.include?(cop)
"`#{cop}`"
else
similar = find_similar_name(cop, [])
similar = NameSimilarity.find_similar_name(cop, all_cop_names)
if similar
"`#{cop}` (did you mean `#{similar}`?)"
else
Expand All @@ -244,10 +243,6 @@ def describe(cop)
end
end

def collect_variable_like_names(scope)
all_cop_names.each { |name| scope << name }
end

def all_cop_names
@all_cop_names ||= Cop.registry.names
end
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/lint/useless_assignment.rb
Expand Up @@ -31,7 +31,6 @@ module Lint
# do_something(some_var)
# end
class UselessAssignment < Cop
include NameSimilarity
MSG = 'Useless assignment to variable - `%<variable>s`.'

def join_force?(force_class)
Expand Down Expand Up @@ -94,7 +93,9 @@ def operator_assignment_message(scope, assignment)
end

def similar_name_message(variable)
similar_name = find_similar_name(variable.name, variable.scope)
variable_like_names = collect_variable_like_names(variable.scope)
similar_name = NameSimilarity.find_similar_name(variable.name,
variable_like_names)
" Did you mean `#{similar_name}`?" if similar_name
end

Expand Down
21 changes: 12 additions & 9 deletions lib/rubocop/name_similarity.rb
Expand Up @@ -3,19 +3,22 @@
module RuboCop
# Common functionality for finding names that are similar to a given name.
module NameSimilarity
MINIMUM_SIMILARITY_TO_SUGGEST = 0.9
module_function

def find_similar_name(target_name, scope)
names = collect_variable_like_names(scope)
def find_similar_name(target_name, names)
similar_names = find_similar_names(target_name, names)

similar_names.first
end

def find_similar_names(target_name, names)
names = names.dup
names.delete(target_name)

scores = names.each_with_object({}) do |name, hash|
score = StringUtil.similarity(target_name, name)
hash[name] = score if score >= MINIMUM_SIMILARITY_TO_SUGGEST
end
spell_checker = DidYouMean::SpellChecker.new(dictionary: names)
similar_names = spell_checker.correct(target_name)

most_similar_name, _max_score = scores.max_by { |_, score| score }
most_similar_name
similar_names
end
end
end
5 changes: 1 addition & 4 deletions lib/rubocop/options.rb
Expand Up @@ -248,10 +248,7 @@ def validate_cop_list(names)
def format_message_from(name, cop_names)
message = 'Unrecognized cop or department: %<name>s.'
message_with_candidate = "%<message>s\nDid you mean? %<candidate>s"
corrections = cop_names.select do |cn|
score = StringUtil.similarity(cn, name)
score >= NameSimilarity::MINIMUM_SIMILARITY_TO_SUGGEST
end.sort
corrections = NameSimilarity.find_similar_names(name, cop_names)

if corrections.empty?
format(message, name: name)
Expand Down
14 changes: 0 additions & 14 deletions lib/rubocop/string_util.rb

This file was deleted.

1 change: 0 additions & 1 deletion rubocop.gemspec
Expand Up @@ -33,7 +33,6 @@ Gem::Specification.new do |s|
'bug_tracker_uri' => 'https://github.com/rubocop-hq/rubocop/issues'
}

s.add_runtime_dependency('jaro_winkler', '~> 1.5.1')
s.add_runtime_dependency('parallel', '~> 1.10')
s.add_runtime_dependency('parser', '>= 2.7.0.1')
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/html_formatter/expected.html
Expand Up @@ -658,7 +658,7 @@ <h1 class="title">RuboCop Inspection Report</h1>
<div class="meta">
<span class="location">Line #3</span> –
<span class="severity warning">warning:</span>
<span class="message">Lint/UselessAssignment: Useless assignment to variable - <code>bar</code>.</span>
<span class="message">Lint/UselessAssignment: Useless assignment to variable - <code>bar</code>. Did you mean <code>baz</code>?</span>
</div>

<pre><code> foo = <span class="highlight warning">bar</span> = baz</code></pre>
Expand Down
61 changes: 0 additions & 61 deletions spec/rubocop/string_util_spec.rb

This file was deleted.