rubocop 1.0.0 → 1.3.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 (96) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +36 -16
  3. data/config/default.yml +141 -14
  4. data/exe/rubocop +1 -1
  5. data/lib/rubocop.rb +16 -0
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  7. data/lib/rubocop/comment_config.rb +1 -1
  8. data/lib/rubocop/config_loader.rb +7 -6
  9. data/lib/rubocop/cop/bundler/duplicated_gem.rb +26 -6
  10. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  11. data/lib/rubocop/cop/commissioner.rb +10 -10
  12. data/lib/rubocop/cop/corrector.rb +3 -1
  13. data/lib/rubocop/cop/force.rb +1 -1
  14. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -3
  15. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +4 -5
  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/layout/block_alignment.rb +3 -4
  19. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  20. data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
  21. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  22. data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
  23. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
  24. data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
  25. data/lib/rubocop/cop/layout/line_length.rb +8 -1
  26. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
  27. data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
  28. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  29. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +23 -2
  30. data/lib/rubocop/cop/lint/debugger.rb +17 -28
  31. data/lib/rubocop/cop/lint/duplicate_branch.rb +93 -0
  32. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +2 -12
  33. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
  34. data/lib/rubocop/cop/lint/else_layout.rb +29 -3
  35. data/lib/rubocop/cop/lint/empty_block.rb +82 -0
  36. data/lib/rubocop/cop/lint/empty_class.rb +93 -0
  37. data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
  38. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +38 -6
  39. data/lib/rubocop/cop/lint/loop.rb +4 -4
  40. data/lib/rubocop/cop/lint/nested_percent_literal.rb +14 -0
  41. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
  42. data/lib/rubocop/cop/lint/number_conversion.rb +46 -13
  43. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +27 -8
  44. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +19 -16
  45. data/lib/rubocop/cop/lint/shadowed_exception.rb +4 -5
  46. data/lib/rubocop/cop/lint/to_enum_arguments.rb +95 -0
  47. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +185 -0
  48. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  49. data/lib/rubocop/cop/lint/useless_method_definition.rb +2 -4
  50. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
  51. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  52. data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
  53. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  54. data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -4
  55. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +11 -1
  56. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
  57. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
  58. data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
  59. data/lib/rubocop/cop/naming/variable_number.rb +98 -8
  60. data/lib/rubocop/cop/offense.rb +3 -3
  61. data/lib/rubocop/cop/style/and_or.rb +1 -3
  62. data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
  63. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
  64. data/lib/rubocop/cop/style/case_like_if.rb +0 -4
  65. data/lib/rubocop/cop/style/collection_compact.rb +91 -0
  66. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +162 -0
  67. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  68. data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
  69. data/lib/rubocop/cop/style/identical_conditional_branches.rb +7 -2
  70. data/lib/rubocop/cop/style/if_inside_else.rb +37 -1
  71. data/lib/rubocop/cop/style/if_unless_modifier.rb +7 -3
  72. data/lib/rubocop/cop/style/infinite_loop.rb +4 -0
  73. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
  74. data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
  75. data/lib/rubocop/cop/style/multiple_comparison.rb +55 -7
  76. data/lib/rubocop/cop/style/negated_if_else_condition.rb +104 -0
  77. data/lib/rubocop/cop/style/nil_lambda.rb +52 -0
  78. data/lib/rubocop/cop/style/raise_args.rb +21 -6
  79. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -1
  80. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  81. data/lib/rubocop/cop/style/semicolon.rb +3 -0
  82. data/lib/rubocop/cop/style/static_class.rb +97 -0
  83. data/lib/rubocop/cop/style/swap_values.rb +108 -0
  84. data/lib/rubocop/cop/style/while_until_modifier.rb +9 -0
  85. data/lib/rubocop/cop/team.rb +6 -1
  86. data/lib/rubocop/cop/util.rb +5 -1
  87. data/lib/rubocop/ext/regexp_node.rb +17 -9
  88. data/lib/rubocop/ext/regexp_parser.rb +84 -0
  89. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  90. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
  91. data/lib/rubocop/magic_comment.rb +2 -2
  92. data/lib/rubocop/options.rb +2 -0
  93. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  94. data/lib/rubocop/target_ruby.rb +57 -1
  95. data/lib/rubocop/version.rb +1 -1
  96. metadata +21 -5
@@ -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'
@@ -554,6 +568,7 @@ require_relative 'rubocop/cop/style/string_literals_in_interpolation'
554
568
  require_relative 'rubocop/cop/style/string_methods'
555
569
  require_relative 'rubocop/cop/style/strip'
556
570
  require_relative 'rubocop/cop/style/struct_inheritance'
571
+ require_relative 'rubocop/cop/style/swap_values'
557
572
  require_relative 'rubocop/cop/style/symbol_array'
558
573
  require_relative 'rubocop/cop/style/symbol_literal'
559
574
  require_relative 'rubocop/cop/style/symbol_proc'
