Skip to content

Commit

Permalink
Do not suggest #name= for #name and vice versa (#180)
Browse files Browse the repository at this point in the history
* Do not suggest #name= for #name and vice versa
* Avoid allocating unnecessary MatchData

Co-authored-by: Jean byroot Boussier <[email protected]>
Co-authored-by: Jean byroot Boussier <[email protected]>
  • Loading branch information
mboeh and casperisfine authored Dec 5, 2022
1 parent ebf0945 commit 0516931
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/did_you_mean/spell_checkers/method_name_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ def method_names
method_names = receiver.methods + receiver.singleton_methods
method_names += receiver.private_methods if @private_call
method_names.uniq!
# Assume that people trying to use a writer are not interested in a reader
# and vice versa
if method_name.match?(/=\Z/)
method_names.select! { |name| name.match?(/=\Z/) }
else
method_names.reject! { |name| name.match?(/=\Z/) }
end
method_names
else
[]
Expand Down
18 changes: 18 additions & 0 deletions test/spell_checking/test_method_name_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class MethodNameCheckTest < Test::Unit::TestCase
include DidYouMean::TestHelper

class User
attr_writer :writer
attr_reader :reader
def friends; end
def first_name; end
def descendants; end
Expand Down Expand Up @@ -144,4 +146,20 @@ def test_does_not_suggest_yield
assert_correction [], error.corrections
assert_not_match(/Did you mean\? +yield/, get_message(error))
end if RUBY_ENGINE != "jruby"

# Do not suggest `name=` for `name`
def test_does_not_suggest_writer
error = assert_raise(NoMethodError) { @user.writer }

assert_correction [], error.corrections
assert_not_match(/Did you mean\? writer=/, get_message(error))
end

# Do not suggest `name` for `name=`
def test_does_not_suggest_reader
error = assert_raise(NoMethodError) { @user.reader = 1 }

assert_correction [], error.corrections
assert_not_match(/Did you mean\? reader/, get_message(error))
end
end

0 comments on commit 0516931

Please sign in to comment.