rubocop 0.86.0 → 0.87.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 (99) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +46 -4
  4. data/lib/rubocop.rb +7 -1
  5. data/lib/rubocop/cli.rb +0 -2
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +40 -5
  7. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  8. data/lib/rubocop/config_loader.rb +22 -62
  9. data/lib/rubocop/config_obsoletion.rb +0 -1
  10. data/lib/rubocop/cop/autocorrect_logic.rb +13 -23
  11. data/lib/rubocop/cop/base.rb +399 -0
  12. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
  13. data/lib/rubocop/cop/commissioner.rb +48 -50
  14. data/lib/rubocop/cop/cop.rb +85 -236
  15. data/lib/rubocop/cop/corrector.rb +38 -115
  16. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  17. data/lib/rubocop/cop/generator.rb +1 -1
  18. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
  19. data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
  20. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -8
  21. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -0
  22. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -2
  23. data/lib/rubocop/cop/layout/multiline_block_layout.rb +0 -1
  24. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +19 -25
  25. data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
  26. data/lib/rubocop/cop/legacy/corrector.rb +29 -0
  27. data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
  28. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  29. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  30. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
  31. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  32. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +27 -23
  33. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
  34. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +8 -0
  35. data/lib/rubocop/cop/lint/syntax.rb +11 -26
  36. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
  37. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
  38. data/lib/rubocop/cop/metrics/block_length.rb +22 -0
  39. data/lib/rubocop/cop/metrics/class_length.rb +25 -2
  40. data/lib/rubocop/cop/metrics/method_length.rb +23 -0
  41. data/lib/rubocop/cop/metrics/module_length.rb +25 -2
  42. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
  43. data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
  44. data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
  45. data/lib/rubocop/cop/mixin/code_length.rb +4 -0
  46. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  47. data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
  48. data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
  49. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
  50. data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -2
  51. data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
  52. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -2
  53. data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
  54. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +2 -2
  55. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  56. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  57. data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
  58. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  59. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  60. data/lib/rubocop/cop/offense.rb +16 -2
  61. data/lib/rubocop/cop/style/accessor_grouping.rb +136 -0
  62. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +121 -0
  63. data/lib/rubocop/cop/style/class_vars.rb +21 -0
  64. data/lib/rubocop/cop/style/date_time.rb +1 -1
  65. data/lib/rubocop/cop/style/dir.rb +2 -2
  66. data/lib/rubocop/cop/style/empty_literal.rb +5 -5
  67. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  68. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
  69. data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
  70. data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
  71. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
  72. data/lib/rubocop/cop/style/proc.rb +1 -1
  73. data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
  74. data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
  75. data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
  76. data/lib/rubocop/cop/style/redundant_fetch_block.rb +26 -7
  77. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  78. data/lib/rubocop/cop/style/redundant_parentheses.rb +7 -1
  79. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -1
  80. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
  81. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  82. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  83. data/lib/rubocop/cop/style/struct_inheritance.rb +2 -2
  84. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  85. data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
  86. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
  87. data/lib/rubocop/cop/team.rb +97 -81
  88. data/lib/rubocop/cop/utils/format_string.rb +1 -2
  89. data/lib/rubocop/name_similarity.rb +1 -3
  90. data/lib/rubocop/options.rb +15 -8
  91. data/lib/rubocop/rake_task.rb +6 -9
  92. data/lib/rubocop/rspec/cop_helper.rb +4 -4
  93. data/lib/rubocop/rspec/expect_offense.rb +10 -16
  94. data/lib/rubocop/rspec/shared_contexts.rb +7 -7
  95. data/lib/rubocop/runner.rb +31 -29
  96. data/lib/rubocop/target_ruby.rb +1 -1
  97. data/lib/rubocop/version.rb +1 -1
  98. metadata +15 -7
  99. data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -63,7 +63,6 @@ module RuboCop
63
63
  'Layout/SpaceAfterControlKeyword' => 'Layout/SpaceAroundKeyword',
64
64
  'Layout/SpaceBeforeModifierKeyword' => 'Layout/SpaceAroundKeyword',
