rubocop 0.92.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -7
  3. data/config/default.yml +169 -59
  4. data/exe/rubocop +1 -1
  5. data/lib/rubocop.rb +15 -3
  6. data/lib/rubocop/cached_data.rb +2 -1
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  8. data/lib/rubocop/cli/command/version.rb +1 -1
  9. data/lib/rubocop/comment_config.rb +1 -1
  10. data/lib/rubocop/config.rb +4 -0
  11. data/lib/rubocop/config_loader.rb +19 -2
  12. data/lib/rubocop/config_loader_resolver.rb +7 -5
  13. data/lib/rubocop/config_validator.rb +7 -6
  14. data/lib/rubocop/cop/badge.rb +9 -24
  15. data/lib/rubocop/cop/base.rb +16 -1
  16. data/lib/rubocop/cop/bundler/duplicated_gem.rb +23 -3
  17. data/lib/rubocop/cop/commissioner.rb +36 -22
  18. data/lib/rubocop/cop/corrector.rb +3 -1
  19. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  20. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  21. data/lib/rubocop/cop/force.rb +1 -1
  22. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -10
  23. data/lib/rubocop/cop/layout/class_structure.rb +7 -0
  24. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  25. data/lib/rubocop/cop/layout/dot_position.rb +6 -9
  26. data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
  27. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +7 -7
  28. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -1
  29. data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
  30. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
  31. data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
  32. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
  33. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -11
  34. data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
  35. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +0 -4
  36. data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
  37. data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
  38. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +2 -0
  39. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +18 -1
  40. data/lib/rubocop/cop/lint/boolean_symbol.rb +3 -0
  41. data/lib/rubocop/cop/lint/debugger.rb +2 -3
  42. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
  43. data/lib/rubocop/cop/lint/else_layout.rb +29 -3
  44. data/lib/rubocop/cop/lint/empty_block.rb +59 -0
  45. data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
  46. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +37 -0
  47. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +17 -3
  48. data/lib/rubocop/cop/lint/loop.rb +0 -4
  49. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  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/parentheses_as_grouped_expression.rb +1 -1
  55. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +78 -0
  56. data/lib/rubocop/cop/lint/to_enum_arguments.rb +95 -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_setter_call.rb +6 -1
  61. data/lib/rubocop/cop/metrics/block_length.rb +3 -1
  62. data/lib/rubocop/cop/metrics/class_length.rb +14 -6
  63. data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -1
  64. data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
  65. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  66. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  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 +82 -8
  72. data/lib/rubocop/cop/offense.rb +18 -5
  73. data/lib/rubocop/cop/security/open.rb +12 -10
  74. data/lib/rubocop/cop/style/access_modifier_declarations.rb +6 -2
  75. data/lib/rubocop/cop/style/accessor_grouping.rb +3 -0
  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 +18 -6
  79. data/lib/rubocop/cop/style/class_equality_comparison.rb +64 -0
  80. data/lib/rubocop/cop/style/collection_compact.rb +85 -0
  81. data/lib/rubocop/cop/style/combinable_loops.rb +8 -1
  82. data/lib/rubocop/cop/style/comment_annotation.rb +6 -0
  83. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +67 -0
  84. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  85. data/lib/rubocop/cop/style/explicit_block_argument.rb +6 -2
  86. data/lib/rubocop/cop/style/for.rb +0 -4
  87. data/lib/rubocop/cop/style/format_string_token.rb +48 -3
  88. data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
  89. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
  90. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -13
  91. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -11
  92. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +7 -11
  93. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  94. data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
  95. data/lib/rubocop/cop/style/multiple_comparison.rb +54 -7
  96. data/lib/rubocop/cop/style/negated_if_else_condition.rb +99 -0
  97. data/lib/rubocop/cop/style/nested_ternary_operator.rb +2 -0
  98. data/lib/rubocop/cop/style/raise_args.rb +21 -9
  99. data/lib/rubocop/cop/style/redundant_begin.rb +36 -8
  100. data/lib/rubocop/cop/style/redundant_condition.rb +5 -1
  101. data/lib/rubocop/cop/style/redundant_interpolation.rb +6 -1
  102. data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
  103. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +45 -24
  104. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -15
  105. data/lib/rubocop/cop/style/redundant_self.rb +3 -0
  106. data/lib/rubocop/cop/style/safe_navigation.rb +16 -4
  107. data/lib/rubocop/cop/style/semicolon.rb +3 -0
  108. data/lib/rubocop/cop/style/string_concatenation.rb +14 -2
  109. data/lib/rubocop/cop/style/swap_values.rb +108 -0
  110. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  111. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
  112. data/lib/rubocop/cop/team.rb +6 -1
  113. data/lib/rubocop/cop/util.rb +5 -1
  114. data/lib/rubocop/cop/variable_force/branch.rb +0 -4
  115. data/lib/rubocop/ext/regexp_node.rb +35 -11
  116. data/lib/rubocop/ext/regexp_parser.rb +84 -0
  117. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  118. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
  119. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  120. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  121. data/lib/rubocop/magic_comment.rb +2 -2
  122. data/lib/rubocop/options.rb +6 -1
  123. data/lib/rubocop/result_cache.rb +8 -2
  124. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  125. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  126. data/lib/rubocop/runner.rb +4 -4
  127. data/lib/rubocop/target_finder.rb +23 -25
  128. data/lib/rubocop/version.rb +56 -6
  129. metadata +22 -8
  130. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +0 -43
