rubocop 1.69.0 → 1.79.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (448) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +23 -19
  4. data/config/default.yml +290 -65
  5. data/config/internal_affairs.yml +20 -0
  6. data/config/obsoletion.yml +8 -3
  7. data/lib/rubocop/cli/command/execute_runner.rb +3 -3
  8. data/lib/rubocop/cli/command/show_cops.rb +24 -2
  9. data/lib/rubocop/cli/command/suggest_extensions.rb +7 -1
  10. data/lib/rubocop/cli.rb +13 -2
  11. data/lib/rubocop/comment_config.rb +2 -2
  12. data/lib/rubocop/config.rb +52 -10
  13. data/lib/rubocop/config_loader.rb +53 -47
  14. data/lib/rubocop/config_loader_resolver.rb +36 -10
  15. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -3
  16. data/lib/rubocop/config_obsoletion/renamed_cop.rb +18 -3
  17. data/lib/rubocop/config_obsoletion.rb +46 -2
  18. data/lib/rubocop/config_validator.rb +25 -14
  19. data/lib/rubocop/cop/autocorrect_logic.rb +44 -39
  20. data/lib/rubocop/cop/base.rb +6 -0
  21. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -2
  22. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  23. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  24. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +5 -2
  25. data/lib/rubocop/cop/gemspec/attribute_assignment.rb +91 -0
  26. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +37 -15
  27. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  28. data/lib/rubocop/cop/gemspec/require_mfa.rb +15 -1
  29. data/lib/rubocop/cop/generator.rb +6 -0
  30. data/lib/rubocop/cop/internal_affairs/cop_enabled.rb +85 -0
  31. data/lib/rubocop/cop/internal_affairs/example_description.rb +9 -5
  32. data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
  33. data/lib/rubocop/cop/internal_affairs/location_expression.rb +2 -1
  34. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
  35. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +3 -2
  36. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +5 -5
  37. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb +63 -0
  38. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +131 -0
  39. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +231 -0
  40. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +92 -0
  41. data/lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb +126 -0
  42. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +4 -3
  43. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +90 -0
  44. data/lib/rubocop/cop/internal_affairs/operator_keyword.rb +4 -2
  45. data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
  46. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
  47. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +3 -1
  48. data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
  49. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +13 -2
  50. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
  51. data/lib/rubocop/cop/internal_affairs.rb +6 -16
  52. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +1 -1
  53. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -8
  54. data/lib/rubocop/cop/layout/block_alignment.rb +3 -1
  55. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
  56. data/lib/rubocop/cop/layout/class_structure.rb +44 -9
  57. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
  58. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  59. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  60. data/lib/rubocop/cop/layout/else_alignment.rb +2 -2
  61. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +2 -2
  62. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +7 -11
  63. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
  64. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +35 -4
  65. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
  66. data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +5 -6
  67. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
  68. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
  69. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
  70. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
  71. data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
  72. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  73. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -9
  74. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -7
  75. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -7
  76. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
  77. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  78. data/lib/rubocop/cop/layout/hash_alignment.rb +8 -6
  79. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -1
  80. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  81. data/lib/rubocop/cop/layout/leading_comment_space.rb +13 -1
  82. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +11 -2
  83. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +7 -1
  84. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -2
  85. data/lib/rubocop/cop/layout/line_length.rb +35 -9
  86. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
  87. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
  88. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +25 -0
  89. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -0
  90. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +4 -4
  91. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
  92. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
  93. data/lib/rubocop/cop/layout/redundant_line_break.rb +16 -11
  94. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -5
  95. data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -1
  96. data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
  97. data/lib/rubocop/cop/layout/space_after_comma.rb +1 -1
  98. data/lib/rubocop/cop/layout/space_after_method_name.rb +1 -1
  99. data/lib/rubocop/cop/layout/space_after_semicolon.rb +11 -1
  100. data/lib/rubocop/cop/layout/space_around_keyword.rb +7 -1
  101. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  102. data/lib/rubocop/cop/layout/space_around_operators.rb +15 -4
  103. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
  104. data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -38
  105. data/lib/rubocop/cop/layout/space_before_comma.rb +1 -1
  106. data/lib/rubocop/cop/layout/space_before_semicolon.rb +1 -1
  107. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +12 -3
  108. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
  109. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +3 -0
  110. data/lib/rubocop/cop/layout/trailing_whitespace.rb +5 -3
  111. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  112. data/lib/rubocop/cop/lint/ambiguous_range.rb +5 -0
  113. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +118 -0
  114. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -3
  115. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -3
  116. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
  117. data/lib/rubocop/cop/lint/circular_argument_reference.rb +4 -3
  118. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +3 -3
  119. data/lib/rubocop/cop/lint/constant_reassignment.rb +148 -0
  120. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
  121. data/lib/rubocop/cop/lint/debugger.rb +3 -3
  122. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  123. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +2 -1
  124. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +1 -1
  125. data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -23
  126. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -1
  127. data/lib/rubocop/cop/lint/duplicate_set_element.rb +20 -7
  128. data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
  129. data/lib/rubocop/cop/lint/empty_expression.rb +0 -2
  130. data/lib/rubocop/cop/lint/empty_interpolation.rb +3 -1
  131. data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
  132. data/lib/rubocop/cop/lint/float_comparison.rb +37 -12
  133. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  134. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
  135. data/lib/rubocop/cop/lint/identity_comparison.rb +19 -15
  136. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  137. data/lib/rubocop/cop/lint/literal_as_condition.rb +125 -10
  138. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +24 -6
  139. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -1
  140. data/lib/rubocop/cop/lint/missing_super.rb +2 -2
  141. data/lib/rubocop/cop/lint/mixed_case_range.rb +3 -3
  142. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -1
  143. data/lib/rubocop/cop/lint/nested_method_definition.rb +9 -5
  144. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  145. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +4 -2
  146. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -3
  147. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
  148. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +19 -31
  149. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
  150. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -1
  151. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -5
  152. data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
  153. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +2 -2
  154. data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
  155. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
  156. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +2 -2
  157. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +261 -0
  158. data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
  159. data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
  160. data/lib/rubocop/cop/lint/refinement_import_methods.rb +1 -1
  161. data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
  162. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  163. data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
  164. data/lib/rubocop/cop/lint/return_in_void_context.rb +9 -11
  165. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -1
  166. data/lib/rubocop/cop/lint/self_assignment.rb +25 -0
  167. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -1
  168. data/lib/rubocop/cop/lint/shared_mutable_default.rb +76 -0
  169. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  170. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
  171. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  172. data/lib/rubocop/cop/lint/syntax.rb +4 -1
  173. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
  174. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  175. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +1 -1
  176. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +3 -1
  177. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -1
  178. data/lib/rubocop/cop/lint/unreachable_code.rb +52 -2
  179. data/lib/rubocop/cop/lint/unreachable_loop.rb +6 -6
  180. data/lib/rubocop/cop/lint/useless_access_modifier.rb +34 -8
  181. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -1
  182. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +71 -0
  183. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +90 -0
  184. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +4 -0
  185. data/lib/rubocop/cop/lint/useless_method_definition.rb +1 -1
  186. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +3 -1
  187. data/lib/rubocop/cop/lint/useless_or.rb +98 -0
  188. data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
  189. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +5 -5
  190. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
  191. data/lib/rubocop/cop/lint/void.rb +14 -11
  192. data/lib/rubocop/cop/message_annotator.rb +7 -3
  193. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  194. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  195. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  196. data/lib/rubocop/cop/metrics/class_length.rb +9 -9
  197. data/lib/rubocop/cop/metrics/collection_literal_length.rb +7 -0
  198. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  199. data/lib/rubocop/cop/metrics/method_length.rb +9 -1
  200. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  201. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  202. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  203. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
  204. data/lib/rubocop/cop/mixin/alignment.rb +3 -3
  205. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  206. data/lib/rubocop/cop/mixin/check_line_breakable.rb +13 -13
  207. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  208. data/lib/rubocop/cop/mixin/comments_help.rb +8 -3
  209. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  210. data/lib/rubocop/cop/mixin/dig_help.rb +1 -1
  211. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
  212. data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
  213. data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
  214. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -2
  215. data/lib/rubocop/cop/mixin/gemspec_help.rb +22 -0
  216. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +15 -14
  217. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +22 -22
  218. data/lib/rubocop/cop/mixin/hash_subset.rb +203 -0
  219. data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
  220. data/lib/rubocop/cop/mixin/line_length_help.rb +27 -10
  221. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  222. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
  223. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  224. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  225. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +68 -30
  226. data/lib/rubocop/cop/mixin/range_help.rb +15 -3
  227. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  228. data/lib/rubocop/cop/mixin/statement_modifier.rb +8 -3
  229. data/lib/rubocop/cop/mixin/string_help.rb +2 -2
  230. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  231. data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
  232. data/lib/rubocop/cop/mixin/trailing_comma.rb +21 -5
  233. data/lib/rubocop/cop/naming/accessor_method_name.rb +6 -6
  234. data/lib/rubocop/cop/naming/block_forwarding.rb +19 -15
  235. data/lib/rubocop/cop/naming/file_name.rb +2 -2
  236. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  237. data/lib/rubocop/cop/naming/method_name.rb +185 -15
  238. data/lib/rubocop/cop/naming/predicate_method.rb +306 -0
  239. data/lib/rubocop/cop/naming/{predicate_name.rb → predicate_prefix.rb} +48 -4
  240. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +4 -4
  241. data/lib/rubocop/cop/naming/variable_name.rb +51 -6
  242. data/lib/rubocop/cop/registry.rb +9 -6
  243. data/lib/rubocop/cop/security/compound_hash.rb +2 -0
  244. data/lib/rubocop/cop/security/eval.rb +2 -1
  245. data/lib/rubocop/cop/security/open.rb +1 -0
  246. data/lib/rubocop/cop/security/yaml_load.rb +3 -2
  247. data/lib/rubocop/cop/style/access_modifier_declarations.rb +66 -15
  248. data/lib/rubocop/cop/style/accessor_grouping.rb +32 -6
  249. data/lib/rubocop/cop/style/and_or.rb +1 -1
  250. data/lib/rubocop/cop/style/arguments_forwarding.rb +57 -44
  251. data/lib/rubocop/cop/style/array_first_last.rb +18 -2
  252. data/lib/rubocop/cop/style/array_intersect.rb +81 -40
  253. data/lib/rubocop/cop/style/block_delimiters.rb +27 -24
  254. data/lib/rubocop/cop/style/case_like_if.rb +1 -1
  255. data/lib/rubocop/cop/style/class_and_module_children.rb +52 -11
  256. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
  257. data/lib/rubocop/cop/style/collection_methods.rb +2 -1
  258. data/lib/rubocop/cop/style/collection_querying.rb +167 -0
  259. data/lib/rubocop/cop/style/combinable_defined.rb +1 -1
  260. data/lib/rubocop/cop/style/combinable_loops.rb +3 -2
  261. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  262. data/lib/rubocop/cop/style/commented_keyword.rb +12 -5
  263. data/lib/rubocop/cop/style/comparable_between.rb +78 -0
  264. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
  265. data/lib/rubocop/cop/style/conditional_assignment.rb +23 -7
  266. data/lib/rubocop/cop/style/data_inheritance.rb +7 -0
  267. data/lib/rubocop/cop/style/def_with_parentheses.rb +18 -5
  268. data/lib/rubocop/cop/style/dig_chain.rb +6 -7
  269. data/lib/rubocop/cop/style/documentation.rb +1 -1
  270. data/lib/rubocop/cop/style/double_negation.rb +4 -4
  271. data/lib/rubocop/cop/style/each_for_simple_loop.rb +4 -7
  272. data/lib/rubocop/cop/style/each_with_object.rb +2 -3
  273. data/lib/rubocop/cop/style/empty_else.rb +4 -2
  274. data/lib/rubocop/cop/style/empty_literal.rb +5 -1
  275. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  276. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +100 -0
  277. data/lib/rubocop/cop/style/endless_method.rb +163 -18
  278. data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
  279. data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -3
  280. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
  281. data/lib/rubocop/cop/style/explicit_block_argument.rb +16 -3
  282. data/lib/rubocop/cop/style/exponential_notation.rb +6 -5
  283. data/lib/rubocop/cop/style/fetch_env_var.rb +34 -7
  284. data/lib/rubocop/cop/style/file_null.rb +20 -4
  285. data/lib/rubocop/cop/style/float_division.rb +8 -4
  286. data/lib/rubocop/cop/style/for.rb +1 -0
  287. data/lib/rubocop/cop/style/format_string_token.rb +38 -11
  288. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
  289. data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
  290. data/lib/rubocop/cop/style/guard_clause.rb +2 -1
  291. data/lib/rubocop/cop/style/hash_conversion.rb +16 -8
  292. data/lib/rubocop/cop/style/hash_each_methods.rb +6 -8
  293. data/lib/rubocop/cop/style/hash_except.rb +35 -147
  294. data/lib/rubocop/cop/style/hash_fetch_chain.rb +104 -0
  295. data/lib/rubocop/cop/style/hash_slice.rb +80 -0
  296. data/lib/rubocop/cop/style/hash_syntax.rb +9 -3
  297. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  298. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  299. data/lib/rubocop/cop/style/identical_conditional_branches.rb +25 -6
  300. data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
  301. data/lib/rubocop/cop/style/if_unless_modifier.rb +36 -9
  302. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +4 -7
  303. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -2
  304. data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
  305. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  306. data/lib/rubocop/cop/style/inverse_methods.rb +16 -12
  307. data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
  308. data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
  309. data/lib/rubocop/cop/style/it_assignment.rb +93 -0
  310. data/lib/rubocop/cop/style/it_block_parameter.rb +121 -0
  311. data/lib/rubocop/cop/style/keyword_parameters_order.rb +14 -8
  312. data/lib/rubocop/cop/style/lambda.rb +1 -0
  313. data/lib/rubocop/cop/style/lambda_call.rb +10 -3
  314. data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
  315. data/lib/rubocop/cop/style/map_into_array.rb +5 -2
  316. data/lib/rubocop/cop/style/map_to_hash.rb +13 -4
  317. data/lib/rubocop/cop/style/map_to_set.rb +4 -5
  318. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +27 -19
  319. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +18 -0
  320. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +3 -2
  321. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
  322. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  323. data/lib/rubocop/cop/style/min_max_comparison.rb +13 -5
  324. data/lib/rubocop/cop/style/missing_else.rb +2 -0
  325. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -2
  326. data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -0
  327. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
  328. data/lib/rubocop/cop/style/multiple_comparison.rb +34 -22
  329. data/lib/rubocop/cop/style/mutable_constant.rb +3 -3
  330. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
  331. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  332. data/lib/rubocop/cop/style/next.rb +44 -0
  333. data/lib/rubocop/cop/style/object_then.rb +15 -15
  334. data/lib/rubocop/cop/style/open_struct_use.rb +5 -5
  335. data/lib/rubocop/cop/style/parallel_assignment.rb +33 -25
  336. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  337. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  338. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  339. data/lib/rubocop/cop/style/proc.rb +2 -2
  340. data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
  341. data/lib/rubocop/cop/style/raise_args.rb +14 -12
  342. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  343. data/lib/rubocop/cop/style/redundant_argument.rb +3 -1
  344. data/lib/rubocop/cop/style/redundant_array_flatten.rb +50 -0
  345. data/lib/rubocop/cop/style/redundant_begin.rb +2 -1
  346. data/lib/rubocop/cop/style/redundant_condition.rb +59 -2
  347. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +16 -5
  348. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +6 -10
  349. data/lib/rubocop/cop/style/redundant_each.rb +1 -1
  350. data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
  351. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -9
  352. data/lib/rubocop/cop/style/redundant_format.rb +262 -0
  353. data/lib/rubocop/cop/style/redundant_freeze.rb +4 -4
  354. data/lib/rubocop/cop/style/redundant_initialize.rb +12 -3
  355. data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
  356. data/lib/rubocop/cop/style/redundant_line_continuation.rb +39 -22
  357. data/lib/rubocop/cop/style/redundant_parentheses.rb +85 -17
  358. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +3 -0
  359. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  360. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  361. data/lib/rubocop/cop/style/redundant_self.rb +10 -6
  362. data/lib/rubocop/cop/style/redundant_self_assignment.rb +14 -28
  363. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  364. data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
  365. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  366. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  367. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
  368. data/lib/rubocop/cop/style/return_nil.rb +2 -2
  369. data/lib/rubocop/cop/style/safe_navigation.rb +46 -16
  370. data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
  371. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  372. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +2 -1
  373. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  374. data/lib/rubocop/cop/style/single_line_do_end_block.rb +4 -3
  375. data/lib/rubocop/cop/style/single_line_methods.rb +13 -11
  376. data/lib/rubocop/cop/style/slicing_with_range.rb +40 -11
  377. data/lib/rubocop/cop/style/sole_nested_conditional.rb +68 -101
  378. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
  379. data/lib/rubocop/cop/style/string_concatenation.rb +15 -14
  380. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  381. data/lib/rubocop/cop/style/string_methods.rb +1 -1
  382. data/lib/rubocop/cop/style/struct_inheritance.rb +8 -1
  383. data/lib/rubocop/cop/style/super_arguments.rb +66 -19
  384. data/lib/rubocop/cop/style/symbol_proc.rb +3 -1
  385. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  386. data/lib/rubocop/cop/style/top_level_method_definition.rb +2 -1
  387. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +11 -2
  388. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
  389. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  390. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
  391. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  392. data/lib/rubocop/cop/style/while_until_modifier.rb +0 -1
  393. data/lib/rubocop/cop/style/yoda_condition.rb +8 -4
  394. data/lib/rubocop/cop/style/yoda_expression.rb +2 -1
  395. data/lib/rubocop/cop/team.rb +1 -1
  396. data/lib/rubocop/cop/util.rb +12 -5
  397. data/lib/rubocop/cop/utils/format_string.rb +10 -5
  398. data/lib/rubocop/cop/variable_force/assignment.rb +7 -3
  399. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  400. data/lib/rubocop/cop/variable_force/variable.rb +10 -3
  401. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
  402. data/lib/rubocop/cop/variable_force.rb +23 -8
  403. data/lib/rubocop/cops_documentation_generator.rb +32 -16
  404. data/lib/rubocop/directive_comment.rb +45 -11
  405. data/lib/rubocop/ext/regexp_node.rb +0 -1
  406. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -1
  407. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  408. data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
  409. data/lib/rubocop/formatter/html_formatter.rb +1 -1
  410. data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
  411. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  412. data/lib/rubocop/formatter/pacman_formatter.rb +2 -1
  413. data/lib/rubocop/lsp/diagnostic.rb +189 -0
  414. data/lib/rubocop/lsp/logger.rb +2 -2
  415. data/lib/rubocop/lsp/routes.rb +10 -26
  416. data/lib/rubocop/lsp/runtime.rb +18 -50
  417. data/lib/rubocop/lsp/server.rb +0 -2
  418. data/lib/rubocop/lsp/stdin_runner.rb +85 -0
  419. data/lib/rubocop/magic_comment.rb +11 -3
  420. data/lib/rubocop/options.rb +28 -12
  421. data/lib/rubocop/path_util.rb +15 -8
  422. data/lib/rubocop/pending_cops_reporter.rb +56 -0
  423. data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
  424. data/lib/rubocop/plugin/load_error.rb +26 -0
  425. data/lib/rubocop/plugin/loader.rb +100 -0
  426. data/lib/rubocop/plugin/not_supported_error.rb +29 -0
  427. data/lib/rubocop/plugin.rb +46 -0
  428. data/lib/rubocop/rake_task.rb +4 -1
  429. data/lib/rubocop/result_cache.rb +26 -24
  430. data/lib/rubocop/rspec/cop_helper.rb +13 -1
  431. data/lib/rubocop/rspec/expect_offense.rb +15 -5
  432. data/lib/rubocop/rspec/shared_contexts.rb +38 -1
  433. data/lib/rubocop/rspec/support.rb +4 -2
  434. data/lib/rubocop/runner.rb +10 -7
  435. data/lib/rubocop/server/cache.rb +51 -13
  436. data/lib/rubocop/server/cli.rb +2 -2
  437. data/lib/rubocop/server/client_command/base.rb +10 -0
  438. data/lib/rubocop/server/client_command/exec.rb +2 -1
  439. data/lib/rubocop/server/client_command/start.rb +11 -1
  440. data/lib/rubocop/target_finder.rb +7 -2
  441. data/lib/rubocop/target_ruby.rb +16 -1
  442. data/lib/rubocop/version.rb +30 -8
  443. data/lib/rubocop.rb +27 -2
  444. data/lib/ruby_lsp/rubocop/addon.rb +75 -0
  445. data/lib/ruby_lsp/rubocop/runtime_adapter.rb +65 -0
  446. metadata +72 -19
  447. data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
  448. data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -28
