rubocop 1.69.2 → 1.75.1

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 (349) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +154 -23
  5. data/config/internal_affairs.yml +20 -0
  6. data/config/obsoletion.yml +3 -1
  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 +1 -1
  11. data/lib/rubocop/comment_config.rb +2 -2
  12. data/lib/rubocop/config.rb +52 -10
  13. data/lib/rubocop/config_loader.rb +52 -9
  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 +20 -9
  19. data/lib/rubocop/cop/autocorrect_logic.rb +1 -1
  20. data/lib/rubocop/cop/base.rb +6 -0
  21. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  22. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  23. data/lib/rubocop/cop/internal_affairs/cop_enabled.rb +85 -0
  24. data/lib/rubocop/cop/internal_affairs/example_description.rb +7 -3
  25. data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
  26. data/lib/rubocop/cop/internal_affairs/location_expression.rb +2 -1
  27. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +3 -2
  28. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  29. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb +63 -0
  30. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +131 -0
  31. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +229 -0
  32. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +91 -0
  33. data/lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb +126 -0
  34. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +4 -3
  35. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +90 -0
  36. data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
  37. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
  38. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +3 -1
  39. data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
  40. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +7 -1
  41. data/lib/rubocop/cop/internal_affairs.rb +6 -16
  42. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +1 -1
  43. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -8
  44. data/lib/rubocop/cop/layout/block_alignment.rb +4 -1
  45. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
  46. data/lib/rubocop/cop/layout/class_structure.rb +9 -9
  47. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +4 -4
  48. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  49. data/lib/rubocop/cop/layout/else_alignment.rb +2 -2
  50. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  51. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +7 -11
  52. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +28 -1
  53. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
  54. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
  55. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
  56. data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
  57. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  58. data/lib/rubocop/cop/layout/first_argument_indentation.rb +3 -8
  59. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -7
  60. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -7
  61. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
  62. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  63. data/lib/rubocop/cop/layout/hash_alignment.rb +6 -4
  64. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -1
  65. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  66. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +7 -1
  67. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -2
  68. data/lib/rubocop/cop/layout/line_length.rb +9 -4
  69. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
  70. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
  71. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +25 -0
  72. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -0
  73. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +4 -4
  74. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
  75. data/lib/rubocop/cop/layout/redundant_line_break.rb +16 -11
  76. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
  77. data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -1
  78. data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
  79. data/lib/rubocop/cop/layout/space_after_comma.rb +1 -1
  80. data/lib/rubocop/cop/layout/space_after_method_name.rb +1 -1
  81. data/lib/rubocop/cop/layout/space_after_semicolon.rb +1 -1
  82. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -0
  83. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  84. data/lib/rubocop/cop/layout/space_around_operators.rb +7 -4
  85. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
  86. data/lib/rubocop/cop/layout/space_before_comma.rb +1 -1
  87. data/lib/rubocop/cop/layout/space_before_semicolon.rb +1 -1
  88. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
  89. data/lib/rubocop/cop/layout/trailing_whitespace.rb +5 -3
  90. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  91. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +119 -0
  92. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -3
  93. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +1 -1
  94. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +3 -3
  95. data/lib/rubocop/cop/lint/constant_reassignment.rb +148 -0
  96. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
  97. data/lib/rubocop/cop/lint/debugger.rb +3 -3
  98. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +1 -1
  99. data/lib/rubocop/cop/lint/duplicate_methods.rb +0 -14
  100. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -1
  101. data/lib/rubocop/cop/lint/duplicate_set_element.rb +20 -7
  102. data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
  103. data/lib/rubocop/cop/lint/empty_expression.rb +0 -2
  104. data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
  105. data/lib/rubocop/cop/lint/float_comparison.rb +6 -8
  106. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  107. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
  108. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  109. data/lib/rubocop/cop/lint/literal_as_condition.rb +103 -9
  110. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +24 -6
  111. data/lib/rubocop/cop/lint/missing_super.rb +2 -2
  112. data/lib/rubocop/cop/lint/mixed_case_range.rb +3 -3
  113. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -1
  114. data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -4
  115. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  116. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +4 -3
  117. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
  118. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +18 -31
  119. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -1
  120. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -5
  121. data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
  122. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
  123. data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
  124. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +2 -2
  125. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +258 -0
  126. data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
  127. data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
  128. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  129. data/lib/rubocop/cop/lint/return_in_void_context.rb +4 -11
  130. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -1
  131. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +8 -1
  132. data/lib/rubocop/cop/lint/shared_mutable_default.rb +76 -0
  133. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  134. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
  135. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  136. data/lib/rubocop/cop/lint/syntax.rb +4 -1
  137. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +1 -4
  138. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +3 -1
  139. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -1
  140. data/lib/rubocop/cop/lint/unreachable_code.rb +2 -1
  141. data/lib/rubocop/cop/lint/unreachable_loop.rb +6 -6
  142. data/lib/rubocop/cop/lint/useless_access_modifier.rb +5 -4
  143. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  144. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +71 -0
  145. data/lib/rubocop/cop/lint/useless_method_definition.rb +1 -1
  146. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +2 -1
  147. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +2 -2
  148. data/lib/rubocop/cop/lint/void.rb +12 -9
  149. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  150. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  151. data/lib/rubocop/cop/metrics/collection_literal_length.rb +7 -0
  152. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  153. data/lib/rubocop/cop/metrics/method_length.rb +9 -1
  154. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  155. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  156. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  157. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
  158. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  159. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  160. data/lib/rubocop/cop/mixin/check_line_breakable.rb +13 -13
  161. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +1 -1
  162. data/lib/rubocop/cop/mixin/comments_help.rb +4 -2
  163. data/lib/rubocop/cop/mixin/dig_help.rb +1 -1
  164. data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
  165. data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
  166. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  167. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +22 -22
  168. data/lib/rubocop/cop/mixin/hash_subset.rb +203 -0
  169. data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
  170. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  171. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  172. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +48 -24
  173. data/lib/rubocop/cop/mixin/range_help.rb +15 -3
  174. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  175. data/lib/rubocop/cop/mixin/statement_modifier.rb +8 -3
  176. data/lib/rubocop/cop/mixin/string_help.rb +2 -2
  177. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  178. data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
  179. data/lib/rubocop/cop/mixin/trailing_comma.rb +15 -3
  180. data/lib/rubocop/cop/naming/block_forwarding.rb +19 -15
  181. data/lib/rubocop/cop/naming/method_name.rb +64 -8
  182. data/lib/rubocop/cop/naming/predicate_name.rb +44 -0
  183. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +3 -3
  184. data/lib/rubocop/cop/naming/variable_name.rb +51 -6
  185. data/lib/rubocop/cop/registry.rb +9 -6
  186. data/lib/rubocop/cop/security/compound_hash.rb +1 -0
  187. data/lib/rubocop/cop/style/access_modifier_declarations.rb +34 -5
  188. data/lib/rubocop/cop/style/accessor_grouping.rb +19 -5
  189. data/lib/rubocop/cop/style/and_or.rb +1 -1
  190. data/lib/rubocop/cop/style/arguments_forwarding.rb +39 -23
  191. data/lib/rubocop/cop/style/array_first_last.rb +18 -2
  192. data/lib/rubocop/cop/style/array_intersect.rb +39 -28
  193. data/lib/rubocop/cop/style/block_delimiters.rb +9 -21
  194. data/lib/rubocop/cop/style/class_and_module_children.rb +35 -10
  195. data/lib/rubocop/cop/style/collection_methods.rb +2 -1
  196. data/lib/rubocop/cop/style/combinable_defined.rb +1 -1
  197. data/lib/rubocop/cop/style/combinable_loops.rb +3 -2
  198. data/lib/rubocop/cop/style/commented_keyword.rb +10 -3
  199. data/lib/rubocop/cop/style/comparable_between.rb +75 -0
  200. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
  201. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -4
  202. data/lib/rubocop/cop/style/documentation.rb +1 -1
  203. data/lib/rubocop/cop/style/double_negation.rb +4 -4
  204. data/lib/rubocop/cop/style/each_for_simple_loop.rb +4 -7
  205. data/lib/rubocop/cop/style/each_with_object.rb +2 -3
  206. data/lib/rubocop/cop/style/empty_else.rb +4 -2
  207. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  208. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  209. data/lib/rubocop/cop/style/endless_method.rb +163 -18
  210. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  211. data/lib/rubocop/cop/style/exact_regexp_match.rb +3 -10
  212. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
  213. data/lib/rubocop/cop/style/explicit_block_argument.rb +15 -2
  214. data/lib/rubocop/cop/style/exponential_notation.rb +3 -3
  215. data/lib/rubocop/cop/style/fetch_env_var.rb +1 -1
  216. data/lib/rubocop/cop/style/float_division.rb +8 -4
  217. data/lib/rubocop/cop/style/for.rb +1 -0
  218. data/lib/rubocop/cop/style/format_string_token.rb +38 -11
  219. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  220. data/lib/rubocop/cop/style/guard_clause.rb +2 -1
  221. data/lib/rubocop/cop/style/hash_each_methods.rb +6 -8
  222. data/lib/rubocop/cop/style/hash_except.rb +24 -148
  223. data/lib/rubocop/cop/style/hash_fetch_chain.rb +105 -0
  224. data/lib/rubocop/cop/style/hash_slice.rb +80 -0
  225. data/lib/rubocop/cop/style/hash_syntax.rb +6 -3
  226. data/lib/rubocop/cop/style/identical_conditional_branches.rb +22 -3
  227. data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
  228. data/lib/rubocop/cop/style/if_unless_modifier.rb +5 -5
  229. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
  230. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  231. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  232. data/lib/rubocop/cop/style/inverse_methods.rb +15 -11
  233. data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
  234. data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
  235. data/lib/rubocop/cop/style/it_assignment.rb +36 -0
  236. data/lib/rubocop/cop/style/it_block_parameter.rb +100 -0
  237. data/lib/rubocop/cop/style/keyword_parameters_order.rb +14 -8
  238. data/lib/rubocop/cop/style/lambda.rb +1 -0
  239. data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
  240. data/lib/rubocop/cop/style/map_into_array.rb +2 -1
  241. data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
  242. data/lib/rubocop/cop/style/map_to_set.rb +3 -2
  243. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +22 -15
  244. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -0
  245. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -1
  246. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
  247. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  248. data/lib/rubocop/cop/style/missing_else.rb +2 -0
  249. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -2
  250. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
  251. data/lib/rubocop/cop/style/multiple_comparison.rb +26 -20
  252. data/lib/rubocop/cop/style/mutable_constant.rb +3 -3
  253. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
  254. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  255. data/lib/rubocop/cop/style/next.rb +44 -0
  256. data/lib/rubocop/cop/style/object_then.rb +14 -15
  257. data/lib/rubocop/cop/style/open_struct_use.rb +5 -5
  258. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -5
  259. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  260. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  261. data/lib/rubocop/cop/style/proc.rb +2 -2
  262. data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
  263. data/lib/rubocop/cop/style/raise_args.rb +14 -12
  264. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  265. data/lib/rubocop/cop/style/redundant_begin.rb +2 -1
  266. data/lib/rubocop/cop/style/redundant_condition.rb +47 -2
  267. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +16 -5
  268. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +6 -10
  269. data/lib/rubocop/cop/style/redundant_each.rb +1 -1
  270. data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
  271. data/lib/rubocop/cop/style/redundant_format.rb +257 -0
  272. data/lib/rubocop/cop/style/redundant_freeze.rb +3 -3
  273. data/lib/rubocop/cop/style/redundant_initialize.rb +12 -3
  274. data/lib/rubocop/cop/style/redundant_line_continuation.rb +34 -13
  275. data/lib/rubocop/cop/style/redundant_parentheses.rb +30 -15
  276. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +3 -0
  277. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  278. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  279. data/lib/rubocop/cop/style/redundant_self.rb +1 -0
  280. data/lib/rubocop/cop/style/redundant_self_assignment.rb +14 -28
  281. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  282. data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
  283. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  284. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
  285. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  286. data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
  287. data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
  288. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  289. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +2 -1
  290. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  291. data/lib/rubocop/cop/style/single_line_do_end_block.rb +4 -3
  292. data/lib/rubocop/cop/style/single_line_methods.rb +6 -7
  293. data/lib/rubocop/cop/style/slicing_with_range.rb +40 -11
  294. data/lib/rubocop/cop/style/sole_nested_conditional.rb +41 -106
  295. data/lib/rubocop/cop/style/string_concatenation.rb +2 -2
  296. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  297. data/lib/rubocop/cop/style/string_methods.rb +1 -1
  298. data/lib/rubocop/cop/style/super_arguments.rb +65 -17
  299. data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
  300. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  301. data/lib/rubocop/cop/style/top_level_method_definition.rb +2 -1
  302. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -1
  303. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
  304. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
  305. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  306. data/lib/rubocop/cop/style/while_until_modifier.rb +0 -1
  307. data/lib/rubocop/cop/style/yoda_condition.rb +8 -4
  308. data/lib/rubocop/cop/style/yoda_expression.rb +2 -1
  309. data/lib/rubocop/cop/util.rb +12 -5
  310. data/lib/rubocop/cop/utils/format_string.rb +10 -5
  311. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  312. data/lib/rubocop/cop/variable_force/variable.rb +9 -2
  313. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
  314. data/lib/rubocop/cop/variable_force.rb +1 -1
  315. data/lib/rubocop/cops_documentation_generator.rb +25 -14
  316. data/lib/rubocop/directive_comment.rb +45 -11
  317. data/lib/rubocop/ext/regexp_node.rb +0 -1
  318. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  319. data/lib/rubocop/lsp/diagnostic.rb +189 -0
  320. data/lib/rubocop/lsp/logger.rb +2 -2
  321. data/lib/rubocop/lsp/routes.rb +7 -23
  322. data/lib/rubocop/lsp/runtime.rb +18 -50
  323. data/lib/rubocop/lsp/server.rb +0 -2
  324. data/lib/rubocop/lsp/stdin_runner.rb +85 -0
  325. data/lib/rubocop/options.rb +28 -12
  326. data/lib/rubocop/path_util.rb +15 -8
  327. data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
  328. data/lib/rubocop/plugin/load_error.rb +26 -0
  329. data/lib/rubocop/plugin/loader.rb +100 -0
  330. data/lib/rubocop/plugin/not_supported_error.rb +29 -0
  331. data/lib/rubocop/plugin.rb +46 -0
  332. data/lib/rubocop/rake_task.rb +4 -1
  333. data/lib/rubocop/result_cache.rb +13 -13
  334. data/lib/rubocop/rspec/cop_helper.rb +13 -1
  335. data/lib/rubocop/rspec/expect_offense.rb +6 -2
  336. data/lib/rubocop/rspec/shared_contexts.rb +39 -1
  337. data/lib/rubocop/rspec/support.rb +4 -2
  338. data/lib/rubocop/runner.rb +10 -7
  339. data/lib/rubocop/server/cache.rb +35 -2
  340. data/lib/rubocop/server/cli.rb +2 -2
  341. data/lib/rubocop/target_finder.rb +1 -0
  342. data/lib/rubocop/target_ruby.rb +16 -1
  343. data/lib/rubocop/version.rb +30 -8
  344. data/lib/rubocop.rb +16 -1
  345. data/lib/ruby_lsp/rubocop/addon.rb +75 -0
  346. data/lib/ruby_lsp/rubocop/runtime_adapter.rb +65 -0
  347. metadata +59 -16
  348. data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
  349. data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -28
