Skip to content

Commit

Permalink
Simplify NameSimilarity.find_similar_name
Browse files Browse the repository at this point in the history
By explicitly passing the collection of "possible names" when calling
`NameSimilarity.find_similar_name`, the calling class doesn't need to
implement a `.collect_variable_like_names` method.
  • Loading branch information
bquorning authored and bbatsov committed May 10, 2020
1 parent 865930c commit 77b59de
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
6 changes: 1 addition & 5 deletions lib/rubocop/cop/lint/redundant_cop_disable_directive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def describe(cop)
elsif all_cop_names.include?(cop)
"`#{cop}`"
else
similar = find_similar_name(cop, [])
similar = find_similar_name(cop, all_cop_names)
if similar
"`#{cop}` (did you mean `#{similar}`?)"
else
Expand All @@ -244,10 +244,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
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/useless_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ 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 = find_similar_name(variable.name, variable_like_names)
" Did you mean `#{similar_name}`?" if similar_name
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/name_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module RuboCop
module NameSimilarity
MINIMUM_SIMILARITY_TO_SUGGEST = 0.9

def find_similar_name(target_name, scope)
names = collect_variable_like_names(scope)
def find_similar_name(target_name, names)
names = names.dup
names.delete(target_name)

scores = names.each_with_object({}) do |name, hash|
Expand Down

0 comments on commit 77b59de

Please sign in to comment.