Skip to content

Commit 99f0402

Browse files
committed
Suggest global variables
1 parent 69e7f24 commit 99f0402

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
module DidYouMean
44
class VariableNameChecker
5-
attr_reader :name, :method_names, :lvar_names, :ivar_names, :cvar_names
5+
attr_reader :name, :method_names, :gvar_names, :lvar_names, :ivar_names, :cvar_names
66

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

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

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

2223
def corrections
2324
@corrections ||= SpellChecker
24-
.new(dictionary: (RB_PREDEFINED_OBJECTS + lvar_names + method_names + ivar_names + cvar_names))
25+
.new(dictionary: (RB_PREDEFINED_OBJECTS + gvar_names + lvar_names + method_names + ivar_names + cvar_names))
2526
.correct(name) - NAMES_TO_EXCLUDE[@name]
2627
end
2728
end

test/spell_checking/variable_name_check_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def test_corrections_include_method_from_module
4949
assert_match "Did you mean? from_module", error.to_s
5050
end
5151

52+
def test_corrections_include_global_variable_name
53+
error = assert_raises(NameError) do
54+
stin
55+
end
56+
57+
assert_correction :$stdin, error.corrections
58+
assert_match "Did you mean? $stdin", error.to_s
59+
end
60+
5261
def test_corrections_include_local_variable_name
5362
person = person = nil
5463
error = (eprson rescue $!) # Do not use @assert_raises here as it changes a scope.

0 commit comments

Comments
 (0)