@@ -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'
@@ -105,7 +106,6 @@ require_relative 'rubocop/cop/mixin/percent_literal'
105
106
  require_relative 'rubocop/cop/mixin/preceding_following_alignment'
106
107
  require_relative 'rubocop/cop/mixin/preferred_delimiters'
107
108
  require_relative 'rubocop/cop/mixin/rational_literal'
108
- require_relative 'rubocop/cop/mixin/regexp_literal_help'
109
109
  require_relative 'rubocop/cop/mixin/rescue_node'
110
110
  require_relative 'rubocop/cop/mixin/safe_assignment'
111
111
  require_relative 'rubocop/cop/mixin/space_after_punctuation'
@@ -263,10 +263,12 @@ require_relative 'rubocop/cop/lint/duplicate_case_condition'
263
263
  require_relative 'rubocop/cop/lint/duplicate_elsif_condition'
264
264
  require_relative 'rubocop/cop/lint/duplicate_hash_key'
265
265
  require_relative 'rubocop/cop/lint/duplicate_methods'
266
+ require_relative 'rubocop/cop/lint/duplicate_regexp_character_class_element'
266
267
  require_relative 'rubocop/cop/lint/duplicate_require'
267
268
  require_relative 'rubocop/cop/lint/duplicate_rescue_exception'
268
269
  require_relative 'rubocop/cop/lint/each_with_object_argument'
269
270
  require_relative 'rubocop/cop/lint/else_layout'
271
+ require_relative 'rubocop/cop/lint/empty_block'
270
272
  require_relative 'rubocop/cop/lint/empty_conditional_body'
271
273
  require_relative 'rubocop/cop/lint/empty_ensure'
272
274
  require_relative 'rubocop/cop/lint/empty_expression'
@@ -279,6 +281,7 @@ require_relative 'rubocop/cop/lint/flip_flop'
279
281
  require_relative 'rubocop/cop/lint/float_comparison'
280
282
  require_relative 'rubocop/cop/lint/float_out_of_range'
281
283
  require_relative 'rubocop/cop/lint/format_parameter_mismatch'
284
+ require_relative 'rubocop/cop/lint/hash_compare_by_identity'
282
285
  require_relative 'rubocop/cop/lint/heredoc_method_call_position'
283
286
  require_relative 'rubocop/cop/lint/identity_comparison'
284
287
  require_relative 'rubocop/cop/lint/implicit_string_concatenation'
@@ -295,6 +298,7 @@ require_relative 'rubocop/cop/lint/multiple_comparison'
295
298
  require_relative 'rubocop/cop/lint/nested_method_definition'
