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

Suggest global variables #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

module DidYouMean
class VariableNameChecker
attr_reader :name, :method_names, :lvar_names, :ivar_names, :cvar_names
attr_reader :name, :method_names, :gvar_names, :lvar_names, :ivar_names, :cvar_names

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

def initialize(exception)
@name = exception.name.to_s.tr("@", "")
@gvar_names = global_variables.select {|g| g[/\$[A-Za-z]/] }
@lvar_names = exception.respond_to?(:local_variables) ? exception.local_variables : []
receiver = exception.receiver

Expand All @@ -21,7 +22,7 @@ def initialize(exception)

def corrections
@corrections ||= SpellChecker
.new(dictionary: (RB_PREDEFINED_OBJECTS + lvar_names + method_names + ivar_names + cvar_names))
.new(dictionary: (RB_PREDEFINED_OBJECTS + gvar_names + lvar_names + method_names + ivar_names + cvar_names))
.correct(name) - NAMES_TO_EXCLUDE[@name]
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/spell_checking/variable_name_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def test_corrections_include_method_from_module
assert_match "Did you mean? from_module", error.to_s
end

def test_corrections_include_global_variable_name
error = assert_raises(NameError) do
stin
end

assert_correction :$stdin, error.corrections
assert_match "Did you mean? $stdin", error.to_s
end

def test_corrections_include_local_variable_name
person = person = nil
error = (eprson rescue $!) # Do not use @assert_raises here as it changes a scope.
Expand Down