65
65
  'Lint/RescueWithoutErrorClass' => 'Style/RescueStandardError',
66
- 'Rails/DefaultScope' => nil,
67
66
  'Style/SpaceAfterControlKeyword' => 'Layout/SpaceAroundKeyword',
68
67
  'Style/SpaceBeforeModifierKeyword' => 'Layout/SpaceAroundKeyword',
69
68
  'Style/TrailingComma' => 'Style/TrailingCommaInArguments, ' \
@@ -13,11 +13,7 @@ module RuboCop
13
13
  end
14
14
 
15
15
  def correctable?
16
- support_autocorrect? || disable_uncorrectable?
17
- end
18
-
19
- def support_autocorrect?
20
- respond_to?(:autocorrect)
16
+ self.class.support_autocorrect? || disable_uncorrectable?
21
17
  end
22
18
 
23
19
  def disable_uncorrectable?
@@ -40,8 +36,9 @@ module RuboCop
40
36
  true
41
37
  end
42
38
 
43
- def disable_offense(node)
44
- range = node.location.expression
39
+ private
40
+
41
+ def disable_offense(range)
45
42
  eol_comment = " # rubocop:todo #{cop_name}"
46
43
  needed_line_length = (range.source_line + eol_comment).length
47
44
  if needed_line_length <= max_line_length
@@ -52,8 +49,6 @@ module RuboCop
52
49
  end
53
50
  end
54
51
 
55
- private
56
-
57
52
  def range_of_first_line(range)
58
53
  begin_of_first_line = range.begin_pos - range.column
59
54
  end_of_first_line = begin_of_first_line + range.source_line.length
@@ -82,23 +77,18 @@ module RuboCop
82
77
  end
83
78
 
84
79
  def disable_offense_at_end_of_line(range, eol_comment)
85
- ->(corrector) { corrector.insert_after(range, eol_comment) }
80
+ Corrector.new(range).insert_after(range, eol_comment)
86
81
  end
87
82
 
88
83
  def disable_offense_before_and_after(range_by_lines)
89
- lambda do |corrector|
90
- range_with_newline = range_by_lines.resize(range_by_lines.size + 1)
91
- leading_whitespace = range_by_lines.source_line[/^\s*/]
92
-
93
- corrector.insert_before(
94
- range_with_newline,
95
- "#{leading_whitespace}# rubocop:todo #{cop_name}\n"
96
- )
97
- corrector.insert_after(
98
- range_with_newline,
99
- "#{leading_whitespace}# rubocop:enable #{cop_name}\n"
100
- )
101
- end
84
+ range_with_newline = range_by_lines.resize(range_by_lines.size + 1)
85
+ leading_whitespace = range_by_lines.source_line[/^\s*/]
86
+
87
+ Corrector.new(range_by_lines).wrap(
88
+ range_with_newline,
89
+ "#{leading_whitespace}# rubocop:todo #{cop_name}\n",
90
+ "#{leading_whitespace}# rubocop:enable #{cop_name}\n"
91
+ )
102
92
  end
103
93
  end
104
94
  end
