rubocop 1.79.2 → 1.87.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 (376) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +185 -20
  5. data/config/obsoletion.yml +9 -0
  6. data/exe/rubocop +1 -8
  7. data/lib/rubocop/cache_config.rb +29 -0
  8. data/lib/rubocop/cli/command/auto_generate_config.rb +30 -4
  9. data/lib/rubocop/cli/command/list_enabled_cops_for.rb +40 -0
  10. data/lib/rubocop/cli/command/lsp.rb +1 -1
  11. data/lib/rubocop/cli/command/mcp.rb +19 -0
  12. data/lib/rubocop/cli/command/show_cops.rb +2 -2
  13. data/lib/rubocop/cli/command/show_docs_url.rb +4 -8
  14. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  15. data/lib/rubocop/cli.rb +35 -9
  16. data/lib/rubocop/comment_config.rb +59 -17
  17. data/lib/rubocop/config.rb +14 -10
  18. data/lib/rubocop/config_finder.rb +1 -1
  19. data/lib/rubocop/config_loader.rb +37 -23
  20. data/lib/rubocop/config_loader_resolver.rb +20 -10
  21. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
  22. data/lib/rubocop/config_store.rb +7 -2
  23. data/lib/rubocop/config_validator.rb +1 -1
  24. data/lib/rubocop/cop/autocorrect_logic.rb +10 -5
  25. data/lib/rubocop/cop/base.rb +8 -2
  26. data/lib/rubocop/cop/bundler/gem_version.rb +28 -28
  27. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -2
  28. data/lib/rubocop/cop/correctors/alignment_corrector.rb +26 -7
  29. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  30. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +7 -2
  31. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -5
  32. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +33 -2
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  34. data/lib/rubocop/cop/correctors.rb +28 -0
  35. data/lib/rubocop/cop/documentation.rb +2 -3
  36. data/lib/rubocop/cop/exclude_limit.rb +31 -5
  37. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  38. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -2
  39. data/lib/rubocop/cop/gemspec/require_mfa.rb +5 -5
  40. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +12 -7
  41. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +8 -8
  42. data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
  43. data/lib/rubocop/cop/internal_affairs/location_exists.rb +28 -2
  44. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
  45. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +9 -9
  46. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb +1 -1
  47. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +3 -1
  48. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
  49. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +4 -4
  50. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  51. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -2
  52. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  53. data/lib/rubocop/cop/layout/begin_end_alignment.rb +1 -1
  54. data/lib/rubocop/cop/layout/case_indentation.rb +3 -1
  55. data/lib/rubocop/cop/layout/class_structure.rb +14 -7
  56. data/lib/rubocop/cop/layout/dot_position.rb +2 -2
  57. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +26 -7
  58. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +31 -13
  59. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +2 -2
  60. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
  61. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
  62. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
  63. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
  64. data/lib/rubocop/cop/layout/end_alignment.rb +10 -3
  65. data/lib/rubocop/cop/layout/first_argument_indentation.rb +34 -1
  66. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +26 -0
  67. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  68. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +25 -25
  69. data/lib/rubocop/cop/layout/hash_alignment.rb +3 -6
  70. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  71. data/lib/rubocop/cop/layout/heredoc_indentation.rb +33 -3
  72. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  73. data/lib/rubocop/cop/layout/indentation_width.rb +123 -7
  74. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
  75. data/lib/rubocop/cop/layout/line_length.rb +26 -9
  76. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +57 -57
  77. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
  78. data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
  79. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +56 -56
  80. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  81. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +229 -39
  82. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +8 -4
  83. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  84. data/lib/rubocop/cop/layout/redundant_line_break.rb +3 -1
  85. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +13 -3
  86. data/lib/rubocop/cop/layout/space_after_comma.rb +2 -10
  87. data/lib/rubocop/cop/layout/space_after_semicolon.rb +1 -1
  88. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  89. data/lib/rubocop/cop/layout/space_around_keyword.rb +4 -2
  90. data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -1
  91. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -8
  92. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  93. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  94. data/lib/rubocop/cop/lint/circular_argument_reference.rb +47 -3
  95. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +4 -3
  96. data/lib/rubocop/cop/lint/constant_reassignment.rb +93 -11
  97. data/lib/rubocop/cop/lint/constant_resolution.rb +6 -6
  98. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +14 -8
  99. data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
  100. data/lib/rubocop/cop/lint/debugger.rb +0 -2
  101. data/lib/rubocop/cop/lint/deprecated_constants.rb +1 -1
  102. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -1
  103. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +4 -4
  104. data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -12
  105. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
  106. data/lib/rubocop/cop/lint/else_layout.rb +19 -0
  107. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  108. data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
  109. data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
  110. data/lib/rubocop/cop/lint/empty_interpolation.rb +11 -0
  111. data/lib/rubocop/cop/lint/empty_when.rb +8 -1
  112. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  113. data/lib/rubocop/cop/lint/float_comparison.rb +1 -1
  114. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  115. data/lib/rubocop/cop/lint/literal_as_condition.rb +5 -1
  116. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  117. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +18 -9
  118. data/lib/rubocop/cop/lint/multiple_comparison.rb +2 -2
  119. data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
  120. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +4 -0
  121. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +4 -2
  122. data/lib/rubocop/cop/lint/number_conversion.rb +6 -6
  123. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
  124. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -13
  125. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +23 -9
  126. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +2 -11
  127. data/lib/rubocop/cop/lint/redundant_require_statement.rb +4 -2
  128. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +23 -6
  129. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +8 -2
  130. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +3 -3
  131. data/lib/rubocop/cop/lint/require_relative_self_path.rb +3 -1
  132. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
  133. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
  134. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
  135. data/lib/rubocop/cop/lint/self_assignment.rb +15 -6
  136. data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
  137. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  138. data/lib/rubocop/cop/lint/struct_new_override.rb +17 -1
  139. data/lib/rubocop/cop/lint/syntax.rb +25 -1
  140. data/lib/rubocop/cop/lint/to_json.rb +12 -16
  141. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
  142. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +1 -1
  143. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  144. data/lib/rubocop/cop/lint/unreachable_code.rb +7 -5
  145. data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
  146. data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
  147. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
  148. data/lib/rubocop/cop/lint/useless_assignment.rb +48 -25
  149. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
  150. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
  151. data/lib/rubocop/cop/lint/useless_or.rb +15 -2
  152. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +1 -1
  153. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +37 -11
  154. data/lib/rubocop/cop/lint/void.rb +39 -12
  155. data/lib/rubocop/cop/message_annotator.rb +1 -1
  156. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  157. data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
  158. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  159. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +4 -3
  160. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +1 -1
  161. data/lib/rubocop/cop/migration/department_name.rb +12 -1
  162. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  163. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +4 -6
  164. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  165. data/lib/rubocop/cop/mixin/configurable_max.rb +6 -5
  166. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
  167. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +5 -5
  168. data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
  169. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
  170. data/lib/rubocop/cop/mixin/line_length_help.rb +21 -2
  171. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -1
  172. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  173. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -1
  174. data/lib/rubocop/cop/mixin/project_index_help.rb +48 -0
  175. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +5 -4
  176. data/lib/rubocop/cop/mixin/statement_modifier.rb +0 -6
  177. data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -5
  178. data/lib/rubocop/cop/mixin.rb +86 -0
  179. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  180. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  181. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  182. data/lib/rubocop/cop/naming/method_name.rb +5 -3
  183. data/lib/rubocop/cop/naming/predicate_method.rb +32 -8
  184. data/lib/rubocop/cop/naming/predicate_prefix.rb +12 -12
  185. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  186. data/lib/rubocop/cop/offense.rb +17 -1
  187. data/lib/rubocop/cop/registry.rb +62 -38
  188. data/lib/rubocop/cop/security/eval.rb +15 -2
  189. data/lib/rubocop/cop/security/io_methods.rb +1 -1
  190. data/lib/rubocop/cop/security/json_load.rb +33 -11
  191. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -4
  192. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
  193. data/lib/rubocop/cop/style/alias.rb +14 -2
  194. data/lib/rubocop/cop/style/and_or.rb +1 -0
  195. data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
  196. data/lib/rubocop/cop/style/array_intersect.rb +46 -12
  197. data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
  198. data/lib/rubocop/cop/style/array_join.rb +4 -2
  199. data/lib/rubocop/cop/style/ascii_comments.rb +6 -3
  200. data/lib/rubocop/cop/style/attr.rb +5 -2
  201. data/lib/rubocop/cop/style/bare_percent_literals.rb +4 -3
  202. data/lib/rubocop/cop/style/begin_block.rb +3 -1
  203. data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
  204. data/lib/rubocop/cop/style/block_delimiters.rb +27 -34
  205. data/lib/rubocop/cop/style/case_equality.rb +15 -13
  206. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  207. data/lib/rubocop/cop/style/class_and_module_children.rb +19 -2
  208. data/lib/rubocop/cop/style/collection_compact.rb +36 -16
  209. data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
  210. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
  211. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -18
  212. data/lib/rubocop/cop/style/constant_visibility.rb +17 -12
  213. data/lib/rubocop/cop/style/copyright.rb +22 -11
  214. data/lib/rubocop/cop/style/date_time.rb +2 -2
  215. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  216. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +6 -1
  217. data/lib/rubocop/cop/style/documentation.rb +6 -6
  218. data/lib/rubocop/cop/style/documentation_method.rb +8 -8
  219. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  220. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  221. data/lib/rubocop/cop/style/each_with_object.rb +2 -0
  222. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  223. data/lib/rubocop/cop/style/empty_class_definition.rb +119 -0
  224. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  225. data/lib/rubocop/cop/style/empty_method.rb +0 -6
  226. data/lib/rubocop/cop/style/encoding.rb +7 -1
  227. data/lib/rubocop/cop/style/end_block.rb +3 -1
  228. data/lib/rubocop/cop/style/endless_method.rb +23 -5
  229. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  230. data/lib/rubocop/cop/style/file_open.rb +84 -0
  231. data/lib/rubocop/cop/style/file_write.rb +18 -16
  232. data/lib/rubocop/cop/style/float_division.rb +15 -1
  233. data/lib/rubocop/cop/style/for.rb +3 -0
  234. data/lib/rubocop/cop/style/format_string.rb +4 -3
  235. data/lib/rubocop/cop/style/format_string_token.rb +49 -5
  236. data/lib/rubocop/cop/style/global_vars.rb +5 -2
  237. data/lib/rubocop/cop/style/guard_clause.rb +27 -22
  238. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +27 -9
  239. data/lib/rubocop/cop/style/hash_conversion.rb +1 -1
  240. data/lib/rubocop/cop/style/hash_lookup_method.rb +106 -0
  241. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  242. data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
  243. data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
  244. data/lib/rubocop/cop/style/if_inside_else.rb +16 -7
  245. data/lib/rubocop/cop/style/if_unless_modifier.rb +57 -17
  246. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -12
  247. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +4 -1
  248. data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
  249. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  250. data/lib/rubocop/cop/style/inline_comment.rb +4 -1
  251. data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
  252. data/lib/rubocop/cop/style/lambda_call.rb +8 -8
  253. data/lib/rubocop/cop/style/magic_comment_format.rb +3 -3
  254. data/lib/rubocop/cop/style/map_join.rb +123 -0
  255. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +15 -2
  256. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +17 -4
  257. data/lib/rubocop/cop/style/method_def_parentheses.rb +2 -4
  258. data/lib/rubocop/cop/style/min_max_comparison.rb +1 -1
  259. data/lib/rubocop/cop/style/module_member_existence_check.rb +110 -0
  260. data/lib/rubocop/cop/style/multiline_if_then.rb +4 -4
  261. data/lib/rubocop/cop/style/multiline_method_signature.rb +2 -4
  262. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  263. data/lib/rubocop/cop/style/negative_array_index.rb +220 -0
  264. data/lib/rubocop/cop/style/nil_comparison.rb +11 -10
  265. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  266. data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
  267. data/lib/rubocop/cop/style/not.rb +2 -0
  268. data/lib/rubocop/cop/style/numeric_literals.rb +3 -2
  269. data/lib/rubocop/cop/style/one_class_per_file.rb +115 -0
  270. data/lib/rubocop/cop/style/one_line_conditional.rb +21 -12
  271. data/lib/rubocop/cop/style/operator_method_call.rb +11 -2
  272. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -2
  273. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
  274. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  275. data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
  276. data/lib/rubocop/cop/style/preferred_hash_methods.rb +12 -12
  277. data/lib/rubocop/cop/style/proc.rb +3 -2
  278. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  279. data/lib/rubocop/cop/style/reduce_to_hash.rb +200 -0
  280. data/lib/rubocop/cop/style/redundant_argument.rb +2 -0
  281. data/lib/rubocop/cop/style/redundant_array_constructor.rb +2 -2
  282. data/lib/rubocop/cop/style/redundant_begin.rb +37 -3
  283. data/lib/rubocop/cop/style/redundant_condition.rb +6 -3
  284. data/lib/rubocop/cop/style/redundant_constant_base.rb +5 -5
  285. data/lib/rubocop/cop/style/redundant_each.rb +3 -3
  286. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  287. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  288. data/lib/rubocop/cop/style/redundant_format.rb +26 -5
  289. data/lib/rubocop/cop/style/redundant_interpolation.rb +11 -2
  290. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
  291. data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
  292. data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
  293. data/lib/rubocop/cop/style/redundant_parentheses.rb +36 -30
  294. data/lib/rubocop/cop/style/redundant_percent_q.rb +5 -3
  295. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +9 -0
  296. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +2 -2
  297. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
  298. data/lib/rubocop/cop/style/redundant_return.rb +3 -1
  299. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  300. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
  301. data/lib/rubocop/cop/style/redundant_sort.rb +7 -7
  302. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +114 -0
  303. data/lib/rubocop/cop/style/regexp_literal.rb +31 -2
  304. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -3
  305. data/lib/rubocop/cop/style/reverse_find.rb +51 -0
  306. data/lib/rubocop/cop/style/safe_navigation.rb +25 -8
  307. data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
  308. data/lib/rubocop/cop/style/select_by_range.rb +197 -0
  309. data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
  310. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  311. data/lib/rubocop/cop/style/semicolon.rb +25 -7
  312. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  313. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
  314. data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
  315. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -3
  316. data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
  317. data/lib/rubocop/cop/style/string_concatenation.rb +17 -13
  318. data/lib/rubocop/cop/style/struct_inheritance.rb +13 -0
  319. data/lib/rubocop/cop/style/super_arguments.rb +2 -2
  320. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  321. data/lib/rubocop/cop/style/symbol_proc.rb +7 -6
  322. data/lib/rubocop/cop/style/tally_method.rb +181 -0
  323. data/lib/rubocop/cop/style/top_level_method_definition.rb +2 -2
  324. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +45 -0
  325. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  326. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
  327. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +11 -11
  328. data/lib/rubocop/cop/style/unless_else.rb +10 -9
  329. data/lib/rubocop/cop/style/unless_logical_operators.rb +3 -3
  330. data/lib/rubocop/cop/style/while_until_modifier.rb +16 -0
  331. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  332. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  333. data/lib/rubocop/cop/team.rb +87 -36
  334. data/lib/rubocop/cop/util.rb +2 -3
  335. data/lib/rubocop/cop/utils/format_string.rb +10 -0
  336. data/lib/rubocop/cop/variable_force/branch.rb +30 -6
  337. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  338. data/lib/rubocop/cop/variable_force.rb +9 -7
  339. data/lib/rubocop/cops_documentation_generator.rb +4 -4
  340. data/lib/rubocop/directive_comment.rb +48 -4
  341. data/lib/rubocop/file_patterns.rb +9 -1
  342. data/lib/rubocop/formatter/clang_style_formatter.rb +5 -2
  343. data/lib/rubocop/formatter/disabled_config_formatter.rb +24 -7
  344. data/lib/rubocop/formatter/formatter_set.rb +2 -2
  345. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  346. data/lib/rubocop/formatter/simple_text_formatter.rb +0 -2
  347. data/lib/rubocop/formatter/tap_formatter.rb +5 -2
  348. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  349. data/lib/rubocop/formatter.rb +22 -21
  350. data/lib/rubocop/lsp/diagnostic.rb +18 -33
  351. data/lib/rubocop/lsp/disable_comment_edits.rb +135 -0
  352. data/lib/rubocop/lsp/routes.rb +43 -7
  353. data/lib/rubocop/lsp/runtime.rb +13 -4
  354. data/lib/rubocop/lsp/stdin_runner.rb +8 -17
  355. data/lib/rubocop/magic_comment.rb +20 -0
  356. data/lib/rubocop/mcp/server.rb +200 -0
  357. data/lib/rubocop/options.rb +35 -4
  358. data/lib/rubocop/path_util.rb +14 -2
  359. data/lib/rubocop/plugin/loader.rb +1 -1
  360. data/lib/rubocop/project_index_loader.rb +66 -0
  361. data/lib/rubocop/rake_task.rb +1 -1
  362. data/lib/rubocop/remote_config.rb +10 -8
  363. data/lib/rubocop/result_cache.rb +61 -38
  364. data/lib/rubocop/rspec/cop_helper.rb +8 -0
  365. data/lib/rubocop/rspec/shared_contexts.rb +39 -5
  366. data/lib/rubocop/rspec/support.rb +2 -1
  367. data/lib/rubocop/runner.rb +134 -57
  368. data/lib/rubocop/server/cache.rb +6 -29
  369. data/lib/rubocop/server/core.rb +2 -0
  370. data/lib/rubocop/target_finder.rb +17 -10
  371. data/lib/rubocop/target_ruby.rb +31 -14
  372. data/lib/rubocop/version.rb +21 -3
  373. data/lib/rubocop.rb +28 -96
  374. data/lib/ruby_lsp/rubocop/addon.rb +23 -8
  375. data/lib/ruby_lsp/rubocop/runtime_adapter.rb +49 -15
  376. metadata +38 -9
