rubocop 0.20.1 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -0
  3. data/README.md +16 -4
  4. data/config/default.yml +37 -10
  5. data/config/enabled.yml +25 -7
  6. data/lib/rubocop.rb +15 -19
  7. data/lib/rubocop/cli.rb +2 -2
  8. data/lib/rubocop/config.rb +40 -3
  9. data/lib/rubocop/config_loader.rb +6 -37
  10. data/lib/rubocop/config_store.rb +0 -1
  11. data/lib/rubocop/cop/commissioner.rb +12 -9
  12. data/lib/rubocop/cop/cop.rb +17 -5
  13. data/lib/rubocop/cop/force.rb +41 -0
  14. data/lib/rubocop/cop/ignored_node.rb +10 -10
  15. data/lib/rubocop/cop/lint/ambiguous_operator.rb +1 -1
  16. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  17. data/lib/rubocop/cop/lint/block_alignment.rb +2 -2
  18. data/lib/rubocop/cop/lint/condition_position.rb +2 -0
  19. data/lib/rubocop/cop/lint/debugger.rb +17 -3
  20. data/lib/rubocop/cop/lint/end_alignment.rb +3 -11
  21. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  22. data/lib/rubocop/cop/lint/rescue_exception.rb +11 -0
  23. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +4 -6
  24. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +39 -0
  25. data/lib/rubocop/cop/lint/unused_block_argument.rb +81 -0
  26. data/lib/rubocop/cop/lint/unused_method_argument.rb +52 -0
  27. data/lib/rubocop/cop/lint/useless_assignment.rb +6 -8
  28. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +6 -0
  29. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  30. data/lib/rubocop/cop/lint/void.rb +1 -1
  31. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +2 -2
  32. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +7 -3
  33. data/lib/rubocop/cop/mixin/check_assignment.rb +11 -0
  34. data/lib/rubocop/cop/mixin/check_methods.rb +12 -0
  35. data/lib/rubocop/cop/mixin/percent_literal.rb +26 -0
  36. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
  37. data/lib/rubocop/cop/mixin/string_help.rb +0 -4
  38. data/lib/rubocop/cop/rails/delegate.rb +109 -0
  39. data/lib/rubocop/cop/style/align_hash.rb +3 -3
  40. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
  41. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  42. data/lib/rubocop/cop/style/dot_position.rb +1 -1
  43. data/lib/rubocop/cop/style/encoding.rb +44 -16
  44. data/lib/rubocop/cop/style/indentation_width.rb +29 -19
  45. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  46. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  47. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +3 -21
  48. data/lib/rubocop/cop/style/predicate_name.rb +1 -1
  49. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  50. data/lib/rubocop/cop/style/redundant_begin.rb +18 -2
  51. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  52. data/lib/rubocop/cop/style/regexp_literal.rb +29 -37
  53. data/lib/rubocop/cop/style/space_after_method_name.rb +1 -1
  54. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  55. data/lib/rubocop/cop/style/trailing_blank_lines.rb +37 -23
  56. data/lib/rubocop/cop/style/trailing_comma.rb +2 -3
  57. data/lib/rubocop/cop/style/trivial_accessors.rb +40 -4
  58. data/lib/rubocop/cop/style/unneeded_capital_w.rb +29 -0
  59. data/lib/rubocop/cop/team.rb +10 -2
  60. data/lib/rubocop/cop/util.rb +2 -2
  61. data/lib/rubocop/cop/{variable_inspector.rb → variable_force.rb} +45 -37
  62. data/lib/rubocop/cop/{variable_inspector → variable_force}/assignment.rb +1 -1
  63. data/lib/rubocop/cop/{variable_inspector → variable_force}/locatable.rb +1 -1
  64. data/lib/rubocop/cop/{variable_inspector → variable_force}/reference.rb +13 -1
  65. data/lib/rubocop/cop/{variable_inspector → variable_force}/scope.rb +9 -1
  66. data/lib/rubocop/cop/{variable_inspector → variable_force}/variable.rb +14 -4
  67. data/lib/rubocop/cop/{variable_inspector → variable_force}/variable_table.rb +1 -1
  68. data/lib/rubocop/file_inspector.rb +3 -1
  69. data/lib/rubocop/formatter/base_formatter.rb +1 -1
  70. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
  71. data/lib/rubocop/formatter/disabled_lines_formatter.rb +2 -2
  72. data/lib/rubocop/formatter/offense_count_formatter.rb +11 -10
  73. data/lib/rubocop/formatter/progress_formatter.rb +2 -2
  74. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  75. data/lib/rubocop/options.rb +74 -58
  76. data/lib/rubocop/path_util.rb +17 -2
  77. data/lib/rubocop/rake_task.rb +23 -5
  78. data/lib/rubocop/version.rb +1 -1
  79. data/relnotes/v0.21.0.md +45 -0
  80. data/rubocop-todo.yml +6 -6
  81. data/rubocop.gemspec +1 -1
  82. data/spec/rubocop/cli_spec.rb +170 -59
  83. data/spec/rubocop/config_spec.rb +48 -3
  84. data/spec/rubocop/config_store_spec.rb +3 -3
  85. data/spec/rubocop/cop/commissioner_spec.rb +9 -7
  86. data/spec/rubocop/cop/cop_spec.rb +0 -2
  87. data/spec/rubocop/cop/force_spec.rb +29 -0
  88. data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +2 -2
  89. data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +1 -1
  90. data/spec/rubocop/cop/lint/block_alignment_spec.rb +24 -24
  91. data/spec/rubocop/cop/lint/condition_position_spec.rb +7 -5
  92. data/spec/rubocop/cop/lint/debugger_spec.rb +26 -9
  93. data/spec/rubocop/cop/lint/end_alignment_spec.rb +6 -3
  94. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +7 -6
  95. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +36 -0
  96. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
  97. data/spec/rubocop/cop/lint/underscore_prefixed_variable_name_spec.rb +179 -0
  98. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +147 -0
  99. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +140 -0
  100. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +50 -48
  101. data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +1 -1
  102. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +2 -0
  103. data/spec/rubocop/cop/rails/delegate_spec.rb +152 -0
  104. data/spec/rubocop/cop/style/encoding_spec.rb +131 -36
  105. data/spec/rubocop/cop/style/indentation_width_spec.rb +79 -0
  106. data/spec/rubocop/cop/style/redundant_begin_spec.rb +32 -0
  107. data/spec/rubocop/cop/style/regexp_literal_spec.rb +83 -13
  108. data/spec/rubocop/cop/style/string_literals_spec.rb +9 -3
  109. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +65 -25
  110. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +76 -0
  111. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +85 -0
  112. data/spec/rubocop/cop/team_spec.rb +43 -0
  113. data/spec/rubocop/cop/{variable_inspector → variable_force}/assignment_spec.rb +3 -3
  114. data/spec/rubocop/cop/{variable_inspector → variable_force}/locatable_spec.rb +3 -3
  115. data/spec/rubocop/cop/{variable_inspector → variable_force}/scope_spec.rb +30 -2
  116. data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_spec.rb +12 -27
  117. data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_table_spec.rb +1 -1
  118. data/spec/rubocop/cop/{variable_inspector_spec.rb → variable_force_spec.rb} +4 -8
  119. data/spec/rubocop/formatter/base_formatter_spec.rb +2 -2
  120. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +1 -1
  121. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  122. data/spec/rubocop/options_spec.rb +2 -2
  123. data/spec/rubocop/path_util_spec.rb +47 -14
  124. data/spec/spec_helper.rb +9 -3
  125. data/spec/support/file_helper.rb +2 -0
  126. metadata +43 -26
  127. data/lib/rubocop/cop/style/final_newline.rb +0 -29
  128. data/spec/rubocop/cop/style/final_newline_spec.rb +0 -30
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Rubocop
4
4
  module Cop
