rubocop 1.72.1 → 1.81.7

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 (316) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +22 -18
  3. data/config/default.yml +240 -65
  4. data/config/internal_affairs.yml +20 -0
  5. data/config/obsoletion.yml +8 -3
  6. data/exe/rubocop +1 -8
  7. data/lib/rubocop/cli/command/auto_generate_config.rb +2 -2
  8. data/lib/rubocop/cli.rb +19 -4
  9. data/lib/rubocop/config.rb +35 -6
  10. data/lib/rubocop/config_loader.rb +8 -40
  11. data/lib/rubocop/config_loader_resolver.rb +9 -7
  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_store.rb +5 -0
  16. data/lib/rubocop/config_validator.rb +7 -6
  17. data/lib/rubocop/cop/autocorrect_logic.rb +22 -14
  18. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  19. data/lib/rubocop/cop/correctors/alignment_corrector.rb +7 -4
  20. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +7 -2
  21. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +5 -2
  22. data/lib/rubocop/cop/gemspec/attribute_assignment.rb +91 -0
  23. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +37 -15
  24. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  25. data/lib/rubocop/cop/gemspec/require_mfa.rb +15 -1
  26. data/lib/rubocop/cop/internal_affairs/example_description.rb +9 -5
  27. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +4 -4
  28. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +6 -2
  29. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +92 -0
  30. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
  31. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
  32. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +6 -1
  33. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
  34. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  35. data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
  36. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
  37. data/lib/rubocop/cop/layout/class_structure.rb +36 -1
  38. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
  39. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  40. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  41. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  42. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  43. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +32 -14
  44. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
  45. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +34 -4
  46. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
  47. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
  48. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
  49. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  50. data/lib/rubocop/cop/layout/hash_alignment.rb +4 -7
  51. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  52. data/lib/rubocop/cop/layout/leading_comment_space.rb +13 -1
  53. data/lib/rubocop/cop/layout/line_length.rb +43 -10
  54. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
  55. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
  56. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +9 -5
  57. data/lib/rubocop/cop/layout/redundant_line_break.rb +9 -5
  58. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +11 -5
  59. data/lib/rubocop/cop/layout/space_after_semicolon.rb +10 -0
  60. data/lib/rubocop/cop/layout/space_around_keyword.rb +6 -1
  61. data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
  62. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
  63. data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -38
  64. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +12 -3
  65. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
  66. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +3 -0
  67. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  68. data/lib/rubocop/cop/lint/ambiguous_range.rb +5 -0
  69. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +2 -3
  70. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
  71. data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -5
  72. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +3 -2
  73. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +13 -7
  74. data/lib/rubocop/cop/lint/debugger.rb +2 -4
  75. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  76. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +5 -2
  77. data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -23
  78. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
  79. data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
  80. data/lib/rubocop/cop/lint/empty_interpolation.rb +14 -1
  81. data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
  82. data/lib/rubocop/cop/lint/float_comparison.rb +32 -10
  83. data/lib/rubocop/cop/lint/identity_comparison.rb +19 -15
  84. data/lib/rubocop/cop/lint/literal_as_condition.rb +124 -10
  85. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +17 -8
  86. data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
  87. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  88. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
  89. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +1 -0
  90. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
  91. data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
  92. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
  93. data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
  94. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
  95. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +43 -13
  96. data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
  97. data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
  98. data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
  99. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
  100. data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
  101. data/lib/rubocop/cop/lint/return_in_void_context.rb +9 -11
  102. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +4 -4
  103. data/lib/rubocop/cop/lint/self_assignment.rb +31 -5
  104. data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
  105. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -1
  106. data/lib/rubocop/cop/lint/shared_mutable_default.rb +12 -1
  107. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  108. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
  109. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  110. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +2 -0
  111. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  112. data/lib/rubocop/cop/lint/unreachable_loop.rb +5 -5
  113. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
  114. data/lib/rubocop/cop/lint/useless_access_modifier.rb +30 -4
  115. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -0
  116. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +9 -12
  117. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +90 -0
  118. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +1 -0
  119. data/lib/rubocop/cop/lint/useless_or.rb +98 -0
  120. data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
  121. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +3 -3
  122. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
  123. data/lib/rubocop/cop/lint/void.rb +16 -2
  124. data/lib/rubocop/cop/message_annotator.rb +7 -3
  125. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  126. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  127. data/lib/rubocop/cop/metrics/method_length.rb +1 -0
  128. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  129. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  130. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  131. data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -3
  132. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  133. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  134. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
  135. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
  136. data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
  137. data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
  138. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -2
  139. data/lib/rubocop/cop/mixin/gemspec_help.rb +22 -0
  140. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +15 -14
  141. data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
  142. data/lib/rubocop/cop/mixin/line_length_help.rb +24 -8
  143. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
  144. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
  145. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  146. data/lib/rubocop/cop/mixin/range_help.rb +12 -0
  147. data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
  148. data/lib/rubocop/cop/mixin/trailing_comma.rb +18 -2
  149. data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
  150. data/lib/rubocop/cop/naming/file_name.rb +2 -2
  151. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  152. data/lib/rubocop/cop/naming/method_name.rb +187 -15
  153. data/lib/rubocop/cop/naming/predicate_method.rb +319 -0
  154. data/lib/rubocop/cop/naming/{predicate_name.rb → predicate_prefix.rb} +4 -4
  155. data/lib/rubocop/cop/naming/variable_name.rb +51 -6
  156. data/lib/rubocop/cop/registry.rb +9 -6
  157. data/lib/rubocop/cop/security/eval.rb +2 -1
  158. data/lib/rubocop/cop/security/json_load.rb +33 -11
  159. data/lib/rubocop/cop/security/open.rb +1 -0
  160. data/lib/rubocop/cop/style/access_modifier_declarations.rb +32 -10
  161. data/lib/rubocop/cop/style/accessor_grouping.rb +32 -6
  162. data/lib/rubocop/cop/style/arguments_forwarding.rb +21 -24
  163. data/lib/rubocop/cop/style/array_intersect.rb +113 -38
  164. data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
  165. data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
  166. data/lib/rubocop/cop/style/block_delimiters.rb +3 -2
  167. data/lib/rubocop/cop/style/case_like_if.rb +1 -1
  168. data/lib/rubocop/cop/style/class_and_module_children.rb +48 -10
  169. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
  170. data/lib/rubocop/cop/style/collection_methods.rb +1 -0
  171. data/lib/rubocop/cop/style/collection_querying.rb +167 -0
  172. data/lib/rubocop/cop/style/combinable_loops.rb +1 -0
  173. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  174. data/lib/rubocop/cop/style/commented_keyword.rb +12 -5
  175. data/lib/rubocop/cop/style/comparable_between.rb +78 -0
  176. data/lib/rubocop/cop/style/conditional_assignment.rb +26 -8
  177. data/lib/rubocop/cop/style/constant_visibility.rb +14 -9
  178. data/lib/rubocop/cop/style/data_inheritance.rb +7 -0
  179. data/lib/rubocop/cop/style/def_with_parentheses.rb +18 -5
  180. data/lib/rubocop/cop/style/dig_chain.rb +1 -1
  181. data/lib/rubocop/cop/style/double_negation.rb +3 -3
  182. data/lib/rubocop/cop/style/empty_literal.rb +4 -0
  183. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +100 -0
  184. data/lib/rubocop/cop/style/endless_method.rb +176 -18
  185. data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
  186. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
  187. data/lib/rubocop/cop/style/explicit_block_argument.rb +3 -3
  188. data/lib/rubocop/cop/style/exponential_notation.rb +5 -4
  189. data/lib/rubocop/cop/style/fetch_env_var.rb +32 -6
  190. data/lib/rubocop/cop/style/float_division.rb +15 -1
  191. data/lib/rubocop/cop/style/for.rb +1 -0
  192. data/lib/rubocop/cop/style/format_string_token.rb +38 -11
  193. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
  194. data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
  195. data/lib/rubocop/cop/style/guard_clause.rb +2 -1
  196. data/lib/rubocop/cop/style/hash_conversion.rb +16 -8
  197. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -2
  198. data/lib/rubocop/cop/style/hash_fetch_chain.rb +104 -0
  199. data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
  200. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  201. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  202. data/lib/rubocop/cop/style/identical_conditional_branches.rb +3 -3
  203. data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
  204. data/lib/rubocop/cop/style/if_unless_modifier.rb +35 -8
  205. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +4 -7
  206. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
  207. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  208. data/lib/rubocop/cop/style/inverse_methods.rb +10 -6
  209. data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
  210. data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
  211. data/lib/rubocop/cop/style/it_assignment.rb +69 -12
  212. data/lib/rubocop/cop/style/it_block_parameter.rb +121 -0
  213. data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
  214. data/lib/rubocop/cop/style/lambda.rb +1 -0
  215. data/lib/rubocop/cop/style/lambda_call.rb +7 -2
  216. data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
  217. data/lib/rubocop/cop/style/map_into_array.rb +4 -1
  218. data/lib/rubocop/cop/style/map_to_hash.rb +12 -3
  219. data/lib/rubocop/cop/style/map_to_set.rb +1 -3
  220. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +9 -8
  221. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +16 -0
  222. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -1
  223. data/lib/rubocop/cop/style/min_max_comparison.rb +13 -5
  224. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
  225. data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -0
  226. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
  227. data/lib/rubocop/cop/style/next.rb +44 -0
  228. data/lib/rubocop/cop/style/nil_comparison.rb +9 -7
  229. data/lib/rubocop/cop/style/object_then.rb +1 -0
  230. data/lib/rubocop/cop/style/one_line_conditional.rb +17 -9
  231. data/lib/rubocop/cop/style/parallel_assignment.rb +32 -20
  232. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  233. data/lib/rubocop/cop/style/proc.rb +1 -0
  234. data/lib/rubocop/cop/style/raise_args.rb +8 -8
  235. data/lib/rubocop/cop/style/redundant_array_flatten.rb +50 -0
  236. data/lib/rubocop/cop/style/redundant_begin.rb +35 -0
  237. data/lib/rubocop/cop/style/redundant_condition.rb +57 -0
  238. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +14 -4
  239. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  240. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -9
  241. data/lib/rubocop/cop/style/redundant_format.rb +79 -18
  242. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -2
  243. data/lib/rubocop/cop/style/redundant_interpolation.rb +12 -3
  244. data/lib/rubocop/cop/style/redundant_line_continuation.rb +1 -4
  245. data/lib/rubocop/cop/style/redundant_parentheses.rb +73 -18
  246. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -0
  247. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
  248. data/lib/rubocop/cop/style/redundant_self.rb +9 -5
  249. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  250. data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
  251. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  252. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
  253. data/lib/rubocop/cop/style/return_nil.rb +2 -2
  254. data/lib/rubocop/cop/style/safe_navigation.rb +61 -14
  255. data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
  256. data/lib/rubocop/cop/style/semicolon.rb +23 -7
  257. data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
  258. data/lib/rubocop/cop/style/single_line_methods.rb +10 -7
  259. data/lib/rubocop/cop/style/sole_nested_conditional.rb +75 -101
  260. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
  261. data/lib/rubocop/cop/style/string_concatenation.rb +18 -15
  262. data/lib/rubocop/cop/style/struct_inheritance.rb +8 -1
  263. data/lib/rubocop/cop/style/super_arguments.rb +1 -2
  264. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  265. data/lib/rubocop/cop/style/symbol_proc.rb +3 -1
  266. data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -0
  267. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +52 -1
  268. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
  269. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  270. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
  271. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  272. data/lib/rubocop/cop/style/unless_else.rb +10 -9
  273. data/lib/rubocop/cop/team.rb +1 -1
  274. data/lib/rubocop/cop/util.rb +1 -1
  275. data/lib/rubocop/cop/utils/format_string.rb +15 -2
  276. data/lib/rubocop/cop/variable_force/assignment.rb +7 -3
  277. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  278. data/lib/rubocop/cop/variable_force/variable.rb +3 -8
  279. data/lib/rubocop/cop/variable_force.rb +26 -9
  280. data/lib/rubocop/cops_documentation_generator.rb +23 -7
  281. data/lib/rubocop/directive_comment.rb +1 -1
  282. data/lib/rubocop/ext/regexp_node.rb +0 -1
  283. data/lib/rubocop/formatter/disabled_config_formatter.rb +19 -5
  284. data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
  285. data/lib/rubocop/formatter/html_formatter.rb +1 -1
  286. data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
  287. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  288. data/lib/rubocop/formatter/pacman_formatter.rb +2 -1
  289. data/lib/rubocop/lsp/diagnostic.rb +25 -24
  290. data/lib/rubocop/lsp/routes.rb +65 -9
  291. data/lib/rubocop/lsp/runtime.rb +5 -5
  292. data/lib/rubocop/lsp/server.rb +2 -2
  293. data/lib/rubocop/lsp/stdin_runner.rb +3 -17
  294. data/lib/rubocop/magic_comment.rb +8 -0
  295. data/lib/rubocop/pending_cops_reporter.rb +56 -0
  296. data/lib/rubocop/plugin/configuration_integrator.rb +2 -0
  297. data/lib/rubocop/plugin/load_error.rb +1 -1
  298. data/lib/rubocop/plugin.rb +9 -2
  299. data/lib/rubocop/result_cache.rb +14 -12
  300. data/lib/rubocop/rspec/cop_helper.rb +6 -1
  301. data/lib/rubocop/rspec/expect_offense.rb +9 -3
  302. data/lib/rubocop/rspec/shared_contexts.rb +34 -0
  303. data/lib/rubocop/rspec/support.rb +3 -0
  304. data/lib/rubocop/runner.rb +10 -4
  305. data/lib/rubocop/server/cache.rb +17 -12
  306. data/lib/rubocop/server/client_command/base.rb +10 -0
  307. data/lib/rubocop/server/client_command/exec.rb +2 -1
  308. data/lib/rubocop/server/client_command/start.rb +11 -1
  309. data/lib/rubocop/target_finder.rb +13 -9
  310. data/lib/rubocop/target_ruby.rb +11 -2
  311. data/lib/rubocop/version.rb +14 -7
  312. data/lib/rubocop.rb +17 -2
  313. data/lib/ruby_lsp/rubocop/addon.rb +25 -10
  314. data/lib/ruby_lsp/rubocop/runtime_adapter.rb +57 -5
  315. metadata +24 -8
  316. data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
