rubocop 1.71.2 → 1.75.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 (205) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/config/default.yml +118 -21
  4. data/config/internal_affairs.yml +20 -0
  5. data/config/obsoletion.yml +3 -1
  6. data/lib/rubocop/cli/command/suggest_extensions.rb +7 -1
  7. data/lib/rubocop/cli.rb +1 -1
  8. data/lib/rubocop/comment_config.rb +1 -1
  9. data/lib/rubocop/config.rb +39 -6
  10. data/lib/rubocop/config_loader.rb +48 -9
  11. data/lib/rubocop/config_loader_resolver.rb +24 -9
  12. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -3
  13. data/lib/rubocop/config_obsoletion/renamed_cop.rb +18 -3
  14. data/lib/rubocop/config_obsoletion.rb +46 -2
  15. data/lib/rubocop/config_validator.rb +2 -1
  16. data/lib/rubocop/cop/internal_affairs/example_description.rb +7 -3
  17. data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
  18. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +1 -1
  19. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +2 -1
  20. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +91 -0
  21. data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
  22. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
  23. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +7 -1
  24. data/lib/rubocop/cop/internal_affairs.rb +2 -16
  25. data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
  26. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
  27. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +4 -4
  28. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  29. data/lib/rubocop/cop/layout/else_alignment.rb +2 -2
  30. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  31. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +3 -3
  32. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +27 -1
  33. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
  34. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
  35. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  36. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  37. data/lib/rubocop/cop/layout/line_length.rb +8 -4
  38. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
  39. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  40. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
  41. data/lib/rubocop/cop/layout/redundant_line_break.rb +9 -5
  42. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -5
  43. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  44. data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
  45. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
  46. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
  47. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
  48. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  49. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  50. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -17
  51. data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
  52. data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
  53. data/lib/rubocop/cop/lint/float_comparison.rb +1 -6
  54. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  55. data/lib/rubocop/cop/lint/literal_as_condition.rb +103 -9
  56. data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
  57. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  58. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
  59. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
  60. data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
  61. data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
  62. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +261 -0
  63. data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
  64. data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
  65. data/lib/rubocop/cop/lint/return_in_void_context.rb +9 -11
  66. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +8 -1
  67. data/lib/rubocop/cop/lint/shared_mutable_default.rb +12 -1
  68. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  69. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
  70. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
  71. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  72. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +2 -0
  73. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  74. data/lib/rubocop/cop/lint/unreachable_loop.rb +5 -5
  75. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -0
  76. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +71 -0
  77. data/lib/rubocop/cop/lint/void.rb +7 -0
  78. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  79. data/lib/rubocop/cop/metrics/method_length.rb +1 -0
  80. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  81. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
  82. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  83. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  84. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  85. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  86. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  87. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  88. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
  89. data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
  90. data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
  91. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +0 -1
  92. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +18 -18
  93. data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
  94. data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
  95. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
  96. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  97. data/lib/rubocop/cop/mixin/range_help.rb +15 -3
  98. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  99. data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
  100. data/lib/rubocop/cop/mixin/trailing_comma.rb +12 -0
  101. data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
  102. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  103. data/lib/rubocop/cop/naming/method_name.rb +64 -8
  104. data/lib/rubocop/cop/naming/predicate_name.rb +44 -0
  105. data/lib/rubocop/cop/naming/variable_name.rb +51 -6
  106. data/lib/rubocop/cop/registry.rb +9 -6
  107. data/lib/rubocop/cop/style/accessor_grouping.rb +19 -5
  108. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  109. data/lib/rubocop/cop/style/array_intersect.rb +39 -28
  110. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  111. data/lib/rubocop/cop/style/class_and_module_children.rb +29 -7
  112. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
  113. data/lib/rubocop/cop/style/collection_methods.rb +1 -0
  114. data/lib/rubocop/cop/style/combinable_loops.rb +1 -0
  115. data/lib/rubocop/cop/style/commented_keyword.rb +10 -3
  116. data/lib/rubocop/cop/style/comparable_between.rb +75 -0
  117. data/lib/rubocop/cop/style/conditional_assignment.rb +3 -0
  118. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  119. data/lib/rubocop/cop/style/empty_literal.rb +4 -0
  120. data/lib/rubocop/cop/style/endless_method.rb +163 -18
  121. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
  122. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  123. data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
  124. data/lib/rubocop/cop/style/for.rb +1 -0
  125. data/lib/rubocop/cop/style/format_string_token.rb +38 -11
  126. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
  127. data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
  128. data/lib/rubocop/cop/style/guard_clause.rb +2 -1
  129. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -2
  130. data/lib/rubocop/cop/style/hash_fetch_chain.rb +105 -0
  131. data/lib/rubocop/cop/style/hash_syntax.rb +3 -0
  132. data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
  133. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  134. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  135. data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
  136. data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
  137. data/lib/rubocop/cop/style/it_block_parameter.rb +100 -0
  138. data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
  139. data/lib/rubocop/cop/style/lambda.rb +1 -0
  140. data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
  141. data/lib/rubocop/cop/style/map_into_array.rb +1 -0
  142. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +4 -4
  143. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -1
  144. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
  145. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
  146. data/lib/rubocop/cop/style/next.rb +44 -0
  147. data/lib/rubocop/cop/style/object_then.rb +1 -0
  148. data/lib/rubocop/cop/style/proc.rb +1 -0
  149. data/lib/rubocop/cop/style/raise_args.rb +8 -8
  150. data/lib/rubocop/cop/style/redundant_begin.rb +1 -0
  151. data/lib/rubocop/cop/style/redundant_condition.rb +57 -0
  152. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +14 -4
  153. data/lib/rubocop/cop/style/redundant_format.rb +257 -0
  154. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  155. data/lib/rubocop/cop/style/redundant_parentheses.rb +20 -5
  156. data/lib/rubocop/cop/style/redundant_self.rb +1 -0
  157. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  158. data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
  159. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
  160. data/lib/rubocop/cop/style/return_nil.rb +2 -2
  161. data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
  162. data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
  163. data/lib/rubocop/cop/style/single_line_methods.rb +3 -3
  164. data/lib/rubocop/cop/style/sole_nested_conditional.rb +41 -106
  165. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  166. data/lib/rubocop/cop/style/super_arguments.rb +1 -2
  167. data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
  168. data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -0
  169. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
  170. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
  171. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  172. data/lib/rubocop/cop/util.rb +2 -2
  173. data/lib/rubocop/cop/utils/format_string.rb +10 -5
  174. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  175. data/lib/rubocop/cop/variable_force/variable.rb +2 -7
  176. data/lib/rubocop/cop/variable_force.rb +1 -1
  177. data/lib/rubocop/cops_documentation_generator.rb +12 -1
  178. data/lib/rubocop/directive_comment.rb +36 -3
  179. data/lib/rubocop/ext/regexp_node.rb +0 -1
  180. data/lib/rubocop/lsp/runtime.rb +6 -4
  181. data/lib/rubocop/lsp/server.rb +0 -2
  182. data/lib/rubocop/lsp/stdin_runner.rb +3 -1
  183. data/lib/rubocop/magic_comment.rb +8 -0
  184. data/lib/rubocop/options.rb +26 -11
  185. data/lib/rubocop/path_util.rb +4 -0
  186. data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
  187. data/lib/rubocop/plugin/load_error.rb +26 -0
  188. data/lib/rubocop/plugin/loader.rb +100 -0
  189. data/lib/rubocop/plugin/not_supported_error.rb +29 -0
  190. data/lib/rubocop/plugin.rb +46 -0
  191. data/lib/rubocop/rake_task.rb +4 -1
  192. data/lib/rubocop/rspec/cop_helper.rb +13 -1
  193. data/lib/rubocop/rspec/shared_contexts.rb +35 -0
  194. data/lib/rubocop/rspec/support.rb +3 -0
  195. data/lib/rubocop/runner.rb +5 -1
  196. data/lib/rubocop/server/cache.rb +47 -11
  197. data/lib/rubocop/server/cli.rb +2 -2
  198. data/lib/rubocop/target_finder.rb +1 -1
  199. data/lib/rubocop/target_ruby.rb +1 -1
  200. data/lib/rubocop/version.rb +30 -8
  201. data/lib/rubocop.rb +10 -1
  202. data/lib/ruby_lsp/rubocop/addon.rb +7 -10
  203. data/lib/ruby_lsp/rubocop/{wraps_built_in_lsp_runtime.rb → runtime_adapter.rb} +25 -10
  204. metadata +43 -12
  205. data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
