Skip to content

Commit 3e6197f

Browse files
committed
Correct RuboCop offences
1 parent 43258fe commit 3e6197f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3000
-3046
lines changed

.rubocop.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ AllCops:
22
Includes:
33
- 'Gemfile'
44
- 'Thorfile'
5+
- 'thor.gemspec'
56

67
# Avoid long parameter lists
78
ParameterLists:
8-
Max: 3
9+
Max: 5
910
CountKeywordArgs: true
1011

1112
MethodLength:
@@ -14,7 +15,7 @@ MethodLength:
1415

1516
# Avoid more than `Max` levels of nesting.
1617
BlockNesting:
17-
Max: 3
18+
Max: 4
1819

1920
# Align with the style guide.
2021
CollectionMethods:

Thorfile

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# encoding: utf-8
2-
$:.unshift File.expand_path("../lib", __FILE__)
1+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
32

43
require 'bundler'
54
require 'thor/rake_compat'
@@ -8,23 +7,23 @@ class Default < Thor
87
include Thor::RakeCompat
98
Bundler::GemHelper.install_tasks
109

11-
desc "build", "Build thor-#{Thor::VERSION}.gem into the pkg directory"
10+
desc 'build', "Build thor-#{Thor::VERSION}.gem into the pkg directory"
1211
def build
13-
Rake::Task["build"].execute
12+
Rake::Task['build'].execute
1413
end
1514

16-
desc "install", "Build and install thor-#{Thor::VERSION}.gem into system gems"
15+
desc 'install', "Build and install thor-#{Thor::VERSION}.gem into system gems"
1716
def install
18-
Rake::Task["install"].execute
17+
Rake::Task['install'].execute
1918
end
2019

21-
desc "release", "Create tag v#{Thor::VERSION} and build and push thor-#{Thor::VERSION}.gem to Rubygems"
20+
desc 'release', "Create tag v#{Thor::VERSION} and build and push thor-#{Thor::VERSION}.gem to Rubygems"
2221
def release
23-
Rake::Task["release"].execute
22+
Rake::Task['release'].execute
2423
end
2524

26-
desc "spec", "Run RSpec code examples"
25+
desc 'spec', 'Run RSpec code examples'
2726
def spec
28-
exec "rspec spec"
27+
exec 'rspec spec'
2928
end
3029
end

lib/thor.rb

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
require 'set'
22
require 'thor/base'
33

4-
class Thor
4+
class Thor # rubocop:disable ClassLength
55
class << self
66
# Allows for custom "Command" package naming.
77
#
88
# === Parameters
99
# name<String>
1010
# options<Hash>
1111
#
12-
def package_name(name, options={})
12+
def package_name(name, options = {})
1313
@package_name = name.nil? || name == '' ? nil : name
1414
end
1515

@@ -18,17 +18,17 @@ def package_name(name, options={})
1818
# ==== Parameters
1919
# meth<Symbol>:: name of the default command
2020
#
21-
def default_command(meth=nil)
21+
def default_command(meth = nil)
2222
@default_command = case meth
23-
when :none
24-
'help'
25-
when nil
26-
@default_command || from_superclass(:default_command, 'help')
27-
else
28-
meth.to_s
29-
end
23+
when :none
24+
'help'
25+
when nil
26+
@default_command || from_superclass(:default_command, 'help')
27+
else
28+
meth.to_s
29+
end
3030
end
31-
alias default_task default_command
31+
alias_method :default_task, :default_command
3232

3333
# Registers another Thor subclass as a command.
3434
#
@@ -37,7 +37,7 @@ def default_command(meth=nil)
3737
# command<String>:: Subcommand name to use
3838
# usage<String>:: Short usage for the subcommand
3939
# description<String>:: Description for the subcommand
40-
def register(klass, subcommand_name, usage, description, options={})
40+
def register(klass, subcommand_name, usage, description, options = {})
4141
if klass <= Thor::Group
4242
desc usage, description, options
4343
define_method(subcommand_name) { |*args| invoke(klass, args) }
@@ -54,7 +54,7 @@ def register(klass, subcommand_name, usage, description, options={})
5454
# description<String>
5555
# options<String>
5656
#
57-
def desc(usage, description, options={})
57+
def desc(usage, description, options = {})
5858
if options[:for]
5959
command = find_and_refresh_command(options[:for])
6060
command.usage = usage if usage
@@ -69,7 +69,7 @@ def desc(usage, description, options={})
6969
# ==== Parameters
7070
# long description<String>
7171
#
72-
def long_desc(long_description, options={})
72+
def long_desc(long_description, options = {})
7373
if options[:for]
7474
command = find_and_refresh_command(options[:for])
7575
command.long_description = long_description if long_description
@@ -91,13 +91,13 @@ def long_desc(long_description, options={})
9191
# ==== Parameters
9292
# Hash[String|Array => Symbol]:: Maps the string or the strings in the array to the given command.
9393
#
94-
def map(mappings=nil)
94+
def map(mappings = nil)
9595
@map ||= from_superclass(:map, {})
9696

