rubocop 1.71.2 → 1.73.2

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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/config/default.yml +55 -13
  4. data/config/internal_affairs.yml +20 -0
  5. data/lib/rubocop/cli/command/suggest_extensions.rb +7 -1
  6. data/lib/rubocop/comment_config.rb +1 -1
  7. data/lib/rubocop/config.rb +4 -0
  8. data/lib/rubocop/config_loader.rb +44 -9
  9. data/lib/rubocop/config_loader_resolver.rb +23 -9
  10. data/lib/rubocop/config_validator.rb +1 -1
  11. data/lib/rubocop/cop/internal_affairs/example_description.rb +7 -3
  12. data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
  13. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +1 -1
  14. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +91 -0
  15. data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
  16. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +7 -1
  17. data/lib/rubocop/cop/internal_affairs.rb +2 -16
  18. data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
  19. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +4 -4
  20. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  21. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  22. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +26 -1
  23. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  25. data/lib/rubocop/cop/layout/line_length.rb +3 -3
  26. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  27. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  28. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
  29. data/lib/rubocop/cop/lint/duplicate_methods.rb +0 -14
  30. data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
  31. data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
  32. data/lib/rubocop/cop/lint/float_comparison.rb +1 -6
  33. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  34. data/lib/rubocop/cop/lint/literal_as_condition.rb +99 -9
  35. data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
  36. data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
  37. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +252 -0
  38. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
  39. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +80 -0
  40. data/lib/rubocop/cop/lint/void.rb +6 -0
  41. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
  42. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  43. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  44. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  45. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +18 -18
  46. data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
  47. data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
  48. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  49. data/lib/rubocop/cop/mixin/range_help.rb +15 -3
  50. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  51. data/lib/rubocop/cop/mixin/trailing_comma.rb +12 -0
  52. data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
  53. data/lib/rubocop/cop/naming/predicate_name.rb +44 -0
  54. data/lib/rubocop/cop/naming/variable_name.rb +64 -6
  55. data/lib/rubocop/cop/style/accessor_grouping.rb +19 -5
  56. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  57. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  58. data/lib/rubocop/cop/style/endless_method.rb +163 -18
  59. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
  60. data/lib/rubocop/cop/style/inverse_methods.rb +8 -5
  61. data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
  62. data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
  63. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  64. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  65. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
  66. data/lib/rubocop/cop/style/redundant_condition.rb +45 -0
  67. data/lib/rubocop/cop/style/redundant_format.rb +250 -0
  68. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  69. data/lib/rubocop/cop/style/redundant_parentheses.rb +18 -4
  70. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  71. data/lib/rubocop/cop/style/single_line_methods.rb +3 -3
  72. data/lib/rubocop/cop/style/sole_nested_conditional.rb +0 -6
  73. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  74. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
  75. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
  76. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  77. data/lib/rubocop/cop/util.rb +1 -1
  78. data/lib/rubocop/cop/utils/format_string.rb +10 -5
  79. data/lib/rubocop/cops_documentation_generator.rb +12 -1
  80. data/lib/rubocop/directive_comment.rb +35 -2
  81. data/lib/rubocop/lsp/runtime.rb +2 -0
  82. data/lib/rubocop/lsp/server.rb +0 -2
  83. data/lib/rubocop/options.rb +26 -11
  84. data/lib/rubocop/path_util.rb +4 -0
  85. data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
  86. data/lib/rubocop/plugin/load_error.rb +26 -0
  87. data/lib/rubocop/plugin/loader.rb +100 -0
  88. data/lib/rubocop/plugin/not_supported_error.rb +29 -0
  89. data/lib/rubocop/plugin.rb +46 -0
  90. data/lib/rubocop/rake_task.rb +4 -1
  91. data/lib/rubocop/rspec/cop_helper.rb +9 -0
  92. data/lib/rubocop/rspec/shared_contexts.rb +15 -0
  93. data/lib/rubocop/rspec/support.rb +1 -0
  94. data/lib/rubocop/server/cache.rb +35 -2
  95. data/lib/rubocop/server/cli.rb +2 -2
  96. data/lib/rubocop/version.rb +17 -2
  97. data/lib/rubocop.rb +5 -1
  98. data/lib/ruby_lsp/rubocop/addon.rb +7 -10
  99. data/lib/ruby_lsp/rubocop/{wraps_built_in_lsp_runtime.rb → runtime_adapter.rb} +5 -8
  100. metadata +36 -10
  101. data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks for useless constant scoping. Private constants must be defined using