@@ -594,6 +609,7 @@ require_relative 'rubocop/formatter/disabled_config_formatter'
594
609
  require_relative 'rubocop/formatter/emacs_style_formatter'
595
610
  require_relative 'rubocop/formatter/file_list_formatter'
596
611
  require_relative 'rubocop/formatter/fuubar_style_formatter'
612
+ require_relative 'rubocop/formatter/git_hub_actions_formatter'
597
613
  require_relative 'rubocop/formatter/html_formatter'
598
614
  require_relative 'rubocop/formatter/json_formatter'
599
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
 
@@ -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
@@ -255,17 +255,18 @@ module RuboCop
255
255
  "Configuration file not found: #{absolute_path}"
256
256
  end
257
257
 
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')
258
+ raise 'SafeYAML is unmaintained, no longer needed and should be removed' if defined?(SafeYAML)
259
+
260
+ if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
261
+ def yaml_safe_load(yaml_code, filename)
263
262
  YAML.safe_load(yaml_code,
264
263
  permitted_classes: [Regexp, Symbol],
265
264
  permitted_symbols: [],
266
265
  aliases: true,
267
266
  filename: filename)
268
- else
267
+ end
268
+ else # Ruby < 2.6
269
+ def yaml_safe_load(yaml_code, filename)
269
270
  YAML.safe_load(yaml_code, [Regexp, Symbol], [], true, filename)
270
271
  end
271
272
  end
@@ -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
@@ -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
@@ -25,7 +25,7 @@ module RuboCop
25
25
  # spec.add_runtime_dependency 'gem_a'
26
26
  # end
27
27
  #
28
- class RubyVersionGlobalsUsage < Cop
28
+ class RubyVersionGlobalsUsage < Base
29
29
  MSG = 'Do not use `RUBY_VERSION` in gemspec file.'
30
30
 
31
31
  def_node_matcher :ruby_version?, '(const {cbase nil?} :RUBY_VERSION)'
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # and spec file when given a valid qualified cop name.
9
9
  # @api private
10
10
  class Generator
11
- # Note: RDoc 5.1.0 or lower has the following issue.
11
+ # NOTE: RDoc 5.1.0 or lower has the following issue.
12
12
  # https://github.com/rubocop-hq/rubocop/issues/7043
13
13
  #
14
14
  # The following `String#gsub` can be replaced with
@@ -210,10 +210,9 @@ module RuboCop
210
210
  end
211
211
 
212
212
  def alt_start_msg(start_loc, source_line_column)
213
- if style != :either
214
- ''
215
- elsif start_loc.line == source_line_column[:line] &&
216
- start_loc.column == source_line_column[:column]
213
+ if style != :either ||
214
+ (start_loc.line == source_line_column[:line] &&
215
+ start_loc.column == source_line_column[:column])
217
216
  ''
218
217
  else
219
218
  " or #{format_source_line_column(source_line_column)}"
@@ -46,7 +46,7 @@ module RuboCop
46
46
  alias on_defs on_def
47
47
 
48
48
  def on_send(node)
49
- return if !node.def_modifier? || node.method?(:using)
49
+ return unless node.def_modifier?
50
50
 
51
51
  method_def = node.each_descendant(:def, :defs).first
52
52
  expr = node.source_range
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Layout
6
6
  # This cop checks the alignment of else keywords. Normally they should
7
- # be aligned with an if/unless/while/until/begin/def keyword, but there
7
+ # be aligned with an if/unless/while/until/begin/def/rescue keyword, but there
8
8
  # are special cases when they should follow the same rules as the
9
9
  # alignment of end.
10
10
  #
@@ -93,7 +93,13 @@ module RuboCop
93
93
  case parent.type
94
94
  when :def, :defs then base_for_method_definition(parent)
95
95
  when :kwbegin then parent.loc.begin
96
- when :block then parent.send_node.source_range
96
+ when :block
97
+ assignment_node = assignment_node(parent)
98
+ if same_line?(parent, assignment_node)
99
+ assignment_node.source_range
100
+ else
101
+ parent.send_node.source_range
102
+ end
97
103
  else node.loc.keyword
98
104
  end
99
105
  end
@@ -136,6 +142,13 @@ module RuboCop
136
142
  )
137
143
  add_offense(else_range, location: else_range, message: message)
138
144
  end
145
+
146
+ def assignment_node(node)
147
+ assignment_node = node.ancestors.first
148
+ return unless assignment_node&.assignment?
149
+
150
+ assignment_node
151
+ end
139
152
  end
140
153
  end
141
154
  end
@@ -141,6 +141,7 @@ module RuboCop
141
141
  def previous_line_empty?(send_line)
142
142
  previous_line = previous_line_ignoring_comments(processed_source,
143
143
  send_line)
144
+ return true unless previous_line
144
145
 
145
146
  block_start?(send_line) ||
146
147
  class_def?(send_line) ||