rubocop 1.34.1 → 1.35.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +22 -0
  4. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +32 -2
  5. data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
  6. data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
  7. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  8. data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
  9. data/lib/rubocop/cop/layout/block_end_newline.rb +2 -0
  10. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
  11. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -0
  12. data/lib/rubocop/cop/layout/indentation_width.rb +2 -0
  13. data/lib/rubocop/cop/layout/line_length.rb +4 -1
  14. data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
  15. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  16. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  17. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
  18. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  19. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
  20. data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
  21. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
  22. data/lib/rubocop/cop/lint/redundant_with_index.rb +13 -10
  23. data/lib/rubocop/cop/lint/redundant_with_object.rb +12 -11
  24. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  25. data/lib/rubocop/cop/lint/unreachable_loop.rb +7 -1
  26. data/lib/rubocop/cop/lint/useless_access_modifier.rb +6 -4
  27. data/lib/rubocop/cop/lint/void.rb +2 -0
  28. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +76 -1
  29. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  30. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -4
  31. data/lib/rubocop/cop/mixin/range_help.rb +2 -3
  32. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  33. data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -2
  34. data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
  35. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  36. data/lib/rubocop/cop/style/combinable_loops.rb +3 -1
  37. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  38. data/lib/rubocop/cop/style/each_with_object.rb +39 -8
  39. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  40. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  41. data/lib/rubocop/cop/style/for.rb +2 -0
  42. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -1
  43. data/lib/rubocop/cop/style/hash_syntax.rb +17 -0
  44. data/lib/rubocop/cop/style/inverse_methods.rb +8 -6
  45. data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
  46. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
  47. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -1
  48. data/lib/rubocop/cop/style/next.rb +2 -0
  49. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  50. data/lib/rubocop/cop/style/object_then.rb +2 -0
  51. data/lib/rubocop/cop/style/proc.rb +4 -1
  52. data/lib/rubocop/cop/style/redundant_begin.rb +2 -0
  53. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  54. data/lib/rubocop/cop/style/redundant_self.rb +2 -0
  55. data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
  56. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  57. data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
  58. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  59. data/lib/rubocop/feature_loader.rb +3 -1
  60. data/lib/rubocop/server/cache.rb +4 -2
  61. data/lib/rubocop/version.rb +1 -1
  62. data/lib/rubocop.rb +2 -1
  63. metadata +7 -5
@@ -5,7 +5,22 @@ module RuboCop
5
5
  # This module checks for Ruby 3.1's hash value omission syntax.
6
6
  module HashShorthandSyntax
7
7
  OMIT_HASH_VALUE_MSG = 'Omit the hash value.'
8
- EXPLICIT_HASH_VALUE_MSG = 'Explicit the hash value.'
8
+ EXPLICIT_HASH_VALUE_MSG = 'Include the hash value.'
9
+ DO_NOT_MIX_MSG_PREFIX = 'Do not mix explicit and implicit hash values.'
10
+ DO_NOT_MIX_OMIT_VALUE_MSG = "#{DO_NOT_MIX_MSG_PREFIX} #{OMIT_HASH_VALUE_MSG}"
11
+ DO_NOT_MIX_EXPLICIT_VALUE_MSG = "#{DO_NOT_MIX_MSG_PREFIX} #{EXPLICIT_HASH_VALUE_MSG}"
12
+
13
+ def on_hash_for_mixed_shorthand(hash_node)
14
+ return if ignore_mixed_hash_shorthand_syntax?(hash_node)
15
+
16
+ hash_value_type_breakdown = breakdown_value_types_of_hash(hash_node)
17
+
18
+ if hash_with_mixed_shorthand_syntax?(hash_value_type_breakdown)
19
+ mixed_shorthand_syntax_check(hash_value_type_breakdown)
20
+ else
21
+ no_mixed_shorthand_syntax_check(hash_value_type_breakdown)
22
+ end
23
+ end
9
24
 
10
25
  def on_pair(node)
11
26
  return if ignore_hash_shorthand_syntax?(node)
@@ -36,8 +51,14 @@ module RuboCop
36
51
  end
37
52
  end
38
53
 
54
+ def ignore_mixed_hash_shorthand_syntax?(hash_node)
55
+ target_ruby_version <= 3.0 || enforced_shorthand_syntax != 'consistent' ||
56
+ !hash_node.hash_type?
57
+ end
58
+
39
59
  def ignore_hash_shorthand_syntax?(pair_node)
40
60
  target_ruby_version <= 3.0 || enforced_shorthand_syntax == 'either' ||
61
+ enforced_shorthand_syntax == 'consistent' ||
41
62
  !pair_node.parent.hash_type?
42
63
  end
43
64
 
@@ -81,6 +102,60 @@ module RuboCop
81
102
 
82
103
  ancestor.respond_to?(:parenthesized?) && !ancestor.parenthesized? && !!right_sibling
