diff options
author | Yuki Nishijima <yk.nishijima@gmail.com> | 2020-05-22 17:17:10 -0400 |
---|---|---|
committer | Yuki Nishijima <yk.nishijima@gmail.com> | 2020-06-06 13:02:08 -0400 |
commit | e5f54465284b4505042fca10ace998e1d29c2313 (patch) | |
tree | 3770bc320b3cb7fdd7e5406bb1fbcd5c4c705c77 /lib/did_you_mean/spell_checkers | |
parent | 0c00a4176ba353d59d8c991428574ef2c2676674 (diff) | |
download | ruby-e5f54465284b4505042fca10ace998e1d29c2313.tar.gz |
Sync did_you_mean
Diffstat (limited to 'lib/did_you_mean/spell_checkers')
-rw-r--r-- | lib/did_you_mean/spell_checkers/method_name_checker.rb | 7 | ||||
-rw-r--r-- | lib/did_you_mean/spell_checkers/require_path_checker.rb | 35 |
2 files changed, 41 insertions, 1 deletions
diff --git a/lib/did_you_mean/spell_checkers/method_name_checker.rb b/lib/did_you_mean/spell_checkers/method_name_checker.rb index 3a38245f0c..0483127d6f 100644 --- a/lib/did_you_mean/spell_checkers/method_name_checker.rb +++ b/lib/did_you_mean/spell_checkers/method_name_checker.rb @@ -43,7 +43,12 @@ module DidYouMean end def corrections - @corrections ||= SpellChecker.new(dictionary: RB_RESERVED_WORDS + method_names).correct(method_name) - names_to_exclude + @corrections ||= begin + dictionary = method_names + dictionary = RB_RESERVED_WORDS + dictionary if @private_call + + SpellChecker.new(dictionary: dictionary).correct(method_name) - names_to_exclude + end end def method_names diff --git a/lib/did_you_mean/spell_checkers/require_path_checker.rb b/lib/did_you_mean/spell_checkers/require_path_checker.rb new file mode 100644 index 0000000000..aaf877b412 --- /dev/null +++ b/lib/did_you_mean/spell_checkers/require_path_checker.rb @@ -0,0 +1,35 @@ +# frozen-string-literal: true + +require_relative "../spell_checker" +require_relative "../tree_spell_checker" + +module DidYouMean + class RequirePathChecker + attr_reader :path + + INITIAL_LOAD_PATH = $LOAD_PATH.dup.freeze + ENV_SPECIFIC_EXT = ".#{RbConfig::CONFIG["DLEXT"]}" + + private_constant :INITIAL_LOAD_PATH, :ENV_SPECIFIC_EXT + + def self.requireables + @requireables ||= INITIAL_LOAD_PATH + .flat_map {|path| Dir.glob("**/???*{.rb,#{ENV_SPECIFIC_EXT}}", base: path) } + .map {|path| path.chomp!(".rb") || path.chomp!(ENV_SPECIFIC_EXT) } + end + + def initialize(exception) + @path = exception.path + end + + def corrections + @corrections ||= begin + threshold = path.size * 2 + dictionary = self.class.requireables.reject {|str| str.size >= threshold } + spell_checker = path.include?("/") ? TreeSpellChecker : SpellChecker + + spell_checker.new(dictionary: dictionary).correct(path).uniq + end + end + end +end |