@@ -110,8 +110,13 @@ module RuboCop
110
110
  end
111
111
 
112
112
  def multiple_assignment_node
113
- return nil unless node.parent&.mlhs_type?
114
- return nil unless (grandparent_node = node.parent&.parent)
113
+ return nil unless (candidate_mlhs_node = node.parent)
114
+
115
+ # In `(foo, bar), *baz`, the splat node must be traversed as well.
116
+ candidate_mlhs_node = candidate_mlhs_node.parent if candidate_mlhs_node.splat_type?
117
+
118
+ return nil unless candidate_mlhs_node.mlhs_type?
119
+ return nil unless (grandparent_node = node.parent.parent)
115
120
  if (node = find_multiple_assignment_node(grandparent_node))
116
121
  return node
117
122
  end
@@ -139,7 +144,6 @@ module RuboCop
139
144
 
140
145
  def find_multiple_assignment_node(grandparent_node)
141
146
  return unless grandparent_node.type == MULTIPLE_LEFT_HAND_SIDE_TYPE
142
- return if grandparent_node.children.any?(&:splat_type?)
143
147
 
144
148
  parent = grandparent_node.parent
145
149
  return parent if parent.type == MULTIPLE_ASSIGNMENT_TYPE
@@ -46,7 +46,7 @@ module RuboCop
46
46
  else