@@ -69,10 +69,11 @@ module RuboCop
69
69
 
70
70
  def each_semicolon
71
71
  tokens_for_lines.each do |line, tokens|
72
- semicolon_pos = semicolon_position(tokens)
72
+ next unless (semicolon_pos = semicolon_position(tokens))
73
+
73
74
  after_expr_pos = semicolon_pos == -1 ? -2 : semicolon_pos
74
75
 
75
- yield line, tokens[semicolon_pos].column, tokens[after_expr_pos] if semicolon_pos
76
+ yield line, tokens[semicolon_pos].column, tokens[after_expr_pos]
76
77
  end
77
78
  end
78
79
 
@@ -119,6 +120,7 @@ module RuboCop
119
120
  tokens[1]&.type == :tSTRING_DBEG && tokens[2]&.semicolon?
120
121
  end
121
122
 
123
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
122
124
  def register_semicolon(line, column, after_expression, token_before_semicolon = nil)
123
125
  range = source_range(processed_source.buffer, line, column)
124
126
 
@@ -130,14 +132,19 @@ module RuboCop
130
132
  # without parentheses.
131
133
  # See: https://github.com/rubocop/rubocop/issues/10791
132
134
  if token_before_semicolon&.regexp_dots?
133
- range_node = find_range_node(token_before_semicolon)
134
- corrector.wrap(range_node, '(', ')') if range_node
135
+ node = find_node(range_nodes, token_before_semicolon)
136
+ elsif token_before_semicolon&.type == :tLABEL
137
+ node = find_node(value_omission_pair_nodes, token_before_semicolon).parent
138
+ space = node.parent.loc.selector.end.join(node.source_range.begin)
139
+ corrector.remove(space)
135
140
  end
