rubocop 0.93.1 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +36 -16
  3. data/config/default.yml +207 -72
  4. data/exe/rubocop +1 -1
  5. data/lib/rubocop.rb +16 -2
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  7. data/lib/rubocop/cli/command/version.rb +1 -1
  8. data/lib/rubocop/comment_config.rb +1 -1
  9. data/lib/rubocop/config.rb +4 -0
  10. data/lib/rubocop/config_loader.rb +33 -7
  11. data/lib/rubocop/config_loader_resolver.rb +7 -5
  12. data/lib/rubocop/config_validator.rb +7 -6
  13. data/lib/rubocop/cop/badge.rb +9 -24
  14. data/lib/rubocop/cop/base.rb +16 -1
  15. data/lib/rubocop/cop/bundler/duplicated_gem.rb +26 -6
  16. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  17. data/lib/rubocop/cop/commissioner.rb +37 -23
  18. data/lib/rubocop/cop/corrector.rb +3 -1
  19. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  20. data/lib/rubocop/cop/force.rb +1 -1
  21. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -3
  22. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +4 -5
  23. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  24. data/lib/rubocop/cop/generator.rb +2 -9
  25. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  26. data/lib/rubocop/cop/layout/block_alignment.rb +3 -4
  27. data/lib/rubocop/cop/layout/class_structure.rb +22 -3
  28. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  29. data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
  30. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  31. data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
  32. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
  33. data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
  34. data/lib/rubocop/cop/layout/line_length.rb +8 -1
  35. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
  36. data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
  37. data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
  38. data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
  39. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +26 -2
  40. data/lib/rubocop/cop/lint/debugger.rb +17 -28
  41. data/lib/rubocop/cop/lint/duplicate_branch.rb +93 -0
  42. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +2 -12
  43. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
  44. data/lib/rubocop/cop/lint/else_layout.rb +29 -3
  45. data/lib/rubocop/cop/lint/empty_block.rb +82 -0
  46. data/lib/rubocop/cop/lint/empty_class.rb +93 -0
  47. data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
  48. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +38 -6
  49. data/lib/rubocop/cop/lint/loop.rb +4 -4
  50. data/lib/rubocop/cop/lint/nested_percent_literal.rb +14 -0
  51. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
  52. data/lib/rubocop/cop/lint/number_conversion.rb +46 -13
  53. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +27 -8
  54. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +19 -16
  55. data/lib/rubocop/cop/lint/shadowed_exception.rb +4 -5
  56. data/lib/rubocop/cop/lint/to_enum_arguments.rb +86 -0
  57. data/lib/rubocop/cop/lint/to_json.rb +1 -1
  58. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +185 -0
  59. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  60. data/lib/rubocop/cop/lint/useless_method_definition.rb +2 -4
  61. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
  62. data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -1
  63. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  64. data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
  65. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  66. data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -4
  67. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +12 -2
  68. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
  69. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
  70. data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
  71. data/lib/rubocop/cop/naming/variable_number.rb +98 -8
  72. data/lib/rubocop/cop/offense.rb +3 -3
  73. data/lib/rubocop/cop/security/open.rb +12 -10
  74. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  75. data/lib/rubocop/cop/style/and_or.rb +1 -3
  76. data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
  77. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
  78. data/lib/rubocop/cop/style/case_like_if.rb +0 -4
  79. data/lib/rubocop/cop/style/collection_compact.rb +91 -0
  80. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +169 -0
  81. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  82. data/lib/rubocop/cop/style/format_string_token.rb +47 -2
  83. data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
  84. data/lib/rubocop/cop/style/identical_conditional_branches.rb +7 -2
  85. data/lib/rubocop/cop/style/if_inside_else.rb +37 -1
  86. data/lib/rubocop/cop/style/if_unless_modifier.rb +7 -3
  87. data/lib/rubocop/cop/style/infinite_loop.rb +4 -0
  88. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
  89. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -13
  90. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -11
  91. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +7 -11
  92. data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
  93. data/lib/rubocop/cop/style/multiple_comparison.rb +55 -7
  94. data/lib/rubocop/cop/style/negated_if_else_condition.rb +106 -0
  95. data/lib/rubocop/cop/style/nil_lambda.rb +52 -0
  96. data/lib/rubocop/cop/style/raise_args.rb +21 -6
  97. data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
  98. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -1
  99. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  100. data/lib/rubocop/cop/style/redundant_self.rb +3 -0
  101. data/lib/rubocop/cop/style/safe_navigation.rb +16 -4
  102. data/lib/rubocop/cop/style/semicolon.rb +3 -0
  103. data/lib/rubocop/cop/style/static_class.rb +97 -0
  104. data/lib/rubocop/cop/style/string_concatenation.rb +13 -1
  105. data/lib/rubocop/cop/style/swap_values.rb +108 -0
  106. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
  107. data/lib/rubocop/cop/style/while_until_modifier.rb +9 -0
  108. data/lib/rubocop/cop/team.rb +6 -1
  109. data/lib/rubocop/cop/util.rb +5 -1
  110. data/lib/rubocop/ext/regexp_node.rb +17 -9
  111. data/lib/rubocop/ext/regexp_parser.rb +84 -0
  112. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  113. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
  114. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  115. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  116. data/lib/rubocop/magic_comment.rb +2 -2
  117. data/lib/rubocop/options.rb +6 -1
  118. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  119. data/lib/rubocop/target_finder.rb +1 -1
  120. data/lib/rubocop/target_ruby.rb +65 -1
  121. data/lib/rubocop/version.rb +56 -6
  122. metadata +22 -6