@@ -254,8 +254,8 @@ module RuboCop
254
254
  end
255
255
  end
256
256
 
257
- # Mix-in module for logical operator control structures.
258
- module LogicalOperator
257
+ # Mix-in module for operator control structures.
258
+ module Operator
259
259
  def always_run?
260
260
  left_body?
261
261
  end
@@ -263,7 +263,15 @@ module RuboCop
263
263
 
264
264
  # left_body && right_body
265
265
  class And < Base
266
- include LogicalOperator
266
+ include Operator
267
+
268
+ define_predicate :left_body?, child_index: 0
269
+ define_predicate :right_body?, child_index: 1
270
+ end
271
+
272
+ # left_body &&= right_body
273
+ class AndAsgn < Base
274
+ include Operator
267
275
 
268
276
  define_predicate :left_body?, child_index: 0
269
277
  define_predicate :right_body?, child_index: 1
@@ -271,7 +279,23 @@ module RuboCop
271
279
 
272
280
  # left_body || right_body
273
281
  class Or < Base
274
- include LogicalOperator
282
+ include Operator
283
+
284
+ define_predicate :left_body?, child_index: 0
285
+ define_predicate :right_body?, child_index: 1
286
+ end
287
+
288
+ # left_body ||= right_body
289
+ class OrAsgn < Base
290
+ include Operator
291
+
292
+ define_predicate :left_body?, child_index: 0
293
+ define_predicate :right_body?, child_index: 1
294
+ end
295
+
296
+ # e.g. left_body += right_body
297
+ class OpAsgn < Base
298
+ include Operator
275
299
 