136
141
 
142
+ corrector.wrap(node, '(', ')') if node
137
143
  corrector.remove(range)
138
144
  end
139
145
  end
140
146
  end
147
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
141
148
 
142
149
  def expressions_per_line(exprs)
143
150
  # create a map matching lines to the number of expressions on them
@@ -153,9 +160,9 @@ module RuboCop
153
160
  end
154
161
  end
155
162
 
156
- def find_range_node(token_before_semicolon)
157
- range_nodes.detect do |range_node|
158
- range_node.source_range.contains?(token_before_semicolon.pos)
163
+ def find_node(nodes, token_before_semicolon)
164
+ nodes.detect do |node|
165
+ node.source_range.overlaps?(token_before_semicolon.pos)
159
166
  end
160
167
  end
161
168
 
@@ -166,6 +173,15 @@ module RuboCop
166
173
  @range_nodes = ast.range_type? ? [ast] : []
167
174
  @range_nodes.concat(ast.each_descendant(:range).to_a)
168
175
  end
176
+
177
+ def value_omission_pair_nodes
178
+ if instance_variable_defined?(:@value_omission_pair_nodes)
179
+ return @value_omission_pair_nodes
180
+ end
181
+
182
+ ast = processed_source.ast
183
+ @value_omission_pair_nodes = ast.each_descendant(:pair).to_a.select(&:value_omission?)
184
+ end
169
185
  end