@@ -96,11 +96,7 @@ module RuboCop
96
96
  end
97
97
 
98
98
  def autocorrect(corrector, node, if_branch)
99
- if node.condition.or_type? || node.condition.assignment?
100
- corrector.wrap(node.condition, '(', ')')
101
- end
102
-
103
- if outer_condition_modify_form?(node, if_branch)
99
+ if node.modifier_form?
104
100
  autocorrect_outer_condition_modify_form(corrector, node, if_branch)
105
101
  else
106
102
  autocorrect_outer_condition_basic(corrector, node, if_branch)
@@ -108,74 +104,48 @@ module RuboCop
108
104
  end
109
105
 
110
106
  def autocorrect_outer_condition_basic(corrector, node, if_branch)
111
- correct_from_unless_to_if(corrector, node) if node.unless?
112
-
113
- outer_condition = node.condition
114
- correct_outer_condition(corrector, outer_condition)
107
+ correct_node(corrector, node)
115
108
 
116
- and_operator = if_branch.unless? ? ' && !' : ' && '
117
109
  if if_branch.modifier_form?
118
- correct_for_guard_condition_style(corrector, outer_condition, if_branch, and_operator)
110
+ correct_for_guard_condition_style(corrector, node, if_branch)
119
111
  else
120
- correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
112
+ correct_for_basic_condition_style(corrector, node, if_branch)
121
113
  correct_for_comment(corrector, node, if_branch)