276
300
  define_predicate :left_body?, child_index: 0
277
301
  define_predicate :right_body?, child_index: 1
@@ -322,8 +346,8 @@ module RuboCop
322
346
  end
323
347
  end
324
348
 
325
- CLASSES_BY_TYPE = Base.classes.each_with_object({}) do |klass, classes|
326
- classes[klass.type] = klass
349
+ CLASSES_BY_TYPE = Base.classes.to_h do |klass|
350
+ [klass.type, klass]
327
351
  end
328
352
  end
329
353
  end
@@ -79,7 +79,7 @@ module RuboCop
79
79
  parent = parent.parent if parent&.begin_type?
80
80
  return false if parent.nil?
81
81
 
82
- parent.type?(:if, :while, :until) && parent.modifier_form?
82
+ parent.basic_conditional? && parent.modifier_form?
83
83
  end
84
84
 
85
85
  def capture_with_block!
@@ -238,11 +238,16 @@ module RuboCop
238
238
  end
239
239
 
240
240
  def process_loop(node)
241
- if POST_CONDITION_LOOP_TYPES.include?(node.type)
241
+ if node.post_condition_loop?
242
242
  # See the comment at the end of file for this behavior.
243
243
  condition_node, body_node = *node
244
244
  process_node(body_node)
245
245
  process_node(condition_node)
