rubocop 1.90.0 → 1.92.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (303) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +241 -11
  4. data/config/obsoletion.yml +9 -0
  5. data/lib/rubocop/cached_data.rb +47 -7
  6. data/lib/rubocop/changed_files.rb +134 -0
  7. data/lib/rubocop/cli/command/cop_names.rb +57 -0
  8. data/lib/rubocop/cli/command/execute_runner.rb +23 -3
  9. data/lib/rubocop/cli/command/explain.rb +34 -0
  10. data/lib/rubocop/cli/command/show_cops.rb +6 -0
  11. data/lib/rubocop/cli/command/show_docs_url.rb +6 -4
  12. data/lib/rubocop/cli.rb +11 -4
  13. data/lib/rubocop/comment_config/directive_range.rb +3 -1
  14. data/lib/rubocop/comment_config/disable_next.rb +49 -3
  15. data/lib/rubocop/comment_config/push_pop.rb +55 -0
  16. data/lib/rubocop/comment_config.rb +32 -53
  17. data/lib/rubocop/config.rb +10 -0
  18. data/lib/rubocop/config_loader.rb +18 -7
  19. data/lib/rubocop/config_loader_resolver.rb +79 -12
  20. data/lib/rubocop/config_obsoletion/cop_rule.rb +2 -1
  21. data/lib/rubocop/config_obsoletion/removed_cop.rb +9 -1
  22. data/lib/rubocop/config_obsoletion.rb +1 -1
  23. data/lib/rubocop/config_validator.rb +29 -3
  24. data/lib/rubocop/cop/autocorrect_logic.rb +10 -5
  25. data/lib/rubocop/cop/badge.rb +1 -1
  26. data/lib/rubocop/cop/base.rb +57 -24
  27. data/lib/rubocop/cop/bundler/gem_comment.rb +0 -1
  28. data/lib/rubocop/cop/bundler/ordered_gems.rb +0 -8
  29. data/lib/rubocop/cop/cop_documentation.rb +81 -0
  30. data/lib/rubocop/cop/cop_explanation.rb +140 -0
  31. data/lib/rubocop/cop/correctors/alignment_corrector.rb +13 -1
  32. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +22 -7
  33. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +23 -2
  35. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +45 -8
  36. data/lib/rubocop/cop/correctors.rb +2 -2
  37. data/lib/rubocop/cop/documentation.rb +1 -1
  38. data/lib/rubocop/cop/gemspec/dependency_version.rb +0 -6
  39. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +0 -6
  40. data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
  41. data/lib/rubocop/cop/generator.rb +1 -1
  42. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -5
  43. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +0 -7
  44. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
  45. data/lib/rubocop/cop/internal_affairs/redundant_context_config_parameter.rb +1 -7
  46. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +1 -7
  47. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -0
  48. data/lib/rubocop/cop/layout/block_alignment.rb +27 -0
  49. data/lib/rubocop/cop/layout/case_indentation.rb +25 -9
  50. data/lib/rubocop/cop/layout/class_structure.rb +9 -1
  51. data/lib/rubocop/cop/layout/comment_indentation.rb +8 -5
  52. data/lib/rubocop/cop/layout/def_end_alignment.rb +4 -0
  53. data/lib/rubocop/cop/layout/else_alignment.rb +1 -0
  54. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +14 -5
  55. data/lib/rubocop/cop/layout/empty_lines.rb +5 -2
  56. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +1 -1
  57. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
  58. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -1
  59. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +23 -1
  60. data/lib/rubocop/cop/layout/end_alignment.rb +16 -10
  61. data/lib/rubocop/cop/layout/extra_spacing.rb +4 -0
  62. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -0
  63. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +17 -4
  64. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +5 -3
  65. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +6 -0
  66. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +4 -0
  67. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +11 -0
  68. data/lib/rubocop/cop/layout/hash_alignment.rb +15 -10
  69. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -0
  70. data/lib/rubocop/cop/layout/heredoc_indentation.rb +15 -0
  71. data/lib/rubocop/cop/layout/indentation_style.rb +4 -1
  72. data/lib/rubocop/cop/layout/indentation_width.rb +4 -4
  73. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +8 -1
  74. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +25 -9
  75. data/lib/rubocop/cop/layout/line_length.rb +71 -19
  76. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +12 -0
  77. data/lib/rubocop/cop/layout/multiline_block_layout.rb +8 -1
  78. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +7 -0
  79. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +6 -0
  80. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +14 -0
  81. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +4 -0
  82. data/lib/rubocop/cop/layout/redundant_line_break.rb +22 -1
  83. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +15 -2
  84. data/lib/rubocop/cop/layout/single_line_block_chain.rb +10 -2
  85. data/lib/rubocop/cop/layout/space_after_not.rb +5 -0
  86. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +10 -4
  87. data/lib/rubocop/cop/layout/space_around_keyword.rb +10 -0
  88. data/lib/rubocop/cop/layout/space_around_operators.rb +5 -1
  89. data/lib/rubocop/cop/layout/space_before_block_braces.rb +4 -1
  90. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -2
  91. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +31 -8
  92. data/lib/rubocop/cop/lint/ambiguous_operator.rb +1 -1
  93. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +2 -1
  94. data/lib/rubocop/cop/lint/argument_mismatch.rb +14 -4
  95. data/lib/rubocop/cop/lint/assignment_in_condition.rb +2 -0
  96. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +88 -14
  97. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
  98. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  99. data/lib/rubocop/cop/lint/empty_conditional_body.rb +3 -1
  100. data/lib/rubocop/cop/lint/literal_as_condition.rb +23 -2
  101. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +63 -19
  102. data/lib/rubocop/cop/lint/loop.rb +24 -2
  103. data/lib/rubocop/cop/lint/misplaced_magic_comment.rb +245 -0
  104. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +19 -4
  105. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  106. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +1 -1
  107. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  108. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +17 -1
  109. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +42 -17
  110. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +13 -2
  111. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +18 -1
  112. data/lib/rubocop/cop/lint/useless_assignment.rb +14 -7
  113. data/lib/rubocop/cop/lint/void.rb +8 -1
  114. data/lib/rubocop/cop/lint.rb +1 -0
  115. data/lib/rubocop/cop/metrics/class_length.rb +4 -2
  116. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +0 -4
  117. data/lib/rubocop/cop/metrics/module_length.rb +4 -2
  118. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  119. data/lib/rubocop/cop/mixin/alignment.rb +5 -1
  120. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  121. data/lib/rubocop/cop/mixin/check_line_breakable.rb +17 -14
  122. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +5 -1
  123. data/lib/rubocop/cop/mixin/code_length.rb +10 -2
  124. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +12 -1
  125. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +69 -0
  126. data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +2 -2
  127. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +26 -2
  128. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +31 -3
  129. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  130. data/lib/rubocop/cop/mixin/min_branches_count.rb +3 -1
  131. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +11 -1
  132. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +9 -0
  133. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  134. data/lib/rubocop/cop/mixin/pattern_matching_help.rb +35 -0
  135. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +29 -3
  136. data/lib/rubocop/cop/mixin/project_index_help.rb +15 -0
  137. data/lib/rubocop/cop/mixin/reparsed_equivalence.rb +13 -3
  138. data/lib/rubocop/cop/mixin/statement_modifier.rb +22 -2
  139. data/lib/rubocop/cop/mixin.rb +4 -2
  140. data/lib/rubocop/cop/naming/accessor_method_name.rb +0 -16
  141. data/lib/rubocop/cop/naming/block_forwarding.rb +4 -1
  142. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  143. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +9 -2
  144. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -3
  145. data/lib/rubocop/cop/naming/predicate_prefix.rb +7 -22
  146. data/lib/rubocop/cop/offense.rb +59 -3
  147. data/lib/rubocop/cop/registry.rb +10 -2
  148. data/lib/rubocop/cop/security/json_load.rb +16 -4
  149. data/lib/rubocop/cop/severity.rb +12 -0
  150. data/lib/rubocop/cop/style/access_modifier_declarations.rb +10 -2
  151. data/lib/rubocop/cop/style/alias.rb +18 -5
  152. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +4 -3
  153. data/lib/rubocop/cop/style/and_or.rb +4 -0
  154. data/lib/rubocop/cop/style/arguments_forwarding.rb +6 -3
  155. data/lib/rubocop/cop/style/attr.rb +3 -4
  156. data/lib/rubocop/cop/style/bitwise_predicate.rb +6 -4
  157. data/lib/rubocop/cop/style/block_delimiters.rb +29 -3
  158. data/lib/rubocop/cop/style/case_like_if.rb +9 -6
  159. data/lib/rubocop/cop/style/class_and_module_children.rb +54 -16
  160. data/lib/rubocop/cop/style/class_methods_definitions.rb +8 -0
  161. data/lib/rubocop/cop/style/combinable_loops.rb +85 -11
  162. data/lib/rubocop/cop/style/concat_array_literals.rb +46 -7
  163. data/lib/rubocop/cop/style/conditional_assignment.rb +4 -0
  164. data/lib/rubocop/cop/style/constant_visibility.rb +6 -2
  165. data/lib/rubocop/cop/style/data_inheritance.rb +14 -0
  166. data/lib/rubocop/cop/style/directive_scope.rb +154 -21
  167. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +118 -18
  168. data/lib/rubocop/cop/style/documentation_method.rb +1 -9
  169. data/lib/rubocop/cop/style/double_negation.rb +11 -0
  170. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  171. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  172. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  173. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  174. data/lib/rubocop/cop/style/empty_method.rb +15 -3
  175. data/lib/rubocop/cop/style/endless_method.rb +91 -11
  176. data/lib/rubocop/cop/style/exact_regexp_match.rb +45 -8
  177. data/lib/rubocop/cop/style/explicit_block_argument.rb +50 -10
  178. data/lib/rubocop/cop/style/file_null.rb +2 -2
  179. data/lib/rubocop/cop/style/float_division.rb +28 -14
  180. data/lib/rubocop/cop/style/for.rb +44 -1
  181. data/lib/rubocop/cop/style/format_string.rb +28 -6
  182. data/lib/rubocop/cop/style/format_string_token.rb +1 -1
  183. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +2 -1
  184. data/lib/rubocop/cop/style/guard_clause.rb +13 -1
  185. data/lib/rubocop/cop/style/hash_conversion.rb +28 -8
  186. data/lib/rubocop/cop/style/hash_lookup_method.rb +35 -9
  187. data/lib/rubocop/cop/style/hash_syntax.rb +16 -3
  188. data/lib/rubocop/cop/style/identical_conditional_branches.rb +22 -3
  189. data/lib/rubocop/cop/style/if_inside_else.rb +21 -1
  190. data/lib/rubocop/cop/style/if_unless_modifier.rb +19 -20
  191. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +4 -0
  192. data/lib/rubocop/cop/style/if_with_semicolon.rb +47 -4
  193. data/lib/rubocop/cop/style/inverse_methods.rb +24 -2
  194. data/lib/rubocop/cop/style/it_block_parameter.rb +32 -3
  195. data/lib/rubocop/cop/style/keyword_parameters_order.rb +3 -1
  196. data/lib/rubocop/cop/style/lambda.rb +18 -0
  197. data/lib/rubocop/cop/style/lambda_call.rb +7 -2
  198. data/lib/rubocop/cop/style/magic_comment_format.rb +1 -1
  199. data/lib/rubocop/cop/style/map_into_array.rb +1 -2
  200. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +45 -10
  201. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +25 -2
  202. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +26 -3
  203. data/lib/rubocop/cop/style/method_def_parentheses.rb +29 -3
  204. data/lib/rubocop/cop/style/missing_else.rb +7 -1
  205. data/lib/rubocop/cop/style/multiline_if_modifier.rb +45 -5
  206. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +7 -0
  207. data/lib/rubocop/cop/style/multiline_method_signature.rb +2 -5
  208. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +7 -3
  209. data/lib/rubocop/cop/style/multiline_when_then.rb +7 -0
  210. data/lib/rubocop/cop/style/mutable_constant.rb +28 -1
  211. data/lib/rubocop/cop/style/negated_if_else_condition.rb +2 -1
  212. data/lib/rubocop/cop/style/negative_array_index.rb +9 -1
  213. data/lib/rubocop/cop/style/nested_modifier.rb +36 -12
  214. data/lib/rubocop/cop/style/nested_ternary_operator.rb +26 -8
  215. data/lib/rubocop/cop/style/next.rb +24 -10
  216. data/lib/rubocop/cop/style/nil_comparison.rb +48 -8
  217. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  218. data/lib/rubocop/cop/style/non_nil_check.rb +18 -2
  219. data/lib/rubocop/cop/style/not.rb +10 -3
  220. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +11 -1
  221. data/lib/rubocop/cop/style/numeric_predicate.rb +21 -5
  222. data/lib/rubocop/cop/style/one_line_conditional.rb +14 -3
  223. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
  224. data/lib/rubocop/cop/style/parentheses_around_condition.rb +6 -1
  225. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +14 -3
  226. data/lib/rubocop/cop/style/reduce_to_hash.rb +17 -1
  227. data/lib/rubocop/cop/style/redundant_begin.rb +20 -3
  228. data/lib/rubocop/cop/style/redundant_condition.rb +34 -17
  229. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -1
  230. data/lib/rubocop/cop/style/redundant_exception.rb +15 -3
  231. data/lib/rubocop/cop/style/redundant_fetch_block.rb +10 -2
  232. data/lib/rubocop/cop/style/redundant_freeze.rb +10 -1
  233. data/lib/rubocop/cop/style/redundant_parentheses.rb +23 -2
  234. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +7 -0
  235. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +6 -5
  236. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +25 -5
  237. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +4 -0
  238. data/lib/rubocop/cop/style/regexp_literal.rb +75 -26
  239. data/lib/rubocop/cop/style/require_order.rb +9 -0
  240. data/lib/rubocop/cop/style/rescue_modifier.rb +5 -9
  241. data/lib/rubocop/cop/style/return_nil.rb +17 -2
  242. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +11 -1
  243. data/lib/rubocop/cop/style/sample.rb +9 -8
  244. data/lib/rubocop/cop/style/self_assignment.rb +12 -15
  245. data/lib/rubocop/cop/style/semicolon.rb +16 -5
  246. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  247. data/lib/rubocop/cop/style/single_line_methods.rb +16 -4
  248. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  249. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +29 -5
  250. data/lib/rubocop/cop/style/string_concatenation.rb +9 -3
  251. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  252. data/lib/rubocop/cop/style/super_arguments.rb +3 -0
  253. data/lib/rubocop/cop/style/symbol_proc.rb +23 -5
  254. data/lib/rubocop/cop/style/ternary_parentheses.rb +23 -1
  255. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  256. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +23 -19
  257. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  258. data/lib/rubocop/cop/style/yoda_condition.rb +24 -9
  259. data/lib/rubocop/cop/style/yoda_expression.rb +10 -1
  260. data/lib/rubocop/cop/style.rb +0 -1
  261. data/lib/rubocop/cop/team.rb +10 -5
  262. data/lib/rubocop/cop/util.rb +1 -1
  263. data/lib/rubocop/cop/variable_force/assignment.rb +1 -1
  264. data/lib/rubocop/cop/variable_force/variable.rb +14 -1
  265. data/lib/rubocop/cops_documentation_generator.rb +23 -2
  266. data/lib/rubocop/directive_comment.rb +37 -8
  267. data/lib/rubocop/formatter/base_formatter.rb +22 -0
  268. data/lib/rubocop/formatter/disabled_config_formatter.rb +19 -1
  269. data/lib/rubocop/formatter/formatter_set.rb +6 -1
  270. data/lib/rubocop/formatter/github_actions_formatter.rb +6 -5
  271. data/lib/rubocop/formatter/html_formatter.rb +2 -2
  272. data/lib/rubocop/formatter/json_formatter.rb +71 -1
  273. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  274. data/lib/rubocop/formatter/offense_count_formatter.rb +3 -1
  275. data/lib/rubocop/formatter/pacman_formatter.rb +3 -1
  276. data/lib/rubocop/formatter/sarif_formatter.rb +186 -0
  277. data/lib/rubocop/formatter/worst_offenders_formatter.rb +3 -1
  278. data/lib/rubocop/formatter.rb +1 -0
  279. data/lib/rubocop/lsp/runtime.rb +47 -11
  280. data/lib/rubocop/lsp/stdin_runner.rb +11 -5
  281. data/lib/rubocop/lsp.rb +5 -2
  282. data/lib/rubocop/magic_comment.rb +27 -0
  283. data/lib/rubocop/mcp/autocorrection_request.rb +52 -0
  284. data/lib/rubocop/mcp/explain_tool.rb +68 -0
  285. data/lib/rubocop/mcp/scope.rb +65 -0
  286. data/lib/rubocop/mcp/server.rb +184 -76
  287. data/lib/rubocop/mcp/source_files.rb +35 -0
  288. data/lib/rubocop/offense_limit.rb +30 -0
  289. data/lib/rubocop/options.rb +97 -4
  290. data/lib/rubocop/path_util.rb +1 -1
  291. data/lib/rubocop/plugin/configuration_integrator.rb +12 -5
  292. data/lib/rubocop/plugin/loader.rb +1 -1
  293. data/lib/rubocop/rake_task.rb +1 -1
  294. data/lib/rubocop/remote_config.rb +8 -5
  295. data/lib/rubocop/result_cache.rb +36 -18
  296. data/lib/rubocop/runner.rb +160 -49
  297. data/lib/rubocop/server/cache.rb +2 -2
  298. data/lib/rubocop/unified_diff.rb +257 -0
  299. data/lib/rubocop/util.rb +69 -0
  300. data/lib/rubocop/version.rb +1 -1
  301. data/lib/rubocop.rb +9 -1
  302. metadata +21 -6
  303. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +0 -46