296
299
  require_relative 'rubocop/cop/lint/nested_percent_literal'
297
300
  require_relative 'rubocop/cop/lint/next_without_accumulator'
301
+ require_relative 'rubocop/cop/lint/no_return_in_begin_end_blocks'
298
302
  require_relative 'rubocop/cop/lint/non_deterministic_require_order'
299
303
  require_relative 'rubocop/cop/lint/non_local_exit_from_iterator'
300
304
  require_relative 'rubocop/cop/lint/number_conversion'
@@ -308,6 +312,7 @@ require_relative 'rubocop/cop/lint/rand_one'
308
312
  require_relative 'rubocop/cop/lint/redundant_cop_disable_directive'
309
313
  require_relative 'rubocop/cop/lint/redundant_cop_enable_directive'
310
314
  require_relative 'rubocop/cop/lint/redundant_require_statement'
315
+ require_relative 'rubocop/cop/lint/redundant_safe_navigation'
311
316
  require_relative 'rubocop/cop/lint/redundant_splat_expansion'
312
317
  require_relative 'rubocop/cop/lint/redundant_string_coercion'
313
318
  require_relative 'rubocop/cop/lint/redundant_with_index'
@@ -329,11 +334,13 @@ require_relative 'rubocop/cop/lint/shadowing_outer_local_variable'
329
334
  require_relative 'rubocop/cop/lint/struct_new_override'
330
335
  require_relative 'rubocop/cop/lint/suppressed_exception'
331
336
  require_relative 'rubocop/cop/lint/syntax'
337
+ require_relative 'rubocop/cop/lint/to_enum_arguments'
332
338
  require_relative 'rubocop/cop/lint/to_json'
333
339
  require_relative 'rubocop/cop/lint/top_level_return_with_argument'
334
340
  require_relative 'rubocop/cop/lint/trailing_comma_in_attribute_declaration'
335
341
  require_relative 'rubocop/cop/lint/underscore_prefixed_variable_name'
336
342
  require_relative 'rubocop/cop/lint/unified_integer'
343
+ require_relative 'rubocop/cop/lint/unmodified_reduce_accumulator'
337
344
  require_relative 'rubocop/cop/lint/unreachable_code'
338
345
  require_relative 'rubocop/cop/lint/unreachable_loop'
339
346
  require_relative 'rubocop/cop/lint/unused_block_argument'
@@ -383,6 +390,7 @@ require_relative 'rubocop/cop/style/access_modifier_declarations'
383
390
  require_relative 'rubocop/cop/style/accessor_grouping'
384
391
  require_relative 'rubocop/cop/style/alias'
385
392
  require_relative 'rubocop/cop/style/and_or'
393
+ require_relative 'rubocop/cop/style/arguments_forwarding'
386
394
  require_relative 'rubocop/cop/style/array_coercion'
387
395
  require_relative 'rubocop/cop/style/array_join'
388
396
  require_relative 'rubocop/cop/style/ascii_comments'
@@ -398,9 +406,11 @@ require_relative 'rubocop/cop/style/case_like_if'
398
406
  require_relative 'rubocop/cop/style/character_literal'
399
407
  require_relative 'rubocop/cop/style/class_and_module_children'
400
408
  require_relative 'rubocop/cop/style/class_check'
409
+ require_relative 'rubocop/cop/style/class_equality_comparison'
401
410
  require_relative 'rubocop/cop/style/class_methods'
402
411
  require_relative 'rubocop/cop/style/class_methods_definitions'
403
412
  require_relative 'rubocop/cop/style/class_vars'
413
+ require_relative 'rubocop/cop/style/collection_compact'
404
414
  require_relative 'rubocop/cop/style/collection_methods'
405
415
  require_relative 'rubocop/cop/style/colon_method_call'
406
416
  require_relative 'rubocop/cop/style/colon_method_definition'
@@ -417,6 +427,7 @@ require_relative 'rubocop/cop/style/dir'
417
427
  require_relative 'rubocop/cop/style/disable_cops_within_source_code_directive'
418
428
  require_relative 'rubocop/cop/style/documentation_method'