@@ -13,5 +13,5 @@ time = Benchmark.realtime do
13
13
  result = cli.run
14
14
  end
15
15
 
16
- puts "Finished in #{time} seconds" if cli.options[:debug]
16
+ puts "Finished in #{time} seconds" if cli.options[:debug] || cli.options[:display_time]
17
17
  exit result
@@ -15,6 +15,7 @@ require 'rubocop-ast'
15
15
 
16
16
  require_relative 'rubocop/ast_aliases'
17
17
  require_relative 'rubocop/ext/regexp_node'
18
+ require_relative 'rubocop/ext/regexp_parser'
18
19
 
19
20
  require_relative 'rubocop/core_ext/string'
20
21
  require_relative 'rubocop/ext/processed_source'
@@ -258,14 +259,18 @@ require_relative 'rubocop/cop/lint/debugger'
258
259
  require_relative 'rubocop/cop/lint/deprecated_class_methods'
259
260
  require_relative 'rubocop/cop/lint/deprecated_open_ssl_constant'
260
261
  require_relative 'rubocop/cop/lint/disjunctive_assignment_in_constructor'
262
+ require_relative 'rubocop/cop/lint/duplicate_branch'
261
263
  require_relative 'rubocop/cop/lint/duplicate_case_condition'
262
264
  require_relative 'rubocop/cop/lint/duplicate_elsif_condition'
263
265
  require_relative 'rubocop/cop/lint/duplicate_hash_key'
264
266
  require_relative 'rubocop/cop/lint/duplicate_methods'
267
+ require_relative 'rubocop/cop/lint/duplicate_regexp_character_class_element'
265
268
  require_relative 'rubocop/cop/lint/duplicate_require'
266
269
  require_relative 'rubocop/cop/lint/duplicate_rescue_exception'
267
270
  require_relative 'rubocop/cop/lint/each_with_object_argument'
268
271
  require_relative 'rubocop/cop/lint/else_layout'
272
+ require_relative 'rubocop/cop/lint/empty_block'
273
+ require_relative 'rubocop/cop/lint/empty_class'
269
274
  require_relative 'rubocop/cop/lint/empty_conditional_body'
270
275
  require_relative 'rubocop/cop/lint/empty_ensure'
271
276
  require_relative 'rubocop/cop/lint/empty_expression'
@@ -295,6 +300,7 @@ require_relative 'rubocop/cop/lint/multiple_comparison'
295
300
  require_relative 'rubocop/cop/lint/nested_method_definition'
