| 1 |
# frozen-string-literal: true |
| 2 |
|
| 3 |
require_relative "../spell_checker" |
| 4 |
require_relative "../tree_spell_checker" |
| 5 |
require "rbconfig" |
| 6 |
|
| 7 |
module DidYouMean |
| 8 |
class RequirePathChecker |
| 9 |
attr_reader :path |
| 10 |
|
| 11 |
INITIAL_LOAD_PATH = $LOAD_PATH.dup.freeze |
| 12 |
Ractor.make_shareable(INITIAL_LOAD_PATH) if defined?(Ractor) |
| 13 |
|
| 14 |
ENV_SPECIFIC_EXT = ".#{RbConfig::CONFIG["DLEXT"]}" |
| 15 |
Ractor.make_shareable(ENV_SPECIFIC_EXT) if defined?(Ractor) |
| 16 |
|
| 17 |
private_constant :INITIAL_LOAD_PATH, :ENV_SPECIFIC_EXT |
| 18 |
|
| 19 |
def self.requireables |
| 20 |
@requireables ||= INITIAL_LOAD_PATH |
| 21 |
.flat_map {|path| Dir.glob("**/???*{.rb,#{ENV_SPECIFIC_EXT}}", base: path) } |
| 22 |
.map {|path| path.chomp!(".rb") || path.chomp!(ENV_SPECIFIC_EXT) } |
| 23 |
end |
| 24 |
|
| 25 |
def initialize(exception) |
| 26 |
@path = exception.path |
| 27 |
end |
| 28 |
|
| 29 |
def corrections |
| 30 |
@corrections ||= begin |
| 31 |
threshold = path.size * 2 |
| 32 |
dictionary = self.class.requireables.reject {|str| str.size >= threshold } |
| 33 |
spell_checker = path.include?("/") ? TreeSpellChecker : SpellChecker |
| 34 |
|
| 35 |
spell_checker.new(dictionary: dictionary).correct(path).uniq |
| 36 |
end |
| 37 |
end |
| 38 |
end |
| 39 |
end |