246
+ elsif node.for_type?
247
+ # In `for item in items` the rightmost expression is evaluated first.
248
+ process_node(node.collection)
249
+ process_node(node.variable)
250
+ process_node(node.body) if node.body
246
251
  else
247
252
  process_children(node)
248
253
  end
@@ -356,17 +361,14 @@ module RuboCop
356
361
  end
357
362
 
358
363
  def reference_assignments(loop_assignments, loop_node)
359
- node = loop_assignments.first.node
360
-
361
364
  # If inside a branching statement, mark all as referenced.
362
365
  # Otherwise, mark only the last assignment as referenced.
363
366
  # Note that `rescue` must be considered as branching because of
364
367
  # the `retry` keyword.
365
- if node.each_ancestor(*BRANCH_NODES).any? || node.parent.each_descendant(*BRANCH_NODES).any?
366
- loop_assignments.each { |assignment| assignment.reference!(loop_node) }
367
- else
368
- loop_assignments.last&.reference!(loop_node)
368
+ loop_assignments.each do |assignment|
369
+ assignment.reference!(loop_node) if assignment.node.each_ancestor(*BRANCH_NODES).any?
369
370
  end
371
+ loop_assignments.last&.reference!(loop_node)
370
372
  end
371
373
 
372
374
  def scanned_node?(node)