@@ -111,6 +111,12 @@ module RuboCop
111
111
  comments.map(&:source_range)
112
112
  end
113
113
 
114
+ def output_record_separator_ranges(ast)
115
+ return [] if ast.nil?
116
+
117
+ ast.each_node(:gvar).select { |gvar| gvar.source == '$\\' }.map(&:source_range)
118
+ end
119
+
114
120
  def last_line(processed_source)
115
121
  last_token = processed_source.tokens.last
116
122
 
@@ -123,7 +129,8 @@ module RuboCop
123
129
 
124
130
  def ignored_ranges
125
131
  @ignored_ranges ||= ignored_literal_ranges(processed_source.ast) +
126
- comment_ranges(processed_source.comments)
132
+ comment_ranges(processed_source.comments) +
133
+ output_record_separator_ranges(processed_source.ast)
127
134
  end
128
135
 
129
136
  def ignored_parent?(node)
@@ -87,10 +87,10 @@ module RuboCop
87
87
  return if children.empty?
88
88
 
89
89
  if style == :aligned && !always_indented?(node)
90
- check_aligned(children, 1)
90
+ check_aligned(children, 1, children[0].loc.column)
91
91
  else
92
92
  check_indented(children)
93
- check_aligned(children, 2)
93
+ check_aligned(children, 2, indented_column(children))
94
94
  end