5
- module VariableInspector
5
+ class VariableForce
6
6
  # A Variable represents existance of a local variable.
7
7
  # This holds a variable declaration node,
8
8
  # and some states of the variable.
@@ -11,7 +11,7 @@ module Rubocop
11
11
  (VARIABLE_ASSIGNMENT_TYPES + DECLARATION_TYPES).freeze
12
12
 
13
13
  attr_reader :name, :declaration_node, :scope,
14
- :assignments, :captured_by_block
14
+ :assignments, :references, :captured_by_block
15
15
  alias_method :captured_by_block?, :captured_by_block
16
16
 
17
17
  def initialize(name, declaration_node, scope)
@@ -26,6 +26,7 @@ module Rubocop
26
26
  @scope = scope
27
27
 
28
28
  @assignments = []
29
+ @references = []
29
30
  @captured_by_block = false
30
31
  end
31
32
 
@@ -34,11 +35,12 @@ module Rubocop
34
35
  end
35
36
 
36
37
  def referenced?
37
- @assignments.any?(&:referenced?)
38
+ !@references.empty?
38
39
  end
39
40
 
40
41
  def reference!(node)
41
42
  reference = Reference.new(node, @scope)
43
+ @references << reference
42
44
  consumed_branch_ids = Set.new
43
45
 