7
+ # `private_constant` or `class << self`. Even if `private` access modifier is used,
8
+ # it is public scope despite its appearance.
9
+ #
10
+ # It does not support autocorrection due to behavior change and multiple ways to fix it.
11
+ # Or a public constant may be intended.
12
+ #
13
+ # @example
14
+ #
15
+ # # bad
16
+ # class Foo
17
+ # private
18
+ # PRIVATE_CONST = 42
19
+ # end
20
+ #
21
+ # # good
22
+ # class Foo
23
+ # PRIVATE_CONST = 42
24
+ # private_constant :PRIVATE_CONST
25
+ # end
26
+ #
27
+ # # good
28
+ # class Foo
29
+ # class << self
30
+ # private
31
+ # PRIVATE_CONST = 42
32
+ # end
33
+ # end
34
+ #
35
+ # # good
36
+ # class Foo
37
+ # PUBLIC_CONST = 42 # If private scope is not intended.
38
+ # end
39
+ #
40
+ class UselessConstantScoping < Base
41
+ MSG = 'Useless `private` access modifier for constant scope.'
42
+
43
+ # @!method private_constants(node)
44
+ def_node_matcher :private_constants, <<~PATTERN
45
+ (send nil? :private_constant $...)
46
+ PATTERN
47
+
48
+ def on_casgn(node)
49
+ return if node.each_ancestor(:sclass).any?
50
+ return unless after_private_modifier?(node.left_siblings)
51
+ return if private_constantize?(node.right_siblings, node.name)
52
+
53
+ add_offense(node)
54
+ end
55
+
56
+ private
57
+
58
+ def after_private_modifier?(left_siblings)
59
+ access_modifier_candidates = left_siblings.compact.select do |left_sibling|
60
+ left_sibling.respond_to?(:send_type?) && left_sibling.send_type?
61
+ end
62
+
63
+ access_modifier_candidates.any? do |candidate|
64
+ candidate.command?(:private) && candidate.arguments.none?
65
+ end
66
+ end
67
+
68
+ def private_constantize?(right_siblings, const_value)
69
+ private_constant_arguments = right_siblings.map { |node| private_constants(node) }
70
+
71
+ private_constant_values = private_constant_arguments.flatten.filter_map do |constant|
72
+ constant.value.to_sym if constant.respond_to?(:value)
73
+ end
74
+
75
+ private_constant_values.include?(const_value)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -125,9 +125,14 @@ module RuboCop
125
125
  check_nonmutating(expr)
126
126
  end
127
127
 
128
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
128
129
  def check_void_op(node, &block)
129
130
  node = node.children.first while node.begin_type?
130
131
  return unless node.call_type? && OPERATORS.include?(node.method_name)
132
+ if !UNARY_OPERATORS.include?(node.method_name) && node.loc.dot && node.arguments.none?
133
+ return
134
+ end
135
+
131
136
  return if block && yield(node)
132
137
 