122
114
  end
123
115
  end
124
116
 
125
- def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
126
- correct_from_unless_to_if(corrector, if_branch, is_modify_form: true) if if_branch.unless?
127
- correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
128
- end
129
-
130
- def correct_from_unless_to_if(corrector, node, is_modify_form: false)
131
- corrector.replace(node.loc.keyword, 'if')
132
-
133
- insert_bang(corrector, node, is_modify_form)
117
+ def correct_node(corrector, node)
118
+ corrector.replace(node.loc.keyword, 'if') if node.unless?
119
+ corrector.replace(node.condition, chainable_condition(node))
134
120
  end
135
121
 
136
- def correct_for_guard_condition_style(corrector, outer_condition, if_branch, and_operator)
137
- condition = if_branch.condition
138
- corrector.insert_after(outer_condition, "#{and_operator}#{replace_condition(condition)}")
122
+ def correct_for_guard_condition_style(corrector, node, if_branch)
123
+ corrector.insert_after(node.condition, " && #{chainable_condition(if_branch)}")
139
124
 
140
- range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
125
+ range = range_between(
126
+ if_branch.loc.keyword.begin_pos, if_branch.condition.source_range.end_pos
127
+ )
141
128
  corrector.remove(range_with_surrounding_space(range, newlines: false))
142
- corrector.remove(if_branch.loc.keyword)
143
129
  end
144
130
 
145
- def correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
131
+ def correct_for_basic_condition_style(corrector, node, if_branch)
146
132
  range = range_between(
147
133
  node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos
148
134
  )
149
- corrector.replace(range, and_operator)
150
- corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
135
+ corrector.replace(range, ' && ')
151
136
 
152
- wrap_condition(corrector, if_branch.condition)
153
- end
137
+ corrector.replace(if_branch.condition, chainable_condition(if_branch))
154
138
 
155
- def wrap_condition(corrector, condition)
156
- # Handle `send` and `block` nodes that need to be wrapped in parens
157
- # FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
158
- # but wrapping the argument list would be a more ergonomic correction.
159
- node_to_check = condition&.any_block_type? ? condition.send_node : condition
160
- return unless wrap_condition?(node_to_check)
161
-
162
- if condition.call_type?
163
- source = parenthesized_method_arguments(condition)
164
-
165
- corrector.replace(condition, source)
166
- else
167
- corrector.wrap(condition, '(', ')')
168
- end
139
+ corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
169
140
  end
170
141
 
171
- def correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
172
- condition = if_branch.condition
173
- corrector.insert_before(condition,
174
- "#{'!' if node.unless?}#{replace_condition(node.condition)} && ")
142
+ def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
143
+ correct_node(corrector, if_branch)
175
144
 
176
- corrector.remove(node.condition)
177
- corrector.remove(range_with_surrounding_space(node.loc.keyword, newlines: false))
178
- corrector.replace(if_branch.loc.keyword, 'if')
145
+ corrector.insert_before(if_branch.condition, "#{chainable_condition(node)} && ")
146
+
147
+ range = range_between(node.loc.keyword.begin_pos, node.condition.source_range.end_pos)
148
+ corrector.remove(range_with_surrounding_space(range, newlines: false))
179
149
  end
180
150
 
181
151
  def correct_for_comment(corrector, node, if_branch)
@@ -187,67 +157,36 @@ module RuboCop
187
157
  corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