47
47
  child_index = case node.type
48
48
  when :module, :sclass then 1
49
- when :def, :class, :block, :numblock then 2
49
+ when :def, :class, :block, :numblock, :itblock then 2
50
50
  when :defs then 3
51
51
  end
52
52
 
@@ -31,11 +31,18 @@ module RuboCop
31
31
  def assign(node)
32
32
  assignment = Assignment.new(node, self)
33
33
 
34
- @assignments.last&.reassigned! unless captured_by_block?
34
+ mark_last_as_reassigned!(assignment)
35
35
 
36
36
  @assignments << assignment
37
37
  end
38
38
 
39
+ def mark_last_as_reassigned!(assignment)
40
+ return if captured_by_block?
41
+ return unless assignment.branch == @assignments.last&.branch
42
+
43
+ @assignments.last&.reassigned!
44
+ end
45
+
39
46
  def referenced?
40
47
  !@references.empty?
41
48
  end
@@ -72,7 +79,7 @@ module RuboCop
72
79
  parent = parent.parent if parent&.begin_type?
73
80
  return false if parent.nil?
74
81
 
75
- (parent.if_type? || parent.while_type? || parent.until_type?) && parent.modifier_form?
82
+ parent.type?(:if, :while, :until) && parent.modifier_form?
76
83
  end