133
138
  add_offense(node.loc.selector,
@@ -135,6 +140,7 @@ module RuboCop
135
140
  autocorrect_void_op(corrector, node)
136
141
  end
137
142
  end
143
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
138
144
 
139
145
  def check_var(node)
140
146
  return unless node.variable? || node.const_type?
@@ -26,6 +26,13 @@ module RuboCop
26
26
  extend NodePattern::Macros
27
27
  include RuboCop::AST::Sexp
28
28
 
29
+ VAR_SETTER_TO_GETTER = {
30
+ lvasgn: :lvar,
31
+ ivasgn: :ivar,
32
+ cvasgn: :cvar,
33
+ gvasgn: :gvar
34
+ }.freeze
35
+
29
36
  # Plug into the calculator
30
37
  def initialize(node, discount_repeated_attributes: false)
31
38
  super(node)
@@ -114,13 +121,6 @@ module RuboCop
114
121
  calls.fetch(value) { yield [calls, value] }
115
122
  end
116
123
 
117
- VAR_SETTER_TO_GETTER = {
118
- lvasgn: :lvar,
119
- ivasgn: :ivar,
120
- cvasgn: :cvar,
121
- gvasgn: :gvar
122
- }.freeze
123
-
124
124
  # @returns `[receiver, method | nil]` for the given setter `node`
125
125
  # or `nil` if it is not a setter.
126
126
  def setter_to_getter(node)
@@ -5,10 +5,10 @@ module RuboCop
5
5
  # This module checks for nodes that should be aligned to the left or right.
6
6
  # This amount is determined by the instance variable @column_delta.
7
7
  module Alignment
8
- private
9
-
10
8
  SPACE = ' '
11
9
 
10
+ private
11
+
12
12
  attr_reader :column_delta
13
13
 
14
14
  def configured_indentation_width
@@ -18,12 +18,12 @@ module RuboCop
18
18
  end
19
19
 
20
20
  # @deprecated Use allowed_line? instead
21
- def ignored_line?
21
+ def ignored_line?(line)
22
22
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
23
23
  `ignored_line?` is deprecated. Use `allowed_line?` instead.
24
24
  WARNING
25
25
 
26
- allowed_line?
26
+ allowed_line?(line)
27
27
  end
28
28
 
29
29
  def matches_allowed_pattern?(line)
@@ -31,12 +31,12 @@ module RuboCop
31
31
  end
32
32
 
33
33
  # @deprecated Use matches_allowed_pattern? instead
34
- def matches_ignored_pattern?
34
+ def matches_ignored_pattern?(line)
35
35
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
36
36
  `matches_ignored_pattern?` is deprecated. Use `matches_allowed_pattern?` instead.
37
37
  WARNING
38
38
 
39
- matches_allowed_pattern?
39
+ matches_allowed_pattern?(line)
40
40
  end
41
41
 
42
42
  def allowed_patterns
@@ -82,7 +82,7 @@ module RuboCop
82
82
  next_sibling.source_range
83
83
  next_sibling.loc.line
84
84
  elsif (parent = node.parent)
85
- if parent.loc.respond_to?(:end) && parent.loc.end
85
+ if parent.loc?(:end)
86
86
  parent.loc.end.line
87
87
  else
88
88
  parent.loc.line
@@ -11,6 +11,24 @@ module RuboCop
11
11
  DO_NOT_MIX_OMIT_VALUE_MSG = "#{DO_NOT_MIX_MSG_PREFIX} #{OMIT_HASH_VALUE_MSG}"
12
12
  DO_NOT_MIX_EXPLICIT_VALUE_MSG = "#{DO_NOT_MIX_MSG_PREFIX} #{EXPLICIT_HASH_VALUE_MSG}"
13
13
 
14
+ DefNode = Struct.new(:node) do
15
+ def selector
16
+ if node.loc.respond_to?(:selector)
17
+ node.loc.selector
18
+ else
19
+ node.loc.keyword
20
+ end
21
+ end
22
+
23
+ def first_argument
24
+ node.first_argument
25
+ end
26
+
27
+ def last_argument
28
+ node.last_argument
29
+ end
30
+ end
31
+
14
32
  def on_hash_for_mixed_shorthand(hash_node)
15
33
  return if ignore_mixed_hash_shorthand_syntax?(hash_node)
16
34
 
@@ -212,24 +230,6 @@ module RuboCop
212
230
  register_offense(pair_node, OMIT_HASH_VALUE_MSG, replacement)
213
231
  end
214
232
  end
215
-
216
- DefNode = Struct.new(:node) do
217
- def selector
218
- if node.loc.respond_to?(:selector)
219
- node.loc.selector
220
- else
221
- node.loc.keyword
222
- end
223
- end
224
-
225
- def first_argument
226
- node.first_argument
227
- end
228
-
229
- def last_argument
230
- node.last_argument
231
- end
232
- end
233
233
  end
234
234
  end
235
235
  # rubocop:enable Metrics/ModuleLength
@@ -23,7 +23,7 @@ module RuboCop
23
23
  (call _ _)
24
24
  (args
25
25
  $(arg _key)
26
- (arg _))
26
+ $(arg _))
27
27
  {
28
28
  $(send
29
29
  {(lvar _key) $_ _ | _ $_ (lvar _key)})
@@ -67,7 +67,7 @@ module RuboCop
67
67
  end
68
68
 
69
69
  def extracts_hash_subset?(block)
70
- block_with_first_arg_check?(block) do |key_arg, send_node, method|
70
+ block_with_first_arg_check?(block) do |key_arg, value_arg, send_node, method|
71
71
  # Only consider methods that have one argument
72
72
  return false unless send_node.arguments.one?
73
73
 
@@ -76,15 +76,22 @@ module RuboCop
76
76
 
77
77
  case method
78
78
  when :include?, :exclude?
79
- send_node.first_argument.source == key_arg.source
79
+ slices_key?(send_node, :first_argument, key_arg, value_arg)
80
80
  when :in?
81
- send_node.receiver.source == key_arg.source
81
+ slices_key?(send_node, :receiver, key_arg, value_arg)
82
82
  else
83
83
  true
84
84
  end
85
85
  end
86
86
  end
87
87
 
88
+ def slices_key?(send_node, method, key_arg, value_arg)
89
+ return false if using_value_variable?(send_node, value_arg)
90
+
91
+ node = method == :receiver ? send_node.receiver : send_node.first_argument
92
+ node.source == key_arg.source
93
+ end
94
+
88
95
  def range_include?(send_node)
89
96
  # When checking `include?`, `exclude?` and `in?` for offenses, if the receiver
90
97
  # or first argument is a range, an offense should not be registered.
@@ -97,6 +104,14 @@ module RuboCop
97
104
  receiver.range_type?
98
105
  end
99
106
 
107
+ def using_value_variable?(send_node, value_arg)
108
+ # If the receiver of `include?` or `exclude?`, or the first argument of `in?` is the
109
+ # hash value block argument, an offense should not be registered.
110
+ # ie. `v.include?(k)` or `k.in?(v)`
111
+ (send_node.receiver.lvar_type? && send_node.receiver.name == value_arg.name) ||
112
+ (send_node.first_argument.lvar_type? && send_node.first_argument.name == value_arg.name)
113
+ end
114
+
100
115
  def supported_subset_method?(method)
101
116
  if active_support_extensions_enabled?
102
117
  ACTIVE_SUPPORT_SUBSET_METHODS.include?(method)
@@ -9,6 +9,80 @@ module RuboCop
9
9
 
10
10
  RESTRICT_ON_SEND = %i[[] to_h].freeze
11
11
 
12
+ # Internal helper class to hold match data
13
+ Captures = Struct.new(:transformed_argname, :transforming_body_expr, :unchanged_body_expr) do
14
+ def noop_transformation?
15
+ transforming_body_expr.lvar_type? &&
16
+ transforming_body_expr.children == [transformed_argname]
17
+ end
18
+
19
+ def transformation_uses_both_args?
20
+ transforming_body_expr.descendants.include?(unchanged_body_expr)
21
+ end
22
+
23
+ def use_transformed_argname?
24
+ transforming_body_expr.each_descendant(:lvar).any? do |node|
25
+ node.source == transformed_argname.to_s
26
+ end
27
+ end
28
+ end
29
+
30
+ # Internal helper class to hold autocorrect data
31
+ Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
32
+ def self.from_each_with_object(node, match)
33
+ new(match, node, 0, 0)
34
+ end
35
+
36
+ def self.from_hash_brackets_map(node, match)
37
+ new(match, node.children.last, 'Hash['.length, ']'.length)
38
+ end
39
+
40
+ def self.from_map_to_h(node, match)
41
+ if node.parent&.block_type? && node.parent.send_node == node
42
+ strip_trailing_chars = 0
43
+ else
44
+ map_range = node.children.first.source_range
45
+ node_range = node.source_range
46
+ strip_trailing_chars = node_range.end_pos - map_range.end_pos
47
+ end
48
+
49
+ new(match, node.children.first, 0, strip_trailing_chars)
50
+ end
51
+
52
+ def self.from_to_h(node, match)
53
+ new(match, node, 0, 0)
54
+ end
55
+
56
+ def strip_prefix_and_suffix(node, corrector)
57
+ expression = node.source_range
58
+ corrector.remove_leading(expression, leading)
59
+ corrector.remove_trailing(expression, trailing)
60
+ end
61
+
62
+ def set_new_method_name(new_method_name, corrector)
63
+ range = block_node.send_node.loc.selector
64
+ if (send_end = block_node.send_node.loc.end)
65
+ # If there are arguments (only true in the `each_with_object`
66
+ # case)
67
+ range = range.begin.join(send_end)
68
+ end
69
+ corrector.replace(range, new_method_name)
70
+ end
71
+
72
+ def set_new_arg_name(transformed_argname, corrector)
73
+ corrector.replace(block_node.arguments, "|#{transformed_argname}|")
74
+ end
75
+
76
+ def set_new_body_expression(transforming_body_expr, corrector)
77
+ body_source = transforming_body_expr.source
78
+ if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
79
+ body_source = "{ #{body_source} }"
80
+ end
81
+
82
+ corrector.replace(block_node.body, body_source)
83
+ end
84
+ end
85
+
12
86
  # @!method array_receiver?(node)
13
87
  def_node_matcher :array_receiver?, <<~PATTERN
14
88
  {(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
@@ -113,80 +187,6 @@ module RuboCop
113
187
  correction.set_new_arg_name(captures.transformed_argname, corrector)
114
188
  correction.set_new_body_expression(captures.transforming_body_expr, corrector)
115
189
  end
116
-
117
- # Internal helper class to hold match data
118
- Captures = Struct.new(:transformed_argname, :transforming_body_expr, :unchanged_body_expr) do
119
- def noop_transformation?
120
- transforming_body_expr.lvar_type? &&
121
- transforming_body_expr.children == [transformed_argname]
122
- end
123
-
124
- def transformation_uses_both_args?
125
- transforming_body_expr.descendants.include?(unchanged_body_expr)
126
- end
127
-
128
- def use_transformed_argname?
129
- transforming_body_expr.each_descendant(:lvar).any? do |node|
130
- node.source == transformed_argname.to_s
131
- end
132
- end
133
- end
134
-
135
- # Internal helper class to hold autocorrect data
136
- Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
137
- def self.from_each_with_object(node, match)
138
- new(match, node, 0, 0)
139
- end
140
-
141
- def self.from_hash_brackets_map(node, match)
142
- new(match, node.children.last, 'Hash['.length, ']'.length)
143
- end
144
-
145
- def self.from_map_to_h(node, match)
146
- if node.parent&.block_type? && node.parent.send_node == node
147
- strip_trailing_chars = 0
148
- else
149
- map_range = node.children.first.source_range
150
- node_range = node.source_range
151
- strip_trailing_chars = node_range.end_pos - map_range.end_pos
152
- end
153
-
154
- new(match, node.children.first, 0, strip_trailing_chars)
155
- end
156
-
157
- def self.from_to_h(node, match)
158
- new(match, node, 0, 0)
159
- end
160
-
161
- def strip_prefix_and_suffix(node, corrector)
162
- expression = node.source_range
163
- corrector.remove_leading(expression, leading)
164
- corrector.remove_trailing(expression, trailing)
165
- end
166
-
167
- def set_new_method_name(new_method_name, corrector)
168
- range = block_node.send_node.loc.selector
169
- if (send_end = block_node.send_node.loc.end)
170
- # If there are arguments (only true in the `each_with_object`
171
- # case)
172
- range = range.begin.join(send_end)
173
- end
174
- corrector.replace(range, new_method_name)
175
- end
176
-
177
- def set_new_arg_name(transformed_argname, corrector)
178
- corrector.replace(block_node.arguments, "|#{transformed_argname}|")
179
- end
180
-
181
- def set_new_body_expression(transforming_body_expr, corrector)
182
- body_source = transforming_body_expr.source
183
- if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
184
- body_source = "{ #{body_source} }"
185
- end
186
-
187
- corrector.replace(block_node.body, body_source)
188
- end
189
- end
190
190
  end
191
191
  end
192
192
  end
@@ -21,7 +21,7 @@ module RuboCop
21
21
  end
22
22
 
23
23
  def begin_source(node)
24
- node.loc.begin.source if node.loc.respond_to?(:begin) && node.loc.begin
24
+ node.loc.begin.source if node.loc?(:begin)
25
25
  end
26
26
 
27
27
  def type(node)
@@ -4,9 +4,10 @@ module RuboCop
4
4
  module Cop
5
5
  # Methods that calculate and return Parser::Source::Ranges
6
6
  module RangeHelp
7
- private
8
-
9
7
  BYTE_ORDER_MARK = 0xfeff # The Unicode codepoint
8
+ NOT_GIVEN = Module.new
9
+
10
+ private
10
11
 
11
12
  def source_range(source_buffer, line_number, column, length = 1)
12
13
  if column.is_a?(Range)
@@ -33,6 +34,18 @@ module RuboCop
33
34
  range_between(node.loc.begin.end_pos, node.loc.end.begin_pos)
34
35
  end
35
36
 
37
+ # A range containing the first to the last argument
38
+ # of a method call or method definition.
39
+ # def foo(a, b:)
40
+ # ^^^^^
41
+ # bar(1, 2, 3, &blk)
42
+ # ^^^^^^^^^^^^^
43
+ # baz { |x, y:, z:| }
44
+ # ^^^^^^^^^
45
+ def arguments_range(node)
46
+ node.first_argument.source_range.join(node.last_argument.source_range)
47
+ end
48
+
36
49
  def range_between(start_pos, end_pos)
37
50
  Parser::Source::Range.new(processed_source.buffer, start_pos, end_pos)
38
51
  end
@@ -51,7 +64,6 @@ module RuboCop
51
64
  Parser::Source::Range.new(buffer, begin_pos, end_pos)
52
65
  end
53
66
 
54
- NOT_GIVEN = Module.new
55
67
  def range_with_surrounding_space(range_positional = NOT_GIVEN, # rubocop:disable Metrics/ParameterLists
56
68
  range: NOT_GIVEN, side: :both, newlines: true,
57
69
  whitespace: false, continuations: false,
@@ -10,7 +10,7 @@ module RuboCop
10
10
  def on_str(node)
11
11
  # Constants like __FILE__ are handled as strings,
12
12
  # but don't respond to begin.
13
- return unless node.loc.respond_to?(:begin) && node.loc.begin
13
+ return unless node.loc?(:begin)
14
14
  return if part_of_ignored_node?(node)
15
15
 
16
16
  if offense?(node)
@@ -4,6 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  # Common methods shared by Style/TrailingCommaInArguments,
6
6
  # Style/TrailingCommaInArrayLiteral and Style/TrailingCommaInHashLiteral
7
+ # rubocop:disable Metrics/ModuleLength
7
8
  module TrailingComma
8
9
  include ConfigurableEnforcedStyle
9
10
  include RangeHelp
@@ -57,6 +58,8 @@ module RuboCop
57
58
  ', unless each item is on its own line'
58
59
  when :consistent_comma
59
60
  ', unless items are split onto multiple lines'
61
+ when :diff_comma
62
+ ', unless that item immediately precedes a newline'
60
63
  else
61
64
  ''
62
65
  end
@@ -68,6 +71,8 @@ module RuboCop
68
71
  multiline?(node) && no_elements_on_same_line?(node)
69
72
  when :consistent_comma
70
73
  multiline?(node) && !method_name_and_arguments_on_same_line?(node)
74
+ when :diff_comma
75
+ multiline?(node) && last_item_precedes_newline?(node)
71
76
  else
72
77
  false
73
78
  end
@@ -130,6 +135,12 @@ module RuboCop
130
135
  range1.last_line == range2.line
131
136
  end
132
137
 
138
+ def last_item_precedes_newline?(node)
139
+ after_last_item =
140
+ range_between(node.children.last.source_range.end_pos, node.loc.end.begin_pos)
141
+ after_last_item.source =~ /\A,?\s*\n/
142
+ end
143
+
133
144
  def avoid_comma(kind, comma_begin_pos, extra_info)
134
145
  range = range_between(comma_begin_pos, comma_begin_pos + 1)
135
146
  article = kind.include?('array') ? 'an' : 'a'
@@ -205,5 +216,6 @@ module RuboCop
205
216
  false
206
217
  end
207
218
  end
219
+ # rubocop:enable Metrics/ModuleLength
208
220
  end
209
221
  end
@@ -14,10 +14,10 @@ module RuboCop
14
14
  # autocorrected.
15
15
  #
16
16
  # [NOTE]
17
- # --
17
+ # ====
18
18
  # Because of a bug in Ruby 3.3.0, when a block is referenced inside of another block,
19
19
  # no offense will be registered until Ruby 3.4:
20
-
20
+ #
21
21
  # [source,ruby]
22
22
  # ----
23
23
  # def foo(&block)
@@ -25,7 +25,7 @@ module RuboCop
25
25
  # block_method { bar(&block) }
26
26
  # end
27
27
  # ----
28
- # --
28
+ # ====
29
29
  #
30
30
  # @example EnforcedStyle: anonymous (default)
31
31
  #
@@ -17,6 +17,10 @@ module RuboCop
17
17
  # they end with a `?`. These methods should be changed to remove the
18
18
  # prefix.
19
19
  #
20
+ # When `UseSorbetSigs` set to true (optional), the cop will only report
21
+ # offenses if the method has a Sorbet `sig` with a return type of
22
+ # `T::Boolean`. Dynamic methods are not supported with this configuration.
23
+ #
20
24
  # @example NamePrefix: ['is_', 'has_', 'have_'] (default)
21
25
  # # bad
22
26
  # def is_even(value)
@@ -58,6 +62,30 @@ module RuboCop
58
62
  # def is_even?(value)
59
63
  # end
60
64
  #
65
+ # @example UseSorbetSigs: false (default)
66
+ # # bad
67
+ # sig { returns(String) }
68
+ # def is_this_thing_on
69
+ # "yes"
70
+ # end
71
+ #
72
+ # # good - Sorbet signature is not evaluated
73
+ # sig { returns(String) }
74
+ # def is_this_thing_on?
75
+ # "yes"
76
+ # end
77
+ #
78
+ # @example UseSorbetSigs: true
79
+ # # bad
80
+ # sig { returns(T::Boolean) }
81
+ # def odd(value)
82
+ # end
83
+ #
84
+ # # good
85
+ # sig { returns(T::Boolean) }
86
+ # def odd?(value)
87
+ # end
88
+ #
61
89
  # @example MethodDefinitionMacros: ['define_method', 'define_singleton_method'] (default)
62
90
  # # bad
63
91
  # define_method(:is_even) { |value| }
@@ -100,6 +128,7 @@ module RuboCop
100
128
  method_name = node.method_name.to_s
101
129
 
102
130
  next if allowed_method_name?(method_name, prefix)
131
+ next if use_sorbet_sigs? && !sorbet_sig?(node, return_type: 'T::Boolean')
103
132
 
104
133
  add_offense(
105
134
  node.loc.name,
@@ -121,6 +150,17 @@ module RuboCop
121
150
 
122
151
  private
123
152
 
153
+ # @!method sorbet_return_type(node)
154
+ def_node_matcher :sorbet_return_type, <<~PATTERN
155
+ (block (send nil? :sig) args (send _ :returns $_type))
156
+ PATTERN
157
+
158
+ def sorbet_sig?(node, return_type: nil)
159
+ return false unless (type = sorbet_return_type(node.left_sibling))
160
+
161
+ type.source == return_type
162
+ end
163
+
124
164
  def allowed_method_name?(method_name, prefix)
125
165
  !(method_name.start_with?(prefix) && # cheap check to avoid allocating Regexp
126
166
  method_name.match?(/^#{prefix}[^0-9]/)) ||
@@ -151,6 +191,10 @@ module RuboCop
151
191
  cop_config['NamePrefix']
152
192
  end
153
193
 
194
+ def use_sorbet_sigs?
195
+ cop_config['UseSorbetSigs']
196
+ end
197
+
154
198
  def method_definition_macros(macro_name)
155
199
  cop_config['MethodDefinitionMacros'].include?(macro_name.to_s)
156
200
  end