rubocop 1.0.0 → 1.4.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 (123) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +36 -16
  3. data/config/default.yml +165 -19
  4. data/exe/rubocop +1 -1
  5. data/lib/rubocop.rb +17 -0
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  7. data/lib/rubocop/cli/command/execute_runner.rb +26 -11
  8. data/lib/rubocop/comment_config.rb +1 -1
  9. data/lib/rubocop/config_loader.rb +14 -5
  10. data/lib/rubocop/config_regeneration.rb +1 -1
  11. data/lib/rubocop/cop/bundler/duplicated_gem.rb +26 -6
  12. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  13. data/lib/rubocop/cop/commissioner.rb +10 -10
  14. data/lib/rubocop/cop/corrector.rb +3 -1
  15. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  16. data/lib/rubocop/cop/force.rb +1 -1
  17. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -3
  18. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +4 -5
  19. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  20. data/lib/rubocop/cop/generator.rb +2 -9
  21. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  22. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  23. data/lib/rubocop/cop/layout/block_alignment.rb +3 -4
  24. data/lib/rubocop/cop/layout/class_structure.rb +15 -3
  25. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  26. data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
  27. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +77 -7
  28. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  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/line_length.rb +8 -1
  33. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
  34. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  35. data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
  36. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  37. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -1
  38. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +26 -2
  39. data/lib/rubocop/cop/lint/debugger.rb +17 -28
  40. data/lib/rubocop/cop/lint/duplicate_branch.rb +93 -0
  41. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +2 -12
  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 +82 -0
  45. data/lib/rubocop/cop/lint/empty_class.rb +93 -0
  46. data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
  47. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +39 -7
  48. data/lib/rubocop/cop/lint/loop.rb +4 -4
  49. data/lib/rubocop/cop/lint/missing_super.rb +7 -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/unmodified_reduce_accumulator.rb +194 -0
  58. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  59. data/lib/rubocop/cop/lint/useless_method_definition.rb +2 -4
  60. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
  61. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  62. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  63. data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
  64. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  65. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  66. data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -4
  67. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -3
  68. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +11 -1
  69. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
  70. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
  71. data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
  72. data/lib/rubocop/cop/naming/variable_number.rb +98 -8
  73. data/lib/rubocop/cop/offense.rb +3 -3
  74. data/lib/rubocop/cop/style/and_or.rb +1 -3
  75. data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
  76. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
  77. data/lib/rubocop/cop/style/case_like_if.rb +0 -4
  78. data/lib/rubocop/cop/style/collection_compact.rb +91 -0
  79. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +169 -0
  80. data/lib/rubocop/cop/style/documentation.rb +12 -1
  81. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  82. data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
  83. data/lib/rubocop/cop/style/identical_conditional_branches.rb +7 -2
  84. data/lib/rubocop/cop/style/if_inside_else.rb +37 -1
  85. data/lib/rubocop/cop/style/if_unless_modifier.rb +7 -3
  86. data/lib/rubocop/cop/style/infinite_loop.rb +4 -0
  87. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
  88. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  89. data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
  90. data/lib/rubocop/cop/style/multiple_comparison.rb +55 -7
  91. data/lib/rubocop/cop/style/negated_if_else_condition.rb +106 -0
  92. data/lib/rubocop/cop/style/nil_lambda.rb +52 -0
  93. data/lib/rubocop/cop/style/raise_args.rb +21 -6
  94. data/lib/rubocop/cop/style/redundant_argument.rb +73 -0
  95. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -1
  96. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  97. data/lib/rubocop/cop/style/semicolon.rb +3 -0
  98. data/lib/rubocop/cop/style/static_class.rb +97 -0
  99. data/lib/rubocop/cop/style/swap_values.rb +108 -0
  100. data/lib/rubocop/cop/style/while_until_modifier.rb +9 -0
  101. data/lib/rubocop/cop/team.rb +6 -1
  102. data/lib/rubocop/cop/util.rb +6 -2
  103. data/lib/rubocop/cop/variable_force/branch.rb +1 -1
  104. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  105. data/lib/rubocop/ext/regexp_node.rb +17 -9
  106. data/lib/rubocop/ext/regexp_parser.rb +84 -0
  107. data/lib/rubocop/formatter/disabled_config_formatter.rb +21 -6
  108. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  109. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
  110. data/lib/rubocop/magic_comment.rb +2 -2
  111. data/lib/rubocop/options.rb +7 -0
  112. data/lib/rubocop/rake_task.rb +2 -2
  113. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  114. data/lib/rubocop/runner.rb +1 -1
  115. data/lib/rubocop/target_finder.rb +1 -1
  116. data/lib/rubocop/target_ruby.rb +65 -1
  117. data/lib/rubocop/version.rb +1 -1
  118. metadata +22 -10
  119. data/assets/logo.png +0 -0
  120. data/assets/output.html.erb +0 -261
  121. data/bin/console +0 -10
  122. data/bin/rubocop-profile +0 -32
  123. data/bin/setup +0 -7