296
301
  require_relative 'rubocop/cop/lint/nested_percent_literal'
297
302
  require_relative 'rubocop/cop/lint/next_without_accumulator'
303
+ require_relative 'rubocop/cop/lint/no_return_in_begin_end_blocks'
298
304
  require_relative 'rubocop/cop/lint/non_deterministic_require_order'
299
305
  require_relative 'rubocop/cop/lint/non_local_exit_from_iterator'
300
306
  require_relative 'rubocop/cop/lint/number_conversion'
@@ -330,11 +336,13 @@ require_relative 'rubocop/cop/lint/shadowing_outer_local_variable'
330
336
  require_relative 'rubocop/cop/lint/struct_new_override'
331
337
  require_relative 'rubocop/cop/lint/suppressed_exception'
332
338
  require_relative 'rubocop/cop/lint/syntax'
339
+ require_relative 'rubocop/cop/lint/to_enum_arguments'
333
340
  require_relative 'rubocop/cop/lint/to_json'
334
341
  require_relative 'rubocop/cop/lint/top_level_return_with_argument'
335
342
  require_relative 'rubocop/cop/lint/trailing_comma_in_attribute_declaration'
336
343
  require_relative 'rubocop/cop/lint/underscore_prefixed_variable_name'
337
344
  require_relative 'rubocop/cop/lint/unified_integer'
345
+ require_relative 'rubocop/cop/lint/unmodified_reduce_accumulator'
338
346
  require_relative 'rubocop/cop/lint/unreachable_code'
339
347
  require_relative 'rubocop/cop/lint/unreachable_loop'
340
348
  require_relative 'rubocop/cop/lint/unused_block_argument'
@@ -384,6 +392,7 @@ require_relative 'rubocop/cop/style/access_modifier_declarations'
384
392
  require_relative 'rubocop/cop/style/accessor_grouping'
385
393
  require_relative 'rubocop/cop/style/alias'
386
394
  require_relative 'rubocop/cop/style/and_or'
395
+ require_relative 'rubocop/cop/style/arguments_forwarding'
387
396
  require_relative 'rubocop/cop/style/array_coercion'
388
397
  require_relative 'rubocop/cop/style/array_join'
389
398
  require_relative 'rubocop/cop/style/ascii_comments'
@@ -403,6 +412,7 @@ require_relative 'rubocop/cop/style/class_equality_comparison'
403
412
  require_relative 'rubocop/cop/style/class_methods'
404
413
  require_relative 'rubocop/cop/style/class_methods_definitions'
405
414
  require_relative 'rubocop/cop/style/class_vars'
415
+ require_relative 'rubocop/cop/style/collection_compact'
406
416
  require_relative 'rubocop/cop/style/collection_methods'
407
417
  require_relative 'rubocop/cop/style/colon_method_call'
408
418
  require_relative 'rubocop/cop/style/colon_method_definition'
@@ -419,6 +429,7 @@ require_relative 'rubocop/cop/style/dir'
419
429
  require_relative 'rubocop/cop/style/disable_cops_within_source_code_directive'
420
430
  require_relative 'rubocop/cop/style/documentation_method'
421
431
  require_relative 'rubocop/cop/style/documentation'
432
+ require_relative 'rubocop/cop/style/document_dynamic_eval_definition'
422
433
  require_relative 'rubocop/cop/style/double_cop_disable_directive'
423
434
  require_relative 'rubocop/cop/style/double_negation'
424
435
  require_relative 'rubocop/cop/style/each_for_simple_loop'
@@ -471,8 +482,7 @@ require_relative 'rubocop/cop/style/redundant_fetch_block'
471
482
  require_relative 'rubocop/cop/style/redundant_file_extension_in_require'
472
483
  require_relative 'rubocop/cop/style/redundant_self_assignment'
473
484
  require_relative 'rubocop/cop/style/sole_nested_conditional'
