rubocop 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +75 -16
  4. data/lib/rubocop.rb +5 -0
  5. data/lib/rubocop/cli.rb +5 -1
  6. data/lib/rubocop/cli/command/execute_runner.rb +26 -11
  7. data/lib/rubocop/cli/command/suggest_extensions.rb +67 -0
  8. data/lib/rubocop/config_loader.rb +12 -4
  9. data/lib/rubocop/config_loader_resolver.rb +5 -1
  10. data/lib/rubocop/config_obsoletion.rb +21 -3
  11. data/lib/rubocop/config_regeneration.rb +1 -1
  12. data/lib/rubocop/config_validator.rb +8 -1
  13. data/lib/rubocop/cop/autocorrect_logic.rb +21 -6
  14. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  15. data/lib/rubocop/cop/generator.rb +2 -9
  16. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  17. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  18. data/lib/rubocop/cop/layout/class_structure.rb +15 -3
  19. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +80 -10
  20. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +6 -1
  21. data/lib/rubocop/cop/layout/end_of_line.rb +5 -5
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -2
  23. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  24. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -1
  25. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +4 -1
  26. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  27. data/lib/rubocop/cop/lint/missing_super.rb +7 -4
  28. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +1 -1
  29. data/lib/rubocop/cop/lint/to_enum_arguments.rb +6 -15
  30. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +85 -0
  31. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +20 -6
  32. data/lib/rubocop/cop/metrics/abc_size.rb +25 -1
  33. data/lib/rubocop/cop/metrics/block_length.rb +13 -7
  34. data/lib/rubocop/cop/metrics/method_length.rb +7 -2
  35. data/lib/rubocop/cop/metrics/parameter_lists.rb +64 -1
  36. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +20 -10
  37. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +146 -0
  38. data/lib/rubocop/cop/metrics/utils/repeated_csend_discount.rb +6 -1
  39. data/lib/rubocop/cop/mixin/configurable_numbering.rb +4 -3
  40. data/lib/rubocop/cop/mixin/enforce_superclass.rb +9 -1
  41. data/lib/rubocop/cop/mixin/ignored_methods.rb +36 -3
  42. data/lib/rubocop/cop/mixin/method_complexity.rb +6 -0
  43. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  44. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -3
  45. data/lib/rubocop/cop/style/and_or.rb +10 -0
  46. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -3
  47. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +8 -1
  48. data/lib/rubocop/cop/style/documentation.rb +12 -1
  49. data/lib/rubocop/cop/style/format_string.rb +8 -3
  50. data/lib/rubocop/cop/style/if_with_semicolon.rb +39 -4
  51. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  52. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -2
  53. data/lib/rubocop/cop/style/negated_if_else_condition.rb +3 -1
  54. data/lib/rubocop/cop/style/numeric_literals.rb +14 -11
  55. data/lib/rubocop/cop/style/redundant_argument.rb +75 -0
  56. data/lib/rubocop/cop/style/redundant_condition.rb +2 -1
  57. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  58. data/lib/rubocop/cop/style/sole_nested_conditional.rb +49 -3
  59. data/lib/rubocop/cop/style/symbol_proc.rb +5 -3
  60. data/lib/rubocop/cop/util.rb +1 -1
  61. data/lib/rubocop/cop/variable_force/branch.rb +1 -1
  62. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  63. data/lib/rubocop/core_ext/hash.rb +20 -0
  64. data/lib/rubocop/ext/regexp_node.rb +5 -10
  65. data/lib/rubocop/ext/regexp_parser.rb +2 -9
  66. data/lib/rubocop/formatter/disabled_config_formatter.rb +21 -6
  67. data/lib/rubocop/options.rb +5 -0
  68. data/lib/rubocop/rake_task.rb +2 -2
  69. data/lib/rubocop/runner.rb +1 -1
  70. data/lib/rubocop/target_finder.rb +1 -1
  71. data/lib/rubocop/target_ruby.rb +12 -4
  72. data/lib/rubocop/version.rb +1 -1
  73. metadata +12 -10
  74. data/bin/console +0 -10
  75. data/bin/rubocop-profile +0 -32
  76. data/bin/setup +0 -7
@@ -92,7 +92,7 @@ module RuboCop
92
92
  keys_appearing_in_both.each do |key|
93
93
  if opts[:unset_nil] && derived_hash[key].nil?
94
94
  result.delete(key)