83
104
  end
105
+
106
+ def breakdown_value_types_of_hash(hash_node)
107
+ hash_node.pairs.group_by do |pair_node|
108
+ if pair_node.value_omission?
109
+ :value_omitted
110
+ elsif require_hash_value?(pair_node.key.source, pair_node)
111
+ :value_needed
112
+ else
113
+ :value_omittable
114
+ end
115
+ end
116
+ end
117
+
118
+ def hash_with_mixed_shorthand_syntax?(hash_value_type_breakdown)
119
+ hash_value_type_breakdown.keys.size > 1
120
+ end
121
+
122
+ def hash_with_values_that_cant_be_omitted?(hash_value_type_breakdown)
123
+ hash_value_type_breakdown[:value_needed]&.any?
124
+ end
125
+
126
+ def each_omitted_value_pair(hash_value_type_breakdown, &block)
127
+ hash_value_type_breakdown[:value_omitted]&.each(&block)
128
+ end
129
+
130
+ def each_omittable_value_pair(hash_value_type_breakdown, &block)
131
+ hash_value_type_breakdown[:value_omittable]&.each(&block)
132
+ end
133
+
134
+ def mixed_shorthand_syntax_check(hash_value_type_breakdown)
135
+ if hash_with_values_that_cant_be_omitted?(hash_value_type_breakdown)
136
+ each_omitted_value_pair(hash_value_type_breakdown) do |pair_node|
137
+ hash_key_source = pair_node.key.source
138
+ replacement = "#{hash_key_source}: #{hash_key_source}"
139
+ register_offense(pair_node, DO_NOT_MIX_EXPLICIT_VALUE_MSG, replacement)
140
+ end
141
+ else
142
+ each_omittable_value_pair(hash_value_type_breakdown) do |pair_node|
143
+ hash_key_source = pair_node.key.source
144
+ replacement = "#{hash_key_source}:"
145
+ register_offense(pair_node, DO_NOT_MIX_OMIT_VALUE_MSG, replacement)
146
+ end
147
+ end
148
+ end
149
+
150
+ def no_mixed_shorthand_syntax_check(hash_value_type_breakdown)
151
+ return if hash_with_values_that_cant_be_omitted?(hash_value_type_breakdown)
152
+
153
+ each_omittable_value_pair(hash_value_type_breakdown) do |pair_node|
154
+ hash_key_source = pair_node.key.source
155
+ replacement = "#{hash_key_source}:"
156
+ register_offense(pair_node, OMIT_HASH_VALUE_MSG, replacement)
157
+ end
158
+ end
84
159
  end
85
160
  end
86
161
  end
@@ -14,7 +14,7 @@ module RuboCop
14
14
  {(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
15
15
  PATTERN
16
16
 
17
- def on_block(node)
17
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
18
18
  on_bad_each_with_object(node) do |*match|
19
19
  handle_possible_offense(node, match, 'each_with_object')
20
20
  end
@@ -29,14 +29,14 @@ module RuboCop
29
29
  end
30
30
  end
31
31
 
32
+ alias on_numblock on_block
33
+
32
34
  private
33
35
 
34
36
  # @!method define_method?(node)
35
37
  def_node_matcher :define_method?, <<~PATTERN
36
- (block
37
- (send nil? :define_method ({sym str} $_))
38
- args
39
- _)
38
+ ({block numblock}
39
+ (send nil? :define_method ({sym str} $_)) _ _)
40
40
  PATTERN
41
41
 
42
42
  def check_complexity(node, method_name)
@@ -70,9 +70,8 @@ module RuboCop
70
70
  Parser::Source::Range.new(buffer, begin_pos, end_pos)
71
71
  end
72
72
 
73
- def range_by_whole_lines(range, include_final_newline: false)
74
- buffer = @processed_source.buffer
75
-
73
+ def range_by_whole_lines(range, include_final_newline: false,
74
+ buffer: @processed_source.buffer)
76
75
  last_line = buffer.source_line(range.last_line)
77
76
  end_offset = last_line.length - range.last_column
78
77
  end_offset += 1 if include_final_newline
@@ -38,7 +38,7 @@ module RuboCop
38
38
  class BlockParameterName < Base
39
39
  include UncommunicativeName
40
40
 
41
- def on_block(node)
41
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
42
42
  return unless node.arguments?
43
43
 
44
44
  check(node, node.arguments)
@@ -73,11 +73,11 @@ module RuboCop
73
73
  {
74
74
  (send _ _
75
75
  (splat (lvar %1))
76
- (block-pass (lvar %2)))
76
+ (block-pass {(lvar %2) nil?}))
77
77
  (send _ _
78
78
  (splat (lvar %1))
79
79
  (hash (kwsplat (lvar %3)))
80
- (block-pass (lvar %2)))
80
+ (block-pass {(lvar %2) nil?}))
81
81
  }