95
95
  end
96
96
 
@@ -110,21 +110,37 @@ module RuboCop
110
110
  PARENT_TYPES_FOR_INDENTED.include?(dstr_node.parent&.type)
111
111
  end
112
112
 
113
- def check_aligned(children, start_index)
113
+ def check_aligned(children, start_index, target_column)
114
114
  base_column = children[start_index - 1].loc.column
115
- children[start_index..].each do |child|
116
- @column_delta = base_column - child.loc.column
117
- add_offense_and_correction(child, MSG_ALIGN) if @column_delta != 0
118
- base_column = child.loc.column
115
+ same_column_runs(children[start_index..]).each do |run|
116
+ column = run.first.loc.column
117
+ if column != base_column && column != target_column
118
+ register_misaligned_run(run, target_column - column)
119
+ end
120
+ base_column = column
121
+ end
122
+ end
123
+
124
+ def same_column_runs(parts)
125
+ parts.chunk_while { |a, b| a.loc.column == b.loc.column }
126
+ end
127
+
128
+ def register_misaligned_run(run, column_delta)
129
+ @column_delta = column_delta
130
+ add_offense(run.first, message: MSG_ALIGN) do |corrector|
131
+ run.each { |child| autocorrect(corrector, child) }
119
132
  end
120
133
  end