95
- elsif base_hash[key].is_a?(Hash)
95
+ elsif merge_hashes?(base_hash, derived_hash, key)
96
96
  result[key] = merge(base_hash[key], derived_hash[key], **opts)
97
97
  elsif should_union?(base_hash, key, opts[:inherit_mode])
98
98
  result[key] = base_hash[key] | derived_hash[key]
@@ -164,6 +164,10 @@ module RuboCop
164
164
  inherit_mode['merge'].include?(key)
165
165
  end
166
166
 
167
+ def merge_hashes?(base_hash, derived_hash, key)
168
+ base_hash[key].is_a?(Hash) && derived_hash[key].is_a?(Hash)
169
+ end
170
+
167
171
  def base_configs(path, inherit_from, file)
168
172
  configs = Array(inherit_from).compact.map do |f|
169
173
  ConfigLoader.load_file(inherited_file(path, f, file))
@@ -104,6 +104,7 @@ module RuboCop
104
104
  OBSOLETE_COPS = Hash[*(RENAMED_COPS + MOVED_COPS + REMOVED_COPS +
105
105
  REMOVED_COPS_WITH_REASON + SPLIT_COPS).flatten]
106
106
 
107
+ # Parameters can be deprecated but not disabled by setting `severity: :warning`
107
108
  OBSOLETE_PARAMETERS = [
108
109
  {
109
110
  cops: %w[Layout/SpaceAroundOperators Style/SpaceAroundOperators],
@@ -201,6 +202,12 @@ module RuboCop
201
202
  parameters: 'NameWhitelist',
202
203
  alternative: '`NameWhitelist` has been renamed to ' \
203
204
  '`AllowedMethods`.'
205
+ },
206
+ {
207
+ cops: %w[Metrics/BlockLength Metrics/MethodLength],
208
+ parameters: 'ExcludedMethods',
209
+ alternative: '`ExcludedMethods` has been renamed to `IgnoredMethods`.',
210
+ severity: :warning
204
211
  }
205
212
  ].freeze
206
213
 
@@ -214,8 +221,11 @@ module RuboCop
214
221
  }
215
222
  ].freeze
216
223
 
224
+ attr_reader :warnings
225
+
217
226
  def initialize(config)
218
227
  @config = config
228
+ @warnings = []
219
229
  end
220
230
 
221
231
  def reject_obsolete_cops_and_parameters
@@ -256,9 +266,17 @@ module RuboCop
256
266
  end
257
267
 
258
268
  def obsolete_parameters
259
- OBSOLETE_PARAMETERS.map do |params|
260
- obsolete_parameter_message(params[:cops], params[:parameters],
261
- params[:alternative])
269
+ OBSOLETE_PARAMETERS.collect do |params|
270
+ messages = obsolete_parameter_message(params[:cops], params[:parameters],
271
+ params[:alternative])
272
+
273
+ # Warnings are collected separately and not added to the error message
274
+ if messages && params.fetch(:severity, :error) == :warning
275
+ @warnings.concat(messages)
276
+ next
277
+ end
278
+
279
+ messages
262
280
  end
263
281
  end
264
282
 
@@ -16,7 +16,7 @@ module RuboCop
16
16
  match = generation_command.match(COMMAND_REGEX)
17
17
  return DEFAULT_OPTIONS unless match
18
18
 
19
- options = match[1].split(' ')
19
+ options = match[1].split
20
20
  Options.new.parse(options).first
21
21
  end
22
22
 
@@ -42,7 +42,7 @@ module RuboCop
42
42
  ConfigLoader.default_configuration.key?(key)
43
43
  end
44
44
 
45
- @config_obsoletion.reject_obsolete_cops_and_parameters
45
+ check_obsoletions
46
46
 
47
47
  alert_about_unrecognized_cops(invalid_cop_names)
48
48
  check_target_ruby
@@ -68,6 +68,13 @@ module RuboCop
68
68
 
69
69
  attr_reader :target_ruby
70
70
 
71
+ def check_obsoletions
72
+ @config_obsoletion.reject_obsolete_cops_and_parameters
73
+ return unless @config_obsoletion.warnings.any?
74
+
75
+ warn Rainbow("Warning: #{@config_obsoletion.warnings.join("\n")}").yellow
76
+ end
77
+
71
78
  def check_target_ruby
72
79
  return if target_ruby.supported?
73
80
 
@@ -39,16 +39,31 @@ module RuboCop
39
39
  private
40
40
 
41
41
  def disable_offense(range)