474
- require_relative 'rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses'
475
- require_relative 'rubocop/cop/style/method_call_with_args_parentheses/require_parentheses'
485
+ require_relative 'rubocop/cop/style/static_class'
476
486
  require_relative 'rubocop/cop/style/method_called_on_do_end_block'
477
487
  require_relative 'rubocop/cop/style/method_def_parentheses'
478
488
  require_relative 'rubocop/cop/style/min_max'
@@ -491,6 +501,7 @@ require_relative 'rubocop/cop/style/multiline_when_then'
491
501
  require_relative 'rubocop/cop/style/multiple_comparison'
492
502
  require_relative 'rubocop/cop/style/mutable_constant'
493
503
  require_relative 'rubocop/cop/style/negated_if'
504
+ require_relative 'rubocop/cop/style/negated_if_else_condition'
494
505
  require_relative 'rubocop/cop/style/negated_unless'
495
506
  require_relative 'rubocop/cop/style/negated_while'
496
507
  require_relative 'rubocop/cop/style/nested_modifier'
@@ -498,6 +509,7 @@ require_relative 'rubocop/cop/style/nested_parenthesized_calls'
498
509
  require_relative 'rubocop/cop/style/nested_ternary_operator'
499
510
  require_relative 'rubocop/cop/style/next'
500
511
  require_relative 'rubocop/cop/style/nil_comparison'
512
+ require_relative 'rubocop/cop/style/nil_lambda'
501
513
  require_relative 'rubocop/cop/style/non_nil_check'
502
514
  require_relative 'rubocop/cop/style/not'
503
515
  require_relative 'rubocop/cop/style/numeric_literals'
@@ -556,6 +568,7 @@ require_relative 'rubocop/cop/style/string_literals_in_interpolation'
556
568
  require_relative 'rubocop/cop/style/string_methods'
557
569
  require_relative 'rubocop/cop/style/strip'
558
570
  require_relative 'rubocop/cop/style/struct_inheritance'
571
+ require_relative 'rubocop/cop/style/swap_values'
559
572
  require_relative 'rubocop/cop/style/symbol_array'
560
573
  require_relative 'rubocop/cop/style/symbol_literal'
561
574
  require_relative 'rubocop/cop/style/symbol_proc'
@@ -596,6 +609,7 @@ require_relative 'rubocop/formatter/disabled_config_formatter'
596
609
  require_relative 'rubocop/formatter/emacs_style_formatter'
597
610
  require_relative 'rubocop/formatter/file_list_formatter'
598
611
  require_relative 'rubocop/formatter/fuubar_style_formatter'
612
+ require_relative 'rubocop/formatter/git_hub_actions_formatter'
599
613
  require_relative 'rubocop/formatter/html_formatter'
600
614
  require_relative 'rubocop/formatter/json_formatter'
601
615
  require_relative 'rubocop/formatter/junit_formatter'
@@ -90,7 +90,7 @@ module RuboCop
90
90
  def reset_config_and_auto_gen_file
91
91
  @config_store = ConfigStore.new
92
92
  @config_store.options_config = @options[:config] if @options[:config]
93
- File.open(AUTO_GENERATED_FILE, 'w') {}
93
+ File.open(AUTO_GENERATED_FILE, 'w') {} # create or truncate if exists
94
94
  add_inheritance_from_auto_generated_file(@options[:config])
95
95
  end
96
96
 
@@ -10,7 +10,7 @@ module RuboCop
10
10
 
11
11
  def run
12
12
  puts RuboCop::Version.version(debug: false) if @options[:version]
13
- puts RuboCop::Version.version(debug: true) if @options[:verbose_version]
13
+ puts RuboCop::Version.version(debug: true, env: env) if @options[:verbose_version]
14
14
  end
15
15
  end
16
16
  end
@@ -8,7 +8,7 @@ module RuboCop
8
8
  REDUNDANT_DISABLE = 'Lint/RedundantCopDisableDirective'
9
9
 
10
10
  # @api private
11
- COP_NAME_PATTERN = '([A-Z]\w+/)?(?:[A-Z]\w+)'
11
+ COP_NAME_PATTERN = '([A-Z]\w+/)*(?:[A-Z]\w+)'
12
12
  # @api private