188
158
  end
189
159
 
190
- def correct_outer_condition(corrector, condition)
191
- return unless require_parentheses?(condition)
160
+ def chainable_condition(node)
161
+ wrapped_condition = add_parentheses_if_needed(node.condition)
192
162
 
193
- end_pos = condition.loc.selector.end_pos
194
- begin_pos = condition.first_argument.source_range.begin_pos
195
- return if end_pos > begin_pos
163
+ return wrapped_condition if node.if?
196
164
 
197
- range = range_between(end_pos, begin_pos)
198
- corrector.remove(range)
199
- corrector.insert_after(range, '(')
200
- corrector.insert_after(condition.last_argument, ')')
165
+ node.condition.and_type? ? "!(#{wrapped_condition})" : "!#{wrapped_condition}"
201
166
  end
202
167
 
203
- def insert_bang(corrector, node, is_modify_form)
204
- condition = node.condition
205
-
206
- if (condition.send_type? && condition.comparison_method? && !condition.parenthesized?) ||
207
- (is_modify_form && wrap_condition?(condition))
208
- corrector.wrap(node.condition, '!(', ')')
209
- elsif condition.and_type?
210
- insert_bang_for_and(corrector, node)
211
- else
212
- corrector.insert_before(condition, '!')
213
- end
214
- end
215
-
216
- def insert_bang_for_and(corrector, node)
217
- lhs, rhs = *node # rubocop:disable InternalAffairs/NodeDestructuring
168
+ def add_parentheses_if_needed(condition)
169
+ # Handle `send` and `block` nodes that need to be wrapped in parens
170
+ # FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
171
+ # but wrapping the argument list would be a more ergonomic correction.
172
+ node_to_check = condition&.any_block_type? ? condition.send_node : condition
173
+ return condition.source unless add_parentheses?(node_to_check)
218
174
 
219
- if lhs.and_type?
220
- insert_bang_for_and(corrector, lhs)
221
- corrector.insert_before(rhs, '!') if rhs
175
+ if parenthesize_method?(condition)
176
+ parenthesized_method_arguments(condition)
222
177
  else
223
- corrector.insert_before(lhs, '!')
224
- corrector.insert_before(rhs, '!')
178
+ "(#{condition.source})"
225
179
  end
226
180
  end
227
181
 
228
- def require_parentheses?(condition)
229
- condition.call_type? && !condition.arguments.empty? && !condition.parenthesized? &&
230
- !condition.comparison_method?
182
+ def parenthesize_method?(node)
183
+ node.call_type? && node.arguments.any? && !node.parenthesized? &&
184
+ !node.comparison_method? && !node.operator_method?
231
185
  end
232
186
 
233
- def arguments_range(node)
234
- range_between(
235
- node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
236
- )
237
- end
238
-
239
- def wrap_condition?(node)
240
- node.operator_keyword? || (node.call_type? && node.arguments.any? && !node.parenthesized?)
241
- end
242
-
243
- def replace_condition(condition)
244
- return condition.source unless wrap_condition?(condition)
245
-
246
- if condition.call_type? && !condition.comparison_method?
247
- parenthesized_method_arguments(condition)
248
- else
249
- "(#{condition.source})"
250
- end
187
+ def add_parentheses?(node)
188
+ node.assignment? || (node.operator_keyword? && !node.and_type?) ||
189
+ (node.call_type? && node.arguments.any? && !node.parenthesized?)
251
190
  end
252
191
 
253
192
  def parenthesized_method_arguments(node)
@@ -260,10 +199,6 @@ module RuboCop
260
199
  def allow_modifier?
261
200
  cop_config['AllowModifier']
262
201
  end
263
-
264
- def outer_condition_modify_form?(node, if_branch)
265
- node.condition.source_range.begin_pos > if_branch.condition.source_range.begin_pos
266
- end
267
202
  end
268
203
  end
269
204
  end
@@ -128,7 +128,7 @@ module RuboCop
128
128
  end
129
129
 
130
130
  def uncorrectable?(part)
131
- part.multiline? || heredoc?(part) || part.each_descendant(:block).any?
131
+ part.multiline? || heredoc?(part) || part.each_descendant(:any_block).any?
132
132
  end
133
133
 
134
134
  def heredoc?(node)
@@ -68,7 +68,6 @@ module RuboCop
68
68
  class SuperArguments < Base
69
69
  extend AutoCorrector
70
70
 
71
- DEF_TYPES = %i[def defs].freeze
72
71
  ASSIGN_TYPES = %i[or_asgn lvasgn].freeze