42
- eol_comment = " # rubocop:todo #{cop_name}"
43
- needed_line_length = (range.source_line + eol_comment).length
44
- if needed_line_length <= max_line_length
45
- disable_offense_at_end_of_line(range_of_first_line(range),
46
- eol_comment)
42
+ heredoc_range = surrounding_heredoc(range)
43
+ if heredoc_range
44
+ disable_offense_before_and_after(range_by_lines(heredoc_range))
47
45
  else
48
- disable_offense_before_and_after(range_by_lines(range))
46
+ eol_comment = " # rubocop:todo #{cop_name}"
47
+ needed_line_length = (range.source_line + eol_comment).length
48
+ if needed_line_length <= max_line_length
49
+ disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
50
+ else
51
+ disable_offense_before_and_after(range_by_lines(range))
52
+ end
49
53
  end
50
54
  end
51
55
 
56
+ def surrounding_heredoc(offense_range)
57
+ # The empty offense range is an edge case that can be reached from the Lint/Syntax cop.
58
+ return nil if offense_range.empty?
59
+
60
+ heredoc_nodes = processed_source.ast.each_descendant.select do |node|
61
+ node.respond_to?(:heredoc?) && node.heredoc?
62
+ end
63
+ heredoc_nodes.map { |node| node.loc.expression.join(node.loc.heredoc_end) }
64
+ .find { |range| range.contains?(offense_range) }
65
+ end
66
+
52
67
  def range_of_first_line(range)
53
68
  begin_of_first_line = range.begin_pos - range.column
54
69
  end_of_first_line = begin_of_first_line + range.source_line.length
@@ -48,7 +48,7 @@ module RuboCop
48
48
  def autocorrect_multiline_words(node, escape, delimiters)
49
49
  contents = process_multiline_words(node, escape, delimiters)
50
50
  contents << end_content(node.source)
51
- contents.join('')
51
+ contents.join
52
52
  end
53
53
 
54
54
  def autocorrect_words(node, escape, delimiters)
@@ -17,7 +17,6 @@ module RuboCop
17
17
  SOURCE_TEMPLATE = <<-RUBY.gsub(/^ {8}/, '')
18
18
  # frozen_string_literal: true
19
19
 
20
- # TODO: when finished, run `rake generate_cops_documentation` to update the docs
21
20
  module RuboCop
22
21
  module Cop
23
22
  module %<department>s
@@ -134,13 +133,13 @@ module RuboCop
134
133
  end
135
134
 
136
135
  def inject_config(config_file_path: 'config/default.yml',
137
- version_added: bump_minor_version)
136
+ version_added: '<<next>>')
138
137
  injector =
139
138
  ConfigurationInjector.new(configuration_file_path: config_file_path,
140
139
  badge: badge,
141
140
  version_added: version_added)
142
141
 
143
- injector.inject do
142
+ injector.inject do # rubocop:disable Lint/UnexpectedBlockArity
144
143
  output.puts(format(CONFIGURATION_ADDED_MESSAGE,
145
144
  configuration_file_path: config_file_path))
146
145
  end
@@ -213,12 +212,6 @@ module RuboCop
213
212
  .gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2')
214
213
  .downcase
215
214
  end
216
-
217
- def bump_minor_version
218
- versions = RuboCop::Version::STRING.split('.')
219
-
220
- "#{versions[0]}.#{versions[1].succ}"
221
- end
222
215
  end
223
216
  end
224
217
  end
@@ -14,7 +14,7 @@ module RuboCop
14
14
  VersionAdded: '%<version_added>s'
15
15
  YAML
16
16
 
17
- def initialize(configuration_file_path:, badge:, version_added:)
17
+ def initialize(configuration_file_path:, badge:, version_added: '<<next>>')
18
18
  @configuration_file_path = configuration_file_path
19
19
  @badge = badge
20
20
  @version_added = version_added
@@ -38,7 +38,7 @@ module RuboCop
38
38
 
39
39
  def assertions_using_described_class_msg
40
40
  described_class_msg(processed_source.ast).reject do |node|
41
- node.ancestors.any?(&method(:rspec_expectation_on_msg?))
41
+ node.ancestors.any? { |ancestor| rspec_expectation_on_msg?(ancestor) }
42
42
  end
43
43
  end
44
44
 
@@ -147,6 +147,10 @@ module RuboCop
147
147
  MSG = '`%<category>s` is supposed to appear before ' \