13
13
  COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
14
14
  # @api private
@@ -37,6 +37,10 @@ module RuboCop
37
37
  new(hash, path).check
38
38
  end
39
39
 
40
+ def loaded_features
41
+ @loaded_features ||= ConfigLoader.loaded_features
42
+ end
43
+
40
44
  def check
41
45
  deprecation_check do |deprecation_message|
42
46
  warn("#{loaded_path} - #{deprecation_message}")
@@ -35,12 +35,13 @@ module RuboCop
35
35
  end
36
36
 
37
37
  def load_file(file)
38
- path = File.absolute_path(file.is_a?(RemoteConfig) ? file.file : file)
38
+ path = file_path(file)
39
39
 
40
40
  hash = load_yaml_configuration(path)
41
41
 
42
42
  # Resolve requires first in case they define additional cops
43
- resolver.resolve_requires(path, hash)
43
+ loaded_features = resolver.resolve_requires(path, hash)
44
+ add_loaded_features(loaded_features)
44
45
 
45
46
  add_missing_namespaces(path, hash)
46
47
 
@@ -172,8 +173,24 @@ module RuboCop
172
173
  resolver.merge_with_default(config, config_file, unset_nil: unset_nil)
173
174
  end
174
175
 
176
+ def loaded_features
177
+ @loaded_features.flatten.compact
178
+ end
179
+
175
180
  private
176
181
 
182
+ def file_path(file)
183
+ File.absolute_path(file.is_a?(RemoteConfig) ? file.file : file)
184
+ end
185
+
186
+ def add_loaded_features(loaded_features)
187
+ if instance_variable_defined?(:@loaded_features)
188
+ instance_variable_get(:@loaded_features) << loaded_features
189
+ else
190
+ instance_variable_set(:@loaded_features, [loaded_features])
191
+ end
192
+ end
193
+
177
194
  def find_project_dotfile(target_dir)
178
195
  find_file_upwards(DOTFILE, target_dir, project_root)
179
196
  end
@@ -239,16 +256,25 @@ module RuboCop
239
256
  end
240
257
 
241
258
  def yaml_safe_load(yaml_code, filename)
242
- if defined?(SafeYAML) && SafeYAML.respond_to?(:load)
243
- SafeYAML.load(yaml_code, filename, whitelisted_tags: %w[!ruby/regexp])
244
- # Ruby 2.6+
245
- elsif Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
259
+ yaml_safe_load!(yaml_code, filename)
260
+ rescue ::StandardError
261
+ if defined?(::SafeYAML)
262
+ raise 'SafeYAML is unmaintained, no longer needed and should be removed'
263
+ end
264
+
265
+ raise
266
+ end
267
+
268
+ if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
269
+ def yaml_safe_load!(yaml_code, filename)
246
270
  YAML.safe_load(yaml_code,
247
271
  permitted_classes: [Regexp, Symbol],
248
272
  permitted_symbols: [],
249
273
  aliases: true,
250
274
  filename: filename)
251
- else
275
+ end
276
+ else # Ruby < 2.6
277
+ def yaml_safe_load!(yaml_code, filename)
252
278
  YAML.safe_load(yaml_code, [Regexp, Symbol], [], true, filename)
253
279
  end
254
280
  end
@@ -9,11 +9,13 @@ module RuboCop
9
9
  class ConfigLoaderResolver
10
10
  def resolve_requires(path, hash)
11
11
  config_dir = File.dirname(path)
12
- Array(hash.delete('require')).each do |r|
13
- if r.start_with?('.')
14
- require(File.join(config_dir, r))
15
- else
16
- require(r)
12
+ hash.delete('require').tap do |loaded_features|
13
+ Array(loaded_features).each do |feature|
14
+ if feature.start_with?('.')
15
+ require(File.join(config_dir, feature))
16
+ else
17
+ require(feature)
18
+ end
17
19
  end
18
20
  end
19
21
  end