121
134
 
122
135
  def check_indented(children)
123
- @column_delta = base_column(children[0]) + configured_indentation_width -
124
- children[1].loc.column
136
+ @column_delta = indented_column(children) - children[1].loc.column
125
137
  add_offense_and_correction(children[1], MSG_INDENT) if @column_delta != 0
126
138
  end
127
139
 
140
+ def indented_column(children)
141
+ base_column(children[0]) + configured_indentation_width
142
+ end
143
+
128
144
  def base_column(child)
129
145
  grandparent = child.parent.parent
130
146
  if grandparent&.pair_type?
@@ -72,6 +72,22 @@ module RuboCop
72
72
 
73
73
  MSG = 'Line is too long. [%<length>d/%<max>d]'
74
74
 
75
+ # A trailing backslash, or a trailing unicode, hex, octal, meta or control escape whose
76
+ # payload may be incomplete. An even run of backslashes is complete on its own.
77
+ # Meta and control prefixes chain (`\M-\C-x`, `\c\M-x`), so a whole chain is one escape,
78
+ # and a braced unicode escape may hold several space-separated codepoints.
79
+ ESCAPE_SEQUENCE_TAIL = /
80
+ (?<!\\)(?:\\\\)*
81
+ (
82
+ \\(?:(?:[MC]-|c)\\)*
83
+ (?:u\{[\da-f\x20]*|u[\da-f]{0,4}|x[\da-f]{0,2}|[0-7]{0,3}|[MC]-?|c)?
84
+ )\z
85
+ /xi.freeze
86
+
87
+ def self.autocorrect_incompatible_with
88
+ [Style::MethodCallWithArgsParentheses]
89
+ end
90
+
75
91
  def on_block(node)
76
92
  check_for_breakable_block(node)
77
93
  end
@@ -104,6 +120,11 @@ module RuboCop
104
120
  alias on_defs on_def
105
121
 
106
122
  def on_new_investigation
123
+ @breakable_range_by_line_index = {}
124
+ @breakable_string_delimiters = {}
125
+ @endless_methods_by_line = {}
126
+ @heredocs = nil
127
+
107
128
  return unless processed_source.raw_source.include?(';')
108
129
 
109
130
  check_for_breakable_semicolons(processed_source)
@@ -209,6 +230,8 @@ module RuboCop
209
230
  return if receiver_contains_heredoc?(block_node)
210
231
 
211
232
  line_index = block_node.loc.line - 1
233
+ return if breakable_string_delimiters[line_index]
234
+
212
235
  range = breakable_block_range(block_node)
213
236
  pos = range.begin_pos + 1
214
237
 
@@ -277,7 +300,8 @@ module RuboCop
277
300
  return if source_range.last_column < max
278
301
  return unless (pos = breakable_string_range(node))
279
302
 
280
- pos.end_pos unless pos.end_pos == source_range.begin_pos
303
+ content_begin_pos = node.loc?(:begin) ? node.loc.begin.end_pos : source_range.begin_pos
304
+ pos.end_pos if pos.end_pos > content_begin_pos
281
305
  end
282
306
 
283
307
  # Locate where to break a string that is too long, ensuring that escape characters
@@ -288,21 +312,55 @@ module RuboCop
288
312
  source_range = node.source_range
289
313
  relevant_substr = largest_possible_string(node)
290
314
 
291
- if (space_pos = relevant_substr.rindex(/\s/))
292
- source_range.resize(space_pos + 1)
293
- elsif (escape_pos = relevant_substr.rindex(/\\(u[\da-f]{0,4}|x[\da-f]{0,2})?\z/))
315
+ if (space_pos = breakable_space_position(node, relevant_substr))
316
+ # A space can separate codepoints inside a braced unicode escape.
317
+ escape_pos = escape_sequence_start(relevant_substr[0..space_pos])
318
+ source_range.resize(escape_pos || (space_pos + 1))
319
+ elsif (escape_pos = escape_sequence_start(relevant_substr))
294
320
  source_range.resize(escape_pos)
295
321
  else
296
- adjustment = max - source_range.last_column - 3
297
- return if adjustment.abs > source_range.size
298
-
299
- source_range.adjust(end_pos: adjustment)
322
+ breakable_string_range_at_limit(source_range)
300
323
  end
301
324
  end
302
325
 
326
+ def breakable_space_position(node, substr)
327
+ limit = string_content_length(node) - 1
328
+ space_pos = substr.rindex(/\s/)
329
+ space_pos = substr[0, limit].rindex(/\s/) if space_pos && space_pos + 1 > limit
330
+ space_pos
331
+ end
332
+
333
+ def escape_sequence_start(text)
334
+ text.match(ESCAPE_SEQUENCE_TAIL)&.begin(1)
335
+ end
336
+
337
+ def breakable_string_range_at_limit(source_range)
338
+ adjustment = max - source_range.last_column - 3
339
+ return if adjustment.abs > source_range.size
340
+
341
+ cut = source_range.adjust(end_pos: adjustment)
342
+ escape_pos = escape_sequence_start(cut.source)
343
+ escape_pos ? source_range.resize(escape_pos) : cut
344
+ end
345
+
346
+ def string_content_length(node)
347
+ content_end = node.loc?(:end) ? node.loc.end.begin_pos : node.source_range.end_pos
348
+
349
+ content_end - node.source_range.begin_pos
350
+ end
351
+
303
352
  def breakable_dstr_begin_position(node)
304
353
  source_range = node.source_range
305
- source_range.begin_pos if source_range.column < max && source_range.last_column >= max
354
+ return unless source_range.column < max && source_range.last_column >= max
355
+ return if opens_string?(node)
356
+
357
+ source_range.begin_pos
358
+ end
359
+
360
+ def opens_string?(begin_node)
361
+ quote = begin_node.parent.loc.begin
362
+
363
+ quote && quote.end_pos == begin_node.source_range.begin_pos
306
364
  end
307
365
 
308
366
  def breakable_range_by_line_index
@@ -493,16 +551,10 @@ module RuboCop
493
551
  # `Max` - end delimiter (quote) - continuation characters (space and slash)
494
552
  max_length = max - 3
495
553
  # Offset by the string's starting column so the broken line actually fits
496
- # within `Max`. When on the same line as its parent, use the column difference;
497
- # otherwise the string is indented on its own line, so subtract that indentation.
498
- # (Without this, an indented string under a multi-line parent never shortens
499
- # below `Max` and the autocorrect loops, inserting empty `"" \` fragments.)
500
- max_length -= if same_line?(node, node.parent)
501
- column_offset_between(node.loc, node.parent.loc)
502
- else
503
- node.loc.column
504
- end
505
- node.source[0...(max_length)]
554
+ # within `Max`, whatever precedes the string on its line.
555
+ max_length -= node.loc.column
556
+
557
+ node.source[0, [max_length, 0].max]
506
558
  end
507
559
  end
508
560
  end
@@ -106,6 +106,18 @@ module RuboCop
106
106
  ALWAYS_SAME_LINE_MESSAGE = 'The closing array brace must be on the ' \
107
107
  'same line as the last array element.'
108
108
 
109
+ # Moving the closing bracket after a hash literal or method call element while
110
+ # `Layout/MultilineHashBraceLayout` or `Layout/MultilineMethodCallBraceLayout` moves
111
+ # that element's closing brace in the same pass places the bracket after whatever
112
+ # follows the moved brace, such as a comment.
113
+ # Moving it while `Style/ConcatArrayLiterals` removes the brackets in the same pass
114
+ # leaves the bracket behind.
115
+ def self.autocorrect_incompatible_with
116
+ [
117
+ Layout::MultilineHashBraceLayout, Layout::MultilineMethodCallBraceLayout, Style::ConcatArrayLiterals
118
+ ]
119
+ end
120
+
109
121
  def on_array(node)
110
122
  check_brace_layout(node)
111
123
  end
@@ -56,6 +56,13 @@ module RuboCop
56
56
  ARG_MSG = 'Block argument expression is not on the same line as the block start.'
57
57
  PIPE_SIZE = '|'.length
58
58
 
59
+ # `Style/Lambda` moves the block arguments of `lambda` into `->(...)`, and
60
+ # `Style/ItBlockParameter` and `Style/HashEachMethods` remove some or all of them,
61
+ # in the same pass, while this cop moves them next to the block start.
62
+ def self.autocorrect_incompatible_with
63
+ [Style::HashEachMethods, Style::ItBlockParameter, Style::Lambda]
64
+ end
65
+
59
66
  def on_block(node)
60
67
  return if node.single_line?
61
68
 
@@ -155,7 +162,7 @@ module RuboCop
155
162
  end
156
163
 
157
164
  def include_trailing_comma?(args)
158
- arg_count = args.each_descendant(:arg).to_a.size
165
+ arg_count = args.each_descendant.count(&:argument_type?)
159
166
  arg_count == 1 && args.source.include?(',')
160
167
  end
161
168
  end
@@ -106,6 +106,13 @@ module RuboCop
106
106
  ALWAYS_SAME_LINE_MESSAGE = 'Closing hash brace must be on the same ' \
107
107
  'line as the last hash element.'
108
108
 
109
+ # Moving the closing brace while `Style/HashAsLastArrayItem` or
110
+ # `Style/RedundantDoubleSplatHashBraces` removes the braces in the same pass leaves
111
+ # the brace behind.
112
+ def self.autocorrect_incompatible_with
113
+ [Style::HashAsLastArrayItem, Style::RedundantDoubleSplatHashBraces]
114
+ end
115
+
109
116
  def on_hash(node)
110
117
  check_brace_layout(node)
111
118
  end
@@ -106,6 +106,12 @@ module RuboCop
106
106
  ALWAYS_SAME_LINE_MESSAGE = 'Closing method call brace must be on ' \
107
107
  'the same line as the last argument.'
108
108
 
109
+ # Moving the closing parenthesis and removing it under `omit_parentheses` in the same pass
110
+ # leaves the moved parenthesis behind.
111
+ def self.autocorrect_incompatible_with
112
+ [Style::MethodCallWithArgsParentheses]
113
+ end
114
+
109
115
  def on_send(node)
110
116
  check_brace_layout(node)
111
117
  end
@@ -73,14 +73,28 @@ module RuboCop
73
73
  end
74
74
 
75
75
  def find_pair_ancestor(node)
76
+ child = node
76
77
  node.each_ancestor do |ancestor|
77
78
  return ancestor if ancestor.pair_type?
78
79
  break if grouped_expression?(ancestor) || inside_arg_list_parentheses?(node, ancestor)
80
+ break if inside_body?(ancestor, child)
81
+
82
+ child = ancestor
79
83
  end
80
84
 
81
85
  nil
82
86
  end
83
87
 
88
+ # A block, `begin`, `if` or loop body is a separate set of statements, so
89
+ # a method chain in it is not the value of a hash pair the body is nested in.
90
+ # The send node of a block is still part of the value.
91
+ def inside_body?(ancestor, child)
92
+ return !child.equal?(ancestor.send_node) if ancestor.any_block_type?
93
+ return !ancestor.ternary? if ancestor.if_type?
94
+
95
+ ancestor.type?(:kwbegin, :while, :until, :for)
96
+ end
97
+
84
98
  def unwrap_block_node(node)
85
99
  node&.any_block_type? ? node.send_node : node
86
100
  end
@@ -118,6 +118,10 @@ module RuboCop
118
118
  ALWAYS_SAME_LINE_MESSAGE = 'Closing method definition brace must be ' \
119
119
  'on the same line as the last parameter.'
120
120
 
121
+ def self.autocorrect_incompatible_with
122
+ [Style::MultilineMethodSignature]
123
+ end
124
+
121
125
  def on_def(node)
122
126
  check_brace_layout(node.arguments)
123
127
  end
@@ -54,6 +54,11 @@ module RuboCop
54
54
 
55
55
  MSG = 'Redundant line break detected.'
56
56
 
57
+ def on_new_investigation
58
+ super
59
+ @offense_line_ranges = []
60
+ end
61
+
57
62
  def on_lvasgn(node)
58
63
  super unless end_with_percent_blank_string?(processed_source)
59
64
  end
@@ -77,12 +82,15 @@ module RuboCop
77
82
  end
78
83
 
79
84
  def check_assignment(node, _rhs)
80
- return unless offense?(node)
85
+ return unless offense?(node) && !part_of_ignored_node?(node)
81
86
 
82
87
  register_offense(node)
83
88
  end
84
89
 
85
90
  def register_offense(node)
91
+ line_range = joined_line_range(node)
92
+ return if too_long_as_single_line?(line_range)
93
+
86
94
  # The exact single-line correction is verified to parse equivalently
87
95
  # before the offense is registered, so a join that would change how
88
96
  # the code parses is never reported or offered.
@@ -92,6 +100,19 @@ module RuboCop
92
100
  corrector.replace(node, to_single_line(node.source).strip)
93
101
  end
94
102
  ignore_node(node)
103
+ @offense_line_ranges.reject! { |range| line_range.cover?(range) }
104
+ @offense_line_ranges << line_range
105
+ end
106
+
107
+ # Offenses registered in the same pass that share a line with `node`
108
+ # get joined onto that line too, so the combined line has to fit.
109
+ def joined_line_range(node)
110
+ sharing_ranges = @offense_line_ranges.select do |range|
111
+ range.begin <= node.last_line && node.first_line <= range.end
112
+ end
113
+ lines = [node.first_line, node.last_line, *sharing_ranges.flat_map(&:minmax)]
114
+
115
+ lines.min..lines.max
95
116
  end
96
117
 
97
118
  def apply_reparse_correction(corrector, node)
@@ -22,6 +22,7 @@ module RuboCop
22
22
  # puts 'error'
23
23
  # end
24
24
  class RescueEnsureAlignment < Base
25
+ include Alignment
25
26
  include RangeHelp
26
27
  include EndKeywordAlignment
27
28
  extend AutoCorrector
@@ -74,9 +75,10 @@ module RuboCop
74
75
  # Some inline node is sitting before current node.
75
76
  return nil unless whitespace.source.strip.empty?
76
77
 
77
- new_column = alignment_location.column
78
+ indentation = alignment_indentation(alignment_location)
79
+ return nil if indentation.nil?
78
80
 
79
- corrector.replace(whitespace, ' ' * new_column)
81
+ corrector.replace(whitespace, indentation)
80
82
  end
81
83
 
82
84
  def format_message(alignment_node, alignment_loc, kw_loc)
@@ -91,6 +93,17 @@ module RuboCop
91
93
  )