77
84
 
78
85
  def capture_with_block!
@@ -100,7 +107,7 @@ module RuboCop
100
107
  end
101
108
 
102
109
  def method_argument?
103
- argument? && %i[def defs].include?(@scope.node.type)
110
+ argument? && @scope.node.any_def_type?
104
111
  end
105
112
 
106
113
  def block_argument?
@@ -100,7 +100,7 @@ module RuboCop
100
100
 
101
101
  # Only block scope allows referencing outer scope variables.
102
102
  node = scope.node
103
- return nil unless node.block_type? || node.numblock_type?
103
+ return nil unless node.any_block_type?
104
104
  end
105
105
 
106
106
  nil
@@ -113,14 +113,14 @@ module RuboCop
113
113
  def accessible_variables
114
114
  scope_stack.reverse_each.with_object([]) do |scope, variables|
115
115
  variables.concat(scope.variables.values)
116
- break variables unless scope.node.block_type? || scope.node.numblock_type?
116
+ break variables unless scope.node.any_block_type?
117
117
  end
118
118
  end
119
119
 
120
120
  private
121
121
 
122
122
  def mark_variable_as_captured_by_block_if_so(variable)
123
- return unless current_scope.node.block_type? || current_scope.node.numblock_type?
123
+ return unless current_scope.node.any_block_type?
124
124
  return if variable.scope == current_scope