44
46
  @assignments.reverse_each do |assignment|
@@ -74,8 +76,16 @@ module Rubocop
74
76
  @captured_by_block || referenced?
75
77
  end
76
78
 
79
+ def argument?
80
+ ARGUMENT_DECLARATION_TYPES.include?(@declaration_node.type)
81
+ end
82
+
77
83
  def method_argument?
78
- METHOD_ARGUMENT_DECLARATION_TYPES.include?(@declaration_node.type)
84
+ argument? && [:def, :defs].include?(@scope.node.type)
85
+ end
86
+
87
+ def block_argument?
88
+ argument? && @scope.node.type == :block
79
89
  end
80
90
 
81
91
  def block_local_variable?
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Rubocop
4
4
  module Cop
5
- module VariableInspector
5
+ class VariableForce
6
6
  # A VariableTable manages the lifetime of all scopes and local variables
7
7
  # in a program.
8
8
  # This holds scopes as stack structure, and provides a way to add local
@@ -109,7 +109,9 @@ module Rubocop
109
109
  cop_classes = Cop::Cop.all
110
110
 
111
111
  if @options[:only]
112
- cop_classes.select! { |c| c.cop_name == @options[:only] }
112
+ cop_classes.select! do |c|
113
+ @options[:only].include?(c.cop_name) || @options[:lint] && c.lint?
114
+ end
113
115
  else
114
116
  # filter out Rails cops unless requested
115
117
  cop_classes.reject!(&:rails?) unless run_rails_cops?(config)
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # rubocop:disable LineLength
3
+ # rubocop:disable LineLength, UnusedMethodArgument
4
4
 
5
5
  module Rubocop
6
6
  module Formatter
@@ -22,12 +22,12 @@ module Rubocop
22
22
  attr_accessor :config_to_allow_offenses
23
23
  end
24
24
 
25
- def file_finished(file, offenses)
25
+ def file_finished(_file, offenses)
26
26
  @cops_with_offenses ||= Hash.new(0)
27
27
  offenses.each { |o| @cops_with_offenses[o.cop_name] += 1 }
28
28
  end
29
29
 
30
- def finished(inspected_files)
30
+ def finished(_inspected_files)
31
31
  output.puts HEADING
32
32
 
33
33
  # Syntax isn't a real cop and it can't be disabled.
@@ -9,7 +9,7 @@ module Rubocop
9
9
 
10
10
  attr_reader :cop_disabled_line_ranges
11
11
 
12
- def started(target_files)
12
+ def started(_target_files)
13
13
  @cop_disabled_line_ranges = {}
14
14
  end
15
15
 
@@ -20,7 +20,7 @@ module Rubocop
20
20
  options[:cop_disabled_line_ranges]
21
21
  end
22
22
 
23
- def finished(inspected_files)
23
+ def finished(_inspected_files)
24
24
  cops_disabled_in_comments_summary
25
25
  end
26
26
 
@@ -19,25 +19,26 @@ module Rubocop
19
19
  @offense_counts = Hash.new(0)
20
20
  end
21
21
 
22
- def file_finished(file, offenses)
22
+ def file_finished(_file, offenses)
23
23
  offenses.each { |o| @offense_counts[o.cop_name] += 1 }
24
24
  end
25
25
 
26
- def finished(inspected_files)
27
- report_summary(inspected_files.count,
28
- ordered_offense_counts(@offense_counts))
26
+ def finished(_inspected_files)
27
+ report_summary(@offense_counts)
29
28
  end
30
29
 
31
- def report_summary(file_count, offense_counts)
30
+ def report_summary(offense_counts)
31
+ per_cop_counts = ordered_offense_counts(offense_counts)
32
+ total_count = total_offense_count(offense_counts)
33
+
32
34
  output.puts
33
35
 
34
- offense_count = total_offense_count(offense_counts)
35
- offense_counts.each do |cop_name, count|
36
- output.puts "#{count.to_s.ljust(offense_count.to_s.length + 2)}" \
36
+ per_cop_counts.each do |cop_name, count|
37
+ output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}" \
37
38
  "#{cop_name}\n"
38
39
  end