@@ -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,6 +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'
485
+ require_relative 'rubocop/cop/style/static_class'
474
486
  require_relative 'rubocop/cop/style/method_called_on_do_end_block'
475
487
  require_relative 'rubocop/cop/style/method_def_parentheses'
476
488
  require_relative 'rubocop/cop/style/min_max'
@@ -489,6 +501,7 @@ require_relative 'rubocop/cop/style/multiline_when_then'
489
501
  require_relative 'rubocop/cop/style/multiple_comparison'
490
502
  require_relative 'rubocop/cop/style/mutable_constant'
491
503
  require_relative 'rubocop/cop/style/negated_if'
504
+ require_relative 'rubocop/cop/style/negated_if_else_condition'
492
505
  require_relative 'rubocop/cop/style/negated_unless'
493
506
  require_relative 'rubocop/cop/style/negated_while'
494
507
  require_relative 'rubocop/cop/style/nested_modifier'
@@ -496,6 +509,7 @@ require_relative 'rubocop/cop/style/nested_parenthesized_calls'
496
509
  require_relative 'rubocop/cop/style/nested_ternary_operator'
497
510
  require_relative 'rubocop/cop/style/next'
498
511
  require_relative 'rubocop/cop/style/nil_comparison'
512
+ require_relative 'rubocop/cop/style/nil_lambda'
499
513
  require_relative 'rubocop/cop/style/non_nil_check'
500
514
  require_relative 'rubocop/cop/style/not'
501
515
  require_relative 'rubocop/cop/style/numeric_literals'
@@ -515,6 +529,7 @@ require_relative 'rubocop/cop/style/preferred_hash_methods'
515
529
  require_relative 'rubocop/cop/style/proc'
516
530
  require_relative 'rubocop/cop/style/raise_args'
517
531
  require_relative 'rubocop/cop/style/random_with_offset'
532
+ require_relative 'rubocop/cop/style/redundant_argument'
518
533
  require_relative 'rubocop/cop/style/redundant_begin'
519
534
  require_relative 'rubocop/cop/style/redundant_capital_w'
520
535
  require_relative 'rubocop/cop/style/redundant_condition'
@@ -554,6 +569,7 @@ require_relative 'rubocop/cop/style/string_literals_in_interpolation'
554
569
  require_relative 'rubocop/cop/style/string_methods'
555
570
  require_relative 'rubocop/cop/style/strip'
556
571
  require_relative 'rubocop/cop/style/struct_inheritance'
572
+ require_relative 'rubocop/cop/style/swap_values'
557
573
  require_relative 'rubocop/cop/style/symbol_array'
558
574
  require_relative 'rubocop/cop/style/symbol_literal'
559
575
  require_relative 'rubocop/cop/style/symbol_proc'
@@ -594,6 +610,7 @@ require_relative 'rubocop/formatter/disabled_config_formatter'
594
610
  require_relative 'rubocop/formatter/emacs_style_formatter'
595
611
  require_relative 'rubocop/formatter/file_list_formatter'
596
612
  require_relative 'rubocop/formatter/fuubar_style_formatter'
613
+ require_relative 'rubocop/formatter/git_hub_actions_formatter'
597
614
  require_relative 'rubocop/formatter/html_formatter'
598
615
  require_relative 'rubocop/formatter/json_formatter'
599
616
  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
 
@@ -22,12 +22,13 @@ module RuboCop
22
22
  def execute_runner(paths)