92
94
  end
93
95
 
96
+ # A run of spaces sized to the target column would be turned back into tabs by
97
+ # `Layout/IndentationStyle`, generally landing on a different column, so under
98
+ # tabs the target line's own indentation is reused. There is nothing to reuse
99
+ # when the target does not start its line.
100
+ def alignment_indentation(location)
101
+ return ' ' * location.column unless using_tabs?
102
+ return unless begins_its_line?(location)
103
+
104
+ location.source_line[/\A\s*/]
105
+ end
106
+
94
107
  # rubocop:disable-next Metrics/AbcSize, Metrics/MethodLength
95
108
  def alignment_source(node, starting_loc)
96
109
  ending_loc =
@@ -25,13 +25,17 @@ module RuboCop
25
25
 
26
26
  MSG = 'Put method call on a separate line if chained to a single line block.'
27
27
 
28
+ # `Style/FloatDivision` and `Style/NilComparison` rewrite a method chained to the block
29
+ # in the same pass, which must not start a line of its own.
28
30
  def self.autocorrect_incompatible_with
29
- [Style::MapToHash]
31
+ [Style::FloatDivision, Style::MapToHash, Style::NilComparison]
30
32
  end
31
33
 
32
34
  def on_send(node)
33
35
  range = offending_range(node)