@@ -18,6 +18,11 @@ module RuboCop
18
18
  # @api private
19
19
  NEW_COPS_VALUES = %w[pending disable enable].freeze
20
20
 
21
+ # @api private
22
+ CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect].to_set.freeze
23
+ CONFIG_CHECK_DEPARTMENTS = %w[pending override_department].freeze
24
+ private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS
25
+
21
26
  def_delegators :@config, :smart_loaded_path, :for_all_cops
22
27
 
23
28
  def initialize(config)
@@ -202,13 +207,9 @@ module RuboCop
202
207
  hash.each do |key, value|
203
208
  check_cop_config_value(value, key) if value.is_a?(Hash)
204
209
 
205
- next unless %w[Enabled
206
- Safe
207
- SafeAutoCorrect
208
- AutoCorrect].include?(key) && value.is_a?(String)
210
+ next unless CONFIG_CHECK_KEYS.include?(key) && value.is_a?(String)
209
211
 
210
- next if key == 'Enabled' &&
211
- %w[pending override_department].include?(value)
212
+ next if key == 'Enabled' && CONFIG_CHECK_DEPARTMENTS.include?(value)
212
213
 
213
214
  raise ValidationError, msg_not_boolean(parent, key, value)
214
215
  end
@@ -10,37 +10,22 @@ module RuboCop
10
10
  # allow for badge references in source files that omit the department for
11
11
  # RuboCop to infer.
12
12
  class Badge
13
- # Error raised when a badge parse fails.
14
- class InvalidBadge < Error
15
- MSG = 'Invalid badge %<badge>p. ' \
16
- 'Expected `Department/CopName` or `CopName`.'
17
-
18
- def initialize(token)
19
- super(format(MSG, badge: token))
20
- end
21
- end
22
-
23
13
  attr_reader :department, :cop_name
24
14
 
25
15
  def self.for(class_name)
26
- new(*class_name.split('::').last(2))
16
+ parts = class_name.split('::')
17
+ name_deep_enough = parts.length >= 4
18
+ new(name_deep_enough ? parts[2..-1] : parts.last(2))
27
19
  end
28
20
 
29
21
  def self.parse(identifier)
30
- parts = identifier.split('/', 2)
31
-
32
- raise InvalidBadge, identifier if parts.size > 2
33
-
34
- if parts.one?
35
- new(nil, *parts)
36
- else
37
- new(*parts)
38
- end
22
+ new(identifier.split('/'))
39
23
  end
40
24
 
41
- def initialize(department, cop_name)
42
- @department = department.to_sym if department
43
- @cop_name = cop_name
25
+ def initialize(class_name_parts)
26
+ department_parts = class_name_parts[0...-1]
27
+ @department = (department_parts.join('/').to_sym unless department_parts.empty?)
28
+ @cop_name = class_name_parts.last
44
29
  end
45
30
 
46
31
  def ==(other)
@@ -66,7 +51,7 @@ module RuboCop
66
51
  end
67
52
 
68
53
  def with_department(department)
69
- self.class.new(department, cop_name)
54
+ self.class.new([department.to_s.split('/'), cop_name].flatten)
70
55
  end
71
56
  end
72
57
  end
@@ -261,6 +261,21 @@ module RuboCop
261
261
  'they are returned as the result of the investigation'
262
262
  end
263
263
 
264
+ ### Reserved for Commissioner
265
+
266
+ # @api private
267
+ def callbacks_needed
268
+ self.class.callbacks_needed
269
+ end
270
+
271
+ # @api private
272
+ def self.callbacks_needed
273
+ @callbacks_needed ||= public_instance_methods.select do |m|
274
+ m.match?(/^on_|^after_/) &&
275
+ !Base.method_defined?(m) # exclude standard "callbacks" like 'on_begin_investigation'
276
+ end
277
+ end
278
+
264
279
  private
265
280
 
266
281
  ### Reserved for Cop::Cop
@@ -291,7 +306,7 @@ module RuboCop
291
306
  end
292
307
 
293
308
  private_class_method def self.restrict_on_send