39
40
  output.puts '--'
40
- output.puts "#{offense_count} Total"
41
+ output.puts "#{total_count} Total"
41
42
 
42
43
  output.puts
43
44
  end
@@ -46,7 +47,7 @@ module Rubocop
46
47
  Hash[offense_counts.sort_by { |k, v| [-v, k] }]
47
48
  end
48
49
 
49
- def total_offense_count(offense_counts = {})
50
+ def total_offense_count(offense_counts)
50
51
  offense_counts.values.inject(0, :+)
51
52
  end
52
53
  end
@@ -19,7 +19,7 @@ module Rubocop
19
19
  @offenses_for_files[file] = offenses
20
20
  end
21
21
 
22
- report_file_as_mark(file, offenses)
22
+ report_file_as_mark(offenses)
23
23
  end
24
24
 
25
25
  def finished(inspected_files)
@@ -40,7 +40,7 @@ module Rubocop
40
40
  @total_correction_count)
41
41
  end
42
42
 
43
- def report_file_as_mark(file, offenses)
43
+ def report_file_as_mark(offenses)
44
44
  mark = if offenses.empty?
45
45
  green('.')
46
46
  else
@@ -19,7 +19,7 @@ module Rubocop
19
19
  fatal: :red
20
20
  }.freeze
21
21
 
22
- def started(target_files)
22
+ def started(_target_files)
23
23
  @total_offense_count = 0
24
24
  @total_correction_count = 0
25
25
  end
@@ -3,6 +3,49 @@
3
3
  require 'optparse'
4
4
 
5
5
  module Rubocop
6
+ # This module contains help texts for command line options.
7
+ module OptionsHelp
8
+ TEXT = {
9
+ only: 'Run only the given cop(s).',
10
+ require: 'Require Ruby file.',
11
+ config: 'Specify configuration file.',
12
+ auto_gen_config: ['Generate a configuration file acting as a',
13
+ 'TODO list.'],
14
+ force_exclusion: ['Force excluding files specified in the',
15
+ 'configuration `Exclude` even if they are',
16
+ 'explicitly passed as arguments.'],
17
+ format: ['Choose an output formatter. This option',
18
+ 'can be specified multiple times to enable',
19
+ 'multiple formatters at the same time.',
20
+ ' [p]rogress (default)',
21
+ ' [s]imple',
22
+ ' [c]lang',
23
+ ' [d]isabled cops via inline comments',
24
+ ' [fu]ubar',
25
+ ' [e]macs',
26
+ ' [j]son',
27
+ ' [fi]les',
28
+ ' [o]ffenses',
29
+ ' custom formatter class name'],
30
+ out: ['Write output to a file instead of STDOUT.',
31
+ 'This option applies to the previously',
32
+ 'specified --format, or the default format',
33
+ 'if no format is specified.'],
34
+ fail_level: 'Minimum severity for exit with error code.',
35
+ show_cops: ['Shows the given cops, or all cops by',
36
+ 'default, and their configurations for the',
37
+ 'current directory.'],
38
+ debug: 'Display debug info.',
39
+ display_cop_names: 'Display cop names in offense messages.',
40
+ rails: 'Run extra Rails cops.',
41
+ lint: 'Run only lint cops.',
42
+ auto_correct: 'Auto-correct offenses.',
43
+ no_color: 'Disable color output.',
44
+ version: 'Display version.',
45
+ verbose_version: 'Display verbose version.'
46
+ }
47
+ end
48
+
6
49
  # This class handles command line options.
7
50
  class Options
8
51
  DEFAULT_FORMATTER = 'progress'
@@ -19,16 +62,15 @@ module Rubocop
19
62
  OptionParser.new do |opts|
20
63
  opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
21
64
 
22
- option(opts, '--only COP', 'Run just one cop.') do
65
+ option(opts, '--only [COP1,COP2,...]') do |list|
66
+ @options[:only] = list.split(',')
23
67
  validate_only_option
24
68
  end
25
69
 
26
70
  add_configuration_options(opts, args)
27
- add_formatting_options(opts, args)
71
+ add_formatting_options(opts)
28
72
 
29
- option(opts, '-r', '--require FILE', 'Require Ruby file.') do |f|
30
- require f
31
- end
73
+ option(opts, '-r', '--require FILE') { |f| require f }
32
74
 
33
75
  add_severity_option(opts)
34
76
  add_flags_with_optional_args(opts)
@@ -45,93 +87,65 @@ module Rubocop
45
87
  private