125
125
 
126
126
  variable.capture_with_block!
@@ -54,7 +54,7 @@ module RuboCop
54
54
 
55
55
  ZERO_ARITY_SUPER_TYPE = :zsuper
56
56
 
57
- TWISTED_SCOPE_TYPES = %i[block numblock class sclass defs module].freeze
57
+ TWISTED_SCOPE_TYPES = %i[block numblock itblock class sclass defs module].freeze
58
58
  SCOPE_TYPES = (TWISTED_SCOPE_TYPES + [:def]).freeze
59
59
 
60
60
  SEND_TYPE = :send
@@ -71,6 +71,8 @@ module RuboCop
71
71
  end
72
72
  end
73
73
 
74
+ BRANCH_NODES = %i[if case case_match rescue].freeze
75
+
74
76
  def variable_table
75
77
  @variable_table ||= VariableTable.new(self)
76
78
  end
@@ -296,7 +298,7 @@ module RuboCop
296
298
  variable_table.accessible_variables.each { |variable| variable.reference!(node) }
297
299
  end
298
300
 
299
- # Mark all assignments which are referenced in the same loop
301
+ # Mark last assignments which are referenced in the same loop
300
302
  # as referenced by ignoring AST order since they would be referenced
301
303
  # in next iteration.
302
304
  def mark_assignments_as_referenced_in_loop(node)
@@ -308,13 +310,12 @@ module RuboCop
308
310
  # would be skipped here.
309
311
  next unless variable
310
312
 
311
- variable.assignments.each do |assignment|
312
- next if assignment_nodes_in_loop.none? do |assignment_node|
313
- assignment_node.equal?(assignment.node)
314
- end
315
-
316
- assignment.reference!(node)
313
+ loop_assignments = variable.assignments.select do |assignment|
314
+ assignment_nodes_in_loop.include?(assignment.node)
317
315
  end
316
+ next unless loop_assignments.any?
317
+
318
+ reference_assignments(loop_assignments, node)
318
319
  end
319
320
  end
320
321
 
@@ -354,6 +355,20 @@ module RuboCop
354
355
  end
355
356
  end
356
357
 
358
+ def reference_assignments(loop_assignments, loop_node)
359
+ node = loop_assignments.first.node
360
+
361
+ # If inside a branching statement, mark all as referenced.
362
+ # Otherwise, mark only the last assignment as referenced.
363
+ # Note that `rescue` must be considered as branching because of
364
+ # 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)
369
+ end
370
+ end
371
+
357
372
  def scanned_node?(node)
358
373
  scanned_nodes.include?(node)
359
374
  end
@@ -7,6 +7,7 @@ require 'yard'
7
7
  # @api private
8
8
  class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
9
9
  include ::RuboCop::Cop::Documentation
10
+
10
11
  CopData = Struct.new(
11
12
  :cop, :description, :example_objects, :safety_objects, :see_objects, :config, keyword_init: true
12
13
  )
@@ -18,7 +19,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
18
19
  description: ->(data) { "#{data.description}\n" },
19
20
  safety: ->(data) { safety_object(data.safety_objects, data.cop) },
20
21
  examples: ->(data) { examples(data.example_objects, data.cop) },
21
- configuration: ->(data) { configurations(data.cop.department, data.config, data.cop) },
22
+ configuration: ->(data) { configurations(data.cop.department, data.cop, data.config) },
22
23
  references: ->(data) { references(data.cop, data.see_objects) }
23
24
  }.freeze
24
25
 
@@ -28,6 +29,12 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
28
29
  #
29
30
  # CopsDocumentationGenerator.new(departments: ['Lint']).call
30
31
  #
32
+ # For plugin extensions, specify `:plugin_name` keyword as follows:
33
+ #
34
+ # CopsDocumentationGenerator.new(
35
+ # departments: ['Performance'], plugin_name: 'rubocop-performance'
36
+ # ).call
37
+ #
31
38
  # You can append additional information:
32
39
  #
33
40
  # callback = ->(data) { required_rails_version(data.cop) }
@@ -36,11 +43,16 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
36
43
  # This will insert the string returned from the lambda _after_ the section from RuboCop itself.
37
44
  # See `CopsDocumentationGenerator::STRUCTURE` for available sections.
38
45
  #
39
- def initialize(departments: [], extra_info: {}, base_dir: Dir.pwd)
46
+ def initialize(departments: [], extra_info: {}, base_dir: Dir.pwd, plugin_name: nil)
40
47
  @departments = departments.map(&:to_sym).sort!
41
48
  @extra_info = extra_info