82
82
  PATTERN
83
83
 
@@ -70,7 +70,7 @@ module RuboCop
70
70
 
71
71
  def on_sclass(node)
72
72
  return unless def_self_style?
73
- return unless node.identifier.source == 'self'
73
+ return unless node.identifier.self_type?
74
74
  return unless all_methods_public?(node)
75
75
 
76
76
  add_offense(node, message: MSG_SCLASS) do |corrector|
@@ -80,6 +80,7 @@ module RuboCop
80
80
 
81
81
  def on_defs(node)
82
82
  return if def_self_style?
83
+ return unless node.receiver.self_type?
83
84
 
84
85
  message = format(MSG, preferred: 'class << self')
85
86
  add_offense(node, message: message)
@@ -48,6 +48,8 @@ module RuboCop
48
48
  check_method_node(node.send_node)
49
49
  end
50
50
 
51
+ alias on_numblock on_block
52
+
51
53
  def on_send(node)
52
54
  return unless implicit_block?(node)
53
55
 
@@ -66,6 +66,8 @@ module RuboCop
66
66
  add_offense(node) if same_collection_looping?(node, node.left_sibling)
67
67
  end
68
68
 
69
+ alias on_numblock on_block
70
+
69
71
  def on_for(node)
70
72
  return unless node.parent&.begin_type?
71
73
 
@@ -82,7 +84,7 @@ module RuboCop
82
84
  end
83
85
 
84
86
  def same_collection_looping?(node, sibling)
85
- sibling&.block_type? &&
87
+ (sibling&.block_type? || sibling&.numblock_type?) &&
86
88
  sibling.send_node.method?(node.method_name) &&
87
89
  sibling.receiver == node.receiver &&
88
90
  sibling.send_node.arguments == node.send_node.arguments
@@ -27,7 +27,7 @@ module RuboCop
27
27
 
28
28
  MSG = 'Use `Integer#times` for a simple loop which iterates a fixed number of times.'
29
29
 
30
- def on_block(node)
30
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
31
31
  return unless offending_each_range(node)
32
32
 
33
33
  send_node = node.send_node
@@ -23,13 +23,8 @@ module RuboCop
23
23
  MSG = 'Use `each_with_object` instead of `%<method>s`.'
24
24
  METHODS = %i[inject reduce].freeze
25
25
 
26
- # @!method each_with_object_candidate?(node)
27
- def_node_matcher :each_with_object_candidate?, <<~PATTERN
28
- (block $(send _ {:inject :reduce} _) $_ $_)
29
- PATTERN
30
-
31
26
  def on_block(node)
32
- each_with_object_candidate?(node) do |method, args, body|
27
+ each_with_object_block_candidate?(node) do |method, args, body|
33
28
  _, method_name, method_arg = *method
34
29
  return if simple_method_arg?(method_arg)
35
30
 
@@ -40,14 +35,38 @@ module RuboCop
40
35
 
41
36
  message = format(MSG, method: method_name)
42
37
  add_offense(method.loc.selector, message: message) do |corrector|
43
- autocorrect(corrector, node, return_value)
38
+ autocorrect_block(corrector, node, return_value)
39
+ end
40
+ end
41
+ end
42
+
43
+ def on_numblock(node)
44
+ each_with_object_numblock_candidate?(node) do |method, body|
45
+ _, method_name, method_arg = *method
46
+ return if simple_method_arg?(method_arg)
47
+
48
+ return unless return_value(body)&.source == '_1'
49
+
50
+ message = format(MSG, method: method_name)
51
+ add_offense(method.loc.selector, message: message) do |corrector|
52
+ autocorrect_numblock(corrector, node)
44
53
  end
45
54
  end
46
55
  end
47
56
 
48
57
  private
49
58
 
50
- def autocorrect(corrector, node, return_value)
59
+ # @!method each_with_object_block_candidate?(node)
60
+ def_node_matcher :each_with_object_block_candidate?, <<~PATTERN
61
+ (block $(send _ {:inject :reduce} _) $_ $_)
62
+ PATTERN
63
+
64
+ # @!method each_with_object_numblock_candidate?(node)
65
+ def_node_matcher :each_with_object_numblock_candidate?, <<~PATTERN
66
+ (numblock $(send _ {:inject :reduce} _) 2 $_)
67
+ PATTERN
68
+
69
+ def autocorrect_block(corrector, node, return_value)
51
70
  corrector.replace(node.send_node.loc.selector, 'each_with_object')
52
71
 
53
72
  first_arg, second_arg = *node.arguments
@@ -62,6 +81,18 @@ module RuboCop
62
81
  end
63
82
  end
64
83
 