170
186
  end
171
187
  end
@@ -56,11 +56,13 @@ module RuboCop
56
56
  end
57
57
  # rubocop:enable Metrics/AbcSize
58
58
  alias on_numblock on_block
59
+ alias on_itblock on_block
59
60
 
60
61
  private
61
62
 
62
63
  def do_line(node)
63
- if node.numblock_type? || node.arguments.children.empty? || node.send_node.lambda_literal?
64
+ if node.type?(:numblock, :itblock) ||
65
+ node.arguments.children.empty? || node.send_node.lambda_literal?
64
66
  node.loc.begin
65
67
  else
66
68
  node.arguments
@@ -8,9 +8,9 @@ module RuboCop
8
8
  #
9
9
  # Endless methods added in Ruby 3.0 are also accepted by this cop.
10
10
  #
11
- # If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line` or
12
- # `allow_always`, single-line methods will be autocorrected to endless
13
- # methods if there is only one statement in the body.
11
+ # If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line`, `allow_always`,
12
+ # `require_single_line`, or `require_always`, single-line methods will be autocorrected
13
+ # to endless methods if there is only one statement in the body.
14
14
  #
15
15
  # @example
16
16
  # # bad
@@ -65,7 +65,7 @@ module RuboCop
65
65
  return false if target_ruby_version < 3.0
