summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKevin Deisz <kevin.deisz@gmail.com>2019-10-29 10:08:37 -0400
committerYuki Nishijima <yk.nishijima@gmail.com>2019-11-30 21:08:19 -0500
commit171803d5d34feb1b4244ca81b9db0a7bc2171c85 (patch)
tree664ee644da144f28152097fbe5ea43329bfc0576 /lib
parenta2fc6a51dd2e1a153559038795e1e2509f9c6a94 (diff)
Promote did_you_mean to default gem
At the moment, there are some problems with regard to bundler + did_you_mean because of did_you_mean being a bundled gem. Since the vendored version of thor inside bundler and ruby itself explicitly requires did_you_mean, it can become difficult to load it when using Bundler.setup. See this issue: https://github.com/yuki24/did_you_mean/issues/117#issuecomment-482733159 for more details.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2689
Diffstat (limited to 'lib')
-rw-r--r--lib/did_you_mean.rb110
-rw-r--r--lib/did_you_mean/core_ext/name_error.rb25
-rw-r--r--lib/did_you_mean/did_you_mean.gemspec23
-rw-r--r--lib/did_you_mean/experimental.rb2
-rw-r--r--lib/did_you_mean/experimental/initializer_name_correction.rb20
-rw-r--r--lib/did_you_mean/experimental/ivar_name_correction.rb76
-rw-r--r--lib/did_you_mean/formatters/plain_formatter.rb33
-rw-r--r--lib/did_you_mean/formatters/verbose_formatter.rb49
-rw-r--r--lib/did_you_mean/jaro_winkler.rb87
-rw-r--r--lib/did_you_mean/levenshtein.rb57
-rw-r--r--lib/did_you_mean/spell_checker.rb46
-rw-r--r--lib/did_you_mean/spell_checkers/key_error_checker.rb20
-rw-r--r--lib/did_you_mean/spell_checkers/method_name_checker.rb56
-rw-r--r--lib/did_you_mean/spell_checkers/name_error_checkers.rb20
-rw-r--r--lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb50
-rw-r--r--lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb82
-rw-r--r--lib/did_you_mean/spell_checkers/null_checker.rb6
-rw-r--r--lib/did_you_mean/tree_spell_checker.rb137
-rw-r--r--lib/did_you_mean/verbose.rb4
-rw-r--r--lib/did_you_mean/version.rb3
20 files changed, 906 insertions, 0 deletions
diff --git a/lib/did_you_mean.rb b/lib/did_you_mean.rb
new file mode 100644
index 0000000000..b8f92579ca
--- /dev/null
+++ b/lib/did_you_mean.rb
@@ -0,0 +1,110 @@
+require_relative "did_you_mean/version"
+require_relative "did_you_mean/core_ext/name_error"
+
+require_relative "did_you_mean/spell_checker"
+require_relative 'did_you_mean/spell_checkers/name_error_checkers'
+require_relative 'did_you_mean/spell_checkers/method_name_checker'
+require_relative 'did_you_mean/spell_checkers/key_error_checker'
+require_relative 'did_you_mean/spell_checkers/null_checker'
+require_relative 'did_you_mean/formatters/plain_formatter'
+require_relative 'did_you_mean/tree_spell_checker'
+
+# The +DidYouMean+ gem adds functionality to suggest possible method/class
+# names upon errors such as +NameError+ and +NoMethodError+. In Ruby 2.3 or
+# later, it is automatically activated during startup.
+#
+# @example
+#
+# methosd
+# # => NameError: undefined local variable or method `methosd' for main:Object
+# # Did you mean? methods
+# # method
+#
+# OBject
+# # => NameError: uninitialized constant OBject
+# # Did you mean? Object
+#
+# @full_name = "Yuki Nishijima"
+# first_name, last_name = full_name.split(" ")
+# # => NameError: undefined local variable or method `full_name' for main:Object
+# # Did you mean? @full_name
+#
+# @@full_name = "Yuki Nishijima"
+# @@full_anme
+# # => NameError: uninitialized class variable @@full_anme in Object
+# # Did you mean? @@full_name
+#
+# full_name = "Yuki Nishijima"
+# full_name.starts_with?("Y")
+# # => NoMethodError: undefined method `starts_with?' for "Yuki Nishijima":String
+# # Did you mean? start_with?
+#
+# hash = {foo: 1, bar: 2, baz: 3}
+# hash.fetch(:fooo)
+# # => KeyError: key not found: :fooo
+# # Did you mean? :foo
+#
+#
+# == Disabling +did_you_mean+
+#
+# Occasionally, you may want to disable the +did_you_mean+ gem for e.g.
+# debugging issues in the error object itself. You can disable it entirely by
+# specifying +--disable-did_you_mean+ option to the +ruby+ command:
+#
+# $ ruby --disable-did_you_mean -e "1.zeor?"
+# -e:1:in `<main>': undefined method `zeor?' for 1:Integer (NameError)
+#
+# When you do not have direct access to the +ruby+ command (e.g.
+# +rails console+, +irb+), you could applyoptions using the +RUBYOPT+
+# environment variable:
+#
+# $ RUBYOPT='--disable-did_you_mean' irb
+# irb:0> 1.zeor?
+# # => NoMethodError (undefined method `zeor?' for 1:Integer)
+#
+#
+# == Getting the original error message
+#
+# Sometimes, you do not want to disable the gem entirely, but need to get the
+# original error message without suggestions (e.g. testing). In this case, you
+# could use the +#original_message+ method on the error object:
+#
+# no_method_error = begin
+# 1.zeor?
+# rescue NoMethodError => error
+# error
+# end
+#
+# no_method_error.message
+# # => NoMethodError (undefined method `zeor?' for 1:Integer)
+# # Did you mean? zero?
+#
+# no_method_error.original_message
+# # => NoMethodError (undefined method `zeor?' for 1:Integer)
+#
+module DidYouMean
+ # Map of error types and spell checker objects.
+ SPELL_CHECKERS = Hash.new(NullChecker)
+
+ # Adds +DidYouMean+ functionality to an error using a given spell checker
+ def self.correct_error(error_class, spell_checker)
+ SPELL_CHECKERS[error_class.name] = spell_checker
+ error_class.prepend(Correctable) unless error_class < Correctable
+ end
+
+ correct_error NameError, NameErrorCheckers
+ correct_error KeyError, KeyErrorChecker
+ correct_error NoMethodError, MethodNameChecker
+
+ # Returns the currenctly set formatter. By default, it is set to +DidYouMean::Formatter+.
+ def self.formatter
+ @@formatter
+ end
+
+ # Updates the primary formatter used to format the suggestions.
+ def self.formatter=(formatter)
+ @@formatter = formatter
+ end
+
+ self.formatter = PlainFormatter.new
+end
diff --git a/lib/did_you_mean/core_ext/name_error.rb b/lib/did_you_mean/core_ext/name_error.rb
new file mode 100644
index 0000000000..77dcd520c0
--- /dev/null
+++ b/lib/did_you_mean/core_ext/name_error.rb
@@ -0,0 +1,25 @@
+module DidYouMean
+ module Correctable
+ def original_message
+ method(:to_s).super_method.call
+ end
+
+ def to_s
+ msg = super.dup
+ suggestion = DidYouMean.formatter.message_for(corrections)
+
+ msg << suggestion if !msg.end_with?(suggestion)
+ msg
+ rescue
+ super
+ end
+
+ def corrections
+ @corrections ||= spell_checker.corrections
+ end
+
+ def spell_checker
+ SPELL_CHECKERS[self.class.to_s].new(self)
+ end
+ end
+end
diff --git a/lib/did_you_mean/did_you_mean.gemspec b/lib/did_you_mean/did_you_mean.gemspec
new file mode 100644
index 0000000000..8a74db8cc4
--- /dev/null
+++ b/lib/did_you_mean/did_you_mean.gemspec
@@ -0,0 +1,23 @@
+# coding: utf-8
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'did_you_mean/version'
+
+Gem::Specification.new do |spec|
+ spec.name = "did_you_mean"
+ spec.version = DidYouMean::VERSION
+ spec.authors = ["Yuki Nishijima"]
+ spec.email = ["mail@yukinishijima.net"]
+ spec.summary = '"Did you mean?" experience in Ruby'
+ spec.description = 'The gem that has been saving people from typos since 2014.'
+ spec.homepage = "https://github.com/ruby/did_you_mean"
+ spec.license = "MIT"
+
+ spec.files = `git ls-files`.split($/).reject{|path| path.start_with?('evaluation/') }
+ spec.test_files = spec.files.grep(%r{^(test)/})
+ spec.require_paths = ["lib"]
+
+ spec.required_ruby_version = '>= 2.5.0'
+
+ spec.add_development_dependency "rake"
+end
diff --git a/lib/did_you_mean/experimental.rb b/lib/did_you_mean/experimental.rb
new file mode 100644
index 0000000000..f8e37e4532
--- /dev/null
+++ b/lib/did_you_mean/experimental.rb
@@ -0,0 +1,2 @@
+warn "Experimental features in the did_you_mean gem has been removed " \
+ "and `require \"did_you_mean/experimental\"' has no effect."
diff --git a/lib/did_you_mean/experimental/initializer_name_correction.rb b/lib/did_you_mean/experimental/initializer_name_correction.rb
new file mode 100644
index 0000000000..b59c98e774
--- /dev/null
+++ b/lib/did_you_mean/experimental/initializer_name_correction.rb
@@ -0,0 +1,20 @@
+# frozen-string-literal: true
+
+require_relative '../levenshtein'
+
+module DidYouMean
+ module Experimental
+ module InitializerNameCorrection
+ def method_added(name)
+ super
+
+ distance = Levenshtein.distance(name.to_s, 'initialize')
+ if distance != 0 && distance <= 2
+ warn "warning: #{name} might be misspelled, perhaps you meant initialize?"
+ end
+ end
+ end
+
+ ::Class.prepend(InitializerNameCorrection)
+ end
+end
diff --git a/lib/did_you_mean/experimental/ivar_name_correction.rb b/lib/did_you_mean/experimental/ivar_name_correction.rb
new file mode 100644
index 0000000000..322e422c6b
--- /dev/null
+++ b/lib/did_you_mean/experimental/ivar_name_correction.rb
@@ -0,0 +1,76 @@
+# frozen-string-literal: true
+
+require_relative '../../did_you_mean'
+
+module DidYouMean
+ module Experimental #:nodoc:
+ class IvarNameCheckerBuilder #:nodoc:
+ attr_reader :original_checker
+
+ def initialize(original_checker) #:nodoc:
+ @original_checker = original_checker
+ end
+
+ def new(no_method_error) #:nodoc:
+ IvarNameChecker.new(no_method_error, original_checker: @original_checker)
+ end
+ end
+
+ class IvarNameChecker #:nodoc:
+ REPLS = {
+ "(irb)" => -> { Readline::HISTORY.to_a.last }
+ }
+
+ TRACE = TracePoint.trace(:raise) do |tp|
+ e = tp.raised_exception
+
+ if SPELL_CHECKERS.include?(e.class.to_s) && !e.instance_variable_defined?(:@frame_binding)
+ e.instance_variable_set(:@frame_binding, tp.binding)
+ end
+ end
+
+ attr_reader :original_checker
+
+ def initialize(no_method_error, original_checker: )
+ @original_checker = original_checker.new(no_method_error)
+
+ @location = no_method_error.backtrace_locations.first
+ @ivar_names = no_method_error.frame_binding.receiver.instance_variables
+
+ no_method_error.remove_instance_variable(:@frame_binding)
+ end
+
+ def corrections
+ original_checker.corrections + ivar_name_corrections
+ end
+
+ def ivar_name_corrections
+ @ivar_name_corrections ||= SpellChecker.new(dictionary: @ivar_names).correct(receiver_name.to_s)
+ end
+
+ private
+
+ def receiver_name
+ return unless @original_checker.receiver.nil?
+
+ abs_path = @location.absolute_path
+ lineno = @location.lineno
+
+ /@(\w+)*\.#{@original_checker.method_name}/ =~ line(abs_path, lineno).to_s && $1
+ end
+
+ def line(abs_path, lineno)
+ if REPLS[abs_path]
+ REPLS[abs_path].call
+ elsif File.exist?(abs_path)
+ File.open(abs_path) do |file|
+ file.detect { file.lineno == lineno }
+ end
+ end
+ end
+ end
+ end
+
+ NameError.send(:attr, :frame_binding)
+ SPELL_CHECKERS['NoMethodError'] = Experimental::IvarNameCheckerBuilder.new(SPELL_CHECKERS['NoMethodError'])
+end
diff --git a/lib/did_you_mean/formatters/plain_formatter.rb b/lib/did_you_mean/formatters/plain_formatter.rb
new file mode 100644
index 0000000000..e2d995f587
--- /dev/null
+++ b/lib/did_you_mean/formatters/plain_formatter.rb
@@ -0,0 +1,33 @@
+# frozen-string-literal: true
+
+module DidYouMean
+ # The +DidYouMean::PlainFormatter+ is the basic, default formatter for the
+ # gem. The formatter responds to the +message_for+ method and it returns a
+ # human readable string.
+ class PlainFormatter
+
+ # Returns a human readable string that contains +corrections+. This
+ # formatter is designed to be less verbose to not take too much screen
+ # space while being helpful enough to the user.
+ #
+ # @example
+ #
+ # formatter = DidYouMean::PlainFormatter.new
+ #
+ # # displays suggestions in two lines with the leading empty line
+ # puts formatter.message_for(["methods", "method"])
+ #
+ # Did you mean? methods
+ # method
+ # # => nil
+ #
+ # # displays an empty line
+ # puts formatter.message_for([])
+ #
+ # # => nil
+ #
+ def message_for(corrections)
+ corrections.empty? ? "" : "\nDid you mean? #{corrections.join("\n ")}"
+ end
+ end
+end
diff --git a/lib/did_you_mean/formatters/verbose_formatter.rb b/lib/did_you_mean/formatters/verbose_formatter.rb
new file mode 100644
index 0000000000..b8fe214d57
--- /dev/null
+++ b/lib/did_you_mean/formatters/verbose_formatter.rb
@@ -0,0 +1,49 @@
+# frozen-string-literal: true
+
+module DidYouMean
+ # The +DidYouMean::VerboseFormatter+ uses extra empty lines to make the
+ # suggestion stand out more in the error message.
+ #
+ # In order to activate the verbose formatter,
+ #
+ # @example
+ #
+ # OBject
+ # # => NameError: uninitialized constant OBject
+ # # Did you mean? Object
+ #
+ # require 'did_you_mean/verbose'
+ #
+ # OBject
+ # # => NameError: uninitialized constant OBject
+ # #
+ # # Did you mean? Object
+ # #
+ #
+ class VerboseFormatter
+
+ # Returns a human readable string that contains +corrections+. This
+ # formatter is designed to be less verbose to not take too much screen
+ # space while being helpful enough to the user.
+ #
+ # @example
+ #
+ # formatter = DidYouMean::PlainFormatter.new
+ #
+ # puts formatter.message_for(["methods", "method"])
+ #
+ #
+ # Did you mean? methods
+ # method
+ #
+ # # => nil
+ #
+ def message_for(corrections)
+ return "" if corrections.empty?
+
+ output = "\n\n Did you mean? ".dup
+ output << corrections.join("\n ")
+ output << "\n "
+ end
+ end
+end
diff --git a/lib/did_you_mean/jaro_winkler.rb b/lib/did_you_mean/jaro_winkler.rb
new file mode 100644
index 0000000000..56db130af4
--- /dev/null
+++ b/lib/did_you_mean/jaro_winkler.rb
@@ -0,0 +1,87 @@
+module DidYouMean
+ module Jaro
+ module_function
+
+ def distance(str1, str2)
+ str1, str2 = str2, str1 if str1.length > str2.length
+ length1, length2 = str1.length, str2.length
+
+ m = 0.0
+ t = 0.0
+ range = (length2 / 2).floor - 1
+ range = 0 if range < 0
+ flags1 = 0
+ flags2 = 0
+
+ # Avoid duplicating enumerable objects
+ str1_codepoints = str1.codepoints
+ str2_codepoints = str2.codepoints
+
+ i = 0
+ while i < length1
+ last = i + range
+ j = (i >= range) ? i - range : 0
+
+ while j <= last
+ if flags2[j] == 0 && str1_codepoints[i] == str2_codepoints[j]
+ flags2 |= (1 << j)
+ flags1 |= (1 << i)
+ m += 1
+ break
+ end
+
+ j += 1
+ end
+
+ i += 1
+ end
+
+ k = i = 0
+ while i < length1
+ if flags1[i] != 0
+ j = index = k
+
+ k = while j < length2
+ index = j
+ break(j + 1) if flags2[j] != 0
+
+ j += 1
+ end
+
+ t += 1 if str1_codepoints[i] != str2_codepoints[index]
+ end
+
+ i += 1
+ end
+ t = (t / 2).floor
+
+ m == 0 ? 0 : (m / length1 + m / length2 + (m - t) / m) / 3
+ end
+ end
+
+ module JaroWinkler
+ WEIGHT = 0.1
+ THRESHOLD = 0.7
+
+ module_function
+
+ def distance(str1, str2)
+ jaro_distance = Jaro.distance(str1, str2)
+
+ if jaro_distance > THRESHOLD
+ codepoints2 = str2.codepoints
+ prefix_bonus = 0
+
+ i = 0
+ str1.each_codepoint do |char1|
+ char1 == codepoints2[i] && i < 4 ? prefix_bonus += 1 : break
+ i += 1
+ end
+
+ jaro_distance + (prefix_bonus * WEIGHT * (1 - jaro_distance))
+ else
+ jaro_distance
+ end
+ end
+ end
+end
diff --git a/lib/did_you_mean/levenshtein.rb b/lib/did_you_mean/levenshtein.rb
new file mode 100644
index 0000000000..098053470f
--- /dev/null
+++ b/lib/did_you_mean/levenshtein.rb
@@ -0,0 +1,57 @@
+module DidYouMean
+ module Levenshtein # :nodoc:
+ # This code is based directly on the Text gem implementation
+ # Copyright (c) 2006-2013 Paul Battley, Michael Neumann, Tim Fletcher.
+ #
+ # Returns a value representing the "cost" of transforming str1 into str2
+ def distance(str1, str2)
+ n = str1.length
+ m = str2.length
+ return m if n.zero?
+ return n if m.zero?
+
+ d = (0..m).to_a
+ x = nil
+
+ # to avoid duplicating an enumerable object, create it outside of the loop
+ str2_codepoints = str2.codepoints
+
+ str1.each_codepoint.with_index(1) do |char1, i|
+ j = 0
+ while j < m
+ cost = (char1 == str2_codepoints[j]) ? 0 : 1
+ x = min3(
+ d[j+1] + 1, # insertion
+ i + 1, # deletion
+ d[j] + cost # substitution
+ )
+ d[j] = i
+ i = x
+
+ j += 1
+ end
+ d[m] = x
+ end
+
+ x
+ end
+ module_function :distance
+
+ private
+
+ # detects the minimum value out of three arguments. This method is
+ # faster than `[a, b, c].min` and puts less GC pressure.
+ # See https://github.com/ruby/did_you_mean/pull/1 for a performance
+ # benchmark.
+ def min3(a, b, c)
+ if a < b && a < c
+ a
+ elsif b < c
+ b
+ else
+ c
+ end
+ end
+ module_function :min3
+ end
+end
diff --git a/lib/did_you_mean/spell_checker.rb b/lib/did_you_mean/spell_checker.rb
new file mode 100644
index 0000000000..e5106abba2
--- /dev/null
+++ b/lib/did_you_mean/spell_checker.rb
@@ -0,0 +1,46 @@
+# frozen-string-literal: true
+
+require_relative "levenshtein"
+require_relative "jaro_winkler"
+
+module DidYouMean
+ class SpellChecker
+ def initialize(dictionary:)
+ @dictionary = dictionary
+ end
+
+ def correct(input)
+ input = normalize(input)
+ threshold = input.length > 3 ? 0.834 : 0.77
+
+ words = @dictionary.select { |word| JaroWinkler.distance(normalize(word), input) >= threshold }
+ words.reject! { |word| input == word.to_s }
+ words.sort_by! { |word| JaroWinkler.distance(word.to_s, input) }
+ words.reverse!
+
+ # Correct mistypes
+ threshold = (input.length * 0.25).ceil
+ corrections = words.select { |c| Levenshtein.distance(normalize(c), input) <= threshold }
+
+ # Correct misspells
+ if corrections.empty?
+ corrections = words.select do |word|
+ word = normalize(word)
+ length = input.length < word.length ? input.length : word.length
+
+ Levenshtein.distance(word, input) < length
+ end.first(1)
+ end
+
+ corrections
+ end
+
+ private
+
+ def normalize(str_or_symbol) #:nodoc:
+ str = str_or_symbol.to_s.downcase
+ str.tr!("@", "")
+ str
+ end
+ end
+end
diff --git a/lib/did_you_mean/spell_checkers/key_error_checker.rb b/lib/did_you_mean/spell_checkers/key_error_checker.rb
new file mode 100644
index 0000000000..be4bea7789
--- /dev/null
+++ b/lib/did_you_mean/spell_checkers/key_error_checker.rb
@@ -0,0 +1,20 @@
+require_relative "../spell_checker"
+
+module DidYouMean
+ class KeyErrorChecker
+ def initialize(key_error)
+ @key = key_error.key
+ @keys = key_error.receiver.keys
+ end
+
+ def corrections
+ @corrections ||= exact_matches.empty? ? SpellChecker.new(dictionary: @keys).correct(@key).map(&:inspect) : exact_matches
+ end
+
+ private
+
+ def exact_matches
+ @exact_matches ||= @keys.select { |word| @key == word.to_s }.map(&:inspect)
+ end
+ end
+end
diff --git a/lib/did_you_mean/spell_checkers/method_name_checker.rb b/lib/did_you_mean/spell_checkers/method_name_checker.rb
new file mode 100644
index 0000000000..3ca8a37e08
--- /dev/null
+++ b/lib/did_you_mean/spell_checkers/method_name_checker.rb
@@ -0,0 +1,56 @@
+require_relative "../spell_checker"
+
+module DidYouMean
+ class MethodNameChecker
+ attr_reader :method_name, :receiver
+
+ 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+, these reserved words require
+ # 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: RB_RESERVED_WORDS + method_names).correct(method_name) - NAMES_TO_EXCLUDE[@receiver.class]
+ end
+
+ def method_names
+ method_names = receiver.methods + receiver.singleton_methods
+ method_names += receiver.private_methods if @private_call
+ method_names.uniq!
+ method_names
+ end
+ end
+end
diff --git a/lib/did_you_mean/spell_checkers/name_error_checkers.rb b/lib/did_you_mean/spell_checkers/name_error_checkers.rb
new file mode 100644
index 0000000000..6e2aaa4cb1
--- /dev/null
+++ b/lib/did_you_mean/spell_checkers/name_error_checkers.rb
@@ -0,0 +1,20 @@
+require_relative 'name_error_checkers/class_name_checker'
+require_relative 'name_error_checkers/variable_name_checker'
+
+module DidYouMean
+ class << (NameErrorCheckers = Object.new)
+ def new(exception)
+ case exception.original_message
+ when /uninitialized constant/
+ ClassNameChecker
+ when /undefined local variable or method/,
+ /undefined method/,
+ /uninitialized class variable/,
+ /no member '.*' in struct/
+ VariableNameChecker
+ else
+ NullChecker
+ end.new(exception)
+ end
+ end
+end
diff --git a/lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb b/lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb
new file mode 100644
index 0000000000..3bd048b27c
--- /dev/null
+++ b/lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb
@@ -0,0 +1,50 @@
+# frozen-string-literal: true
+
+require 'delegate'
+require_relative "../../spell_checker"
+
+module DidYouMean
+ class ClassNameChecker
+ attr_reader :class_name
+
+ def initialize(exception)
+ @class_name, @receiver, @original_message = exception.name, exception.receiver, exception.original_message
+ end
+
+ def corrections
+ @corrections ||= SpellChecker.new(dictionary: class_names)
+ .correct(class_name)
+ .map(&:full_name)
+ .reject {|qualified_name| @original_message.include?(qualified_name) }
+ end
+
+ def class_names
+ scopes.flat_map do |scope|
+ scope.constants.map do |c|
+ ClassName.new(c, scope == Object ? "" : "#{scope}::")
+ end
+ end
+ end
+
+ def scopes
+ @scopes ||= @receiver.to_s.split("::").inject([Object]) do |_scopes, scope|
+ _scopes << _scopes.last.const_get(scope)
+ end.uniq
+ end
+
+ class ClassName < SimpleDelegator
+ attr :namespace
+
+ def initialize(name, namespace = '')
+ super(name)
+ @namespace = namespace
+ end
+
+ def full_name
+ self.class.new("#{namespace}#{__getobj__}")
+ end
+ end
+
+ private_constant :ClassName
+ end
+end
diff --git a/lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb b/lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb
new file mode 100644
index 0000000000..3e51b4fa3a
--- /dev/null
+++ b/lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb
@@ -0,0 +1,82 @@
+# frozen-string-literal: true
+
+require_relative "../../spell_checker"
+
+module DidYouMean
+ class VariableNameChecker
+ attr_reader :name, :method_names, :lvar_names, :ivar_names, :cvar_names
+
+ NAMES_TO_EXCLUDE = { 'foo' => [:fork, :for] }
+ NAMES_TO_EXCLUDE.default = []
+
+ # +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("@", "")
+ @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
+
+ def corrections
+ @corrections ||= SpellChecker
+ .new(dictionary: (RB_RESERVED_WORDS + lvar_names + method_names + ivar_names + cvar_names))
+ .correct(name) - NAMES_TO_EXCLUDE[@name]
+ end
+ end
+end
diff --git a/lib/did_you_mean/spell_checkers/null_checker.rb b/lib/did_you_mean/spell_checkers/null_checker.rb
new file mode 100644
index 0000000000..1306f69d4a
--- /dev/null
+++ b/lib/did_you_mean/spell_checkers/null_checker.rb
@@ -0,0 +1,6 @@
+module DidYouMean
+ class NullChecker
+ def initialize(*); end
+ def corrections; [] end
+ end
+end
diff --git a/lib/did_you_mean/tree_spell_checker.rb b/lib/did_you_mean/tree_spell_checker.rb
new file mode 100644
index 0000000000..6a5b485413
--- /dev/null
+++ b/lib/did_you_mean/tree_spell_checker.rb
@@ -0,0 +1,137 @@
+module DidYouMean
+ # spell checker for a dictionary that has a tree
+ # structure, see doc/tree_spell_checker_api.md
+ class TreeSpellChecker
+ attr_reader :dictionary, :dimensions, :separator, :augment
+
+ def initialize(dictionary:, separator: '/', augment: nil)
+ @dictionary = dictionary
+ @separator = separator
+ @augment = augment
+ @dimensions = parse_dimensions
+ end
+
+ def correct(input)
+ plausibles = plausible_dimensions input
+ return no_idea(input) if plausibles.empty?
+ suggestions = find_suggestions input, plausibles
+ return no_idea(input) if suggestions.empty?
+ suggestions
+ end
+
+ private
+
+ def parse_dimensions
+ ParseDimensions.new(dictionary, separator).call
+ end
+
+ def find_suggestions(input, plausibles)
+ states = plausibles[0].product(*plausibles[1..-1])
+ paths = possible_paths states
+ leaf = input.split(separator).last
+ ideas = find_ideas(paths, leaf)
+ ideas.compact.flatten
+ end
+
+ def no_idea(input)
+ return [] unless augment
+ ::DidYouMean::SpellChecker.new(dictionary: dictionary).correct(input)
+ end
+
+ def find_ideas(paths, leaf)
+ paths.map do |path|
+ names = find_leaves(path)
+ ideas = CorrectElement.new.call names, leaf
+ ideas_to_paths ideas, leaf, names, path
+ end
+ end
+
+ def ideas_to_paths(ideas, leaf, names, path)
+ return nil if ideas.empty?
+ return [path + separator + leaf] if names.include? leaf
+ ideas.map { |str| path + separator + str }
+ end
+
+ def find_leaves(path)
+ dictionary.map do |str|
+ next unless str.include? "#{path}#{separator}"
+ str.gsub("#{path}#{separator}", '')
+ end.compact
+ end
+
+ def possible_paths(states)
+ states.map do |state|
+ state.join separator
+ end
+ end
+
+ def plausible_dimensions(input)
+ elements = input.split(separator)[0..-2]
+ elements.each_with_index.map do |element, i|
+ next if dimensions[i].nil?
+ CorrectElement.new.call dimensions[i], element
+ end.compact
+ end
+ end
+
+ # parses the elements in each dimension
+ class ParseDimensions
+ def initialize(dictionary, separator)
+ @dictionary = dictionary
+ @separator = separator
+ end
+
+ def call
+ leafless = remove_leaves
+ dimensions = find_elements leafless
+ dimensions.map do |elements|
+ elements.to_set.to_a
+ end
+ end
+
+ private
+
+ def remove_leaves
+ dictionary.map do |a|
+ elements = a.split(separator)
+ elements[0..-2]
+ end.to_set.to_a
+ end
+
+ def find_elements(leafless)
+ max_elements = leafless.map(&:size).max
+ dimensions = Array.new(max_elements) { [] }
+ (0...max_elements).each do |i|
+ leafless.each do |elements|
+ dimensions[i] << elements[i] unless elements[i].nil?
+ end
+ end
+ dimensions
+ end
+
+ attr_reader :dictionary, :separator
+ end
+
+ # identifies the elements close to element
+ class CorrectElement
+ def initialize
+ end
+
+ def call(names, element)
+ return names if names.size == 1
+ str = normalize element
+ return [str] if names.include? str
+ checker = ::DidYouMean::SpellChecker.new(dictionary: names)
+ checker.correct(str)
+ end
+
+ private
+
+ def normalize(leaf)
+ str = leaf.dup
+ str.downcase!
+ return str unless str.include? '@'
+ str.tr!('@', ' ')
+ end
+ end
+end
diff --git a/lib/did_you_mean/verbose.rb b/lib/did_you_mean/verbose.rb
new file mode 100644
index 0000000000..4e86f167ea
--- /dev/null
+++ b/lib/did_you_mean/verbose.rb
@@ -0,0 +1,4 @@
+require_relative '../did_you_mean'
+require_relative 'formatters/verbose_formatter'
+
+DidYouMean.formatter = DidYouMean::VerboseFormatter.new
diff --git a/lib/did_you_mean/version.rb b/lib/did_you_mean/version.rb
new file mode 100644
index 0000000000..cdfeea8026
--- /dev/null
+++ b/lib/did_you_mean/version.rb
@@ -0,0 +1,3 @@
+module DidYouMean
+ VERSION = "1.3.1"
+end