@@ -194,10 +194,10 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
194
194
 
195
195
  def configurations(department, cop, cop_config)
196
196
  header = ['Name', 'Default value', 'Configurable values']
197
- configs = cop_config
198
- .each_key
199
- .reject { |key| key.start_with?('Supported') }
200
- .reject { |key| key.start_with?('AllowMultipleStyles') }
197
+ configs = cop_config.each_key.reject do |key|
198
+ key == 'AllowMultipleStyles' ||
199
+ (key != 'SupportedTypes' && key.start_with?('Supported'))
200
+ end
201
201
  return '' if configs.empty?
202
202
 
203
203
  content = configs.map do |name|
@@ -14,11 +14,17 @@ module RuboCop
14
14
  # @api private
15
15
  COP_NAME_PATTERN = '([A-Za-z]\w+/)*(?:[A-Za-z]\w+)'
16
16
  # @api private
17
+ COP_NAME_PATTERN_NC = '(?:[A-Za-z]\w+/)*[A-Za-z]\w+'
18
+ # @api private
19
+ COP_NAMES_PATTERN_NC = "(?:#{COP_NAME_PATTERN_NC} , )*#{COP_NAME_PATTERN_NC}"
20
+ # @api private
17
21
  COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
18
22
  # @api private
19
23
  COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})"
20
24
  # @api private
21
- AVAILABLE_MODES = %w[disable enable todo].freeze
25
+ PUSH_POP_ARGS_PATTERN = "([+\\-]#{COP_NAME_PATTERN_NC}(?:\\s+[+\\-]#{COP_NAME_PATTERN_NC})*)"
26
+ # @api private
27
+ AVAILABLE_MODES = %w[disable enable todo push pop].freeze
22
28
  # @api private
23
29
  DIRECTIVE_MARKER_PATTERN = '# rubocop : '
24
30
  # @api private
@@ -27,7 +33,7 @@ module RuboCop
27
33
  DIRECTIVE_HEADER_PATTERN = "#{DIRECTIVE_MARKER_PATTERN}((?:#{AVAILABLE_MODES.join('|')}))\\b"
28
34
  # @api private
29
35
  DIRECTIVE_COMMENT_REGEXP = Regexp.new(
30
- "#{DIRECTIVE_HEADER_PATTERN} #{COPS_PATTERN}"
36
+ "#{DIRECTIVE_HEADER_PATTERN}(?:\\s+#{COPS_PATTERN}|\\s+#{PUSH_POP_ARGS_PATTERN})?"
31
37
  .gsub(' ', '\s*')
32
38
  )
33
39
  # @api private
@@ -46,7 +52,8 @@ module RuboCop
46
52
  def initialize(comment, cop_registry = Cop::Registry.global)
47
53
  @comment = comment
48
54
  @cop_registry = cop_registry
