2020-05-22 17:17:10 -04:00
|
|
|
# frozen-string-literal: true
|
|
|
|
|
|
|
|
require_relative "../spell_checker"
|
|
|
|
require_relative "../tree_spell_checker"
|
2021-01-18 19:00:01 -05:00
|
|
|
require "rbconfig"
|
2020-05-22 17:17:10 -04:00
|
|
|
|
|
|
|
module DidYouMean
|
|
|
|
class RequirePathChecker
|
|
|
|
attr_reader :path
|
|
|
|
|
|
|
|
INITIAL_LOAD_PATH = $LOAD_PATH.dup.freeze
|
2021-12-24 07:04:49 -05:00
|
|
|
Ractor.make_shareable(INITIAL_LOAD_PATH) if defined?(Ractor)
|
|
|
|
|
|
|
|
ENV_SPECIFIC_EXT = ".#{RbConfig::CONFIG["DLEXT"]}"
|
|
|
|
Ractor.make_shareable(ENV_SPECIFIC_EXT) if defined?(Ractor)
|
2020-05-22 17:17:10 -04:00
|
|
|
|
|
|
|
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
|