46
88
 
47
89
  def add_configuration_options(opts, args)
48
- option(opts, '-c', '--config FILE', 'Specify configuration file.')
90
+ option(opts, '-c', '--config FILE')
49
91
 
50
- option(opts, '--auto-gen-config',
51
- 'Generate a configuration file acting as a', 'TODO list.') do
92
+ option(opts, '--auto-gen-config') do
52
93
  validate_auto_gen_config_option(args)
53
94
  @options[:formatters] = [[DEFAULT_FORMATTER],
54
95
  [Formatter::DisabledConfigFormatter,
55
96
  ConfigLoader::AUTO_GENERATED_FILE]]
56
97
  end
57
98
 
58
- option(opts, '--force-exclusion',
59
- 'Force excluding files specified in the',
60
- 'configuration `Exclude` even if they are',
61
- 'explicitly passed as arguments.')
99
+ option(opts, '--force-exclusion')
62
100
  end
63
101
 
64
- FORMAT_HELP = ['Choose an output formatter. This option',
65
- 'can be specified multiple times to enable',
66
- 'multiple formatters at the same time.',
67
- ' [p]rogress (default)',
68
- ' [s]imple',
69
- ' [c]lang',
70
- ' [d]isabled cops via inline comments',
71
- ' [fu]ubar',
72
- ' [e]macs',
73
- ' [j]son',
74
- ' [fi]les',
75
- ' [o]ffenses',
76
- ' custom formatter class name']
77
-
78
- def add_formatting_options(opts, args)
79
- option(opts, '-f', '--format FORMATTER', *FORMAT_HELP) do |key|
102
+ def add_formatting_options(opts)
103
+ option(opts, '-f', '--format FORMATTER') do |key|
80
104
  @options[:formatters] ||= []
81
105
  @options[:formatters] << [key]
82
106
  end
83
107
 
84
- option(opts, '-o', '--out FILE',
85
- 'Write output to a file instead of STDOUT.',
86
- 'This option applies to the previously',
87
- 'specified --format, or the default format',
88
- 'if no format is specified.') do |path|
108
+ option(opts, '-o', '--out FILE') do |path|
89
109
  @options[:formatters] ||= [[DEFAULT_FORMATTER]]
90
110
  @options[:formatters].last << path
91
111
  end
92
112
  end
93
113
 
94
114
  def add_severity_option(opts)
95
- opts.on('--fail-level SEVERITY',
96
- Rubocop::Cop::Severity::NAMES,
97
- Rubocop::Cop::Severity::CODE_TABLE,
98
- 'Minimum severity for exit with error code.') do |severity|
99
- @options[:fail_level] = severity
100
- end
115
+ option(opts, '--fail-level SEVERITY',
116
+ Rubocop::Cop::Severity::NAMES,
117
+ Rubocop::Cop::Severity::CODE_TABLE) do |severity|
118
+ @options[:fail_level] = severity
119
+ end
101
120
  end
102
121
 
103
122
  def add_flags_with_optional_args(opts)
104
- option(opts, '--show-cops [cop1,cop2,...]',
105
- 'Shows the given cops, or all cops by',
106
- 'default, and their configurations for the',
107
- 'current directory.') do |list|
123
+ option(opts, '--show-cops [COP1,COP2,...]') do |list|
108
124
  @options[:show_cops] = list.nil? ? [] : list.split(',')
109
125
  end
110
126
  end
111
127
 
112
128
  def add_boolean_flags(opts)
113
- option(opts, '-d', '--debug', 'Display debug info.')
114
- option(opts,
115
- '-D', '--display-cop-names',
116
- 'Display cop names in offense messages.')
117
- option(opts, '-R', '--rails', 'Run extra Rails cops.')
118
- option(opts, '-l', '--lint', 'Run only lint cops.')
119
- option(opts, '-a', '--auto-correct', 'Auto-correct offenses.')
129
+ option(opts, '-d', '--debug')
130
+ option(opts, '-D', '--display-cop-names')
131
+ option(opts, '-R', '--rails')
132
+ option(opts, '-l', '--lint')
133
+ option(opts, '-a', '--auto-correct')
120
134
 
121
135
  @options[:color] = true
122
- opts.on('-n', '--no-color', 'Disable color output.') do
123
- @options[:color] = false
124
- end
136
+ option(opts, '-n', '--no-color') { @options[:color] = false }
125
137
 