34
- add_offense(range) { |corrector| corrector.insert_before(range, "\n") } if range
36
+ return if !range || heredoc_opened_before?(node.receiver, range)
37
+
38
+ add_offense(range) { |corrector| corrector.insert_before(range, "\n") }
35
39
  end
36
40
  alias on_csend on_send
37
41
 
@@ -63,6 +67,10 @@ module RuboCop
63
67
  # l.(1) has no selector, so we use the opening parenthesis instead
64
68
  node.loc.selector || node.loc.begin
65
69
  end
70
+
71
+ def heredoc_opened_before?(receiver, range)
72
+ receiver.each_node(:any_str).any? { |str| str.heredoc? && str.first_line == range.line }
73
+ end
66
74
  end
67
75
  end
68
76
  end
@@ -20,6 +20,7 @@ module RuboCop
20
20
 
21
21
  def on_send(node)
22
22
  return unless node.prefix_bang? && whitespace_after_operator?(node)
23
+ return if unary_tilde?(node.receiver)
23
24
 
24
25
  add_offense(node) do |corrector|
25
26
  corrector.remove(
@@ -33,6 +34,10 @@ module RuboCop
33
34
  def whitespace_after_operator?(node)
34
35
  node.receiver.source_range.begin_pos - node.source_range.begin_pos > 1
35
36
  end
37
+
38
+ def unary_tilde?(receiver)
39
+ receiver.send_type? && receiver.unary_operation? && receiver.method?(:~)
40
+ end
36
41
  end
37
42
  end
38
43
  end
@@ -5,7 +5,9 @@ module RuboCop
5
5
  module Layout
6
6
  # Checks the spacing inside and after block parameters pipes. Line breaks
7
7
  # inside parameter pipes are checked by `Layout/MultilineBlockLayout` and
8
- # not by this cop.
8
+ # not by this cop. Spaces inside a lambda's parameter parentheses are left
9
+ # to `Layout/SpaceInsideParens` when that cop is enabled, so the examples
10
+ # below apply to a lambda only when it is not.
9
11
  #
10
12
  # @example EnforcedStyleInsidePipes: no_space (default)
11
13
  # # bad
@@ -29,12 +31,12 @@ module RuboCop
29
31
  include RangeHelp
30
32
  extend AutoCorrector
31
33
 
32
- def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
34
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler -- checks spacing inside a block parameter list
33
35
  arguments = node.arguments
34
36
 
35
37
  return unless node.arguments? && pipes?(arguments)
36
38
 
37
- check_inside_pipes(arguments)
39
+ check_inside_pipes(arguments) unless checked_by_space_inside_parens?(arguments)
38
40
  check_after_closing_pipe(arguments) if node.body
39
41
  check_each_arg(arguments)
40
42
  end
@@ -53,6 +55,10 @@ module RuboCop
53
55
  'EnforcedStyleInsidePipes'
54
56
  end
55
57
 
58
+ def checked_by_space_inside_parens?(arguments)
59
+ arguments.loc.begin.source == '(' && config.cop_enabled?('Layout/SpaceInsideParens')
60
+ end
61
+
56
62
  def check_inside_pipes(arguments)
57
63
  case style
58
64
  when :no_space
@@ -142,7 +148,7 @@ module RuboCop
142
148
  if node
143
149
  corrector.insert_before(node, ' ')
144
150
  else
145
- corrector.insert_after(target, ' ')
151
+ corrector.insert_after(range_between(space_begin_pos, space_begin_pos), ' ')
146
152
  end
147
153
  end
148
154
  end
@@ -209,6 +209,7 @@ module RuboCop
209
209
  end
210
210
 
211
211
  return unless space_after_missing?(range)
212
+ return if block_brace_with_no_space_style?(node, range)
212
213
 
213
214
  add_offense(range, message: format(MSG_AFTER, range: range.source)) do |corrector|
214
215
  corrector.insert_after(range, ' ')
@@ -233,6 +234,15 @@ module RuboCop
233
234
  !/[\s;,#\\)}\].]/.match?(char)
234
235
  end
235
236
 
237
+ def block_brace_with_no_space_style?(node, range)
238
+ return false unless (parent = node.parent)
239
+ return false unless parent.any_block_type? && parent.loc.begin&.begin_pos == range.end_pos
240
+
241
+ space_before_block_braces_config = config.for_cop('Layout/SpaceBeforeBlockBraces')
242
+ space_before_block_braces_config.fetch('Enabled', true) &&
243
+ space_before_block_braces_config['EnforcedStyle'] == 'no_space'
244
+ end
245
+
236
246
  def accepted_opening_delimiter?(range, char)
237
247
  return true unless char
238
248
 
@@ -258,7 +258,7 @@ module RuboCop
258
258
  return false unless allow_for_alignment?
259
259
  return false unless with_space.source.start_with?(EXCESSIVE_SPACE)
260
260
 
261
- return !aligned_with_operator?(operator) unless type == :assignment
261
+ return !aligned_with_operator?(operator) unless grouped_alignment_type?(type)
262
262
 
263
263
  token = Token.new(operator, nil, operator.source)
264
264
  align_preceding = aligned_with_preceding_equals_operator(token)
@@ -269,6 +269,10 @@ module RuboCop
269
269
  aligned_with_subsequent_equals_operator(token) != :yes
270
270
  end
271
271
 
272
+ def grouped_alignment_type?(type)
273
+ type == :assignment || (type == :special_asgn && force_equal_sign_alignment?)
274
+ end
275
+
272
276
  def excess_trailing_space?(right_operand, with_space)
273
277
  with_space.source.end_with?(EXCESSIVE_SPACE) &&
274
278
  (!allow_for_alignment? || !aligned_with_something?(right_operand))
@@ -49,8 +49,11 @@ module RuboCop
49
49
  MISSING_MSG = 'Space missing to the left of {.'
50
50
  DETECTED_MSG = 'Space detected to the left of {.'
51
51
 
52
+ # `Style/BlockDelimiters` decides whether to put a space before the `do`
53
+ # it writes by looking at the source before this cop removed the space,
54
+ # so the two corrections cannot be applied in the same pass.
52
55
  def self.autocorrect_incompatible_with
53
- [Style::SymbolProc]
56
+ [Style::BlockDelimiters, Style::SymbolProc]
54
57
  end
55
58
 
56
59
  def on_block(node)
@@ -117,7 +117,7 @@ module RuboCop
117
117
  elsif style == :space
118
118
  SpaceCorrector.add_space(processed_source, corrector, left, right)
119
119
  else
120
- compact_corrections(corrector, tokens, left, right)
120
+ compact_corrections(corrector, node, tokens, left, right)
121
121
  end
122
122
  end
123
123
 
@@ -178,6 +178,8 @@ module RuboCop
178
178
  space_offenses(node, left, nil, MSG, start_ok: start_ok, end_ok: true)
179
179
  end
180
180
 
181
+ return if closing_bracket_kept_apart?(node, tokens, left, right)
182
+
181
183
  if qualifies_for_compact?(tokens, right)
182
184
  compact_offense(node, right)
183
185
  elsif !multi_dimensional_array?(tokens, right)
@@ -212,13 +214,24 @@ module RuboCop
212
214
  end
213
215
  end
214
216
 
215
- def compact_corrections(corrector, tokens, left, right)
217
+ # A closing bracket on its own line is only collapsed when the opening
218
+ # bracket ends up on the first element's line, so that the brackets
219
+ # stay symmetrical (see `Layout/MultilineArrayBraceLayout`).
220
+ def closing_bracket_kept_apart?(node, tokens, left, right)
221
+ end_has_own_line?(right) &&
222
+ !multi_dimensional_array?(tokens, left, side: :left) &&
223
+ !same_line?(left, node.children.first)
224
+ end
225
+
226
+ def compact_corrections(corrector, node, tokens, left, right)
216
227
  if multi_dimensional_array?(tokens, left, side: :left)
217
228
  compact(corrector, left, :right)
218
229
  elsif !left.space_after?
219
230
  corrector.insert_after(left.pos, ' ')
220
231
  end
221
232
 
233
+ return if closing_bracket_kept_apart?(node, tokens, left, right)
234
+
222
235
  if multi_dimensional_array?(tokens, right)
223
236
  compact(corrector, right, :left)
224
237
  elsif !right.space_before?