|
1 | 1 | 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 |
16 | 26 |
|
17 | 27 | # Thor::Error is raised when it's caused by wrong usage of thor classes. Those
|
18 | 28 | # errors have their backtrace suppressed and are nicely shown to the user.
|
@@ -53,7 +63,7 @@ def initialize(command, all_commands, namespace)
|
53 | 63 | super(message)
|
54 | 64 | end
|
55 | 65 |
|
56 |
| - prepend Correctable if Correctable |
| 66 | + prepend Correctable if defined?(Correctable) |
57 | 67 | end
|
58 | 68 | UndefinedTaskError = UndefinedCommandError
|
59 | 69 |
|
@@ -92,24 +102,12 @@ def initialize(switches, unknown)
|
92 | 102 | super("Unknown switches #{unknown.map(&:inspect).join(', ')}")
|
93 | 103 | end
|
94 | 104 |
|
95 |
| - prepend Correctable if Correctable |
| 105 | + prepend Correctable if defined?(Correctable) |
96 | 106 | end
|
97 | 107 |
|
98 | 108 | class RequiredArgumentMissingError < InvocationError
|
99 | 109 | end
|
100 | 110 |
|
101 | 111 | class MalformattedArgumentError < InvocationError
|
102 | 112 | 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 |
115 | 113 | end
|
0 commit comments