126
- option(opts, '-v', '--version', 'Display version.')
127
- option(opts, '-V', '--verbose-version', 'Display verbose version.')
138
+ option(opts, '-v', '--version')
139
+ option(opts, '-V', '--verbose-version')
128
140
  end
129
141
 
130
142
  # Sets a value in the @options hash, based on the given long option and its
131
143
  # value, in addition to calling the block if a block is given.
132
144
  def option(opts, *args)
145
+ long_opt_symbol = long_opt_symbol(args)
146
+ args += Array(OptionsHelp::TEXT[long_opt_symbol])
133
147
  opts.on(*args) do |arg|
134
- @options[long_opt_symbol(args)] = arg
148
+ @options[long_opt_symbol] = arg
135
149
  yield arg if block_given?
136
150
  end
137
151
  end
@@ -174,8 +188,10 @@ module Rubocop
174
188
  end
175
189
 
176
190
  def validate_only_option
177
- if Cop::Cop.all.none? { |c| c.cop_name == @options[:only] }
178
- fail ArgumentError, "Unrecognized cop name: #{@options[:only]}."
191
+ @options[:only].each do |cop_to_run|
192
+ if Cop::Cop.all.none? { |c| c.cop_name == cop_to_run }
193
+ fail ArgumentError, "Unrecognized cop name: #{cop_to_run}."
194
+ end
179
195
  end
180
196
  end
181
197
 
@@ -10,11 +10,26 @@ module Rubocop
10
10
  path_name.relative_path_from(Pathname.new(base_dir)).to_s
11
11
  end
12
12
 
13
- def match_path?(pattern, path)
13
+ # TODO: The old way of matching patterns is flawed, so a new one has been
14
+ # introduced. We keep supporting the old way for a while and issue
15
+ # deprecation warnings when a pattern is used that produced a match with
16
+ # the old way but doesn't match with the new.
17
+ def match_path?(pattern, path, config_path)
14
18
  case pattern
15
19
  when String
16
20
  basename = File.basename(path)
17
- path == pattern || basename == pattern || File.fnmatch(pattern, path)
21
+ old_match = basename == pattern || File.fnmatch?(pattern, path)
22
+ new_match = File.fnmatch?(pattern, path, File::FNM_PATHNAME)
23
+ if old_match && !new_match
24
+ instruction = if basename == pattern
25
+ ". Change to '**/#{pattern}'."
26
+ elsif pattern.end_with?('**')
27
+ ". Change to '#{pattern}/*'."
28
+ end
29
+ warn("Warning: Deprecated pattern style '#{pattern}' in " \
30
+ "#{config_path}#{instruction}")
31
+ end
32
+ old_match || new_match
18
33
  when Regexp
19
34
  path =~ pattern
20
35
  end
@@ -27,24 +27,30 @@ module Rubocop
27
27
  if task_block
28
28
  task_block.call(*[self, task_args].slice(0, task_block.arity))
29
29
  end
30
- run_task(verbose)
30
+ run_main_task(verbose)
31
31
  end
32
32
  end
33
+
34
+ setup_subtasks(name)
35
+ end
36
+
37
+ def run_main_task(verbose)
38
+ run_cli(verbose, full_options)
33
39
  end
34
40
 
35
- def run_task(verbose)
41
+ private
42
+
43
+ def run_cli(verbose, options)
36
44
  # We lazy-load rubocop so that the task doesn't dramatically impact the
37
45
  # load time of your Rakefile.
38
46
  require 'rubocop'
39
47
 
40
48
  cli = CLI.new
41
49
  puts 'Running RuboCop...' if verbose
42
- result = cli.run(full_options)
50
+ result = cli.run(options)
43
51
  abort('RuboCop failed!') if fail_on_error unless result == 0
44
52
  end
45
53
 
46
- private
47
-
48
54
  def full_options
49
55
  [].tap do |result|
50
56
  result.concat(formatters.map { |f| ['--format', f] }.flatten)
@@ -66,5 +72,17 @@ module Rubocop
66
72
  @options = []
67
73
  @formatters = [Rubocop::Options::DEFAULT_FORMATTER]
68
74
  end
75
+
76
+ def setup_subtasks(name)
77
+ namespace name do
78
+
79
+ desc 'Auto-correct RuboCop offenses'
80
+
81
+ task :auto_correct do
82
+ options = full_options.unshift('--auto-correct')
83
+ run_cli(verbose, options)
84
+ end
85
+ end
86
+ end
69
87
  end
70
88
  end