419
429
  require_relative 'rubocop/cop/style/documentation'
430
+ require_relative 'rubocop/cop/style/document_dynamic_eval_definition'
420
431
  require_relative 'rubocop/cop/style/double_cop_disable_directive'
421
432
  require_relative 'rubocop/cop/style/double_negation'
422
433
  require_relative 'rubocop/cop/style/each_for_simple_loop'
@@ -469,8 +480,6 @@ require_relative 'rubocop/cop/style/redundant_fetch_block'
469
480
  require_relative 'rubocop/cop/style/redundant_file_extension_in_require'
470
481
  require_relative 'rubocop/cop/style/redundant_self_assignment'
471
482
  require_relative 'rubocop/cop/style/sole_nested_conditional'
472
- require_relative 'rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses'
473
- require_relative 'rubocop/cop/style/method_call_with_args_parentheses/require_parentheses'
474
483
  require_relative 'rubocop/cop/style/method_called_on_do_end_block'
475
484
  require_relative 'rubocop/cop/style/method_def_parentheses'
476
485
  require_relative 'rubocop/cop/style/min_max'
@@ -489,6 +498,7 @@ require_relative 'rubocop/cop/style/multiline_when_then'
489
498
  require_relative 'rubocop/cop/style/multiple_comparison'
490
499
  require_relative 'rubocop/cop/style/mutable_constant'
491
500
  require_relative 'rubocop/cop/style/negated_if'
501
+ require_relative 'rubocop/cop/style/negated_if_else_condition'
492
502
  require_relative 'rubocop/cop/style/negated_unless'
493
503
  require_relative 'rubocop/cop/style/negated_while'
494
504
  require_relative 'rubocop/cop/style/nested_modifier'
@@ -554,6 +564,7 @@ require_relative 'rubocop/cop/style/string_literals_in_interpolation'
554
564
  require_relative 'rubocop/cop/style/string_methods'
555
565
  require_relative 'rubocop/cop/style/strip'
556
566
  require_relative 'rubocop/cop/style/struct_inheritance'
567
+ require_relative 'rubocop/cop/style/swap_values'
557
568
  require_relative 'rubocop/cop/style/symbol_array'
558
569
  require_relative 'rubocop/cop/style/symbol_literal'
559
570
  require_relative 'rubocop/cop/style/symbol_proc'
@@ -594,6 +605,7 @@ require_relative 'rubocop/formatter/disabled_config_formatter'
594
605
  require_relative 'rubocop/formatter/emacs_style_formatter'
595
606
  require_relative 'rubocop/formatter/file_list_formatter'
596
607
  require_relative 'rubocop/formatter/fuubar_style_formatter'
608
+ require_relative 'rubocop/formatter/git_hub_actions_formatter'
597
609
  require_relative 'rubocop/formatter/html_formatter'
598
610
  require_relative 'rubocop/formatter/json_formatter'
599
611
  require_relative 'rubocop/formatter/junit_formatter'
@@ -21,6 +21,7 @@ module RuboCop
21
21
  private
22
22
 
23
23
  def serialize_offense(offense)
24
+ status = :uncorrected if %i[corrected corrected_with_todo].include?(offense.status)
24
25
  {
25
26
  # Calling #to_s here ensures that the serialization works when using
26
27
  # other json serializers such as Oj. Some of these gems do not call
@@ -32,7 +33,7 @@ module RuboCop
32
33
  },
33
34
  message: message(offense),
34
35
  cop_name: offense.cop_name,
35
- status: :uncorrected
36
+ status: status || offense.status
36
37
  }
37
38
  end
38
39
 
@@ -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
@@ -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,6 +25,16 @@ module RuboCop
25
25
  #
26
26
  # # good
27
27
  # gem 'rubocop', groups: [:development, :test]
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
+ #
28
38
  class DuplicatedGem < Cop
29
39
  include RangeHelp
30
40
 
@@ -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)
@@ -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,21 +106,34 @@ 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
@@ -127,7 +141,7 @@ module RuboCop
127
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