66
66
  return false if disallow_endless_method_style?
67
67
  return false unless body_node
68
- return false if body_node.parent.assignment_method? ||
68
+ return false if body_node.basic_conditional? || body_node.parent.assignment_method? ||
69
69
  NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES.include?(body_node.type)
70
70
 
71
71
  !body_node.type?(:begin, :kwbegin)
@@ -86,10 +86,10 @@ module RuboCop
86
86
  end
87
87
 
88
88
  def correct_to_endless(corrector, node)
89
- self_receiver = node.self_receiver? ? 'self.' : ''
89
+ receiver = "#{node.receiver.source}." if node.receiver
90
90
  arguments = node.arguments.any? ? node.arguments.source : '()'
91
91
  body_source = method_body_source(node.body)
92
- replacement = "def #{self_receiver}#{node.method_name}#{arguments} = #{body_source}"
92
+ replacement = "def #{receiver}#{node.method_name}#{arguments} = #{body_source}"
93
93
 
94
94
  corrector.replace(node, replacement)
95
95
  end
@@ -130,7 +130,10 @@ module RuboCop
130
130
  end
131
131
 
132
132
  def require_parentheses?(method_body)
133
- method_body.send_type? && !method_body.arguments.empty? && !method_body.comparison_method?
133
+ return false unless method_body.send_type?
134
+ return false if method_body.arithmetic_operation?
135
+
136
+ !method_body.arguments.empty? && !method_body.comparison_method?
134
137
  end
135
138
 
136
139
  def disallow_endless_method_style?
@@ -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,56 @@ 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? && !part_of_ignored_node?(node)
119
+ corrector.replace(node.condition, chainable_condition(node))
120
+ ignore_node(node)
134
121
  end
135
122
 
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)}")
123
+ def correct_for_guard_condition_style(corrector, node, if_branch)
124
+ corrector.insert_after(node.condition, " && #{chainable_condition(if_branch)}")
139
125
 
140
- range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
126
+ range = range_between(
127
+ if_branch.loc.keyword.begin_pos, if_branch.condition.source_range.end_pos
128
+ )
141
129
  corrector.remove(range_with_surrounding_space(range, newlines: false))
142
- corrector.remove(if_branch.loc.keyword)
143
130
  end
144
131
 
145
- def correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
132
+ # rubocop:disable Metrics/AbcSize
133
+ def correct_for_basic_condition_style(corrector, node, if_branch)
146
134
  range = range_between(
147
135
  node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos
148
136
  )
