Class: DidYouMean::VariableNameChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb

Constant Summary collapse

NAMES_TO_EXCLUDE =
{ 'foo' => [:fork, :for] }
RB_RESERVED_WORDS =

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.

%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__
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception) ⇒ VariableNameChecker

Returns a new instance of VariableNameChecker.



65
66
67
68
69
70
71
72
73
74
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb', line 65

def initialize(exception)
  @name       = exception.name.to_s.tr("@", "")
  @lvar_names = exception.respond_to?(:local_variables) ? exception.local_variables : []
  receiver    = exception.receiver

  @method_names = receiver.methods + receiver.private_methods
  @ivar_names   = receiver.instance_variables
  @cvar_names   = receiver.class.class_variables
  @cvar_names  += receiver.class_variables if receiver.kind_of?(Module)
end

Instance Attribute Details

#cvar_namesObject (readonly)

Returns the value of attribute cvar_names.



7
8
9
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb', line 7

def cvar_names
  @cvar_names
end

#ivar_namesObject (readonly)

Returns the value of attribute ivar_names.



7
8
9
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb', line 7

def ivar_names
  @ivar_names
end

#lvar_namesObject (readonly)

Returns the value of attribute lvar_names.



7
8
9
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb', line 7

def lvar_names
  @lvar_names
end

#method_namesObject (readonly)

Returns the value of attribute method_names.



7
8
9
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb', line 7

def method_names
  @method_names
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb', line 7

def name
  @name
end

Instance Method Details

#correctionsObject



76
77
78
79
80
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb', line 76

def corrections
  @corrections ||= SpellChecker
                 .new(dictionary: (RB_RESERVED_WORDS + lvar_names + method_names + ivar_names + cvar_names))
                 .correct(name) - NAMES_TO_EXCLUDE[@name]
end