148
148
  '`%<previous>s`.'
149
149
 
150
+ def_node_matcher :dynamic_constant?, <<~PATTERN
151
+ (casgn nil? _ (send ...))
152
+ PATTERN
153
+
150
154
  # Validates code style on class declaration.
151
155
  # Add offense when find a node out of expected order.
152
156
  def on_class(class_node)
@@ -168,11 +172,10 @@ module RuboCop
168
172
 
169
173
  # Autocorrect by swapping between two nodes autocorrecting them
170
174
  def autocorrect(corrector, node)
171
- node_classification = classify(node)
172
175
  previous = node.left_siblings.find do |sibling|
173
- classification = classify(sibling)
174
- !ignore?(classification) && node_classification != classification
176
+ !ignore_for_autocorrect?(node, sibling)
175
177
  end
178
+ return unless previous
176
179
 
177
180
  current_range = source_range_with_comment(node)
178
181
  previous_range = source_range_with_comment(previous)
@@ -241,6 +244,15 @@ module RuboCop
241
244
  expected_order.index(classification).nil?
242
245
  end
243
246
 
247
+ def ignore_for_autocorrect?(node, sibling)
248
+ classification = classify(node)
249
+ sibling_class = classify(sibling)
250
+
251
+ ignore?(sibling_class) ||
252
+ classification == sibling_class ||
253
+ dynamic_constant?(node)
254
+ end
255
+
244
256
  def humanize_node(node)
245
257
  if node.def_type?
246
258
  return :initializer if node.method?(:initialize)
@@ -3,17 +3,18 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # This cop checks whether method definitions are
7
- # separated by one empty line.
6
+ # This cop checks whether class/module/method definitions are
7
+ # separated by one or more empty lines.
8
8
  #
9
9
  # `NumberOfEmptyLines` can be an integer (default is 1) or
10
10
  # an array (e.g. [1, 2]) to specify a minimum and maximum
11
11
  # number of empty lines permitted.
12
12
  #
13
13
  # `AllowAdjacentOneLineDefs` configures whether adjacent
14
- # one-line method definitions are considered an offense.
14
+ # one-line definitions are considered an offense.
15
15
  #
16
- # @example
16
+ # @example EmptyLineBetweenMethodDefs: true (default)
17
+ # # checks for empty lines between method definitions.
17
18
  #
18
19
  # # bad
19
20
  # def a
@@ -29,11 +30,57 @@ module RuboCop
29
30
  #
30
31
  # def b
31
32
  # end
33
+ #
34
+ # @example EmptyLineBetweenClassDefs: true (default)
35
+ # # checks for empty lines between class definitions.
36
+ #
37
+ # # bad
38
+ # class A
39
+ # end
40
+ # class B
41
+ # end
42
+ # def b
43
+ # end
44
+ #
45
+ # @example
46
+ #
47
+ # # good
48
+ # class A
49
+ # end
50
+ #
51
+ # class B
52
+ # end
53
+ #
54
+ # def b
55
+ # end
56
+ #
57
+ # @example EmptyLineBetweenModuleDefs: true (default)
58
+ # # checks for empty lines between module definitions.
59
+ #
60
+ # # bad
61
+ # module A
62
+ # end
63
+ # module B
64
+ # end
65
+ # def b
66
+ # end
67
+ #
68
+ # @example
69
+ #
70
+ # # good
71
+ # module A
72
+ # end
73
+ #
74
+ # module B
75
+ # end
76
+ #
77
+ # def b
78
+ # end
32
79
  class EmptyLineBetweenDefs < Base
33
80
  include RangeHelp
34
81
  extend AutoCorrector
35
82
 
36
- MSG = 'Use empty lines between method definitions.'
83
+ MSG = 'Use empty lines between %<type>s definitions.'
37
84
 
38
85
  def self.autocorrect_incompatible_with
39
86
  [Layout::EmptyLines]
@@ -47,7 +94,7 @@ module RuboCop
47
94
  def on_begin(node)
48
95
  node.children.each_cons(2) do |prev, n|
49
96
  nodes = [prev, n]
50
- check_defs(nodes) if nodes.all?(&method(:def_node?))
97
+ check_defs(nodes) if nodes.all? { |def_candidate| candidate?(def_candidate) }
51
98
  end
52
99
  end
53
100
 
@@ -57,8 +104,9 @@ module RuboCop
57
104
  return if nodes.all?(&:single_line?) &&