42
49
  @cops = RuboCop::Cop::Registry.global
43
50
  @config = RuboCop::ConfigLoader.default_configuration
51
+ # NOTE: For example, this prevents excessive plugin loading before another task executes,
52
+ # in cases where plugins are already loaded by `internal_investigation`.
53
+ if plugin_name && @config.loaded_plugins.none? { |plugin| plugin.about.name == plugin_name }
54
+ RuboCop::Plugin.integrate_plugins(RuboCop::Config.new, [plugin_name])
55
+ end
44
56
  @base_dir = base_dir
45
57
  @docs_path = "#{base_dir}/docs/modules/ROOT"
46
58
  FileUtils.mkdir_p("#{@docs_path}/pages")
@@ -180,17 +192,17 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
180
192
  content
181
193
  end
182
194
 
183
- def configurations(department, pars, cop)
184
- return '' if pars.empty?
185
-
195
+ def configurations(department, cop, cop_config)
186
196
  header = ['Name', 'Default value', 'Configurable values']
187
- configs = pars
197
+ configs = cop_config
188
198
  .each_key
189
199
  .reject { |key| key.start_with?('Supported') }
190
200
  .reject { |key| key.start_with?('AllowMultipleStyles') }
201
+ return '' if configs.empty?
202
+
191
203
  content = configs.map do |name|
192
- configurable = configurable_values(pars, name)
193
- default = format_table_value(pars[name])
204
+ configurable = configurable_values(cop_config, name)
205
+ default = format_table_value(cop_config[name])
194
206
 
195
207
  [configuration_name(department, name), default, configurable]
196
208
  end
@@ -206,17 +218,17 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
206
218
  end
207
219
 
208
220
  # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
209
- def configurable_values(pars, name)
221
+ def configurable_values(cop_config, name)
210
222
  case name
211
223
  when /^Enforced/
212
224
  supported_style_name = RuboCop::Cop::Util.to_supported_styles(name)
213
- format_table_value(pars[supported_style_name])
225
+ format_table_value(cop_config[supported_style_name])
214
226
  when 'IndentationWidth'
215
227
  'Integer'
216
228
  when 'Database'
217
- format_table_value(pars['SupportedDatabases'])
229
+ format_table_value(cop_config['SupportedDatabases'])
218
230
  else
219
- case pars[name]
231
+ case cop_config[name]
220
232
  when String
221
233
  'String'
222
234
  when Integer
@@ -316,10 +328,14 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
316
328
  def print_cop_with_doc(cop) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
317
329
  cop_config = config.for_cop(cop)
318
330
  non_display_keys = %w[
319
- AutoCorrect Description Enabled StyleGuide Reference Safe SafeAutoCorrect VersionAdded
320
- VersionChanged
331
+ Enabled
332
+ Description
333
+ StyleGuide
334
+ Reference References
335
+ Safe SafeAutoCorrect AutoCorrect
336
+ VersionAdded VersionChanged
321
337
  ]
322
- pars = cop_config.reject { |k| non_display_keys.include? k }
338
+ parameters = cop_config.reject { |k| non_display_keys.include? k }
323
339
  description = 'No documentation'
324
340
  example_objects = safety_objects = see_objects = []
325
341
  cop_code(cop) do |code_object|
@@ -329,7 +345,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
329
345
  see_objects = code_object.tags('see')
330
346
  end
331
347
  data = CopData.new(cop: cop, description: description, example_objects: example_objects,
332
- safety_objects: safety_objects, see_objects: see_objects, config: pars)
348
+ safety_objects: safety_objects, see_objects: see_objects, config: parameters)
333
349
  cops_body(data)
334
350
  end
335
351
 
@@ -12,16 +12,30 @@ module RuboCop
12
12
  # @api private
13
13
  LINT_SYNTAX_COP = "#{LINT_DEPARTMENT}/Syntax"
14
14
  # @api private
15
- COP_NAME_PATTERN = '([A-Z]\w+/)*(?:[A-Z]\w+)'
15
+ COP_NAME_PATTERN = '([A-Za-z]\w+/)*(?:[A-Za-z]\w+)'
16
16
  # @api private
17
17
  COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
18
18
  # @api private
19
19
  COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})"
20
20
  # @api private
21
+ AVAILABLE_MODES = %w[disable enable todo].freeze
22
+ # @api private
23
+ DIRECTIVE_MARKER_PATTERN = '# rubocop : '
24
+ # @api private
25
+ DIRECTIVE_MARKER_REGEXP = Regexp.new(DIRECTIVE_MARKER_PATTERN.gsub(' ', '\s*'))
26
+ # @api private
27
+ DIRECTIVE_HEADER_PATTERN = "#{DIRECTIVE_MARKER_PATTERN}((?:#{AVAILABLE_MODES.join('|')}))\\b"
28
+ # @api private
21
29
  DIRECTIVE_COMMENT_REGEXP = Regexp.new(
22
- "# rubocop : ((?:disable|enable|todo))\\b #{COPS_PATTERN}"
30
+ "#{DIRECTIVE_HEADER_PATTERN} #{COPS_PATTERN}"
23
31
  .gsub(' ', '\s*')
24
32
  )
33
+ # @api private
34
+ TRAILING_COMMENT_MARKER = '--'
35
+ # @api private
36
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP = Regexp.new(
37
+ "\\A#{DIRECTIVE_HEADER_PATTERN}\\s*\\z".gsub(' ', '\s*')
38
+ )
25
39
 
26
40
  def self.before_comment(line)
27
41
  line.split(DIRECTIVE_COMMENT_REGEXP).first
@@ -32,9 +46,28 @@ module RuboCop
32
46
  def initialize(comment, cop_registry = Cop::Registry.global)
33
47
  @comment = comment
34
48
  @cop_registry = cop_registry
49
+ @match_data = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
35
50
  @mode, @cops = match_captures
36
51
  end
37
52
 