149
- corrector.replace(range, and_operator)
150
- corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
151
-
152
- wrap_condition(corrector, if_branch.condition)
153
- end
154
-
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)
137
+ corrector.replace(range, ' && ')
161
138
 
162
- if condition.call_type?
163
- source = parenthesized_method_arguments(condition)
139
+ corrector.replace(if_branch.condition, chainable_condition(if_branch))
164
140
 
165
- corrector.replace(condition, source)
166
- else
167
- corrector.wrap(condition, '(', ')')
168
- end
141
+ end_range = if same_line?(node.loc.end, node.if_branch.loc.end)
142
+ node.loc.end
143
+ else
144
+ range_by_whole_lines(node.loc.end, include_final_newline: true)
145
+ end
146
+ corrector.remove(end_range)
169
147
  end
148
+ # rubocop:enable Metrics/AbcSize
170
149
 
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)} && ")
150
+ def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
151
+ correct_node(corrector, if_branch)
152
+
153
+ corrector.insert_before(if_branch.condition, "#{chainable_condition(node)} && ")
175
154
 
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')
155
+ range = range_between(node.loc.keyword.begin_pos, node.condition.source_range.end_pos)
156
+ corrector.remove(range_with_surrounding_space(range, newlines: false))
179
157
  end
180
158
 
181
159
  def correct_for_comment(corrector, node, if_branch)
@@ -187,67 +165,47 @@ module RuboCop
187
165
  corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
188
166
  end
189
167
 
190
- def correct_outer_condition(corrector, condition)
191
- return unless require_parentheses?(condition)
168
+ def chainable_condition(node)
169
+ wrapped_condition = add_parentheses_if_needed(node.condition)
192
170
 
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
171
+ return wrapped_condition if node.if?
196
172
 
197
- range = range_between(end_pos, begin_pos)
198
- corrector.remove(range)
199
- corrector.insert_after(range, '(')
200
- corrector.insert_after(condition.last_argument, ')')
173
+ node.condition.and_type? ? "!(#{wrapped_condition})" : "!#{wrapped_condition}"
201
174
  end
202
175
 
203
- def insert_bang(corrector, node, is_modify_form)
204
- condition = node.condition
176
+ def add_parentheses_if_needed(condition)
177
+ # Handle `send` and `block` nodes that need to be wrapped in parens
178
+ # FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
179
+ # but wrapping the argument list would be a more ergonomic correction.
180
+ node_to_check = condition&.any_block_type? ? condition.send_node : condition
181
+ return condition.source unless add_parentheses?(node_to_check)
205
182
 
206
- if (condition.send_type? && condition.comparison_method? && !condition.parenthesized?) ||
207
- (is_modify_form && wrap_condition?(condition))
208
- corrector.wrap(node.condition, '!(', ')')
183
+ if parenthesize_method?(condition)
184
+ parenthesized_method_arguments(condition)
209
185
  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
218
-
219
- if lhs.and_type?
220
- insert_bang_for_and(corrector, lhs)
221
- corrector.insert_before(rhs, '!') if rhs
186
+ parenthesized_and(condition)
222
187
  else
223
- corrector.insert_before(lhs, '!')
224
- corrector.insert_before(rhs, '!')
188
+ "(#{condition.source})"
225
189
  end
226
190
  end
227
191
 
228
- def require_parentheses?(condition)
229
- condition.call_type? && !condition.arguments.empty? && !condition.parenthesized? &&
230
- !condition.comparison_method?
192
+ def parenthesize_method?(node)
193
+ node.call_type? && node.arguments.any? && !node.parenthesized? &&
194
+ !node.comparison_method? && !node.operator_method?
231
195
  end
232
196
 
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
197
+ def add_parentheses?(node)
198
+ return true if node.assignment? || node.or_type?
199
+ return true if assignment_in_and?(node)
200
+ return false unless node.call_type?
238
201
 
239
- def wrap_condition?(node)
240
- node.operator_keyword? || (node.call_type? && node.arguments.any? && !node.parenthesized?)
202
+ (node.arguments.any? && !node.parenthesized?) || node.prefix_not?
241
203
  end
242
204
 
243
- def replace_condition(condition)
244
- return condition.source unless wrap_condition?(condition)
205
+ def assignment_in_and?(node)
206
+ return false unless node.and_type?
245
207
 