9797
if mappings
9898
mappings.each do |key, value|
9999
if key.respond_to?(:each)
100-
key.each {|subkey| @map[subkey] = value}
100+
key.each { |subkey| @map[subkey] = value }
101101
else
102102
@map[key] = value
103103
end
@@ -114,13 +114,13 @@ def map(mappings=nil)
114114
# is the type of the option. Can be :string, :array, :hash, :boolean, :numeric
115115
# or :required (string). If you give a value, the type of the value is used.
116116
#
117-
def method_options(options=nil)
117+
def method_options(options = nil)
118118
@method_options ||= {}
119119
build_options(options, @method_options) if options
120120
@method_options
121121
end
122122

123-
alias options method_options
123+
alias_method :options, :method_options
124124

125125
# Adds an option to the set of method options. If :for is given as option,
126126
# it allows you to change the options from a previous defined command.
@@ -148,16 +148,16 @@ def method_options(options=nil)
148148
# :banner - String to show on usage notes.
149149
# :hide - If you want to hide this option from the help.
150150
#
151-
def method_option(name, options={})
151+
def method_option(name, options = {})
152152
scope = if options[:for]
153-
find_and_refresh_command(options[:for]).options
154-
else
155-
method_options
156-
end
153+
find_and_refresh_command(options[:for]).options
154+
else
155+
method_options
156+
end
157157

158158
build_option(name, options, scope)
159159
end
160-
alias option method_option
160+
alias_method :option, :method_option
161161

162162
# Prints help information for the given command.
163163
#
@@ -170,18 +170,18 @@ def command_help(shell, command_name)
170170
command = all_commands[meth]
171171
handle_no_command_error(meth) unless command
172172

173-
shell.say "Usage:"
173+
shell.say 'Usage:'
174174
shell.say " #{banner(command)}"
175175
shell.say
176176
class_options_help(shell, nil => command.options.map { |_, o| o })
177177
if command.long_description
178-
shell.say "Description:"
178+
shell.say 'Description:'
179179
shell.print_wrapped(command.long_description, :indent => 2)
180180
else
181181
shell.say command.description
182182
end
183183
end
184-
alias task_help command_help
184+
alias_method :task_help, :command_help
185185

186186
# Prints help information for this class.
187187
#
@@ -193,12 +193,12 @@ def help(shell, subcommand = false)
193193
Thor::Util.thor_classes_in(self).each do |klass|
194194
list += klass.printable_commands(false)
195195
end
196-
list.sort!{ |a,b| a[0] <=> b[0] }
196+
list.sort! { |a, b| a[0] <=> b[0] }
197197

198198
if @package_name
199199
shell.say "#{@package_name} commands:"
200200
else
201-
shell.say "Commands:"
201+
shell.say 'Commands:'
202202
end
203203

204204
shell.print_table(list, :indent => 2, :truncate => true)
@@ -212,34 +212,34 @@ def printable_commands(all = true, subcommand = false)
212212
next if command.hidden?
213213
item = []
214214
item << banner(command, false, subcommand)
215-
item << (command.description ? "# #{command.description.gsub(/\s+/m,' ')}" : "")
215+
item << (command.description ? "# #{command.description.gsub(/\s+/m, ' ')}" : '')
216216
item
217217
end.compact
218218
end
219-
alias printable_tasks printable_commands
219+
alias_method :printable_tasks, :printable_commands
220220

221221
def subcommands
222222
@subcommands ||= from_superclass(:subcommands, [])
223223
end
224-
alias subtasks subcommands
224+
alias_method :subtasks, :subcommands
225225

226226
def subcommand(subcommand, subcommand_class)
227-
self.subcommands << subcommand.to_s
227+
subcommands << subcommand.to_s
228228
subcommand_class.subcommand_help subcommand
229229

230230
define_method(subcommand) do |*args|
231231
args, opts = Thor::Arguments.split(args)
232232
invoke subcommand_class, args, opts, :invoked_via_subcommand => true, :class_options => options
233233
end
234234
end
235-
alias subtask subcommand
235+
alias_method :subtask, :subcommand
236236

237237
# Extend check unknown options to accept a hash of conditions.
238238
#
239239
# === Parameters
240240
# options<Hash>: A hash containing :only and/or :except keys
241-
def check_unknown_options!(options={})
242-
@check_unknown_options ||= Hash.new
241+
def check_unknown_options!(options = {})
242+
@check_unknown_options ||= {}
243243
options.each do |key, value|
244244
if value
245245
@check_unknown_options[key] = Array(value)
@@ -320,7 +320,7 @@ def stop_on_unknown_option?(command) #:nodoc:
320320
protected
321321