53
+ # Checks if the comment starts with `# rubocop:` marker
54
+ def start_with_marker?
55
+ comment.text.start_with?(DIRECTIVE_MARKER_REGEXP)
56
+ end
57
+
58
+ # Checks if the comment is malformed as a `# rubocop:` directive
59
+ def malformed?
60
+ return true if !start_with_marker? || @match_data.nil?
61
+
62
+ tail = @match_data.post_match.lstrip
63
+ !(tail.empty? || tail.start_with?(TRAILING_COMMENT_MARKER))
64
+ end
65
+
66
+ # Checks if the directive comment is missing a cop name
67
+ def missing_cop_name?
68
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP.match?(comment.text)
69
+ end
70
+
38
71
  # Checks if this directive relates to single line
39
72
  def single_line?
40
73
  !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP)
@@ -55,7 +88,7 @@ module RuboCop
55
88
 
56
89
  # Returns match captures to directive comment pattern
57
90
  def match_captures
58
- @match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures
91
+ @match_captures ||= @match_data&.captures
59
92
  end
60
93
 
61
94
  # Checks if this directive disables cops
@@ -88,10 +121,15 @@ module RuboCop
88
121
  @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names
89
122
  end
90
123
 
124
+ # Returns an array of cops for this directive comment, without resolving departments
125
+ def raw_cop_names
126
+ @raw_cop_names ||= (cops || '').split(/,\s*/)
127
+ end
128
+
91
129
  # Returns array of specified in this directive department names
92
130
  # when all department disabled
93
131
  def department_names
94
- splitted_cops_string.select { |cop| department?(cop) }
132
+ raw_cop_names.select { |cop| department?(cop) }
95
133
  end
96
134
 
97
135
  # Checks if directive departments include cop
@@ -101,11 +139,11 @@ module RuboCop
101
139
 
102
140
  # Checks if cop department has already used in directive comment
103
141
  def overridden_by_department?(cop)
104
- in_directive_department?(cop) && splitted_cops_string.include?(cop)
142
+ in_directive_department?(cop) && raw_cop_names.include?(cop)
105
143
  end
106
144
 
107
145
  def directive_count
108
- splitted_cops_string.count
146
+ raw_cop_names.count
109
147
  end
110
148
 
111
149
  # Returns line number for directive
@@ -115,12 +153,8 @@ module RuboCop
115
153
 
116
154
  private
117
155
 
118
- def splitted_cops_string
119
- (cops || '').split(/,\s*/)
120
- end
121
-
122
156
  def parsed_cop_names
123
- cops = splitted_cops_string.map do |name|
157
+ cops = raw_cop_names.map do |name|
124
158
  department?(name) ? cop_names_for_department(name) : name
125
159
  end.flatten
126
160
  cops - [LINT_SYNTAX_COP]
@@ -43,7 +43,6 @@ module RuboCop
43
43
  def named_capturing?(exp, event, named)
44
44
  event == :enter &&
45
45
  named == exp.respond_to?(:name) &&
46
- !exp.text.start_with?('(?<=') &&
47
46
  exp.respond_to?(:capturing?) &&
48
47
  exp.capturing?
49
48
  end
@@ -164,7 +164,7 @@ module RuboCop
164
164
 
165
165
  def cop_config_params(default_cfg, cfg)
166
166
  default_cfg.keys -