84
+ def autocorrect_numblock(corrector, node)
85
+ corrector.replace(node.send_node.loc.selector, 'each_with_object')
86
+
87
+ # We don't remove the return value to avoid a clobbering error.
88
+ node.body.each_descendant do |var|
89
+ next unless var.lvar_type?
90
+
91
+ corrector.replace(var, '_2') if var.source == '_1'
92
+ corrector.replace(var, '_1') if var.source == '_2'
93
+ end
94
+ end
95
+
65
96
  def simple_method_arg?(method_arg)
66
97
  method_arg&.basic_literal?
67
98
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
 
29
29
  MSG = 'Omit pipes for the empty block parameters.'
30
30
 
31
- def on_block(node)
31
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
32
32
  send_node = node.send_node
33
33
  check(node) unless send_node.send_type? && send_node.lambda_literal?
34
34
  end
@@ -23,7 +23,7 @@ module RuboCop
23
23
 
24
24
  MSG = 'Omit parentheses for the empty lambda parameters.'
25
25
 
26
- def on_block(node)
26
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
27
27
  send_node = node.send_node
28
28
  return unless send_node.send_type?
29
29
 
@@ -75,6 +75,8 @@ module RuboCop
75
75
  end
76
76
  end
77
77
 
78
+ alias on_numblock on_block
79
+
78
80
  private
79
81
 
80
82
  def suspect_enumerable?(node)
@@ -35,13 +35,15 @@ module RuboCop
35
35
 
36
36
  # @!method kv_each(node)
37
37
  def_node_matcher :kv_each, <<~PATTERN
38
- (block $(send (send _ ${:keys :values}) :each) ...)
38
+ ({block numblock} $(send (send _ ${:keys :values}) :each) ...)
39
39
  PATTERN
40
40
 
41
41
  def on_block(node)
42
42
  register_kv_offense(node)
43
43
  end
44
44
 
45
+ alias on_numblock on_block
46
+
45
47
  private
46
48
 
47
49
  def register_kv_offense(node)
@@ -28,6 +28,7 @@ module RuboCop
28
28
  # * always - forces use of the 3.1 syntax (e.g. {foo:})
29
29
  # * never - forces use of explicit hash literal value
30
30
  # * either - accepts both shorthand and explicit use of hash literal value
31
+ # * consistent - like "always", but will avoid mixing styles in a single hash
31
32
  #
32
33
  # @example EnforcedStyle: ruby19 (default)
33
34
  # # bad
@@ -89,6 +90,20 @@ module RuboCop
89
90
  # # good
90
91
  # {foo:, bar:}
91
92
  #
93
+ # @example EnforcedShorthandSyntax: consistent
94
+ #
95
+ # # bad
96
+ # {foo: , bar: bar}
97
+ #
98
+ # # good
99
+ # {foo:, bar:}
100
+ #
101
+ # # bad
102
+ # {foo: , bar: baz}
103
+ #
104
+ # # good
105
+ # {foo: foo, bar: baz}
106
+ #
92
107
  class HashSyntax < Base
93
108
  include ConfigurableEnforcedStyle
94
109
  include HashShorthandSyntax
@@ -104,6 +119,8 @@ module RuboCop
104
119
 
105
120
  return if pairs.empty?
106
121
 
122
+ on_hash_for_mixed_shorthand(node)
123
+
107
124
  if style == :hash_rockets || force_hash_rockets?(pairs)
108
125
  hash_rockets_check(pairs)
109
126
  elsif style == :ruby19_no_mixed_keys
@@ -59,18 +59,18 @@ module RuboCop
59
59
  def_node_matcher :inverse_candidate?, <<~PATTERN
60
60
  {
61
61
  (send $(send $(...) $_ $...) :!)
62
- (send (block $(send $(...) $_) $...) :!)
62
+ (send ({block numblock} $(send $(...) $_) $...) :!)
63
63
  (send (begin $(send $(...) $_ $...)) :!)
64
64
  }
65
65
  PATTERN
66
66
 
67
67
  # @!method inverse_block?(node)
68
68
  def_node_matcher :inverse_block?, <<~PATTERN
69
- (block $(send (...) $_) ... { $(send ... :!)
70
- $(send (...) {:!= :!~} ...)
71
- (begin ... $(send ... :!))
72
- (begin ... $(send (...) {:!= :!~} ...))
73
- })
69
+ ({block numblock} $(send (...) $_) ... { $(send ... :!)
70
+ $(send (...) {:!= :!~} ...)
71
+ (begin ... $(send ... :!))
72
+ (begin ... $(send (...) {:!= :!~} ...))
73
+ })
74
74
  PATTERN
75
75
 
76
76
  def on_send(node)
@@ -102,6 +102,8 @@ module RuboCop
102
102
  end
103
103
  end
104
104
 
105
+ alias on_numblock on_block
106
+
105
107
  private
106
108
 
107
109
  def correct_inverse_method(corrector, node)