322322
# The method responsible for dispatching given the args.
323-
def dispatch(meth, given_args, given_opts, config) #:nodoc:
323+
def dispatch(meth, given_args, given_opts, config) #:nodoc: # rubocop:disable MethodLength
324324
# There is an edge case when dispatching from a subcommand.
325325
# A problem occurs invoking the default command. This case occurs
326326
# when arguments are passed and a default command is defined, and
@@ -331,7 +331,7 @@ def dispatch(meth, given_args, given_opts, config) #:nodoc:
331331
# the command normally. If the first item in given_args is not
332332
# a command then use the default command. The given_args will be
333333
# intact later since dup was used.
334-
if config[:invoked_via_subcommand] && given_args.size >= 1 && default_command != "help" && given_args.first != default_command
334+
if config[:invoked_via_subcommand] && given_args.size >= 1 && default_command != 'help' && given_args.first != default_command
335335
meth ||= retrieve_command_name(given_args.dup)
336336
command = all_commands[normalize_command_name(meth)]
337337
command ||= all_commands[normalize_command_name(default_command)]
@@ -382,16 +382,16 @@ def create_command(meth) #:nodoc:
382382
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
383383
@usage, @desc, @long_desc, @method_options, @hide = nil
384384
true
385-
elsif self.all_commands[meth] || meth == "method_missing"
385+
elsif all_commands[meth] || meth == 'method_missing'
386386
true
387387
else
388388
puts "[WARNING] Attempted to create command #{meth.inspect} without usage or description. " <<
389-
"Call desc if you want this method to be available as command or declare it inside a " <<
389+
'Call desc if you want this method to be available as command or declare it inside a ' <<
390390
"no_commands{} block. Invoked from #{caller[1].inspect}."
391391
false
392392
end
393393
end
394-
alias create_task create_command
394+
alias_method :create_task, :create_command
395395

396396
def initialize_added #:nodoc:
397397
class_options.merge!(method_options)
@@ -407,7 +407,7 @@ def retrieve_command_name(args) #:nodoc:
407407
nil
408408
end
409409
end
410-
alias retrieve_task_name retrieve_command_name
410+
alias_method :retrieve_task_name, :retrieve_command_name
411411

412412
# receives a (possibly nil) command name and returns a name that is in
413413
# the commands hash. In addition to normalizing aliases, this logic
@@ -421,7 +421,7 @@ def normalize_command_name(meth) #:nodoc:
421421

422422
possibilities = find_command_possibilities(meth)
423423
if possibilities.size > 1
424-
raise AmbiguousTaskError, "Ambiguous command #{meth} matches [#{possibilities.join(', ')}]"
424+
fail AmbiguousTaskError, "Ambiguous command #{meth} matches [#{possibilities.join(', ')}]"
425425
elsif possibilities.size < 1
426426
meth = meth || default_command
427427
elsif map[meth]
@@ -430,9 +430,9 @@ def normalize_command_name(meth) #:nodoc:
430430
meth = possibilities.first
431431
end
432432

433-
meth.to_s.gsub('-','_') # treat foo-bar as foo_bar
433+
meth.to_s.gsub('-', '_') # treat foo-bar as foo_bar
434434
end
435-
alias normalize_task_name normalize_command_name
435+
alias_method :normalize_task_name, :normalize_command_name
436436

437437
# this is the logic that takes the command name passed in by the user
438438
# and determines whether it is an unambiguous substrings of a command or
@@ -450,23 +450,22 @@ def find_command_possibilities(meth)
450450
possibilities
451451
end
452452
end
453-
alias find_task_possibilities find_command_possibilities
453+
alias_method :find_task_possibilities, :find_command_possibilities
454454

455455
def subcommand_help(cmd)
456-
desc "help [COMMAND]", "Describe subcommands or one specific subcommand"
457-
class_eval <<-RUBY
456+
desc 'help [COMMAND]', 'Describe subcommands or one specific subcommand'
457+
class_eval "
458458
def help(command = nil, subcommand = true); super; end
459-
RUBY
459+
"
460460
end
461-
alias subtask_help subcommand_help
462-
461+
alias_method :subtask_help, :subcommand_help
463462
end
464463

465464
include Thor::Base
466465

467466
map HELP_MAPPINGS => :help
468467

469-
desc "help [COMMAND]", "Describe available commands or one specific command"
468+
desc 'help [COMMAND]', 'Describe available commands or one specific command'
470469
def help(command = nil, subcommand = false)
471470
command ? self.class.command_help(shell, command) : self.class.help(shell, subcommand)
472471
end

0 commit comments

Comments
 (0)