246
- if condition.call_type? && !condition.comparison_method?
247
- parenthesized_method_arguments(condition)
248
- else
249
- "(#{condition.source})"
250
- end
208
+ node.each_descendant.any?(&:assignment?)
251
209
  end
252
210
 
253
211
  def parenthesized_method_arguments(node)
@@ -257,12 +215,28 @@ module RuboCop
257
215
  "#{method_call}(#{arguments})"
258
216
  end
259
217
 
260
- def allow_modifier?
261
- cop_config['AllowModifier']
218
+ def parenthesized_and(node)
219
+ # We only need to add parentheses around the last clause if it's an assignment,
220
+ # because other clauses will be unchanged by merging conditionals.
221
+ lhs = node.lhs.source
222
+ rhs = parenthesized_and_clause(node.rhs)
223
+ operator = range_with_surrounding_space(node.loc.operator, whitespace: true).source
224
+
225
+ "#{lhs}#{operator}#{rhs}"
262
226
  end
263
227
 
264
- def outer_condition_modify_form?(node, if_branch)
265
- node.condition.source_range.begin_pos > if_branch.condition.source_range.begin_pos
228
+ def parenthesized_and_clause(node)
229
+ if node.and_type?
230
+ parenthesized_and(node)
231
+ elsif node.assignment?
232
+ "(#{node.source})"
233
+ else
234
+ node.source
235
+ end
236
+ end
237
+
238
+ def allow_modifier?
239
+ cop_config['AllowModifier']
266
240
  end
267
241
  end
268
242
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Check for parentheses around stabby lambda arguments.
6
+ # Checks for parentheses around stabby lambda arguments.
7
7
  # There are two different styles. Defaults to `require_parentheses`.
8
8
  #
9
9
  # @example EnforcedStyle: require_parentheses (default)
@@ -51,7 +51,6 @@ module RuboCop
51
51
  # Pathname.new('/') + 'test'
52
52
  #
53
53
  class StringConcatenation < Base
54
- include RangeHelp
55
54
  extend AutoCorrector
56
55
 
57
56
  MSG = 'Prefer string interpolation to string concatenation.'
@@ -101,7 +100,7 @@ module RuboCop
101
100
  node.receiver.str_type? &&
102
101
  node.first_argument.str_type? &&
103
102
  node.multiline? &&
104
- node.source =~ /\+\s*\n/
103
+ node.source.match?(/\+\s*\n/)
105
104
  end
106
105
 
107
106
  def find_topmost_plus_node(node)
@@ -128,7 +127,7 @@ module RuboCop
128
127
  end
129
128
 
130
129
  def uncorrectable?(part)
131
- part.multiline? || heredoc?(part) || part.each_descendant(:block).any?
130
+ part.multiline? || heredoc?(part) || part.each_descendant(:any_block).any?
132
131
  end
133
132
 
134
133
  def heredoc?(node)
@@ -142,22 +141,26 @@ module RuboCop
142
141
  end
143
142
 
144
143
  def replacement(parts)
145
- interpolated_parts = parts.map do |part|
146
- case part.type
147
- when :str
148
- adjust_str(part)
149
- when :dstr
150
- part.children.all?(&:str_type?) ? adjust_str(part) : contents_range(part).source
151
- else
152
- "\#{#{part.source}}"
153
- end
154
- end
144
+ interpolated_parts = parts.map { |part| adjust_str(part) }
155
145
 
156
146
  "\"#{handle_quotes(interpolated_parts).join}\""
157
147
  end
158
148
 