23
23
  runner = Runner.new(@options, @config_store)
24
24
 
25
- all_passed = runner.run(paths)
26
- display_warning_summary(runner.warnings)
27
- display_error_summary(runner.errors)
28
- maybe_print_corrected_source
25
+ all_pass_or_excluded = with_redirect do
26
+ all_passed = runner.run(paths)
27
+ display_summary(runner)
28
+ all_passed || @options[:auto_gen_config]
29
+ end
29
30
 
30
- all_pass_or_excluded = all_passed || @options[:auto_gen_config]
31
+ maybe_print_corrected_source
31
32
 
32
33
  if runner.aborting?
33
34
  STATUS_INTERRUPTED
@@ -38,6 +39,25 @@ module RuboCop
38
39
  end
39
40
  end
40
41
 
42
+ def with_redirect
43
+ if @options[:stderr]
44
+ orig_stdout = $stdout.dup
45
+ $stdout.reopen($stderr)
46
+
47
+ result = yield
48
+
49
+ $stdout.reopen(orig_stdout)
50
+ result
51
+ else
52
+ yield
53
+ end
54
+ end
55
+
56
+ def display_summary(runner)
57
+ display_warning_summary(runner.warnings)
58
+ display_error_summary(runner.errors)
59
+ end
60
+
41
61
  def display_warning_summary(warnings)
42
62
  return if warnings.empty?
43
63
 
@@ -69,14 +89,9 @@ module RuboCop
69
89
  # See: https://github.com/rubocop-hq/rubocop/issues/8673
70
90
  return if INTEGRATION_FORMATTERS.include?(@options[:format])
71
91
 
72
- # If we are asked to autocorrect source code read from stdin, the only
73
- # reasonable place to write it is to stdout
74
- # Unfortunately, we also write other information to stdout
75
- # So a delimiter is needed for tools to easily identify where the
76
- # autocorrected source begins
77
92
  return unless @options[:stdin] && @options[:auto_correct]
78
93
 
79
- puts '=' * 20
94
+ (@options[:stderr] ? $stderr : $stdout).puts '=' * 20
80
95
  print @options[:stdin]
81
96
  end
82
97
  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
@@ -256,16 +256,25 @@ module RuboCop
256
256
  end
257
257
 
258
258
  def yaml_safe_load(yaml_code, filename)
259
- if defined?(SafeYAML) && SafeYAML.respond_to?(:load)
260
- SafeYAML.load(yaml_code, filename, whitelisted_tags: %w[!ruby/regexp])
261
- # Ruby 2.6+
262
- 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)
263
270
  YAML.safe_load(yaml_code,
264
271
  permitted_classes: [Regexp, Symbol],
265
272
  permitted_symbols: [],
266
273
  aliases: true,
267
274
  filename: filename)
268
- else
275
+ end
276
+ else # Ruby < 2.6
277
+ def yaml_safe_load!(yaml_code, filename)
269
278
  YAML.safe_load(yaml_code, [Regexp, Symbol], [], true, filename)
270
279
  end
271
280
  end
@@ -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
 
@@ -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.'
@@ -65,13 +65,13 @@ module RuboCop
65
65
  c = '#' if NO_CHILD_NODES.include?(node_type) # has Children?
66
66
 
67
67
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
68
- def on_#{node_type}(node)
69
- trigger_responding_cops(:on_#{node_type}, node)
70
- #{r} trigger_restricted_cops(:on_#{node_type}, node)
71
- #{c} super(node)
72
- #{c} trigger_responding_cops(:after_#{node_type}, node)
73
- #{c}#{r} trigger_restricted_cops(:after_#{node_type}, node)
74
- 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
75
75
  RUBY
76
76
  end
77
77
 
@@ -97,7 +97,7 @@ module RuboCop
97
97
  def trigger_responding_cops(callback, node)
98
98
  @callbacks[callback]&.each do |cop|
99
99
  with_cop_error_handling(cop, node) do
100
- cop.send(callback, node)
100
+ cop.public_send(callback, node)
101
101
  end
102
102
  end
103
103
  end
@@ -133,12 +133,12 @@ module RuboCop
133
133
  name = node.method_name
134
134
  @restricted_map[event][name]&.each do |cop|
135
135
  with_cop_error_handling(cop, node) do
136
- cop.send(event, node)
136
+ cop.public_send(event, node)
137
137
  end
138
138
  end
139
139
  end
140
140
 
141
- # Note: mutates `callbacks` in place
141
+ # NOTE: mutates `callbacks` in place
142
142
  def restricted_map(callbacks)
143
143
  map = {}
144
144
  callbacks&.select! do |cop|
@@ -9,6 +9,8 @@ module RuboCop
9
9
  # The nodes modified by the corrections should be part of the
10
10
  # AST of the source_buffer.
11
11
  class Corrector < ::Parser::Source::TreeRewriter
12
+ NOOP_CONSUMER = ->(diagnostic) {} # noop
13
+
12
14
  # @param source [Parser::Source::Buffer, or anything
13
15
  # leading to one via `(processed_source.)buffer`]
14
16
  #
@@ -23,7 +25,7 @@ module RuboCop
23
25
  )