@@ -0,0 +1,399 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # A scaffold for concrete cops.
6
+ #
7
+ # The Cop::Base class is meant to be extended.
8
+ #
9
+ # Cops track offenses and can autocorrect them on the fly.
10
+ #
11
+ # A commissioner object is responsible for traversing the AST and invoking
12
+ # the specific callbacks on each cop.
13
+ #
14
+ # First the callback `on_new_investigation` is called;
15
+ # if a cop needs to do its own processing of the AST or depends on
16
+ # something else.
17
+ #
18
+ # Then callbacks like `on_def`, `on_send` (see AST::Traversal) are called
19
+ # with their respective nodes.
20
+ #
21
+ # Finally the callback `on_investigation_end` is called.
22
+ #
23
+ # Within these callbacks, cops are meant to call `add_offense` or
24
+ # `add_global_offense`. Use the `processed_source` method to
25
+ # get the currently processed source being investigated.
26
+ #
27
+ # In case of invalid syntax / unparseable content,
28
+ # the callback `on_other_file` is called instead of all the other
29
+ # `on_...` callbacks.
30
+ #
31
+ # Private methods are not meant for custom cops consumption,
32
+ # nor are any instance variables.
33
+ #
34
+ class Base # rubocop:disable Metrics/ClassLength
35
+ extend RuboCop::AST::Sexp
36
+ extend NodePattern::Macros
37
+ include RuboCop::AST::Sexp
38
+ include Util
39
+ include IgnoredNode
40
+ include AutocorrectLogic
41
+
42
+ attr_reader :config, :processed_source
43
+
44
+ # Reports of an investigation.
45
+ # Immutable
46
+ # Consider creation API private
47
+ InvestigationReport = Struct.new(:cop, :processed_source, :offenses, :corrector)
48
+
49
+ # List of cops that should not try to autocorrect at the same
50
+ # time as this cop
51
+ #
52
+ # @return [Array<RuboCop::Cop::Cop>]
53
+ #
54
+ # @api public
55
+ def self.autocorrect_incompatible_with
56
+ []
57
+ end
58
+
59
+ def initialize(config = nil, options = nil)
60
+ @config = config || Config.new
61
+ @options = options || { debug: false }
62
+ reset_investigation
63
+ end
64
+
65
+ # Called before all on_... have been called
66
+ # When refining this method, always call `super`
67
+ def on_new_investigation
68
+ # Typically do nothing here
69
+ end
70
+
71
+ # Called after all on_... have been called
72
+ # When refining this method, always call `super`
73
+ def on_investigation_end
74
+ # Typically do nothing here
75
+ end
76
+
77
+ # Called instead of all on_... callbacks for unrecognized files / syntax errors
78
+ # When refining this method, always call `super`
79
+ def on_other_file
80
+ # Typically do nothing here
81
+ end
82
+
83
+ # Override and return the Force class(es) you need to join
84
+ def self.joining_forces; end
85
+
86
+ # Gets called if no message is specified when calling `add_offense` or
87
+ # `add_global_offense`
88
+ # Cops are discouraged to override this; instead pass your message directly
89
+ def message(_range = nil)
90
+ self.class::MSG
91
+ end
92
+
93
+ # Adds an offense that has no particular location.
94
+ # No correction can be applied to global offenses
95
+ def add_global_offense(message = nil, severity: nil)
96
+ severity = find_severity(nil, severity)
97
+ message = find_message(nil, message)
98
+ @current_offenses <<
99
+ Offense.new(severity, Offense::NO_LOCATION, message, name, :unsupported)
100
+ end
101
+
102
+ # Adds an offense on the specified range (or node with an expression)
103
+ # Unless that offense is disabled for this range, a corrector will be yielded
104
+ # to provide the cop the opportunity to autocorrect the offense.
105
+ # If message is not specified, the method `message` will be called.
106
+ def add_offense(node_or_range, message: nil, severity: nil, &block)
107
+ range = range_from_node_or_range(node_or_range)
108
+ return unless @current_offense_locations.add?(range)
109
+
110
+ range_to_pass = callback_argument(range)
111
+
112
+ severity = find_severity(range_to_pass, severity)
113
+ message = find_message(range_to_pass, message)
114
+
115
+ status, corrector = enabled_line?(range.line) ? correct(range, &block) : :disabled
116
+
117
+ @current_offenses << Offense.new(severity, range, message, name, status, corrector)
118
+ end
119
+
120
+ # This method should be overridden when a cop's behavior depends
121
+ # on state that lives outside of these locations:
122
+ #
123
+ # (1) the file under inspection
124
+ # (2) the cop's source code
125
+ # (3) the config (eg a .rubocop.yml file)
126
+ #
127
+ # For example, some cops may want to look at other parts of
128
+ # the codebase being inspected to find violations. A cop may
129
+ # use the presence or absence of file `foo.rb` to determine
130
+ # whether a certain violation exists in `bar.rb`.
131
+ #
132
+ # Overriding this method allows the cop to indicate to RuboCop's
133
+ # ResultCache system when those external dependencies change,
134
+ # ie when the ResultCache should be invalidated.
135
+ def external_dependency_checksum
136
+ nil
137
+ end
138
+
139
+ def self.inherited(subclass)
140
+ Registry.global.enlist(subclass)
141
+ end
142
+
143
+ # Call for abstract Cop classes
144
+ def self.exclude_from_registry
145
+ Registry.global.dismiss(self)
146
+ end
147
+
148
+ # Returns if class supports auto_correct.
149
+ # It is recommended to extend AutoCorrector instead of overriding
150
+ def self.support_autocorrect?
151
+ false
152
+ end
153
+
154
+ ### Naming
155
+
156
+ def self.badge
157
+ @badge ||= Badge.for(name)
158
+ end
159
+
160
+ def self.cop_name
161
+ badge.to_s
162
+ end
163
+
164
+ def self.department
165
+ badge.department
166
+ end
167
+
168
+ def self.lint?
169
+ department == :Lint
170
+ end
171
+
172
+ # Returns true if the cop name or the cop namespace matches any of the
173
+ # given names.
174
+ def self.match?(given_names)
175
+ return false unless given_names
176
+
177
+ given_names.include?(cop_name) ||
178
+ given_names.include?(department.to_s)
179
+ end
180
+
181
+ def cop_name
182
+ @cop_name ||= self.class.cop_name
183
+ end
184
+
185
+ alias name cop_name
186
+
187
+ ### Configuration Helpers
188
+
189
+ def cop_config
190
+ # Use department configuration as basis, but let individual cop
191
+ # configuration override.
192
+ @cop_config ||= @config.for_cop(self.class.department.to_s)
193
+ .merge(@config.for_cop(self))
194
+ end
195
+
196
+ def config_to_allow_offenses
197
+ Formatter::DisabledConfigFormatter
198
+ .config_to_allow_offenses[cop_name] ||= {}
199
+ end
200
+
201
+ def config_to_allow_offenses=(hash)
202
+ Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] =
203
+ hash
204
+ end
205
+
206
+ def target_ruby_version
207
+ @config.target_ruby_version
208
+ end
209
+
210
+ def target_rails_version
211
+ @config.target_rails_version
212
+ end
213
+
214
+ def relevant_file?(file)
215
+ file == RuboCop::AST::ProcessedSource::STRING_SOURCE_NAME ||
216
+ file_name_matches_any?(file, 'Include', true) &&
217
+ !file_name_matches_any?(file, 'Exclude', false)
218
+ end
219
+
220
+ def excluded_file?(file)
221
+ !relevant_file?(file)
222
+ end
223
+
224
+ ### Persistence
225
+
226
+ # Override if your cop should be called repeatedly for multiple investigations
227
+ # Between calls to `on_new_investigation` and `on_investigation_end`,
228
+ # the result of `processed_source` will remain constant.
229
+ # You should invalidate any caches that depend on the current `processed_source`
230
+ # in the `on_new_investigation` callback.
231
+ # If your cop does autocorrections, be aware that your instance may be called
232
+ # multiple times with the same `processed_source.path` but different content.
233
+ def self.support_multiple_source?
234
+ false
235
+ end
236
+
237
+ # @api private
238
+ # Called between investigations
239
+ def ready
240
+ return self if self.class.support_multiple_source?
241
+
242
+ self.class.new(@config, @options)
243
+ end
244
+
245
+ ### Reserved for Cop::Cop
246
+
247
+ # @deprecated Make potential errors with previous API more obvious
248
+ def offenses
249
+ raise 'The offenses are not directly available; ' \
250
+ 'they are returned as the result of the investigation'
251
+ end
252
+
253
+ private
254
+
255
+ ### Reserved for Cop::Cop
256
+
257
+ def callback_argument(range)
258
+ range
259
+ end
260
+
261
+ def apply_correction(corrector)
262
+ @current_corrector&.merge!(corrector) if corrector
263
+ end
264
+
265
+ def correction_strategy
266
+ return :unsupported unless correctable?
267
+ return :uncorrected unless autocorrect?
268
+
269
+ :attempt_correction
270
+ end
271
+
272
+ ### Reserved for Commissioner:
273
+
274
+ # Called before any investigation
275
+ def begin_investigation(processed_source)
276
+ @current_offenses = []
277
+ @current_offense_locations = Set[]
278
+ @currently_disabled_lines = Set[]
279
+ @processed_source = processed_source
280
+ @current_corrector = Corrector.new(@processed_source) if @processed_source.valid_syntax?
281
+ end
282
+
283
+ # Called to complete an investigation
284
+ def complete_investigation
285
+ InvestigationReport.new(self, processed_source, @current_offenses, @current_corrector)
286
+ ensure
287
+ reset_investigation
288
+ end
289
+
290
+ ### Actually private methods
291
+
292
+ def reset_investigation
293
+ @currently_disabled_lines = @current_offenses = @processed_source = @current_corrector = nil
294
+ end
295
+
296
+ # @return [Symbol, Corrector] offense status
297
+ def correct(range)
298
+ status = correction_strategy
299
+
300
+ if block_given?
301
+ corrector = Corrector.new(self)
302
+ yield corrector
303
+ if !corrector.empty? && !self.class.support_autocorrect?
304
+ raise "The Cop #{name} must `extend AutoCorrector` to be able to autocorrect"
305
+ end
306
+ end
307
+
308
+ status = attempt_correction(range, corrector) if status == :attempt_correction
309
+
310
+ [status, corrector]
311
+ end
312
+
313
+ # @return [Symbol] offense status
314
+ def attempt_correction(range, corrector)
315
+ if corrector && !corrector.empty?
316
+ status = :corrected
317
+ elsif disable_uncorrectable?
318
+ corrector = disable_uncorrectable(range)
319
+ status = :corrected_with_todo
320
+ else
321
+ return :uncorrected
322
+ end
323
+
324
+ apply_correction(corrector) if corrector
325
+ status
326
+ end
327
+
328
+ def disable_uncorrectable(range)
329
+ line = range.line
330
+ return unless @currently_disabled_lines.add?(line)
331
+
332
+ disable_offense(range)
333
+ end
334
+
335
+ def range_from_node_or_range(node_or_range)
336
+ if node_or_range.respond_to?(:loc)
337
+ node_or_range.loc.expression
338
+ elsif node_or_range.is_a?(::Parser::Source::Range)
339
+ node_or_range
340
+ else
341
+ extra = ' (call `add_global_offense`)' if node_or_range.nil?
342
+ raise "Expected a Source::Range, got #{node_or_range.inspect}#{extra}"
343
+ end
344
+ end
345
+
346
+ def find_message(range, message)
347
+ annotate(message || message(range))
348
+ end
349
+
350
+ def annotate(message)
351
+ RuboCop::Cop::MessageAnnotator.new(
352
+ config, cop_name, cop_config, @options
353
+ ).annotate(message)
354
+ end
355
+
356
+ def file_name_matches_any?(file, parameter, default_result)
357
+ patterns = cop_config[parameter]
358
+ return default_result unless patterns
359
+
360
+ path = nil
361
+ patterns.any? do |pattern|
362
+ # Try to match the absolute path, as Exclude properties are absolute.
363
+ next true if match_path?(pattern, file)
364
+
365
+ # Try with relative path.
366
+ path ||= config.path_relative_to_config(file)
367
+ match_path?(pattern, path)
368
+ end
369
+ end
370
+
371
+ def enabled_line?(line_number)
372
+ return true if @options[:ignore_disable_comments] || !@processed_source
373
+
374
+ @processed_source.comment_config.cop_enabled_at_line?(self, line_number)
375
+ end
376
+
377
+ def find_severity(_range, severity)
378
+ custom_severity || severity || default_severity
379
+ end
380
+
381
+ def default_severity
382
+ self.class.lint? ? :warning : :convention
383
+ end
384
+
385
+ def custom_severity
386
+ severity = cop_config['Severity']
387
+ return unless severity
388
+
389
+ if Severity::NAMES.include?(severity.to_sym)
390
+ severity.to_sym
391
+ else
392
+ message = "Warning: Invalid severity '#{severity}'. " \
393
+ "Valid severities are #{Severity::NAMES.join(', ')}."
394
+ warn(Rainbow(message).red)
395
+ end
396
+ end
397
+ end
398
+ end
399
+ end