294
- @restrict_on_send ||= self::RESTRICT_ON_SEND.to_set.freeze
309
+ @restrict_on_send ||= self::RESTRICT_ON_SEND.to_a.freeze
295
310
  end
296
311
 
297
312
  # Called before any investigation
@@ -25,13 +25,23 @@ module RuboCop
25
25
  #
26
26
  # # good
27
27
  # gem 'rubocop', groups: [:development, :test]
28
- class DuplicatedGem < Cop
28
+ #
29
+ # # good - conditional declaration
30
+ # if Dir.exist?(local)
31
+ # gem 'rubocop', path: local
32
+ # elsif ENV['RUBOCOP_VERSION'] == 'master'
33
+ # gem 'rubocop', git: 'https://github.com/rubocop-hq/rubocop.git'
34
+ # else
35
+ # gem 'rubocop', '~> 0.90.0'
36
+ # end
37
+ #
38
+ class DuplicatedGem < Base
29
39
  include RangeHelp
30
40
 
31
41
  MSG = 'Gem `%<gem_name>s` requirements already given on line '\
32
42
  '%<line_of_first_occurrence>d of the Gemfile.'
33
43
 
34
- def investigate(processed_source)
44
+ def on_new_investigation
35
45
  return if processed_source.blank?
36
46
 
37
47
  duplicated_gem_nodes.each do |nodes|
@@ -53,11 +63,21 @@ module RuboCop
53
63
  gem_declarations(processed_source.ast)
54
64
  .group_by(&:first_argument)
55
65
  .values
56
- .select { |nodes| nodes.size > 1 && !condition?(nodes) }
66
+ .select { |nodes| nodes.size > 1 && !conditional_declaration?(nodes) }
57
67
  end
58
68
 
59
- def condition?(nodes)
60
- nodes[0].parent&.if_type? && nodes[0].parent == nodes[1].parent
69
+ def conditional_declaration?(nodes)
70
+ parent = nodes[0].parent
71
+ return false unless parent&.if_type? || parent&.when_type?
72
+
73
+ root_conditional_node = parent.if_type? ? parent : parent.parent
74
+ nodes.all? { |node| within_conditional?(node, root_conditional_node) }
75
+ end
76
+
77
+ def within_conditional?(node, conditional_node)
78
+ conditional_node.branches.any? do |branch|
79
+ branch == node || branch.child_nodes.include?(node)
80
+ end
61
81
  end
62
82
 
63
83
  def register_offense(node, gem_name, line_of_first_occurrence)
@@ -69,7 +89,7 @@ module RuboCop
69
89
  gem_name: gem_name,
70
90
  line_of_first_occurrence: line_of_first_occurrence
71
91
  )
72
- add_offense(node, location: offense_location, message: message)
92
+ add_offense(offense_location, message: message)
73
93
  end
74
94
  end
75
95
  end
@@ -58,7 +58,7 @@ module RuboCop
58
58
  # # Version 2.1 introduces breaking change baz
59
59
  # gem 'bar', '< 2.1'
60
60
  #
61
- class GemComment < Cop
61
+ class GemComment < Base
62
62
  include DefNode
63
63
 
64
64
  MSG = 'Missing gem description comment.'
@@ -7,7 +7,7 @@ module RuboCop
7
7
  class Commissioner
8
8
  include RuboCop::AST::Traversal
9
9
 
10
- RESTRICTED_CALLBACKS = %i[on_send on_csend].freeze
10
+ RESTRICTED_CALLBACKS = %i[on_send on_csend after_send after_csend].freeze
11
11
  private_constant :RESTRICTED_CALLBACKS
12
12
 
13
13
  # How a Commissioner returns the results of the investigation
@@ -45,8 +45,7 @@ module RuboCop
45
45
  @cops = cops
46
46
  @forces = forces
47
47
  @options = options
48
- @callbacks = Hash.new { |h, k| h[k] = cops_callbacks_for(k) }
49
- @restricted_map = {}
48
+ initialize_callbacks
50
49
 
