Skip to content

Commit

Permalink
Suggest reserved words if they match
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki24 committed Dec 14, 2018
1 parent df1c0ff commit 2a082a7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
31 changes: 30 additions & 1 deletion lib/did_you_mean/spell_checkers/method_name_checker.rb
Expand Up @@ -7,14 +7,43 @@ class MethodNameChecker
NAMES_TO_EXCLUDE = { NilClass => nil.methods }
NAMES_TO_EXCLUDE.default = []

# +MethodNameChecker::RB_RESERVED_WORDS+ is the list of reserved words in
# Ruby that take an argument. Unlike
# +VariableNameChecker::RB_RESERVED_WORDS+, those reserved words reqquires
# an argument, and a +NoMethodError+ is raised due to the presence of the
# argument.
#
# The +MethodNameChecker+ will use this list to suggest a reversed word if
# a +NoMethodError+ is raised and found closest matches.
#
# Also see +VariableNameChecker::RB_RESERVED_WORDS+.
RB_RESERVED_WORDS = %i(
alias
case
def
defined?
elsif
end
ensure
for
rescue
super
undef
unless
until
when
while
yield
)

def initialize(exception)
@method_name = exception.name
@receiver = exception.receiver
@private_call = exception.respond_to?(:private_call?) ? exception.private_call? : false
end

def corrections
@corrections ||= SpellChecker.new(dictionary: method_names).correct(method_name) - NAMES_TO_EXCLUDE[@receiver.class]
@corrections ||= SpellChecker.new(dictionary: RB_RESERVED_WORDS + method_names).correct(method_name) - NAMES_TO_EXCLUDE[@receiver.class]
end

def method_names
Expand Down
Expand Up @@ -6,9 +6,61 @@ module DidYouMean
class VariableNameChecker
attr_reader :name, :method_names, :lvar_names, :ivar_names, :cvar_names

NAMES_TO_EXCLUDE = { 'foo' => [:fork] }
NAMES_TO_EXCLUDE = { 'foo' => [:fork, :for] }
NAMES_TO_EXCLUDE.default = []
RB_PREDEFINED_OBJECTS = [:false, :true, :nil, :yield]

# +VariableNameChecker::RB_RESERVED_WORDS+ is the list of all reserved
# words in Ruby. They could be declared like methods are, and a typo would
# cause Ruby to raise a +NameError+ because of the way they are declared.
#
# The +:VariableNameChecker+ will use this list to suggest a reversed word
# if a +NameError+ is raised and found closest matches, excluding:
#
# * +do+
# * +if+
# * +in+
# * +or+
#
# Also see +MethodNameChecker::RB_RESERVED_WORDS+.
RB_RESERVED_WORDS = %i(
BEGIN
END
alias
and
begin
break
case
class
def
defined?
else
elsif
end
ensure
false
for
module
next
nil
not
redo
rescue
retry
return
self
super
then
true
undef
unless
until
when
while
yield
__LINE__
__FILE__
__ENCODING__
)

def initialize(exception)
@name = exception.name.to_s.tr("@", "")
Expand All @@ -23,7 +75,7 @@ def initialize(exception)

def corrections
@corrections ||= SpellChecker
.new(dictionary: (RB_PREDEFINED_OBJECTS + lvar_names + method_names + ivar_names + cvar_names))
.new(dictionary: (RB_RESERVED_WORDS + lvar_names + method_names + ivar_names + cvar_names))
.correct(name) - NAMES_TO_EXCLUDE[@name]
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/spell_checking/method_name_check_test.rb
Expand Up @@ -116,4 +116,11 @@ def test_does_not_append_suggestions_three_times

assert_equal 1, error.to_s.scan(/Did you mean/).count
end

def test_suggests_yield
error = assert_raises(NoMethodError) { yeild(1) }

assert_correction :yield, error.corrections
assert_match "Did you mean? yield", error.to_s
end
end
9 changes: 8 additions & 1 deletion test/spell_checking/variable_name_check_test.rb
Expand Up @@ -69,7 +69,11 @@ def test_corrections_include_ruby_predefined_objects
end

nil_error = assert_raises(NameError) do
some_var = nol
some_var = nul
end

file_error = assert_raises(NameError) do
__FIEL__
end

assert_correction :false, false_error.corrections
Expand All @@ -80,6 +84,9 @@ def test_corrections_include_ruby_predefined_objects

assert_correction :nil, nil_error.corrections
assert_match "Did you mean? nil", nil_error.to_s

assert_correction :__FILE__, file_error.corrections
assert_match "Did you mean? __FILE__", file_error.to_s
end

def test_suggests_yield
Expand Down

0 comments on commit 2a082a7

Please sign in to comment.