159
- def adjust_str(node)
160
- single_quoted?(node) ? node.value.gsub(/(\\|")/, '\\\\\&') : node.value.inspect[1..-2]
149
+ def adjust_str(part)
150
+ case part.type
151
+ when :str
152
+ if single_quoted?(part)
153
+ part.value.gsub(/(\\|"|#\{|#@|#\$)/, '\\\\\&')
154
+ else
155
+ part.value.inspect[1..-2]
156
+ end
157
+ when :dstr, :begin
158
+ part.children.map do |child|
159
+ adjust_str(child)
160
+ end.join
161
+ else
162
+ "\#{#{part.source}}"
163
+ end
161
164
  end
162
165
 
163
166
  def handle_quotes(parts)
@@ -3,7 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for inheritance from Struct.new.
6
+ # Checks for inheritance from `Struct.new`. Inheriting from `Struct.new`
7
+ # adds a superfluous level in inheritance tree.
7
8
  #
8
9
  # @safety
9
10
  # Autocorrection is unsafe because it will change the inheritance
@@ -17,12 +18,18 @@ module RuboCop
17
18
  # end
18
19
  # end
19
20
  #
21
+ # Person.ancestors
22
+ # # => [Person, #<Class:0x000000010b4e14a0>, Struct, (...)]
23
+ #
20
24
  # # good
21
25
  # Person = Struct.new(:first_name, :last_name) do
22
26
  # def age
23
27
  # 42
24
28
  # end
25
29
  # end
30
+ #
31
+ # Person.ancestors
32
+ # # => [Person, Struct, (...)]
26
33
  class StructInheritance < Base
27
34
  include RangeHelp
28
35
  extend AutoCorrector
@@ -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
 
@@ -81,7 +81,7 @@ module RuboCop
81
81
 
82
82
  content = *sym
83
83
  content = content.map { |c| c.is_a?(AST::Node) ? c.source : c }.join
84
- content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s\(\)]*\))/, '')
84
+ content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s()]*\))/, '')
85
85
 
86
86
  content.include?(' ') || DELIMITERS.any? do |delimiter|
87
87
  content_without_delimiter_pairs.include?(delimiter)
@@ -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?(',')
@@ -268,7 +270,7 @@ module RuboCop
268
270
  end
269
271
 
270
272
  def allow_if_method_has_argument?(send_node)
271
- !!cop_config.fetch('AllowMethodsWithArguments', false) && !send_node.arguments.count.zero?
273
+ !!cop_config.fetch('AllowMethodsWithArguments', false) && send_node.arguments.any?
272
274
  end
273
275
 
274
276
  def allow_comments?
@@ -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
 
@@ -10,6 +10,9 @@ module RuboCop
10
10
  # for all parenthesized multi-line method calls with arguments.
11
11
  # * `comma`: Requires a comma after the last argument, but only for
12
12
  # parenthesized method calls where each argument is on its own line.
13
+ # * `diff_comma`: Requires a comma after the last argument, but only
14
+ # when that argument is followed by an immediate newline, even if
15
+ # there is an inline comment on the same line.
13
16
  # * `no_comma`: Requires that there is no comma after the last
14
17
  # argument.
15
18
  #
@@ -75,14 +78,62 @@ module RuboCop
75
78
  # 2,
76
79
  # )
77
80
  #
81
+ # @example EnforcedStyleForMultiline: diff_comma
82
+ # # bad
83
+ # method(1, 2,)
84
+ #
85
+ # # good
86
+ # method(1, 2)
87
+ #
88
+ # # good
89
+ # method(
90
+ # 1, 2,
91
+ # 3,
92
+ # )
93
+ #
94
+ # # good
95
+ # method(
96
+ # 1, 2, 3,
97
+ # )
98
+ #
99
+ # # good
100
+ # method(
101
+ # 1,
102
+ # 2,
103
+ # )
104
+ #
105
+ # # bad
106
+ # method(1, [
107
+ # 2,
108
+ # ],)
109
+ #
110
+ # # good
111
+ # method(1, [
112
+ # 2,
113
+ # ])
114
+ #
115
+ # # bad
116
+ # object[1, 2,
117
+ # 3, 4,]
118
+ #
119
+ # # good
120
+ # object[1, 2,
121
+ # 3, 4]
122
+ #
78
123
  # @example EnforcedStyleForMultiline: no_comma (default)
79
124
  # # bad
80
125
  # method(1, 2,)
81
126
  #
127
+ # # bad
128
+ # object[1, 2,]
129
+ #
82
130
  # # good
83
131
  # method(1, 2)
84
132
  #
85
133
  # # good
134
+ # object[1, 2]
135
+ #
136
+ # # good
86
137
  # method(
87
138
  # 1,
88
139
  # 2
@@ -96,7 +147,7 @@ module RuboCop
96
147
  end
97
148
 
98
149
  def on_send(node)
99
- return unless node.arguments? && node.parenthesized?
150
+ return unless node.arguments? && (node.parenthesized? || node.method?(:[]))
100
151
 
101
152
  check(node, node.arguments, 'parameter of %<article>s method call',
102
153
  node.last_argument.source_range.end_pos,