24
26
 
25
27
  # Don't print warnings to stderr if corrections conflict with each other
26
- diagnostics.consumer = ->(diagnostic) {}
28
+ diagnostics.consumer = NOOP_CONSUMER
27
29
  end
28
30
 
29
31
  alias rewrite process # Legacy
@@ -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)
@@ -31,7 +31,7 @@ module RuboCop
31
31
  cops.each do |cop|
32
32
  next unless cop.respond_to?(method_name)
33
33
 
34
- cop.send(method_name, *args)
34
+ cop.public_send(method_name, *args)
35
35
  end
36
36
  end
37
37
 
@@ -34,7 +34,7 @@ module RuboCop
34
34
  # spec.add_runtime_dependency('parallel', '~> 1.10')
35
35
  # spec.add_runtime_dependency('parser', '>= 2.3.3.1', '< 3.0')
36
36
  # end
37
- class DuplicatedAssignment < Cop
37
+ class DuplicatedAssignment < Base
38
38
  include RangeHelp
39
39
 
40
40
  MSG = '`%<assignment>s` method calls already given on line '\
@@ -54,7 +54,7 @@ module RuboCop
54
54
  (lvar #match_block_variable_name?) #assignment_method? ...)
55
55
  PATTERN
56
56
 
57
- def investigate(processed_source)
57
+ def on_new_investigation
58
58
  return if processed_source.blank?
59
59
 
60
60
  duplicated_assignment_method_nodes.each do |nodes|
@@ -96,7 +96,7 @@ module RuboCop
96
96
  assignment: assignment,
97
97
  line_of_first_occurrence: line_of_first_occurrence
98
98
  )
99
- add_offense(node, location: offense_location, message: message)
99
+ add_offense(offense_location, message: message)
100
100
  end
101
101
  end
102
102
  end
@@ -45,7 +45,7 @@ module RuboCop
45
45
  # Gem::Specification.new do |spec|
46
46
  # spec.required_ruby_version = '~> 2.5'
47
47
  # end
48
- class RequiredRubyVersion < Cop
48
+ class RequiredRubyVersion < Base
49
49
  include RangeHelp
50
50
 
51
51
  NOT_EQUAL_MSG = '`required_ruby_version` (%<required_ruby_version>s, ' \
@@ -64,7 +64,7 @@ module RuboCop
64
64
  PATTERN
65
65
 
66
66
  # rubocop:disable Metrics/AbcSize
67
- def investigate(processed_source)
67
+ def on_new_investigation
68
68
  version_def = required_ruby_version(processed_source.ast).first
69
69
 
70
70
  if version_def
@@ -72,13 +72,12 @@ module RuboCop
72
72
  return if !ruby_version || ruby_version == target_ruby_version.to_s
73
73
 
74
74
  add_offense(
75
- processed_source.ast,
76
- location: version_def.loc.expression,
75
+ version_def.loc.expression,
77
76
  message: not_equal_message(ruby_version, target_ruby_version)
78
77
  )
79
78
  else
80
79
  range = source_range(processed_source.buffer, 1, 0)
81
- add_offense(nil, location: range, message: MISSING_MSG)
80
+ add_offense(range, message: MISSING_MSG)
82
81
  end
83
82
  end
84
83
  # rubocop:enable Metrics/AbcSize