Skip to content

Commit 941fdae

Browse files
committed
Suggest global variables
I don't know how to make it work with Ractor.
1 parent 50e6199 commit 941fdae

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

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
@@ -4,7 +4,7 @@
44

55
module DidYouMean
66
class VariableNameChecker
7-
attr_reader :name, :method_names, :lvar_names, :ivar_names, :cvar_names
7+
attr_reader :name, :method_names, :gvar_names, :lvar_names, :ivar_names, :cvar_names
88

99
NAMES_TO_EXCLUDE = { 'foo' => [:fork, :for] }
1010
NAMES_TO_EXCLUDE.default = []
@@ -67,6 +67,7 @@ class VariableNameChecker
6767

6868
def initialize(exception)
6969
@name = exception.name.to_s.tr("@", "")
70+
@gvar_names = defined?(Ractor) ? [] : global_variables.select {|g| g[/\$[A-Za-z]/] }
7071
@lvar_names = exception.respond_to?(:local_variables) ? exception.local_variables : []
7172
receiver = exception.receiver
7273

@@ -78,7 +79,7 @@ def initialize(exception)
7879

7980
def corrections
8081
@corrections ||= SpellChecker
81-
.new(dictionary: (RB_RESERVED_WORDS + lvar_names + method_names + ivar_names + cvar_names))
82+
.new(dictionary: (RB_RESERVED_WORDS + gvar_names + lvar_names + method_names + ivar_names + cvar_names))
8283
.correct(name).uniq - NAMES_TO_EXCLUDE[@name]
8384
end
8485
end

test/spell_checking/test_variable_name_check.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ def test_corrections_include_method_from_module
5151
assert_match "Did you mean? from_module", get_message(error)
5252
end
5353

54+
def test_corrections_include_global_variable_name
55+
return if defined?(Ractor)
56+
57+
error = assert_raise(NameError) { stin }
58+
59+
assert_correction :$stdin, error.corrections
60+
assert_match "Did you mean? $stdin", get_message(error)
61+
end
62+
5463
def test_corrections_include_local_variable_name
5564
if RUBY_ENGINE != "jruby"
5665
person = person = nil

0 commit comments

Comments
 (0)