Skip to content

Commit d9ec76b

Browse files
Reimplement did_you_mean suggestions to keep behaviour accross rubies
Ruby 3.2 will introduce `Exception#detailed_message` and `did_you_mean` has been already updated in Ruby 3.2 to use that. The new behaviour means not changing the original `Exception#message`. That means it is hard to get the previous error output, because `Exception#detailed_message` includes not only `did_you_mean` decorations, but also additional information like the exception class. To fix this, I bring the old did_you_mean behavior into Thor, so that the above changes do not affect us.
1 parent 5089c9b commit d9ec76b

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

lib/thor/error.rb

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
class Thor
2-
Correctable = if defined?(DidYouMean::SpellChecker) && defined?(DidYouMean::Correctable) # rubocop:disable Naming/ConstantName
3-
# In order to support versions of Ruby that don't have keyword
4-
# arguments, we need our own spell checker class that doesn't take key
5-
# words. Even though this code wouldn't be hit because of the check
6-
# above, it's still necessary because the interpreter would otherwise be
7-
# unable to parse the file.
8-
class NoKwargSpellChecker < DidYouMean::SpellChecker # :nodoc:
9-
def initialize(dictionary)
10-
@dictionary = dictionary
11-
end
12-
end
13-
14-
DidYouMean::Correctable
15-
end
2+
if defined?(DidYouMean::SpellChecker)
3+
# In order to support versions of Ruby that don't have keyword
4+
# arguments, we need our own spell checker class that doesn't take key
5+
# words. Even though this code wouldn't be hit because of the check
6+
# above, it's still necessary because the interpreter would otherwise be
7+
# unable to parse the file.
8+
class NoKwargSpellChecker < DidYouMean::SpellChecker # :nodoc:
9+
def initialize(dictionary)
10+
@dictionary = dictionary
11+
end
12+
end
13+
end
14+
15+
if defined?(DidYouMean::Correctable)
16+
module Correctable
17+
def to_s
18+
super + DidYouMean.formatter.message_for(corrections)
19+
end
20+
21+
def corrections
22+
@corrections ||= self.class.const_get(:SpellChecker).new(self).corrections
23+
end
24+
end
25+
end
1626

1727
# Thor::Error is raised when it's caused by wrong usage of thor classes. Those
1828
# errors have their backtrace suppressed and are nicely shown to the user.
@@ -53,7 +63,7 @@ def initialize(command, all_commands, namespace)
5363
super(message)
5464
end
5565

56-
prepend Correctable if Correctable
66+
prepend Correctable if defined?(Correctable)
5767
end
5868
UndefinedTaskError = UndefinedCommandError
5969

@@ -92,24 +102,12 @@ def initialize(switches, unknown)
92102
super("Unknown switches #{unknown.map(&:inspect).join(', ')}")
93103
end
94104

95-
prepend Correctable if Correctable
105+
prepend Correctable if defined?(Correctable)
96106
end
97107

98108
class RequiredArgumentMissingError < InvocationError
99109
end
100110

101111
class MalformattedArgumentError < InvocationError
102112
end
103-
104-
if Correctable
105-
if DidYouMean.respond_to?(:correct_error)
106-
DidYouMean.correct_error(Thor::UndefinedCommandError, UndefinedCommandError::SpellChecker)
107-
DidYouMean.correct_error(Thor::UnknownArgumentError, UnknownArgumentError::SpellChecker)
108-
else
109-
DidYouMean::SPELL_CHECKERS.merge!(
110-
'Thor::UndefinedCommandError' => UndefinedCommandError::SpellChecker,
111-
'Thor::UnknownArgumentError' => UnknownArgumentError::SpellChecker
112-
)
113-
end
114-
end
115113
end

0 commit comments

Comments
 (0)