Skip to content

Commit

Permalink
Sync did_you_mean
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki24 committed May 12, 2020
1 parent 7cc55f4 commit 946dadd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/did_you_mean.rb
Expand Up @@ -6,6 +6,7 @@
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/spell_checkers/require_path_checker'
require_relative 'did_you_mean/formatters/plain_formatter'
require_relative 'did_you_mean/tree_spell_checker'

Expand Down Expand Up @@ -95,8 +96,9 @@ def self.correct_error(error_class, spell_checker)
correct_error NameError, NameErrorCheckers
correct_error KeyError, KeyErrorChecker
correct_error NoMethodError, MethodNameChecker
correct_error LoadError, RequirePathChecker

# Returns the currenctly set formatter. By default, it is set to +DidYouMean::Formatter+.
# Returns the currently set formatter. By default, it is set to +DidYouMean::Formatter+.
def self.formatter
@@formatter
end
Expand Down
7 changes: 6 additions & 1 deletion lib/did_you_mean/spell_checkers/method_name_checker.rb
Expand Up @@ -43,7 +43,12 @@ def initialize(exception)
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
Expand Down
33 changes: 33 additions & 0 deletions lib/did_you_mean/spell_checkers/require_path_checker.rb
@@ -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
7 changes: 7 additions & 0 deletions test/did_you_mean/spell_checking/test_method_name_check.rb
Expand Up @@ -137,4 +137,11 @@ def test_suggests_yield
assert_correction :yield, error.corrections
assert_match "Did you mean? yield", error.to_s
end

def test_does_not_suggest_yield
error = assert_raise(NoMethodError) { 1.yeild }

assert_correction [], error.corrections
assert_not_match(/Did you mean\? +yield/, error.to_s)
end if RUBY_ENGINE != "jruby"
end
30 changes: 30 additions & 0 deletions test/did_you_mean/spell_checking/test_require_path_check.rb
@@ -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
1 change: 1 addition & 0 deletions test/did_you_mean/test_verbose_formatter.rb
Expand Up @@ -3,6 +3,7 @@
class VerboseFormatterTest < Test::Unit::TestCase
def setup
require_relative File.join(DidYouMean::TestHelper.root, 'verbose')

DidYouMean.formatter = DidYouMean::VerboseFormatter.new
end

Expand Down

0 comments on commit 946dadd

Please sign in to comment.