167
- %w[Description StyleGuide Reference Enabled Exclude Safe
167
+ %w[Description StyleGuide Reference References Enabled Exclude Safe
168
168
  SafeAutoCorrect VersionAdded VersionChanged VersionRemoved] -
169
169
  cfg.keys
170
170
  end
@@ -178,6 +178,7 @@ module RuboCop
178
178
  next unless value.is_a?(Array)
179
179
  next if value.empty?
180
180
 
181
+ value.map! { |v| v.nil? ? '~' : v } # Change nil back to ~ as in the YAML file.
181
182
  output_buffer.puts "# #{param}: #{value.uniq.join(', ')}"
182
183
  end
183
184
  end
@@ -77,7 +77,7 @@ module RuboCop
77
77
  case formatter_type
78
78
  when Class
79
79
  formatter_type
80
- when /\A[A-Z]/
80
+ when /\A(::)?[A-Z]/
81
81
  custom_formatter_class(formatter_type)
82
82
  else
83
83
  builtin_formatter_class(formatter_type)
@@ -22,7 +22,7 @@ module RuboCop
22
22
 
23
23
  @severest_offense = nil
24
24
 
25
- file_phrase = target_files.count == 1 ? 'file' : 'files'
25
+ file_phrase = target_files.one? ? 'file' : 'files'
26
26
 
27
27
  # 185/407 files |====== 45 ======> | ETA: 00:00:04
28
28
  # %c / %C | %w > %i | %e
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'cgi'
3
+ require 'cgi/escape'
4
4
  require 'erb'
5
5
 
6
6
  module RuboCop
@@ -6,6 +6,7 @@ module RuboCop
6
6
  class MarkdownFormatter < BaseFormatter
7
7
  include TextUtil
8
8
  include PathUtil
9
+
9
10
  attr_reader :files, :summary
10
11
 
11
12
  def initialize(output, options = {})
@@ -24,7 +24,7 @@ module RuboCop
24
24
 
25
25
  return unless output.tty?
26
26
 
27
- file_phrase = target_files.count == 1 ? 'file' : 'files'
27
+ file_phrase = target_files.one? ? 'file' : 'files'
28
28
 
29
29
  # 185/407 files |====== 45 ======> | ETA: 00:00:04
30
30
  # %c / %C | %w > %i | %e
@@ -9,6 +9,7 @@ module RuboCop
9
9
  # https://github.com/go-labs/rspec_pacman_formatter
10
10
  class PacmanFormatter < ClangStyleFormatter
11
11
  include TextUtil
12
+
12
13
  attr_accessor :progress_line
13
14
 
14
15
  FALLBACK_TERMINAL_WIDTH = 80
@@ -58,7 +59,7 @@ module RuboCop
58
59
  return pacdots(@total_files) unless @total_files > cols
59
60
  return pacdots(cols) unless (@total_files / cols).eql?(@repetitions)
60
61
 
61
- pacdots((@total_files - (cols * @repetitions)))
62
+ pacdots(@total_files - (cols * @repetitions))
62
63
  end
63
64
 
64
65
  def pacdots(number)
@@ -0,0 +1,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'severity'
4
+
5
+ #
6
+ # This code is based on https://github.com/standardrb/standard.
7
+ #
8
+ # Copyright (c) 2023 Test Double, Inc.
9
+ #
10
+ # The MIT License (MIT)
11
+ #
12
+ # https://github.com/standardrb/standard/blob/main/LICENSE.txt
13
+ #
14
+ module RuboCop
15
+ module LSP
16
+ # Diagnostic for Language Server Protocol of RuboCop.
17
+ # @api private
18
+ class Diagnostic
19
+ def initialize(document_encoding, offense, uri, cop_class)
20
+ @document_encoding = document_encoding
21
+ @offense = offense
22
+ @uri = uri
23
+ @cop_class = cop_class
24
+ end
25
+
26
+ def to_lsp_code_actions
27
+ code_actions = []
28
+
29
+ code_actions << autocorrect_action if correctable?
30
+ code_actions << disable_line_action
31
+
32
+ code_actions
33
+ end
34
+
35
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
36
+ def to_lsp_diagnostic(config)
37
+ highlighted = @offense.highlighted_area
38
+
39
+ LanguageServer::Protocol::Interface::Diagnostic.new(
40
+ message: message,
41
+ source: 'RuboCop',
42
+ code: @offense.cop_name,
43
+ code_description: code_description(config),
44
+ severity: severity,
45
+ range: LanguageServer::Protocol::Interface::Range.new(
46
+ start: LanguageServer::Protocol::Interface::Position.new(
47
+ line: @offense.line - 1,
48
+ character: highlighted.begin_pos
49
+ ),
50
+ end: LanguageServer::Protocol::Interface::Position.new(
51
+ line: @offense.line - 1,
52
+ character: highlighted.end_pos
53
+ )
54
+ ),
55
+ data: {
56
+ correctable: correctable?,
57
+ code_actions: to_lsp_code_actions
58
+ }
59
+ )
60
+ end
61
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
62
+
63
+ private
64
+
65
+ def message
66
+ message = @offense.message
67
+ message += "\n\nThis offense is not autocorrectable.\n" unless correctable?
68
+ message
69
+ end
70
+
71
+ def severity
72
+ Severity.find_by(@offense.severity.name)
73
+ end
74
+
75
+ def code_description(config)
76
+ return unless @cop_class
77
+ return unless (doc_url = @cop_class.documentation_url(config))
78
+
79
+ LanguageServer::Protocol::Interface::CodeDescription.new(href: doc_url)
80
+ end
81
+
82
+ # rubocop:disable Metrics/MethodLength
83
+ def autocorrect_action
84
+ LanguageServer::Protocol::Interface::CodeAction.new(
85
+ title: "Autocorrect #{@offense.cop_name}",
86
+ kind: LanguageServer::Protocol::Constant::CodeActionKind::QUICK_FIX,
87
+ edit: LanguageServer::Protocol::Interface::WorkspaceEdit.new(
88
+ document_changes: [
89
+ LanguageServer::Protocol::Interface::TextDocumentEdit.new(
90
+ text_document: LanguageServer::Protocol::Interface::OptionalVersionedTextDocumentIdentifier.new(
91
+ uri: ensure_uri_scheme(@uri.to_s).to_s,
92
+ version: nil
93
+ ),
94
+ edits: correctable? ? offense_replacements : []
95
+ )
96
+ ]
97
+ ),
98
+ is_preferred: true
99
+ )
100
+ end
101
+ # rubocop:enable Metrics/MethodLength
102
+
103
+ # rubocop:disable Metrics/MethodLength
104
+ def offense_replacements
105
+ @offense.corrector.as_replacements.map do |range, replacement|
106
+ LanguageServer::Protocol::Interface::TextEdit.new(
107
+ range: LanguageServer::Protocol::Interface::Range.new(
108
+ start: LanguageServer::Protocol::Interface::Position.new(
109
+ line: range.line - 1,
110
+ character: range.column
111
+ ),
112
+ end: LanguageServer::Protocol::Interface::Position.new(
113
+ line: range.last_line - 1,
114
+ character: range.last_column
115
+ )
116
+ ),
117
+ new_text: replacement
118
+ )
119
+ end
120
+ end
121
+ # rubocop:enable Metrics/MethodLength
122
+
123
+ # rubocop:disable Metrics/MethodLength
124
+ def disable_line_action
125
+ LanguageServer::Protocol::Interface::CodeAction.new(
126
+ title: "Disable #{@offense.cop_name} for this line",
127
+ kind: LanguageServer::Protocol::Constant::CodeActionKind::QUICK_FIX,
128
+ edit: LanguageServer::Protocol::Interface::WorkspaceEdit.new(
129
+ document_changes: [
130
+ LanguageServer::Protocol::Interface::TextDocumentEdit.new(
131
+ text_document: LanguageServer::Protocol::Interface::OptionalVersionedTextDocumentIdentifier.new(
132
+ uri: ensure_uri_scheme(@uri.to_s).to_s,
133
+ version: nil
134
+ ),
135
+ edits: line_disable_comment
136
+ )
137
+ ]
138
+ )
139
+ )
140
+ end
141
+ # rubocop:enable Metrics/MethodLength
142
+
143
+ 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]
163
+ end
164
+
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
173
+ else
174
+ line.length
175
+ end
176
+ end
177
+
178
+ def correctable?
179
+ !@offense.corrector.nil?
180
+ end
181
+
182
+ def ensure_uri_scheme(uri)
183
+ uri = URI.parse(uri)
184
+ uri.scheme = 'file' if uri.scheme.nil?
185
+ uri
186
+ end
187
+ end
188
+ end
189
+ end