@@ -93,9 +93,9 @@ module RuboCop
93
93
  (call $(call _ $:sort_by _) ${:last :first})
94
94
  (send $(send _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
95
95
 
96
- (call ({block numblock} $(call _ ${:sort_by :sort}) ...) ${:last :first})
96
+ (call (any_block $(call _ ${:sort_by :sort}) ...) ${:last :first})
97
97
  (call
98
- ({block numblock} $(call _ ${:sort_by :sort}) ...)
98
+ (any_block $(call _ ${:sort_by :sort}) ...)
99
99
  ${:[] :at :slice} {(int 0) (int -1)}
100
100
  )
101
101
  }
@@ -21,6 +21,7 @@ module RuboCop
21
21
 
22
22
  MSG_BLOCK = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'
23
23
  MSG_NUMBLOCK = 'Use `sort` instead of `sort_by { _1 }`.'
24
+ MSG_ITBLOCK = 'Use `sort` instead of `sort_by { it }`.'
24
25
 
25
26
  def on_block(node)
26
27
  redundant_sort_by_block(node) do |send, var_name|
@@ -36,7 +37,17 @@ module RuboCop
36
37
  redundant_sort_by_numblock(node) do |send|
37
38
  range = sort_by_range(send, node)
38
39
 
39
- add_offense(range, message: format(MSG_NUMBLOCK)) do |corrector|
40
+ add_offense(range, message: MSG_NUMBLOCK) do |corrector|
41
+ corrector.replace(range, 'sort')
42
+ end
43
+ end
44
+ end
45
+
46
+ def on_itblock(node)
47
+ redundant_sort_by_itblock(node) do |send|
48
+ range = sort_by_range(send, node)
49
+
50
+ add_offense(range, message: MSG_ITBLOCK) do |corrector|
40
51
  corrector.replace(range, 'sort')
41
52
  end
42
53
  end
@@ -54,6 +65,11 @@ module RuboCop
54
65
  (numblock $(call _ :sort_by) 1 (lvar :_1))
55
66
  PATTERN
56
67
 
68
+ # @!method redundant_sort_by_itblock(node)
69
+ def_node_matcher :redundant_sort_by_itblock, <<~PATTERN
70
+ (itblock $(call _ :sort_by) _ (lvar :it))
71
+ PATTERN
72
+
57
73
  def sort_by_range(send, node)
58
74
  range_between(send.loc.selector.begin_pos, node.loc.end.end_pos)
59
75
  end
@@ -41,7 +41,7 @@ module RuboCop
41
41
  MSG = 'Redundant escape of %<char>s inside string literal.'
42
42
 
43
43
  def on_str(node)
44
- return if node.parent&.regexp_type? || node.parent&.xstr_type? || node.character_literal?
44
+ return if node.parent&.type?(:regexp, :xstr) || node.character_literal?
45
45
 
46
46
  str_contents_range = str_contents_range(node)
47
47
 
@@ -147,7 +147,7 @@ module RuboCop
147
147
  end
148
148
 
149
149
  def heredoc?(node)
150
- (node.str_type? || node.dstr_type?) && node.heredoc?
150
+ node.type?(:str, :dstr) && node.heredoc?
151
151
  end
152
152
 
153
153
  def delimiter?(node, char)
@@ -67,11 +67,13 @@ module RuboCop
67
67
  node.parent && parentheses?(node.parent)
68
68
  end
69
69
 
70
+ # rubocop:disable Metrics/AbcSize
70
71
  def correct_rescue_block(corrector, node, parenthesized)
71
72
  operation = node.body
72
73
 
73
74
  node_indentation, node_offset = indentation_and_offset(node, parenthesized)
74
75
 
76
+ corrector.wrap(operation, '[', ']') if operation.array_type? && !operation.bracketed?
75
77
  corrector.remove(range_between(operation.source_range.end_pos, node.source_range.end_pos))
76
78
  corrector.insert_before(operation, "begin\n#{node_indentation}")
77
79
  corrector.insert_after(heredoc_end(operation) || operation, <<~RESCUE_CLAUSE.chop)
@@ -81,6 +83,7 @@ module RuboCop
81
83
  #{node_offset}end
82
84
  RESCUE_CLAUSE
83
85
  end
86
+ # rubocop:enable Metrics/AbcSize
84
87
 
85
88
  def indentation_and_offset(node, parenthesized)
86
89
  node_indentation = indentation(node)
@@ -83,7 +83,7 @@ module RuboCop
83
83
  end
84
84
 
85
85
  def scoped_node?(node)
86
- node.def_type? || node.defs_type? || node.lambda?
86
+ node.type?(:def, :defs) || node.lambda?
87
87
  end
88
88
 
89
89
  # @!method chained_send?(node)
@@ -312,7 +312,7 @@ module RuboCop
312
312
  end
313
313
 
314
314
  def chain_length(method_chain, method)
315
- method.each_ancestor(:send, :csend).inject(0) do |total, ancestor|
315
+ method.each_ancestor(:call).inject(0) do |total, ancestor|
316
316
  break total + 1 if ancestor == method_chain
317
317
 
318
318
  total + 1
@@ -323,7 +323,7 @@ module RuboCop
323
323
  return true if unsafe_method?(method)
324
324
 
325
325
  method.each_ancestor(:send).any? do |ancestor|
326
- break true unless config.for_cop('Lint/SafeNavigationChain')['Enabled']
326
+ break true unless config.cop_enabled?('Lint/SafeNavigationChain')
327
327
 
328
328
  break true if unsafe_method?(ancestor)
329
329
  break true if nil_methods.include?(ancestor.method_name)
@@ -59,6 +59,7 @@ module RuboCop
59
59
  {
60
60
  (block call (args (arg $_)) ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
61
61
  (numblock call $1 ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
62
+ (itblock call $_ ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
62
63
  }
63
64
  PATTERN
64
65
 
@@ -137,6 +138,7 @@ module RuboCop
137
138
  return unless (block_arg_name, regexp_method_send_node = regexp_match?(block_node))
138
139
 
139
140
  block_arg_name = :"_#{block_arg_name}" if block_node.numblock_type?
141
+
140
142
  return unless calls_lvar?(regexp_method_send_node, block_arg_name)
141
143
 
142
144
  regexp_method_send_node
@@ -150,7 +152,8 @@ module RuboCop
150
152
  return node.child_nodes.first if node.match_with_lvasgn_type?
151
153
 
152
154
  if node.receiver.lvar_type? &&
153
- (block.numblock_type? || node.receiver.source == block.first_argument.source)
155
+ (block.type?(:numblock, :itblock) ||
156
+ node.receiver.source == block.first_argument.source)
154
157
  node.first_argument
155
158
  elsif node.first_argument.lvar_type?
156
159
  node.receiver
@@ -164,7 +164,7 @@ module RuboCop
164
164
 
165
165
  ast = processed_source.ast
166
166
  @range_nodes = ast.range_type? ? [ast] : []
167
- @range_nodes.concat(ast.each_descendant(:irange, :erange).to_a)
167
+ @range_nodes.concat(ast.each_descendant(:range).to_a)
168
168
  end
169
169
  end
170
170
  end
@@ -68,7 +68,7 @@ module RuboCop
68
68
  def on_send(node)
69
69
  return if allow_send? && !node.method?(:public_send)
70
70
  return unless (first_argument = node.first_argument)
71
- return unless STATIC_METHOD_NAME_NODE_TYPES.include?(first_argument.type)
71
+ return unless first_argument.type?(*STATIC_METHOD_NAME_NODE_TYPES)
72
72
 
73
73
  offense_range = offense_range(node)
74
74
  method_name = first_argument.value
@@ -83,6 +83,7 @@ module RuboCop
83
83
  end
84
84
  end
85
85
  end
86
+ alias on_csend on_send
86
87
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
87
88
 
88
89
  private
@@ -10,7 +10,7 @@ module RuboCop
10
10
  # parameters.
11
11
  #
12
12
  # Configuration option: Methods
13
- # Should be set to use this cop. Array of hashes, where each key is the
13
+ # Should be set to use this cop. `Array` of hashes, where each key is the
14
14
  # method name and value - array of argument names.
15
15
  #
16
16
  # @example Methods: [{reduce: %w[a b]}]
@@ -56,11 +56,13 @@ module RuboCop
56
56
  end
57
57
  # rubocop:enable Metrics/AbcSize
58
58
  alias on_numblock on_block
59
+ alias on_itblock on_block
59
60
 
60
61
  private
61
62
 
62
63
  def do_line(node)
63
- if node.numblock_type? || node.arguments.children.empty? || node.send_node.lambda_literal?
64
+ if node.type?(:numblock, :itblock) ||
65
+ node.arguments.children.empty? || node.send_node.lambda_literal?
64
66
  node.loc.begin
65
67
  else
66
68
  node.arguments
@@ -68,8 +70,7 @@ module RuboCop
68
70
  end
69
71
 
70
72
  def single_line_blocks_preferred?
71
- redundant_line_break_config = @config.for_cop('Layout/RedundantLineBreak')
72
- redundant_line_break_config['Enabled'] && redundant_line_break_config['InspectBlocks']
73
+ @config.for_enabled_cop('Layout/RedundantLineBreak')['InspectBlocks']
73
74
  end
74
75
  end
75
76
  end
@@ -8,9 +8,9 @@ module RuboCop
8
8
  #
9
9
  # Endless methods added in Ruby 3.0 are also accepted by this cop.
10
10
  #
11
- # If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line` or
12
- # `allow_always`, single-line methods will be autocorrected to endless
13
- # methods if there is only one statement in the body.
11
+ # If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line`, `allow_always`,
12
+ # `require_single_line`, or `require_always`, single-line methods will be autocorrected
13
+ # to endless methods if there is only one statement in the body.
14
14
  #
15
15
  # @example
16
16
  # # bad
@@ -68,7 +68,7 @@ module RuboCop
68
68
  return false if body_node.parent.assignment_method? ||
69
69
  NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES.include?(body_node.type)
70
70
 
71
- !(body_node.begin_type? || body_node.kwbegin_type?)
71
+ !body_node.type?(:begin, :kwbegin)
72
72
  end
73
73
 
74
74
  def correct_to_multiline(corrector, node)
@@ -134,10 +134,9 @@ module RuboCop
134
134
  end
135
135
 
136
136
  def disallow_endless_method_style?
137
- endless_method_config = config.for_cop('Style/EndlessMethod')
138
- return true unless endless_method_config['Enabled']
137
+ return true unless config.cop_enabled?('Style/EndlessMethod')
139
138
 
140
- endless_method_config['EnforcedStyle'] == 'disallow'
139
+ config.for_cop('Style/EndlessMethod')['EnforcedStyle'] == 'disallow'
141
140
  end
142
141
  end
143
142
  end
@@ -78,38 +78,67 @@ module RuboCop
78
78
  return unless node.arguments.one?
79
79
 
80
80
  range_node = node.first_argument
81
- selector = node.loc.selector
82
- unless (message, removal_range = offense_message_with_removal_range(range_node, selector))
83
- return
84
- end
81
+ offense_range = find_offense_range(node)
82
+ return unless (message, removal_range =
83
+ offense_message_with_removal_range(node, range_node, offense_range))
84
+
85
+ # Changing the range to beginningless or endless when unparenthesized
86
+ # changes the semantics of the code, and thus will not be considered
87
+ # an offense.
88
+ return if removal_range != offense_range && unparenthesized_call?(node)
85
89
 
86
- add_offense(selector, message: message) do |corrector|
90
+ add_offense(offense_range, message: message) do |corrector|
87
91
  corrector.remove(removal_range)
88
92
  end
89
93
  end
94
+ alias on_csend on_send
90
95
 
91
96
  private
92
97
 
93
- def offense_message_with_removal_range(range_node, selector)
98
+ def unparenthesized_call?(node)
99
+ node.loc.dot && !node.parenthesized?
100
+ end
101
+
102
+ def find_offense_range(node)
103
+ if node.loc.dot
104
+ node.loc.dot.join(node.source_range.end)
105
+ else
106
+ node.loc.selector
107
+ end
108
+ end
109
+
110
+ def offense_message_with_removal_range(node, range_node, offense_range)
94
111
  if range_from_zero_till_minus_one?(range_node)
95
- [format(MSG_USELESS_RANGE, prefer: selector.source), selector]
112
+ [format(MSG_USELESS_RANGE, prefer: offense_range.source), offense_range]
96
113
  elsif range_till_minus_one?(range_node)
97
114
  [
98
- format(MSG, prefer: endless(range_node), current: selector.source), range_node.end
115
+ offense_message_for_partial_range(node, endless(range_node), offense_range),
116
+ range_node.end
99
117
  ]
100
118
  elsif range_from_zero?(range_node) && target_ruby_version >= 2.7
101
119
  [
102
- format(MSG, prefer: beginless(range_node), current: selector.source), range_node.begin
120
+ offense_message_for_partial_range(node, beginless(range_node), offense_range),
121
+ range_node.begin
103
122
  ]
104
123
  end
105
124
  end
106
125
 
126
+ def offense_message_for_partial_range(node, prefer, offense_range)
127
+ current = node.loc.dot ? arguments_source(node) : offense_range.source
128
+ prefer = "[#{prefer}]" unless node.loc.dot
129
+ format(MSG, prefer: prefer, current: current)
130
+ end
131
+
107
132
  def endless(range_node)
108
- "[#{range_node.begin.source}#{range_node.loc.operator.source}]"
133
+ "#{range_node.begin.source}#{range_node.loc.operator.source}"
109
134
  end
110
135
 
111
136
  def beginless(range_node)
112
- "[#{range_node.loc.operator.source}#{range_node.end.source}]"
137
+ "#{range_node.loc.operator.source}#{range_node.end.source}"
138
+ end
139
+
140
+ def arguments_source(node)
141
+ node.first_argument.source_range.join(node.last_argument.source_range.end).source
113
142
  end
114
143
  end
115
144
  end
@@ -96,11 +96,7 @@ module RuboCop
96
96
  end
97
97
 
98
98
  def autocorrect(corrector, node, if_branch)
99
- if node.condition.or_type? || node.condition.assignment?
100
- corrector.wrap(node.condition, '(', ')')
101
- end
102
-
103
- if outer_condition_modify_form?(node, if_branch)
99
+ if node.modifier_form?
104
100
  autocorrect_outer_condition_modify_form(corrector, node, if_branch)
105
101
  else
106
102
  autocorrect_outer_condition_basic(corrector, node, if_branch)
@@ -108,74 +104,48 @@ module RuboCop
108
104
  end
109
105
 
110
106
  def autocorrect_outer_condition_basic(corrector, node, if_branch)
111
- correct_from_unless_to_if(corrector, node) if node.unless?
112
-
113
- outer_condition = node.condition
114
- correct_outer_condition(corrector, outer_condition)
107
+ correct_node(corrector, node)
115
108
 
116
- and_operator = if_branch.unless? ? ' && !' : ' && '
117
109
  if if_branch.modifier_form?
118
- correct_for_guard_condition_style(corrector, outer_condition, if_branch, and_operator)
110
+ correct_for_guard_condition_style(corrector, node, if_branch)
119
111
  else
120
- correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
112
+ correct_for_basic_condition_style(corrector, node, if_branch)
121
113
  correct_for_comment(corrector, node, if_branch)
122
114
  end
123
115
  end
124
116
 
125
- def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
126
- correct_from_unless_to_if(corrector, if_branch, is_modify_form: true) if if_branch.unless?
127
- correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
117
+ def correct_node(corrector, node)
118
+ corrector.replace(node.loc.keyword, 'if') if node.unless?
119
+ corrector.replace(node.condition, chainable_condition(node))
128
120
  end
129
121
 
130
- def correct_from_unless_to_if(corrector, node, is_modify_form: false)
131
- corrector.replace(node.loc.keyword, 'if')
132
-
133
- insert_bang(corrector, node, is_modify_form)
134
- end
122
+ def correct_for_guard_condition_style(corrector, node, if_branch)
123
+ corrector.insert_after(node.condition, " && #{chainable_condition(if_branch)}")
135
124
 
136
- def correct_for_guard_condition_style(corrector, outer_condition, if_branch, and_operator)
137
- condition = if_branch.condition
138
- corrector.insert_after(outer_condition, "#{and_operator}#{replace_condition(condition)}")
139
-
140
- range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
125
+ range = range_between(
126
+ if_branch.loc.keyword.begin_pos, if_branch.condition.source_range.end_pos
127
+ )
141
128
  corrector.remove(range_with_surrounding_space(range, newlines: false))
142
- corrector.remove(if_branch.loc.keyword)
143
129
  end
144
130
 
145
- def correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
131
+ def correct_for_basic_condition_style(corrector, node, if_branch)
146
132
  range = range_between(
147
133
  node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos
148
134
  )
149
- corrector.replace(range, and_operator)
150
- corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
151
-
152
- wrap_condition(corrector, if_branch.condition)
153
- end
154
-
155
- def wrap_condition(corrector, condition)
156
- # Handle `send` and `block` nodes that need to be wrapped in parens
157
- # FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
158
- # but wrapping the argument list would be a more ergonomic correction.
159
- node_to_check = condition&.block_type? ? condition.send_node : condition
160
- return unless wrap_condition?(node_to_check)
135
+ corrector.replace(range, ' && ')
161
136
 
162
- if condition.call_type?
163
- source = parenthesized_method_arguments(condition)
137
+ corrector.replace(if_branch.condition, chainable_condition(if_branch))
164
138
 
165
- corrector.replace(condition, source)
166
- else
167
- corrector.wrap(condition, '(', ')')
168
- end
139
+ corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
169
140
  end
170
141
 
171
- def correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
172
- condition = if_branch.condition
173
- corrector.insert_before(condition,
174
- "#{'!' if node.unless?}#{replace_condition(node.condition)} && ")
142
+ def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
143
+ correct_node(corrector, if_branch)
175
144
 
176
- corrector.remove(node.condition)
177
- corrector.remove(range_with_surrounding_space(node.loc.keyword, newlines: false))
178
- corrector.replace(if_branch.loc.keyword, 'if')
145
+ corrector.insert_before(if_branch.condition, "#{chainable_condition(node)} && ")
146
+
147
+ range = range_between(node.loc.keyword.begin_pos, node.condition.source_range.end_pos)
148
+ corrector.remove(range_with_surrounding_space(range, newlines: false))
179
149
  end
180
150
 
181
151
  def correct_for_comment(corrector, node, if_branch)
@@ -187,67 +157,36 @@ module RuboCop
187
157
  corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
188
158
  end
189
159
 
190
- def correct_outer_condition(corrector, condition)
191
- return unless require_parentheses?(condition)
160
+ def chainable_condition(node)
161
+ wrapped_condition = add_parentheses_if_needed(node.condition)
192
162
 
193
- end_pos = condition.loc.selector.end_pos
194
- begin_pos = condition.first_argument.source_range.begin_pos
195
- return if end_pos > begin_pos
163
+ return wrapped_condition if node.if?
196
164
 
197
- range = range_between(end_pos, begin_pos)
198
- corrector.remove(range)
199
- corrector.insert_after(range, '(')
200
- corrector.insert_after(condition.last_argument, ')')
165
+ node.condition.and_type? ? "!(#{wrapped_condition})" : "!#{wrapped_condition}"
201
166
  end
202
167
 
203
- def insert_bang(corrector, node, is_modify_form)
204
- condition = node.condition
205
-
206
- if (condition.send_type? && condition.comparison_method? && !condition.parenthesized?) ||
207
- (is_modify_form && wrap_condition?(condition))
208
- corrector.wrap(node.condition, '!(', ')')
209
- elsif condition.and_type?
210
- insert_bang_for_and(corrector, node)
211
- else
212
- corrector.insert_before(condition, '!')
213
- end
214
- end
215
-
216
- def insert_bang_for_and(corrector, node)
217
- lhs, rhs = *node # rubocop:disable InternalAffairs/NodeDestructuring
168
+ def add_parentheses_if_needed(condition)
169
+ # Handle `send` and `block` nodes that need to be wrapped in parens
170
+ # FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
171
+ # but wrapping the argument list would be a more ergonomic correction.
172
+ node_to_check = condition&.any_block_type? ? condition.send_node : condition
173
+ return condition.source unless add_parentheses?(node_to_check)
218
174
 
219
- if lhs.and_type?
220
- insert_bang_for_and(corrector, lhs)
221
- corrector.insert_before(rhs, '!') if rhs
175
+ if parenthesize_method?(condition)
176
+ parenthesized_method_arguments(condition)
222
177
  else
223
- corrector.insert_before(lhs, '!')
224
- corrector.insert_before(rhs, '!')
178
+ "(#{condition.source})"
225
179
  end
226
180
  end
227
181
 
228
- def require_parentheses?(condition)
229
- condition.call_type? && !condition.arguments.empty? && !condition.parenthesized? &&
230
- !condition.comparison_method?
231
- end
232
-
233
- def arguments_range(node)
234
- range_between(
235
- node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
236
- )
182
+ def parenthesize_method?(node)
183
+ node.call_type? && node.arguments.any? && !node.parenthesized? &&
184
+ !node.comparison_method? && !node.operator_method?
237
185
  end
238
186
 
239
- def wrap_condition?(node)
240
- node.operator_keyword? || (node.call_type? && node.arguments.any? && !node.parenthesized?)
241
- end
242
-
243
- def replace_condition(condition)
244
- return condition.source unless wrap_condition?(condition)
245
-
246
- if condition.call_type?
247
- parenthesized_method_arguments(condition)
248
- else
249
- "(#{condition.source})"
250
- end
187
+ def add_parentheses?(node)
188
+ node.assignment? || (node.operator_keyword? && !node.and_type?) ||
189
+ (node.call_type? && node.arguments.any? && !node.parenthesized?)
251
190
  end
252
191
 
253
192
  def parenthesized_method_arguments(node)
@@ -260,10 +199,6 @@ module RuboCop
260
199
  def allow_modifier?
261
200
  cop_config['AllowModifier']
262
201
  end
263
-
264
- def outer_condition_modify_form?(node, if_branch)
265
- node.condition.source_range.begin_pos > if_branch.condition.source_range.begin_pos
266
- end
267
202
  end
268
203
  end
269
204
  end
@@ -128,11 +128,11 @@ module RuboCop
128
128
  end
129
129
 
130
130
  def uncorrectable?(part)
131
- part.multiline? || heredoc?(part) || part.each_descendant(:block).any?
131
+ part.multiline? || heredoc?(part) || part.each_descendant(:any_block).any?
132
132
  end
133
133
 
134
134
  def heredoc?(node)
135
- return false unless node.str_type? || node.dstr_type?
135
+ return false unless node.type?(:str, :dstr)
136
136
 
137
137
  node.heredoc?
138
138
  end
@@ -69,7 +69,7 @@ module RuboCop
69
69
  end
70
70
 
71
71
  def all_string_literals?(nodes)
72
- nodes.all? { |n| n.str_type? || n.dstr_type? }
72
+ nodes.all? { |n| n.type?(:str, :dstr) }
73
73
  end
74
74
 
75
75
  def detect_quote_styles(node)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Enforces the use of consistent method names
7
- # from the String class.
7
+ # from the `String` class.
8
8
  #
9
9
  # @example
10
10
  # # bad