58
105
  cop_config['AllowAdjacentOneLineDefs']
59
106
 
60
- location = nodes.last.loc.keyword.join(nodes.last.loc.name)
61
- add_offense(location) do |corrector|
107
+ correction_node = nodes.last
108
+ location = correction_node.loc.keyword.join(correction_node.loc.name)
109
+ add_offense(location, message: message(correction_node)) do |corrector|
62
110
  autocorrect(corrector, *nodes)
63
111
  end
64
112
  end
@@ -83,10 +131,32 @@ module RuboCop
83
131
 
84
132
  private
85
133
 
86
- def def_node?(node)
134
+ def candidate?(node)
87
135
  return unless node
88
136
 
89
- node.def_type? || node.defs_type?
137
+ method_candidate?(node) || class_candidate?(node) || module_candidate?(node)
138
+ end
139
+
140
+ def method_candidate?(node)
141
+ cop_config['EmptyLineBetweenMethodDefs'] && (node.def_type? || node.defs_type?)
142
+ end
143
+
144
+ def class_candidate?(node)
145
+ cop_config['EmptyLineBetweenClassDefs'] && node.class_type?
146
+ end
147
+
148
+ def module_candidate?(node)
149
+ cop_config['EmptyLineBetweenModuleDefs'] && node.module_type?
150
+ end
151
+
152
+ def message(node)
153
+ type = case node.type
154
+ when :def, :defs
155
+ :method
156
+ else
157
+ node.type
158
+ end
159
+ format(MSG, type: type)
90
160
  end
91
161
 
92
162
  def multiple_blank_lines_groups?(first_def_node, second_def_node)
@@ -45,7 +45,8 @@ module RuboCop
45
45
  MSG = 'Empty line detected around arguments.'
46
46
 
47
47
  def on_send(node)
48
- return if node.single_line? || node.arguments.empty?
48
+ return if node.single_line? || node.arguments.empty? ||
49
+ receiver_and_method_call_on_different_lines?(node)
49
50
 
50
51
  extra_lines(node) do |range|
51
52
  add_offense(range) do |corrector|
@@ -57,6 +58,10 @@ module RuboCop
57
58
 
58
59
  private
59
60
 
61
+ def receiver_and_method_call_on_different_lines?(node)
62
+ node.receiver && node.receiver.loc.last_line != node.loc.selector.line
63
+ end
64
+
60
65
  def empty_lines(node)
61
66
  lines = processed_lines(node)
62
67
  lines.select! { |code, _| code.empty? }
@@ -37,14 +37,14 @@ module RuboCop
37
37
  # # good
38
38
  # puts 'Hello' # Return character is CR+LF on all platfoms.
39
39
  #
40
- class EndOfLine < Cop
40
+ class EndOfLine < Base
41
41
  include ConfigurableEnforcedStyle
42
42
  include RangeHelp
43
43
 
44
44
  MSG_DETECTED = 'Carriage return character detected.'
45
45
  MSG_MISSING = 'Carriage return character missing.'
46
46
 
47
- def investigate(processed_source)
47
+ def on_new_investigation
48
48
  last_line = last_line(processed_source)
49
49
 
50
50
  processed_source.raw_source.each_line.with_index do |line, index|
@@ -54,9 +54,9 @@ module RuboCop
54
54
  next unless msg
55
55
  next if unimportant_missing_cr?(index, last_line, line)
56
56
 
57
- range =
58
- source_range(processed_source.buffer, index + 1, 0, line.length)
59
- add_offense(nil, location: range, message: msg)
57
+ range = source_range(processed_source.buffer, index + 1, 0, line.length)
58
+
59
+ add_offense(range, message: msg)
60
60
  # Usually there will be carriage return characters on all or none
61
61
  # of the lines in a file, so we report only one offense.
62
62
  break
@@ -207,8 +207,13 @@ module RuboCop
207
207
  PATTERN
208
208
 
209
209
  def base_range(send_node, arg_node)
210
- range_between(send_node.source_range.begin_pos,
211
- arg_node.source_range.begin_pos)
210
+ parent = send_node.parent
211
+ start_node = if parent && (parent.splat_type? || parent.kwsplat_type?)
212
+ send_node.parent
213
+ else
214
+ send_node
215
+ end
216
+ range_between(start_node.source_range.begin_pos, arg_node.source_range.begin_pos)
212
217
  end
213
218
 
214
219
  # Returns the column of the given range. For single line ranges, this