49
- @match_data = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
55
+ match_data = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
56
+ @match_data = match_data&.pre_match&.match?(/\A#\s*\z/) ? nil : match_data
50
57
  @mode, @cops = match_captures
51
58
  end
52
59
 
@@ -58,6 +65,7 @@ module RuboCop
58
65
  # Checks if the comment is malformed as a `# rubocop:` directive
59
66
  def malformed?
60
67
  return true if !start_with_marker? || @match_data.nil?
68
+ return true if missing_cop_name?
61
69
 
62
70
  tail = @match_data.post_match.lstrip
63
71
  !(tail.empty? || tail.start_with?(TRAILING_COMMENT_MARKER))
@@ -65,6 +73,8 @@ module RuboCop
65
73
 
66
74
  # Checks if the directive comment is missing a cop name
67
75
  def missing_cop_name?
76
+ return false if push? || pop?
77
+
68
78
  MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP.match?(comment.text)
69
79
  end
70
80
 
@@ -88,7 +98,13 @@ module RuboCop
88
98
 
89
99
  # Returns match captures to directive comment pattern
90
100
  def match_captures
91
- @match_captures ||= @match_data&.captures
101
+ @match_captures ||= @match_data && begin
102
+ captures = @match_data.captures
103
+ mode = captures[0]
104
+ # COPS_PATTERN is at captures[1], PUSH_POP_ARGS_PATTERN is at captures[4]
105
+ cops = captures[1] || captures[4]
106
+ [mode, cops]
107
+ end
92
108
  end
93
109
 
94
110
  # Checks if this directive disables cops
@@ -101,6 +117,21 @@ module RuboCop
101
117
  mode == 'enable'
102
118
  end
103
119
 
120
+ # Checks if this directive is a push
121
+ def push?
122
+ mode == 'push'
123
+ end
124
+
125
+ # Checks if this directive is a pop
126
+ def pop?
127
+ mode == 'pop'
128
+ end
129
+
130
+ # Returns the push arguments as a hash of cop names with their operations
131
+ def push_args
132
+ @push_args ||= parse_push_args
133
+ end
134
+
104
135
  # Checks if this directive enables all cops
105
136
  def enabled_all?
106
137
  !disabled? && all_cops?
@@ -176,5 +207,18 @@ module RuboCop
176
207
  def exclude_lint_department_cops(cops)
177
208
  cops - [LINT_REDUNDANT_DIRECTIVE_COP, LINT_SYNTAX_COP]
178
209
  end
210
+
211
+ def parse_push_args
212
+ return {} unless push? && cops
213
+
214
+ args = {}
215
+ cops.split.each do |cop_spec|
216
+ op = cop_spec[0]
217
+ cop_name = cop_spec[1..]
218
+ args[op] ||= []
219
+ args[op] << cop_name
220
+ end
221
+ args
222
+ end
179
223
  end
180
224
  end
@@ -21,11 +21,19 @@ module RuboCop
21
21
  def initialize(patterns)
22
22
  @strings = Set.new
23
23
  @patterns = []
24
+ @match_cache = {}
24
25
  partition_patterns(patterns)
25
26
  end
26
27
 
27
28
  def match?(path)
28
- @strings.include?(path) || @patterns.any? { |pattern| PathUtil.match_path?(pattern, path) }
29
+ # `FilePatterns.from` memoizes one instance per pattern array (by identity),
30
+ # so this cache is shared across every cop using the same Include/Exclude
31
+ # list. Patterns are immutable within a run, so caching by path is safe.
32
+ cached = @match_cache[path]
33
+ return cached unless cached.nil?
34
+
35
+ @match_cache[path] =
36
+ @strings.include?(path) || @patterns.any? { |pattern| PathUtil.match_path?(pattern, path) }
29
37
  end
30
38
 
31
39
  private
@@ -47,8 +47,11 @@ module RuboCop
47
47
  def report_highlighted_area(highlighted_area)
48
48
  space_area = highlighted_area.source_buffer.slice(0...highlighted_area.begin_pos)
49
49
  source_area = highlighted_area.source
50
- output.puts("#{' ' * Unicode::DisplayWidth.of(space_area)}" \
51
- "#{'^' * Unicode::DisplayWidth.of(source_area)}")
50
+ output.puts("#{to_whitespace(space_area)}#{'^' * Unicode::DisplayWidth.of(source_area)}")
51
+ end
52
+
53
+ def to_whitespace(string)
54
+ "#{string.delete("^\t")}#{' ' * Unicode::DisplayWidth.of(string.delete("\t"))}"
52
55
  end
53
56
  end
54
57
  end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Formatter
5
5
  # This formatter displays a YAML configuration file where all cops that
6
6
  # detected any offenses are configured to not detect the offense.
7
- class DisabledConfigFormatter < BaseFormatter
7
+ class DisabledConfigFormatter < BaseFormatter # rubocop:disable Metrics/ClassLength
8
8
  include PathUtil
9
9
 
10
10
  HEADING = <<~COMMENTS
@@ -17,6 +17,23 @@ module RuboCop
17
17
  # versions of RuboCop, may require this file to be generated again.
18
18
  COMMENTS
19
19
 
20
+ EXCLUDED_CONFIG_KEYS = %w[
21
+ AutoCorrect
22
+ Description
23
+ Enabled
24
+ Exclude
25
+ Include
26
+ Reference
27
+ References
28
+ Safe
29
+ SafeAutoCorrect
30
+ Severity
31
+ StyleGuide
32
+ VersionAdded
33
+ VersionChanged
34
+ VersionRemoved
35
+ ].freeze
36
+
20
37
  @config_to_allow_offenses = {}
21
38
  @detected_styles = {}
22
39
 
@@ -114,6 +131,9 @@ module RuboCop
114
131
  end
115
132
 
116
133
  def set_max(cfg, cop_name)
134
+ exclude_limits = RuboCop::ExcludeLimit.read_limits(cop_name)
135
+ cfg[:exclude_limit] = exclude_limits unless exclude_limits.empty?
136
+
117
137
  return unless cfg[:exclude_limit]
118
138
 
119
139
  cfg.merge!(cfg[:exclude_limit]) if should_set_max?(cop_name)
@@ -163,10 +183,7 @@ module RuboCop
163
183
  end
164
184
 
165
185
  def cop_config_params(default_cfg, cfg)
166
- default_cfg.keys -
167
- %w[Description StyleGuide Reference References Enabled Exclude Safe
168
- SafeAutoCorrect VersionAdded VersionChanged VersionRemoved] -
169
- cfg.keys
186
+ default_cfg.keys - EXCLUDED_CONFIG_KEYS - cfg.keys
170
187
  end
171
188
 
172
189
  def output_cop_param_comments(output_buffer, params, default_cfg)
@@ -178,7 +195,7 @@ module RuboCop
178
195
  next unless value.is_a?(Array)
179
196
  next if value.empty?
180
197
 
181
- value.map! { |v| v.nil? ? '~' : v } # Change nil back to ~ as in the YAML file.
198
+ value = value.map { |v| v.nil? ? '~' : v } # Change nil back to ~ as in the YAML file.
182
199
  output_buffer.puts "# #{param}: #{value.uniq.join(', ')}"
183
200
  end
184
201
  end
@@ -219,7 +236,7 @@ module RuboCop
219
236
 
220
237
  def output_exclude_list(output_buffer, offending_files, cop_name)
221
238
  require 'pathname'
222
- parent = Pathname.new(Dir.pwd)
239
+ parent = Pathname.new(PathUtil.pwd)
223
240
 
224
241
  output_buffer.puts ' Exclude:'
225
242
  excludes(offending_files, cop_name, parent).each do |exclude_path|
@@ -57,7 +57,7 @@ module RuboCop
57
57
  if output_path
58
58
  dir_path = File.dirname(output_path)
59
59
  FileUtils.mkdir_p(dir_path)
60
- output = File.open(output_path, 'w')
60
+ output = File.open(output_path, 'w') # rubocop:disable Style/FileOpen
61
61
  else
62
62
  output = $stdout
63
63
  end
@@ -86,7 +86,7 @@ module RuboCop
86
86
 
87
87
  def builtin_formatter_class(specified_key)
88
88
  matching_keys = BUILTIN_FORMATTERS_FOR_KEYS.keys.select do |key|
89
- /^\[#{specified_key}\]/.match?(key) || specified_key == key.delete('[]')
89
+ key.start_with?("[#{specified_key}]") || specified_key == key.delete('[]')
90
90
  end
91
91
 
92
92
  if matching_keys.empty?
@@ -93,7 +93,7 @@ module RuboCop
93
93
 
94
94
  def classname_attribute_value(file)
95
95
  @classname_attribute_value_cache ||= Hash.new do |hash, key|
96
- hash[key] = key.delete_suffix('.rb').gsub("#{Dir.pwd}/", '').tr('/', '.')
96
+ hash[key] = key.delete_suffix('.rb').gsub("#{PathUtil.pwd}/", '').tr('/', '.')
97
97
  end
98
98
  @classname_attribute_value_cache[file]
99
99
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'colorizable'
4
-
5
3
  module RuboCop
6
4
  module Formatter
7
5
  # A basic formatter that displays only files with offenses.
@@ -39,8 +39,11 @@ module RuboCop
39
39
  def report_highlighted_area(highlighted_area)
40
40
  space_area = highlighted_area.source_buffer.slice(0...highlighted_area.begin_pos)
41
41
  source_area = highlighted_area.source
42
- output.puts("# #{' ' * Unicode::DisplayWidth.of(space_area)}" \
43
- "#{'^' * Unicode::DisplayWidth.of(source_area)}")
42
+ output.puts("# #{to_whitespace(space_area)}#{'^' * Unicode::DisplayWidth.of(source_area)}")
43
+ end
44
+
45
+ def to_whitespace(string)
46
+ "#{string.delete("^\t")}#{' ' * Unicode::DisplayWidth.of(string.delete("\t"))}"
44
47
  end
45
48
 
46
49
  def report_offense(file, offense)
@@ -24,7 +24,7 @@ module RuboCop
24
24
  def file_finished(file, offenses)
25
25
  return if offenses.empty?
26
26
 
27
- path = Pathname.new(file).relative_path_from(Pathname.new(Dir.pwd))
27
+ path = Pathname.new(file).relative_path_from(Pathname.new(PathUtil.pwd))
28
28
  @offense_counts[path] = offenses.size
29
29
  end
30
30
 
@@ -3,32 +3,33 @@
3
3
  module RuboCop
4
4
  # The bootstrap module for formatter.
5
5
  module Formatter
6
- require_relative 'formatter/text_util'
6
+ autoload :Colorizable, 'rubocop/formatter/colorizable'
7
+ autoload :TextUtil, 'rubocop/formatter/text_util'
7
8
 
8
- require_relative 'formatter/base_formatter'
9
- require_relative 'formatter/simple_text_formatter'
9
+ autoload :BaseFormatter, 'rubocop/formatter/base_formatter'
10
+ autoload :SimpleTextFormatter, 'rubocop/formatter/simple_text_formatter'
10
11
 
11
12
  # relies on simple text
12
- require_relative 'formatter/clang_style_formatter'
13
- require_relative 'formatter/disabled_config_formatter'
14
- require_relative 'formatter/emacs_style_formatter'
15
- require_relative 'formatter/file_list_formatter'
16
- require_relative 'formatter/fuubar_style_formatter'
17
- require_relative 'formatter/github_actions_formatter'
18
- require_relative 'formatter/html_formatter'
19
- require_relative 'formatter/json_formatter'
20
- require_relative 'formatter/junit_formatter'
21
- require_relative 'formatter/markdown_formatter'
22
- require_relative 'formatter/offense_count_formatter'
23
- require_relative 'formatter/pacman_formatter'
24
- require_relative 'formatter/progress_formatter'
25
- require_relative 'formatter/quiet_formatter'
26
- require_relative 'formatter/tap_formatter'
27
- require_relative 'formatter/worst_offenders_formatter'
13
+ autoload :ClangStyleFormatter, 'rubocop/formatter/clang_style_formatter'
14
+ autoload :DisabledConfigFormatter, 'rubocop/formatter/disabled_config_formatter'
15
+ autoload :EmacsStyleFormatter, 'rubocop/formatter/emacs_style_formatter'
16
+ autoload :FileListFormatter, 'rubocop/formatter/file_list_formatter'
17
+ autoload :FuubarStyleFormatter, 'rubocop/formatter/fuubar_style_formatter'
18
+ autoload :GitHubActionsFormatter, 'rubocop/formatter/github_actions_formatter'
19
+ autoload :HTMLFormatter, 'rubocop/formatter/html_formatter'
20
+ autoload :JSONFormatter, 'rubocop/formatter/json_formatter'
21
+ autoload :JUnitFormatter, 'rubocop/formatter/junit_formatter'
22
+ autoload :MarkdownFormatter, 'rubocop/formatter/markdown_formatter'
23
+ autoload :OffenseCountFormatter, 'rubocop/formatter/offense_count_formatter'
24
+ autoload :PacmanFormatter, 'rubocop/formatter/pacman_formatter'
25
+ autoload :ProgressFormatter, 'rubocop/formatter/progress_formatter'
26
+ autoload :QuietFormatter, 'rubocop/formatter/quiet_formatter'
27
+ autoload :TapFormatter, 'rubocop/formatter/tap_formatter'
28
+ autoload :WorstOffendersFormatter, 'rubocop/formatter/worst_offenders_formatter'
28
29
 
29
30
  # relies on progress formatter
30
- require_relative 'formatter/auto_gen_config_formatter'
31
+ autoload :AutoGenConfigFormatter, 'rubocop/formatter/auto_gen_config_formatter'
31
32
 
32
- require_relative 'formatter/formatter_set'
33
+ autoload :FormatterSet, 'rubocop/formatter/formatter_set'
33
34
  end
34
35
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'language_server-protocol'
4
+ require_relative 'disable_comment_edits'
3
5
  require_relative 'severity'
4
6
 
5
7
  #
@@ -16,11 +18,12 @@ module RuboCop
16
18
  # Diagnostic for Language Server Protocol of RuboCop.
17
19
  # @api private
18
20
  class Diagnostic
19
- def initialize(document_encoding, offense, uri, cop_class)
21
+ def initialize(document_encoding, offense, uri, cop_class, processed_source = nil)
20
22
  @document_encoding = document_encoding
21
23
  @offense = offense
22
24
  @uri = uri
23
25
  @cop_class = cop_class
26
+ @processed_source = processed_source
24
27
  end
25
28
 
26
29
  def to_lsp_code_actions
@@ -45,11 +48,11 @@ module RuboCop
45
48
  range: LanguageServer::Protocol::Interface::Range.new(
46
49
  start: LanguageServer::Protocol::Interface::Position.new(
47
50
  line: @offense.line - 1,
48
- character: highlighted.begin_pos
51
+ character: to_position_character(highlighted.begin_pos)
49
52
  ),
50
53
  end: LanguageServer::Protocol::Interface::Position.new(
51
54
  line: @offense.line - 1,
52
- character: highlighted.end_pos
55
+ character: to_position_character(highlighted.end_pos)
53
56
  )
54
57
  ),
55
58
  data: {
@@ -107,11 +110,11 @@ module RuboCop
107
110
  range: LanguageServer::Protocol::Interface::Range.new(
108
111
  start: LanguageServer::Protocol::Interface::Position.new(
109
112
  line: range.line - 1,
110
- character: range.column
113
+ character: to_position_character(range.column)
111
114
  ),
112
115
  end: LanguageServer::Protocol::Interface::Position.new(
113
116
  line: range.last_line - 1,
114
- character: range.last_column
117
+ character: to_position_character(range.last_column)
115
118
  )
116
119
  ),
117
120
  new_text: replacement
@@ -141,37 +144,19 @@ module RuboCop
141
144
  # rubocop:enable Metrics/MethodLength
142
145
 
143
146
  def line_disable_comment
144
- new_text = if @offense.source_line.include?(' # rubocop:disable ')
145
- ",#{@offense.cop_name}"
146
- else
147
- " # rubocop:disable #{@offense.cop_name}"
148
- end
149
-
150
- eol = LanguageServer::Protocol::Interface::Position.new(
151
- line: @offense.line - 1,
152
- character: length_of_line(@offense.source_line)
153
- )
154
-
155
- # TODO: fails for multiline strings - may be preferable to use block
156
- # comments to disable some offenses
157
- inline_comment = LanguageServer::Protocol::Interface::TextEdit.new(
158
- range: LanguageServer::Protocol::Interface::Range.new(start: eol, end: eol),
159
- new_text: new_text
160
- )
161
-
162
- [inline_comment]
147
+ DisableCommentEdits.new(
148
+ offense: @offense,
149
+ document_encoding: @document_encoding,
150
+ processed_source: @processed_source
151
+ ).edits
163
152
  end
164
153
 
165
- def length_of_line(line)
166
- if @document_encoding == Encoding::UTF_16LE
167
- line_length = 0
168
- line.codepoints.each do |codepoint|
169
- line_length += 1
170
- line_length += 1 if codepoint > RubyLsp::Document::Scanner::SURROGATE_PAIR_START
171
- end
172
- line_length
154
+ def to_position_character(utf8_index)
155
+ str = @offense.source_line[0, utf8_index]
156
+ if @document_encoding == Encoding::UTF_16LE || @document_encoding.nil?
157
+ str.length + str.b.count("\xf0-\xff".b)
173
158
  else
174
- line.length
159
+ str.length
175
160
  end
176
161
  end
177
162