51
50
  reset
52
51
  end
@@ -61,16 +60,18 @@ module RuboCop
61
60
  method_name = :"on_#{node_type}"
62
61
  next unless method_defined?(method_name)
63
62
 
64
- if RESTRICTED_CALLBACKS.include?(method_name)
65
- trigger_restricted = "trigger_restricted_cops(:on_#{node_type}, node)"
66
- end
63
+ # Hacky: Comment-out code as needed
64
+ r = '#' unless RESTRICTED_CALLBACKS.include?(method_name) # has Restricted?
65
+ c = '#' if NO_CHILD_NODES.include?(node_type) # has Children?
67
66
 
68
67
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
69
- def on_#{node_type}(node)
70
- trigger_responding_cops(:on_#{node_type}, node)
71
- #{trigger_restricted}
72
- #{'super(node)' unless NO_CHILD_NODES.include?(node_type)}
73
- end
68
+ def on_#{node_type}(node) # def on_send(node)
69
+ trigger_responding_cops(:on_#{node_type}, node) # trigger_responding_cops(:on_send, node)
70
+ #{r} trigger_restricted_cops(:on_#{node_type}, node) # trigger_restricted_cops(:on_send, node)
71
+ #{c} super(node) # super(node)
72
+ #{c} trigger_responding_cops(:after_#{node_type}, node) # trigger_responding_cops(:after_send, node)
73
+ #{c}#{r} trigger_restricted_cops(:after_#{node_type}, node) # trigger_restricted_cops(:after_send, node)
74
+ end # end
74
75
  RUBY
75
76
  end
76
77
 
@@ -94,9 +95,9 @@ module RuboCop
94
95
  private
95
96
 
96
97
  def trigger_responding_cops(callback, node)
97
- @callbacks[callback].each do |cop|
98
+ @callbacks[callback]&.each do |cop|
98
99
  with_cop_error_handling(cop, node) do
99
- cop.send(callback, node)
100
+ cop.public_send(callback, node)
100
101
  end
101
102
  end
102
103
  end
@@ -105,29 +106,42 @@ module RuboCop
105
106
  @errors = []
106
107
  end
107
108
 
108
- def cops_callbacks_for(callback)
109
- callbacks = @cops.select do |cop|
110
- cop.respond_to?(callback)
111
- end
112
- if RESTRICTED_CALLBACKS.include?(callback)
113
- @restricted_map[callback] = restricted_map(callbacks)
109
+ def initialize_callbacks
110
+ @callbacks = build_callbacks(@cops)
111
+ @restricted_map = restrict_callbacks(@callbacks)
112
+ end
113
+
114
+ def build_callbacks(cops)
115
+ callbacks = {}
116
+ cops.each do |cop|
117
+ cop.callbacks_needed.each do |callback|
118
+ (callbacks[callback] ||= []) << cop
119
+ end
114
120
  end
115
121
  callbacks
116
122
  end
117
123
 
124
+ def restrict_callbacks(callbacks)
125
+ restricted = {}
126
+ RESTRICTED_CALLBACKS.each do |callback|
127
+ restricted[callback] = restricted_map(callbacks[callback])
128
+ end
129
+ restricted
130
+ end
131
+
118
132
  def trigger_restricted_cops(event, node)
119
133
  name = node.method_name
120
- @restricted_map.fetch(event)[name]&.each do |cop|
134
+ @restricted_map[event][name]&.each do |cop|
121
135
  with_cop_error_handling(cop, node) do
122
- cop.send(event, node)
136
+ cop.public_send(event, node)
123
137
  end
124
138
  end
125
139
  end
126
140
 
127
- # Note: mutates `callbacks` in place
141
+ # NOTE: mutates `callbacks` in place
128
142
  def restricted_map(callbacks)
129
143
  map = {}
130
- callbacks.select! do |cop|
144
+ callbacks&.select! do |cop|
131
145
  restrictions = cop.class.send :restrict_on_send
132
146
  restrictions.each do |name|
133
147
  (map[name] ||= []) << cop