73
72
 
74
73
  MSG = 'Call `super` without arguments and parentheses when the signature is identical.'
@@ -100,7 +99,7 @@ module RuboCop
100
99
  # `super` used within the block is always allowed.
101
100
  break if node.any_block_type? && !block_sends_to_super?(super_node, node)
102
101
 
103
- break node if DEF_TYPES.include?(node.type)
102
+ break node if node.any_def_type?
104
103
  end
105
104
  end
106
105
 
@@ -159,6 +159,7 @@ module RuboCop
159
159
  {
160
160
  (block $#symbol_proc_receiver? $(args (arg _var)) (send (lvar _var) $_))
161
161
  (numblock $#symbol_proc_receiver? $1 (send (lvar :_1) $_))
162
+ (itblock $#symbol_proc_receiver? $_ (send (lvar :it) $_))
162
163
  }
163
164
  PATTERN
164
165
 
@@ -185,6 +186,7 @@ module RuboCop
185
186
  end
186
187
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
187
188
  alias on_numblock on_block
189
+ alias on_itblock on_block
188
190
 
189
191
  def destructuring_block_argument?(argument_node)
190
192
  argument_node.one? && argument_node.source.include?(',')
@@ -64,6 +64,7 @@ module RuboCop
64
64
  end
65
65
 
66
66
  alias on_numblock on_block
67
+ alias on_itblock on_block
67
68
 
68
69
  private
69
70
 
@@ -6,12 +6,13 @@ module RuboCop
6
6
  # Checks for trailing comma in array literals.
7
7
  # The configuration options are:
8
8
  #
9
- # * `consistent_comma`: Requires a comma after the
10
- # last item of all non-empty, multiline array literals.
11
- # * `comma`: Requires a comma after last item in an array,
12
- # but only when each item is on its own line.
13
- # * `no_comma`: Does not require a comma after the
14
- # last item in an array
9
+ # * `consistent_comma`: Requires a comma after the last item of all non-empty, multiline array
10
+ # literals.
11
+ # * `comma`: Requires a comma after the last item in an array, but only when each item is on
12
+ # its own line.
13
+ # * `diff_comma`: Requires a comma after the last item in an array, but only when that item is
14
+ # followed by an immediate newline.
15
+ # * `no_comma`: Does not require a comma after the last item in an array
15
16
  #
16
17
  # @example EnforcedStyleForMultiline: consistent_comma
17
18
  # # bad
@@ -37,6 +38,14 @@ module RuboCop
37
38
  # 2,
38
39
  # ]
39
40
  #
41
+ # # bad
42
+ # a = [1, 2,
43
+ # 3, 4]
44
+ #
45
+ # # good
46
+ # a = [1, 2,
47
+ # 3, 4,]
48
+ #
40
49
  # @example EnforcedStyleForMultiline: comma
41
50
  # # bad
42
51
  # a = [1, 2,]
@@ -72,6 +81,38 @@ module RuboCop
72
81
  # 2,
73
82
  # ]
74
83
  #
84
+ # @example EnforcedStyleForMultiline: diff_comma
85
+ # # bad
86
+ # a = [1, 2,]
87
+ #
88
+ # # good
89
+ # a = [1, 2]
90
+ #
91
+ # # good
92
+ # a = [
93
+ # 1, 2,
94
+ # 3,
95
+ # ]
96
+ #
97
+ # # good
98
+ # a = [
99
+ # 1, 2, 3,
100
+ # ]
101
+ #
102
+ # # good
103
+ # a = [
104
+ # 1,
105
+ # 2,
106
+ # ]
107
+ #
108
+ # # bad
109
+ # a = [1, 2,
110
+ # 3, 4,]
111
+ #
112
+ # # good
113
+ # a = [1, 2,
114
+ # 3, 4]
115
+ #
75
116
  # @example EnforcedStyleForMultiline: no_comma (default)
76
117
  # # bad
77
118
  # a = [1, 2,]
@@ -6,12 +6,13 @@ module RuboCop
6
6
  # Checks for trailing comma in hash literals.
7
7
  # The configuration options are:
8
8
  #
9
- # * `consistent_comma`: Requires a comma after the
10
- # last item of all non-empty, multiline hash literals.
11
- # * `comma`: Requires a comma after the last item in a hash,
12
- # but only when each item is on its own line.
13
- # * `no_comma`: Does not require a comma after the
14
- # last item in a hash
9
+ # * `consistent_comma`: Requires a comma after the last item of all non-empty, multiline hash
10
+ # literals.
11
+ # * `comma`: Requires a comma after the last item in a hash, but only when each item is on its
12
+ # own line.
13
+ # * `diff_comma`: Requires a comma after the last item in a hash, but only when that item is
14
+ # followed by an immediate newline.
15
+ # * `no_comma`: Does not require a comma after the last item in a hash
15
16
  #
16
17
  # @example EnforcedStyleForMultiline: consistent_comma
17
18
  #
@@ -38,6 +39,14 @@ module RuboCop
38
39
  # bar: 2,
39
40
  # }
40
41
  #
42
+ # # bad
43
+ # a = { foo: 1, bar: 2,
44
+ # baz: 3, qux: 4 }
45
+ #
46
+ # # good
47
+ # a = { foo: 1, bar: 2,
48
+ # baz: 3, qux: 4, }
49
+ #
41
50
  # @example EnforcedStyleForMultiline: comma
42
51
  #
43
52
  # # bad
@@ -74,6 +83,39 @@ module RuboCop
74
83
  # bar: 2,
75
84
  # }
76
85
  #
86
+ # @example EnforcedStyleForMultiline: diff_comma
87
+ #
88
+ # # bad
89
+ # a = { foo: 1, bar: 2, }
90
+ #
91
+ # # good
92
+ # a = { foo: 1, bar: 2 }
93
+ #
94
+ # # good
95
+ # a = {
96
+ # foo: 1, bar: 2,
97
+ # qux: 3,
98
+ # }
99
+ #
100
+ # # good
101
+ # a = {
102
+ # foo: 1, bar: 2, qux: 3,
103
+ # }
104
+ #
105
+ # # good
106
+ # a = {
107
+ # foo: 1,
108
+ # bar: 2,
109
+ # }
110
+ #
111
+ # # bad
112
+ # a = { foo: 1, bar: 2,
113
+ # baz: 3, qux: 4, }
114
+ #
115
+ # # good
116
+ # a = { foo: 1, bar: 2,
117
+ # baz: 3, qux: 4 }
118
+ #
77
119
  # @example EnforcedStyleForMultiline: no_comma (default)
78
120
  #
79
121
  # # bad
@@ -113,7 +113,7 @@ module RuboCop
113
113
  private
114
114
 
115
115
  def in_module_or_instance_eval?(node)
116
- node.each_ancestor(:block, :class, :sclass, :module).each do |pnode|
116
+ node.each_ancestor(:any_block, :class, :sclass, :module).each do |pnode|
117
117
  case pnode.type
118
118
  when :class, :sclass
119
119
  return false
@@ -32,7 +32,7 @@ module RuboCop
32
32
  end
33
33
 
34
34
  def parentheses?(node)
35
- node.loc.respond_to?(:end) && node.loc.end&.is?(')')
35
+ node.loc_is?(:end, ')')
36
36
  end
37
37
 
38
38
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@@ -76,7 +76,7 @@ module RuboCop
76
76
  loc = node.loc
77
77
  selector = if node.type?(:super, :yield)
78
78
  loc.keyword
79
- elsif node.type?(:def, :defs)
79
+ elsif node.any_def_type?
80
80
  loc.name
81
81
  else
82
82
  loc.selector
@@ -5,15 +5,19 @@ module RuboCop
5
5
  module Utils
6
6
  # Parses {Kernel#sprintf} format strings.
7
7
  class FormatString
8
- DIGIT_DOLLAR = /(\d+)\$/.freeze
8
+ # Escaping the `#` in `INTERPOLATION` and `TEMPLATE_NAME` is necessary to
9
+ # avoid a bug in Ruby 3.2.0
10
+ # See: https://bugs.ruby-lang.org/issues/19379
11
+ DIGIT_DOLLAR = /(?<arg_number>\d+)\$/.freeze
12
+ INTERPOLATION = /\#\{.*?\}/.freeze
9
13
  FLAG = /[ #0+-]|#{DIGIT_DOLLAR}/.freeze
10
14
  NUMBER_ARG = /\*#{DIGIT_DOLLAR}?/.freeze
11
- NUMBER = /\d+|#{NUMBER_ARG}/.freeze
15
+ NUMBER = /\d+|#{NUMBER_ARG}|#{INTERPOLATION}/.freeze
12
16
  WIDTH = /(?<width>#{NUMBER})/.freeze
13
- PRECISION = /\.(?<precision>#{NUMBER})/.freeze
17
+ PRECISION = /\.(?<precision>#{NUMBER}?)/.freeze
14
18
  TYPE = /(?<type>[bBdiouxXeEfgGaAcps])/.freeze
15
19
  NAME = /<(?<name>\w+)>/.freeze
16
- TEMPLATE_NAME = /\{(?<name>\w+)\}/.freeze
20
+ TEMPLATE_NAME = /(?<!\#)\{(?<name>\w+)\}/.freeze
17
21
 
18
22
  SEQUENCE = /
19
23
  % (?<type>%)
@@ -41,7 +45,7 @@ module RuboCop
41
45
  #
42
46
  # @see https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-format
43
47
  class FormatSequence
44
- attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type
48
+ attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type, :arg_number
45
49
 
46
50
  def initialize(match)
47
51
  @source = match[0]
@@ -52,6 +56,7 @@ module RuboCop
52
56
  @precision = match[:precision]
53
57
  @name = match[:name]
54
58
  @type = match[:type]
59
+ @arg_number = match[:arg_number]
55
60
  end
56
61
 
57
62
  def percent?
@@ -46,7 +46,7 @@ module RuboCop
46
46
  else
47
47
  child_index = case node.type
48
48
  when :module, :sclass then 1
49
- when :def, :class, :block, :numblock then 2
49
+ when :def, :class, :block, :numblock, :itblock then 2
50
50
  when :defs then 3
51
51
  end
52
52
 
@@ -6,8 +6,6 @@ module RuboCop
6
6
  # A Variable represents existence of a local variable.
7
7
  # This holds a variable declaration node and some states of the variable.
8
8
  class Variable
9
- extend NodePattern::Macros
10
-
11
9
  VARIABLE_DECLARATION_TYPES = (VARIABLE_ASSIGNMENT_TYPES + ARGUMENT_DECLARATION_TYPES).freeze
12
10
 
13
11
  attr_reader :name, :declaration_node, :scope, :assignments, :references, :captured_by_block
@@ -40,14 +38,11 @@ module RuboCop
40
38
 
41
39
  def mark_last_as_reassigned!(assignment)
42
40
  return if captured_by_block?
43
- return if candidate_condition?(assignment.node.parent)
41
+ return unless assignment.branch == @assignments.last&.branch
44
42
 
45
43
  @assignments.last&.reassigned!
46
44
  end
47
45
 
48
- # @!method candidate_condition?(node)
49
- def_node_matcher :candidate_condition?, '[{if case case_match when}]'
50
-
51
46
  def referenced?
52
47
  !@references.empty?
53
48
  end
@@ -112,7 +107,7 @@ module RuboCop
112
107
  end
113
108
 
114
109
  def method_argument?
115
- argument? && %i[def defs].include?(@scope.node.type)
110
+ argument? && @scope.node.any_def_type?
116
111
  end
117
112
 
118
113
  def block_argument?
@@ -54,7 +54,7 @@ module RuboCop
54
54
 
55
55
  ZERO_ARITY_SUPER_TYPE = :zsuper
56
56
 
57
- TWISTED_SCOPE_TYPES = %i[block numblock class sclass defs module].freeze
57
+ TWISTED_SCOPE_TYPES = %i[block numblock itblock class sclass defs module].freeze
58
58
  SCOPE_TYPES = (TWISTED_SCOPE_TYPES + [:def]).freeze
59
59
 
60
60
  SEND_TYPE = :send
@@ -28,6 +28,12 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
28
28
  #
29
29
  # CopsDocumentationGenerator.new(departments: ['Lint']).call
30
30
  #
31
+ # For plugin extensions, specify `:plugin_name` keyword as follows:
32
+ #
33
+ # CopsDocumentationGenerator.new(
34
+ # departments: ['Performance'], plugin_name: 'rubocop-performance'
35
+ # ).call
36
+ #
31
37
  # You can append additional information:
32
38
  #
33
39
  # callback = ->(data) { required_rails_version(data.cop) }
@@ -36,11 +42,16 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
36
42
  # This will insert the string returned from the lambda _after_ the section from RuboCop itself.
37
43
  # See `CopsDocumentationGenerator::STRUCTURE` for available sections.
38
44
  #
39
- def initialize(departments: [], extra_info: {}, base_dir: Dir.pwd)
45
+ def initialize(departments: [], extra_info: {}, base_dir: Dir.pwd, plugin_name: nil)
40
46
  @departments = departments.map(&:to_sym).sort!
41
47
  @extra_info = extra_info
42
48
  @cops = RuboCop::Cop::Registry.global
43
49
  @config = RuboCop::ConfigLoader.default_configuration
50
+ # NOTE: For example, this prevents excessive plugin loading before another task executes,
51
+ # in cases where plugins are already loaded by `internal_investigation`.
52
+ if plugin_name && @config.loaded_plugins.none? { |plugin| plugin.about.name == plugin_name }
53
+ RuboCop::Plugin.integrate_plugins(RuboCop::Config.new, [plugin_name])
54
+ end
44
55
  @base_dir = base_dir
45
56
  @docs_path = "#{base_dir}/docs/modules/ROOT"
46
57
  FileUtils.mkdir_p("#{@docs_path}/pages")
@@ -12,16 +12,30 @@ module RuboCop
12
12
  # @api private
13
13
  LINT_SYNTAX_COP = "#{LINT_DEPARTMENT}/Syntax"
14
14
  # @api private
15
- COP_NAME_PATTERN = '([A-Z]\w+/)*(?:[A-Z]\w+)'
15
+ COP_NAME_PATTERN = '([A-Za-z]\w+/)*(?:[A-Za-z]\w+)'
16
16
  # @api private
17
17
  COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
18
18
  # @api private
19
19
  COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})"
20
20
  # @api private
21
+ AVAILABLE_MODES = %w[disable enable todo].freeze
22
+ # @api private
23
+ DIRECTIVE_MARKER_PATTERN = '# rubocop : '
24
+ # @api private
25
+ DIRECTIVE_MARKER_REGEXP = Regexp.new(DIRECTIVE_MARKER_PATTERN.gsub(' ', '\s*'))
26
+ # @api private
27
+ DIRECTIVE_HEADER_PATTERN = "#{DIRECTIVE_MARKER_PATTERN}((?:#{AVAILABLE_MODES.join('|')}))\\b"
28
+ # @api private
21
29
  DIRECTIVE_COMMENT_REGEXP = Regexp.new(
22
- "# rubocop : ((?:disable|enable|todo))\\b #{COPS_PATTERN}"
30
+ "#{DIRECTIVE_HEADER_PATTERN} #{COPS_PATTERN}"
23
31
  .gsub(' ', '\s*')
24
32
  )
33
+ # @api private
34
+ TRAILING_COMMENT_MARKER = '--'
35
+ # @api private
36
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP = Regexp.new(
37
+ "\\A#{DIRECTIVE_HEADER_PATTERN}\\s*\\z".gsub(' ', '\s*')
38
+ )
25
39
 
26
40
  def self.before_comment(line)
27
41
  line.split(DIRECTIVE_COMMENT_REGEXP).first
@@ -32,9 +46,28 @@ module RuboCop
32
46
  def initialize(comment, cop_registry = Cop::Registry.global)
33
47
  @comment = comment
34
48
  @cop_registry = cop_registry
49
+ @match_data = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
35
50
  @mode, @cops = match_captures
36
51
  end
37
52
 
53
+ # Checks if the comment starts with `# rubocop:` marker
54
+ def start_with_marker?
55
+ comment.text.start_with?(DIRECTIVE_MARKER_REGEXP)
56
+ end
57
+
58
+ # Checks if the comment is malformed as a `# rubocop:` directive
59
+ def malformed?
60
+ return true if !start_with_marker? || @match_data.nil?
61
+
62
+ tail = @match_data.post_match.lstrip
63
+ !(tail.empty? || tail.start_with?(TRAILING_COMMENT_MARKER))
64
+ end
65
+
66
+ # Checks if the directive comment is missing a cop name
67
+ def missing_cop_name?
68
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP.match?(comment.text)
69
+ end
70
+
38
71
  # Checks if this directive relates to single line
39
72
  def single_line?
40
73
  !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP)
@@ -55,7 +88,7 @@ module RuboCop
55
88
 
56
89
  # Returns match captures to directive comment pattern
57
90
  def match_captures
58
- @match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures
91
+ @match_captures ||= @match_data&.captures
59
92
  end
60
93
 
61
94
  # Checks if this directive disables cops
@@ -43,7 +43,6 @@ module RuboCop
43
43
  def named_capturing?(exp, event, named)
44
44
  event == :enter &&
45
45
  named == exp.respond_to?(:name) &&
46
- !exp.text.start_with?('(?<=') &&
47
46
  exp.respond_to?(:capturing?) &&
48
47
  exp.capturing?
49
48
  end