Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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 | ||
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) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/did_you_mean/spell_checking/test_require_path_check.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require_relative '../helper' | ||
|
||
class RequirePathCheckTest < Test::Unit::TestCase | ||
include DidYouMean::TestHelper | ||
|
||
def test_load_error_from_require_has_suggestions | ||
error = assert_raise LoadError do | ||
require 'open_struct' | ||
end | ||
|
||
assert_correction 'ostruct', error.corrections | ||
assert_match "Did you mean? ostruct", error.to_s | ||
end | ||
|
||
def test_load_error_from_require_for_nested_files_has_suggestions | ||
error = assert_raise LoadError do | ||
require 'net/htt' | ||
end | ||
|
||
assert_correction 'net/http', error.corrections | ||
assert_match "Did you mean? net/http", error.to_s | ||
|
||
error = assert_raise LoadError do | ||
require 'net-http' | ||
end | ||
|
||
assert_correction ['net/http', 'net/https'], error.corrections | ||
assert_match "Did you mean? net/http", error.to_s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters