[ruby/did_you_mean] Do not suggest #name= for #name and vice versa

(https://github.com/ruby/did_you_mean/pull/180)

* Do not suggest #name= for #name and vice versa
* Avoid allocating unnecessary MatchData

Co-authored-by: Jean byroot Boussier <jean.boussier+github@shopify.com>
Co-authored-by: Jean byroot Boussier <jean.boussier+github@shopify.com>
This commit is contained in:
Matthew Boeh 2022-12-05 05:16:27 -08:00 коммит произвёл git
Родитель 74923aaf31
Коммит 1602d75c34
2 изменённых файлов: 25 добавлений и 0 удалений

Просмотреть файл

@ -59,6 +59,13 @@ module DidYouMean
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
[]

Просмотреть файл

@ -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
@ -144,4 +146,20 @@ class MethodNameCheckTest < Test::Unit::TestCase
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