rubocop 1.48.1 → 1.62.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (386) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +7 -5
  4. data/assets/output.css.erb +159 -0
  5. data/assets/output.html.erb +1 -160
  6. data/config/default.yml +217 -35
  7. data/config/obsoletion.yml +5 -0
  8. data/lib/rubocop/cli/command/auto_generate_config.rb +22 -8
  9. data/lib/rubocop/cli/command/execute_runner.rb +7 -2
  10. data/lib/rubocop/cli/command/lsp.rb +19 -0
  11. data/lib/rubocop/cli.rb +16 -8
  12. data/lib/rubocop/config.rb +9 -3
  13. data/lib/rubocop/config_finder.rb +14 -4
  14. data/lib/rubocop/config_loader.rb +8 -9
  15. data/lib/rubocop/config_loader_resolver.rb +4 -3
  16. data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
  17. data/lib/rubocop/config_obsoletion.rb +13 -10
  18. data/lib/rubocop/config_validator.rb +14 -7
  19. data/lib/rubocop/cop/autocorrect_logic.rb +36 -13
  20. data/lib/rubocop/cop/base.rb +23 -4
  21. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
  22. data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
  23. data/lib/rubocop/cop/bundler/gem_comment.rb +3 -3
  24. data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
  25. data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
  26. data/lib/rubocop/cop/cop.rb +2 -2
  27. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
  28. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +4 -8
  29. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +5 -13
  30. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
  31. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  32. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  33. data/lib/rubocop/cop/exclude_limit.rb +1 -1
  34. data/lib/rubocop/cop/gemspec/dependency_version.rb +2 -2
  35. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +3 -3
  36. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  37. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
  38. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +5 -1
  39. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  40. data/lib/rubocop/cop/internal_affairs/cop_description.rb +33 -9
  41. data/lib/rubocop/cop/internal_affairs/example_description.rb +45 -24
  42. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +2 -2
  43. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
  44. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
  45. data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +8 -6
  46. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
  47. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
  48. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +127 -33
  49. data/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb +34 -0
  50. data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
  51. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +29 -2
  52. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  53. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  54. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  55. data/lib/rubocop/cop/layout/class_structure.rb +8 -0
  56. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
  57. data/lib/rubocop/cop/layout/dot_position.rb +1 -5
  58. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  59. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
  60. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +14 -7
  61. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +27 -4
  62. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  63. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
  64. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
  65. data/lib/rubocop/cop/layout/end_alignment.rb +20 -4
  66. data/lib/rubocop/cop/layout/extra_spacing.rb +3 -4
  67. data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -1
  68. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +22 -7
  69. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
  70. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
  71. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
  72. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
  73. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  74. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +6 -6
  75. data/lib/rubocop/cop/layout/heredoc_indentation.rb +4 -1
  76. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  77. data/lib/rubocop/cop/layout/indentation_width.rb +3 -3
  78. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  79. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  80. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +17 -9
  81. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
  82. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
  83. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
  84. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
  85. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
  86. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
  87. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
  88. data/lib/rubocop/cop/layout/redundant_line_break.rb +33 -11
  89. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
  90. data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
  91. data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
  92. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  93. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
  94. data/lib/rubocop/cop/layout/space_around_operators.rb +53 -21
  95. data/lib/rubocop/cop/layout/space_before_block_braces.rb +19 -10
  96. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  97. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
  98. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +1 -1
  99. data/lib/rubocop/cop/layout/space_inside_parens.rb +3 -3
  100. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  101. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
  102. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
  103. data/lib/rubocop/cop/lint/assignment_in_condition.rb +4 -4
  104. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
  105. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
  106. data/lib/rubocop/cop/lint/debugger.rb +19 -5
  107. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +3 -3
  108. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
  109. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
  110. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
  111. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +46 -19
  112. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  113. data/lib/rubocop/cop/lint/empty_conditional_body.rb +1 -1
  114. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  115. data/lib/rubocop/cop/lint/erb_new_arguments.rb +6 -7
  116. data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
  117. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
  118. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +1 -1
  119. data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
  120. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
  121. data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
  122. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
  123. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
  124. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
  125. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  126. data/lib/rubocop/cop/lint/missing_super.rb +34 -5
  127. data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
  128. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  129. data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
  130. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
  131. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
  132. data/lib/rubocop/cop/lint/number_conversion.rb +14 -4
  133. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
  134. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
  135. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
  136. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  137. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
  138. data/lib/rubocop/cop/lint/redundant_require_statement.rb +12 -3
  139. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +72 -8
  140. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
  141. data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -2
  142. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  143. data/lib/rubocop/cop/lint/rescue_type.rb +1 -3
  144. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +14 -8
  145. data/lib/rubocop/cop/lint/script_permission.rb +3 -3
  146. data/lib/rubocop/cop/lint/self_assignment.rb +38 -0
  147. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
  148. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  149. data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
  150. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
  151. data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
  152. data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
  153. data/lib/rubocop/cop/lint/symbol_conversion.rb +8 -3
  154. data/lib/rubocop/cop/lint/syntax.rb +6 -3
  155. data/lib/rubocop/cop/lint/to_enum_arguments.rb +19 -6
  156. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
  157. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  158. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +2 -2
  159. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
  160. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  161. data/lib/rubocop/cop/lint/useless_assignment.rb +94 -10
  162. data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
  163. data/lib/rubocop/cop/lint/useless_times.rb +2 -2
  164. data/lib/rubocop/cop/lint/void.rb +104 -14
  165. data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
  166. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  167. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  168. data/lib/rubocop/cop/metrics/class_length.rb +8 -2
  169. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  170. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
  171. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +31 -3
  172. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  173. data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
  174. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  175. data/lib/rubocop/cop/mixin/comments_help.rb +19 -11
  176. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
  177. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  178. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  179. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
  180. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  181. data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
  182. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -3
  183. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  184. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +6 -8
  185. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  186. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  187. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
  188. data/lib/rubocop/cop/mixin/string_help.rb +4 -2
  189. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  190. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
  191. data/lib/rubocop/cop/naming/block_forwarding.rb +13 -5
  192. data/lib/rubocop/cop/naming/constant_name.rb +2 -3
  193. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  194. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
  195. data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
  196. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +26 -11
  197. data/lib/rubocop/cop/naming/predicate_name.rb +2 -2
  198. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
  199. data/lib/rubocop/cop/naming/variable_name.rb +6 -1
  200. data/lib/rubocop/cop/registry.rb +1 -1
  201. data/lib/rubocop/cop/security/open.rb +2 -2
  202. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -2
  203. data/lib/rubocop/cop/style/accessor_grouping.rb +6 -2
  204. data/lib/rubocop/cop/style/alias.rb +9 -8
  205. data/lib/rubocop/cop/style/arguments_forwarding.rb +411 -63
  206. data/lib/rubocop/cop/style/array_first_last.rb +64 -0
  207. data/lib/rubocop/cop/style/array_intersect.rb +13 -5
  208. data/lib/rubocop/cop/style/attr.rb +11 -1
  209. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
  210. data/lib/rubocop/cop/style/begin_block.rb +1 -2
  211. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
  212. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  213. data/lib/rubocop/cop/style/block_delimiters.rb +5 -4
  214. data/lib/rubocop/cop/style/case_like_if.rb +5 -5
  215. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
  216. data/lib/rubocop/cop/style/class_check.rb +1 -0
  217. data/lib/rubocop/cop/style/class_equality_comparison.rb +58 -40
  218. data/lib/rubocop/cop/style/class_vars.rb +3 -3
  219. data/lib/rubocop/cop/style/collection_compact.rb +35 -12
  220. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  221. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  222. data/lib/rubocop/cop/style/combinable_loops.rb +36 -8
  223. data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
  224. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -1
  225. data/lib/rubocop/cop/style/conditional_assignment.rb +11 -10
  226. data/lib/rubocop/cop/style/copyright.rb +6 -3
  227. data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
  228. data/lib/rubocop/cop/style/date_time.rb +5 -4
  229. data/lib/rubocop/cop/style/dir.rb +1 -1
  230. data/lib/rubocop/cop/style/dir_empty.rb +8 -14
  231. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  232. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +2 -2
  233. data/lib/rubocop/cop/style/documentation.rb +1 -1
  234. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  235. data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
  236. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  237. data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
  238. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  239. data/lib/rubocop/cop/style/eval_with_location.rb +8 -19
  240. data/lib/rubocop/cop/style/exact_regexp_match.rb +69 -0
  241. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  242. data/lib/rubocop/cop/style/file_empty.rb +3 -3
  243. data/lib/rubocop/cop/style/file_read.rb +2 -2
  244. data/lib/rubocop/cop/style/for.rb +3 -1
  245. data/lib/rubocop/cop/style/format_string.rb +24 -3
  246. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +4 -2
  247. data/lib/rubocop/cop/style/guard_clause.rb +28 -0
  248. data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
  249. data/lib/rubocop/cop/style/hash_each_methods.rb +106 -33
  250. data/lib/rubocop/cop/style/hash_except.rb +25 -13
  251. data/lib/rubocop/cop/style/hash_syntax.rb +9 -2
  252. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  253. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  254. data/lib/rubocop/cop/style/identical_conditional_branches.rb +34 -5
  255. data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
  256. data/lib/rubocop/cop/style/if_unless_modifier.rb +41 -12
  257. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  258. data/lib/rubocop/cop/style/inverse_methods.rb +14 -13
  259. data/lib/rubocop/cop/style/invertible_unless_condition.rb +54 -8
  260. data/lib/rubocop/cop/style/lambda.rb +3 -3
  261. data/lib/rubocop/cop/style/lambda_call.rb +5 -0
  262. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +8 -10
  263. data/lib/rubocop/cop/style/map_to_hash.rb +19 -6
  264. data/lib/rubocop/cop/style/map_to_set.rb +4 -1
  265. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +27 -16
  266. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +45 -40
  267. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
  268. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  269. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
  270. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  271. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  272. data/lib/rubocop/cop/style/multiline_method_signature.rb +16 -4
  273. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +6 -4
  274. data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
  275. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  276. data/lib/rubocop/cop/style/next.rb +1 -1
  277. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  278. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  279. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  280. data/lib/rubocop/cop/style/object_then.rb +5 -3
  281. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  282. data/lib/rubocop/cop/style/operator_method_call.rb +8 -2
  283. data/lib/rubocop/cop/style/parallel_assignment.rb +29 -23
  284. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
  285. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
  286. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  287. data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
  288. data/lib/rubocop/cop/style/raise_args.rb +4 -1
  289. data/lib/rubocop/cop/style/redundant_argument.rb +10 -4
  290. data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
  291. data/lib/rubocop/cop/style/redundant_assignment.rb +10 -2
  292. data/lib/rubocop/cop/style/redundant_begin.rb +10 -2
  293. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -10
  294. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +39 -0
  295. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +93 -5
  296. data/lib/rubocop/cop/style/redundant_each.rb +7 -4
  297. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  298. data/lib/rubocop/cop/style/redundant_fetch_block.rb +9 -7
  299. data/lib/rubocop/cop/style/redundant_filter_chain.rb +118 -0
  300. data/lib/rubocop/cop/style/redundant_line_continuation.rb +203 -0
  301. data/lib/rubocop/cop/style/redundant_parentheses.rb +72 -23
  302. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  303. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
  304. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  305. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
  306. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +3 -2
  307. data/lib/rubocop/cop/style/redundant_return.rb +14 -3
  308. data/lib/rubocop/cop/style/redundant_self.rb +17 -2
  309. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +8 -1
  310. data/lib/rubocop/cop/style/redundant_sort.rb +10 -9
  311. data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
  312. data/lib/rubocop/cop/style/redundant_string_escape.rb +5 -4
  313. data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
  314. data/lib/rubocop/cop/style/require_order.rb +11 -5
  315. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
  316. data/lib/rubocop/cop/style/return_nil.rb +6 -2
  317. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +95 -0
  318. data/lib/rubocop/cop/style/sample.rb +3 -4
  319. data/lib/rubocop/cop/style/select_by_regexp.rb +22 -11
  320. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  321. data/lib/rubocop/cop/style/semicolon.rb +20 -4
  322. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  323. data/lib/rubocop/cop/style/single_argument_dig.rb +7 -3
  324. data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
  325. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  326. data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
  327. data/lib/rubocop/cop/style/sole_nested_conditional.rb +8 -4
  328. data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
  329. data/lib/rubocop/cop/style/string_chars.rb +1 -0
  330. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
  331. data/lib/rubocop/cop/style/strip.rb +7 -4
  332. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  333. data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
  334. data/lib/rubocop/cop/style/symbol_array.rb +35 -15
  335. data/lib/rubocop/cop/style/symbol_proc.rb +36 -0
  336. data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
  337. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  338. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
  339. data/lib/rubocop/cop/style/unpack_first.rb +11 -14
  340. data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
  341. data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
  342. data/lib/rubocop/cop/style/yoda_expression.rb +8 -7
  343. data/lib/rubocop/cop/team.rb +1 -1
  344. data/lib/rubocop/cop/util.rb +1 -1
  345. data/lib/rubocop/cop/utils/regexp_ranges.rb +113 -0
  346. data/lib/rubocop/cop/variable_force/assignment.rb +45 -4
  347. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  348. data/lib/rubocop/cop/variable_force.rb +1 -0
  349. data/lib/rubocop/cops_documentation_generator.rb +26 -7
  350. data/lib/rubocop/directive_comment.rb +10 -8
  351. data/lib/rubocop/ext/regexp_node.rb +10 -5
  352. data/lib/rubocop/ext/regexp_parser.rb +5 -2
  353. data/lib/rubocop/file_finder.rb +4 -7
  354. data/lib/rubocop/formatter/disabled_config_formatter.rb +17 -6
  355. data/lib/rubocop/formatter/html_formatter.rb +35 -14
  356. data/lib/rubocop/formatter/json_formatter.rb +0 -1
  357. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  358. data/lib/rubocop/formatter/offense_count_formatter.rb +12 -2
  359. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  360. data/lib/rubocop/formatter.rb +1 -1
  361. data/lib/rubocop/lsp/logger.rb +22 -0
  362. data/lib/rubocop/lsp/routes.rb +246 -0
  363. data/lib/rubocop/lsp/runtime.rb +99 -0
  364. data/lib/rubocop/lsp/server.rb +71 -0
  365. data/lib/rubocop/lsp/severity.rb +27 -0
  366. data/lib/rubocop/lsp.rb +29 -0
  367. data/lib/rubocop/magic_comment.rb +13 -11
  368. data/lib/rubocop/options.rb +26 -10
  369. data/lib/rubocop/path_util.rb +6 -2
  370. data/lib/rubocop/result_cache.rb +6 -3
  371. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  372. data/lib/rubocop/rspec/expect_offense.rb +8 -8
  373. data/lib/rubocop/rspec/shared_contexts.rb +42 -18
  374. data/lib/rubocop/rspec/support.rb +2 -0
  375. data/lib/rubocop/runner.rb +15 -6
  376. data/lib/rubocop/server/cache.rb +1 -1
  377. data/lib/rubocop/server/client_command/exec.rb +3 -3
  378. data/lib/rubocop/server/helper.rb +1 -1
  379. data/lib/rubocop/server/server_command/exec.rb +1 -2
  380. data/lib/rubocop/string_interpreter.rb +3 -3
  381. data/lib/rubocop/target_finder.rb +91 -81
  382. data/lib/rubocop/target_ruby.rb +85 -78
  383. data/lib/rubocop/version.rb +27 -8
  384. data/lib/rubocop.rb +22 -0
  385. metadata +59 -14
  386. /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
@@ -7,12 +7,24 @@ module RuboCop
7
7
  # scope.
8
8
  # The basic idea for this cop was from the warning of `ruby -cw`:
9
9
  #
10
- # assigned but unused variable - foo
10
+ # [source,console]
11
+ # ----
12
+ # assigned but unused variable - foo
13
+ # ----
11
14
  #
12
15
  # Currently this cop has advanced logic that detects unreferenced
13
16
  # reassignments and properly handles varied cases such as branch, loop,
14
17
  # rescue, ensure, etc.
15
18
  #
19
+ # NOTE: Given the assignment `foo = 1, bar = 2`, removing unused variables
20
+ # can lead to a syntax error, so this case is not autocorrected.
21
+ #
22
+ # @safety
23
+ # This cop's autocorrection is unsafe because removing assignment from
24
+ # operator assignment can cause NameError if this assignment has been used to declare
25
+ # local variable. For example, replacing `a ||= 1` to `a || 1` may cause
26
+ # "undefined local variable or method `a' for main:Object (NameError)".
27
+ #
16
28
  # @example
17
29
  #
18
30
  # # bad
@@ -31,6 +43,10 @@ module RuboCop
31
43
  # do_something(some_var)
32
44
  # end
33
45
  class UselessAssignment < Base
46
+ extend AutoCorrector
47
+
48
+ include RangeHelp
49
+
34
50
  MSG = 'Useless assignment to variable - `%<variable>s`.'
35
51
 
36
52
  def self.joining_forces
@@ -41,23 +57,24 @@ module RuboCop
41
57
  scope.variables.each_value { |variable| check_for_unused_assignments(variable) }
42
58
  end
43
59
 
60
+ # rubocop:disable Metrics/AbcSize
44
61
  def check_for_unused_assignments(variable)
45
62
  return if variable.should_be_unused?
46
63
 
47
64
  variable.assignments.each do |assignment|
48
- next if assignment.used?
65
+ next if assignment.used? || part_of_ignored_node?(assignment.node)
49
66
 
50
67
  message = message_for_useless_assignment(assignment)
68
+ range = offense_range(assignment)
51
69
 
52
- location = if assignment.regexp_named_capture?
53
- assignment.node.children.first.source_range
54
- else
55
- assignment.node.loc.name
56
- end
70
+ add_offense(range, message: message) do |corrector|
71
+ autocorrect(corrector, assignment) unless sequential_assignment?(assignment.node)
72
+ end
57
73
 
58
- add_offense(location, message: message)
74
+ ignore_node(assignment.node) if chained_assignment?(assignment.node)
59
75
  end
60
76
  end
77
+ # rubocop:enable Metrics/AbcSize
61
78
 
62
79
  def message_for_useless_assignment(assignment)
63
80
  variable = assignment.variable
@@ -65,6 +82,28 @@ module RuboCop
65
82
  format(MSG, variable: variable.name) + message_specification(assignment, variable).to_s
66
83
  end
67
84
 
85
+ def offense_range(assignment)
86
+ if assignment.regexp_named_capture?
87
+ assignment.node.children.first.source_range
88
+ else
89
+ assignment.node.loc.name
90
+ end
91
+ end
92
+
93
+ def sequential_assignment?(node)
94
+ if node.lvasgn_type? && node.expression&.array_type? &&
95
+ node.each_descendant.any?(&:assignment?)
96
+ return true
97
+ end
98
+ return false unless node.parent
99
+
100
+ sequential_assignment?(node.parent)
101
+ end
102
+
103
+ def chained_assignment?(node)
104
+ node.respond_to?(:expression) && node.expression&.lvasgn_type?
105
+ end
106
+
68
107
  def message_specification(assignment, variable)
69
108
  if assignment.multiple_assignment?
70
109
  multiple_assignment_message(variable.name)
@@ -84,8 +123,7 @@ module RuboCop
84
123
  return_value_node = return_value_node_of_scope(scope)
85
124
  return unless assignment.meta_assignment_node.equal?(return_value_node)
86
125
 
87
- " Use `#{assignment.operator.sub(/=$/, '')}` " \
88
- "instead of `#{assignment.operator}`."
126
+ " Use `#{assignment.operator.delete_suffix('=')}` instead of `#{assignment.operator}`."
89
127
  end
90
128
 
91
129
  def similar_name_message(variable)
@@ -119,6 +157,52 @@ module RuboCop
119
157
 
120
158
  node.receiver.nil? && !node.arguments?
121
159
  end
160
+
161
+ # rubocop:disable Metrics/AbcSize
162
+ def autocorrect(corrector, assignment)
163
+ if assignment.exception_assignment?
164
+ remove_exception_assignment_part(corrector, assignment.node)
165
+ elsif assignment.multiple_assignment? || assignment.rest_assignment? ||
166
+ assignment.for_assignment?
167
+ rename_variable_with_underscore(corrector, assignment.node)
168
+ elsif assignment.operator_assignment?
169
+ remove_trailing_character_from_operator(corrector, assignment.node)
170
+ elsif assignment.regexp_named_capture?
171
+ replace_named_capture_group_with_non_capturing_group(corrector, assignment.node,
172
+ assignment.variable.name)
173
+ else
174
+ remove_local_variable_assignment_part(corrector, assignment.node)
175
+ end
176
+ end
177
+ # rubocop:enable Metrics/AbcSize
178
+
179
+ def remove_exception_assignment_part(corrector, node)
180
+ corrector.remove(
181
+ range_between(
182
+ (node.parent.children.first&.source_range || node.parent.location.keyword).end_pos,
183
+ node.source_range.end_pos
184
+ )
185
+ )
186
+ end
187
+
188
+ def rename_variable_with_underscore(corrector, node)
189
+ corrector.replace(node, '_')
190
+ end
191
+
192
+ def remove_trailing_character_from_operator(corrector, node)
193
+ corrector.remove(node.parent.location.operator.end.adjust(begin_pos: -1))
194
+ end
195
+
196
+ def replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name)
197
+ corrector.replace(
198
+ node.children.first,
199
+ node.children.first.source.sub(/\(\?<#{variable_name}>/, '(?:')
200
+ )
201
+ end
202
+
203
+ def remove_local_variable_assignment_part(corrector, node)
204
+ corrector.replace(node, node.expression.source)
205
+ end
122
206
  end
123
207
  end
124
208
  end
@@ -41,15 +41,23 @@ module RuboCop
41
41
  MSG = 'Useless method definition detected.'
42
42
 
43
43
  def on_def(node)
44
- return if use_rest_or_optional_args?(node)
44
+ return if method_definition_with_modifier?(node) || use_rest_or_optional_args?(node)
45
45
  return unless delegating?(node.body, node)
46
46
 
47
- add_offense(node) { |corrector| corrector.remove(node) }
47
+ add_offense(node) do |corrector|
48
+ range = node.parent&.send_type? ? node.parent : node
49
+
50
+ corrector.remove(range)
51
+ end
48
52
  end
49
53
  alias on_defs on_def
50
54
 
51
55
  private
52
56
 
57
+ def method_definition_with_modifier?(node)
58
+ node.parent&.send_type? && !node.parent&.non_bare_access_modifier?
59
+ end
60
+
53
61
  def use_rest_or_optional_args?(node)
54
62
  node.arguments.any? { |arg| arg.restarg_type? || arg.optarg_type? || arg.kwoptarg_type? }
55
63
  end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Lint
6
6
  # Checks for uses of `Integer#times` that will never yield
7
- # (when the integer <= 0) or that will only ever yield once
7
+ # (when the integer ``<= 0``) or that will only ever yield once
8
8
  # (`1.times`).
9
9
  #
10
10
  # @safety
@@ -64,7 +64,7 @@ module RuboCop
64
64
  remove_node(corrector, node)
65
65
  elsif !proc_name.empty?
66
66
  autocorrect_block_pass(corrector, node, proc_name)
67
- else
67
+ elsif node.block_type?
68
68
  autocorrect_block(corrector, node)
69
69
  end
70
70
  end
@@ -6,6 +6,16 @@ module RuboCop
6
6
  # Checks for operators, variables, literals, lambda, proc and nonmutating
7
7
  # methods used in void context.
8
8
  #
9
+ # `each` blocks are allowed to prevent false positives.
10
+ # For example, the expression inside the `each` block below.
11
+ # It's not void, especially when the receiver is an `Enumerator`:
12
+ #
13
+ # [source,ruby]
14
+ # ----
15
+ # enumerator = [1, 2, 3].filter
16
+ # enumerator.each { |item| item >= 2 } #=> [2, 3]
17
+ # ----
18
+ #
9
19
  # @example CheckForMethodsWithNoSideEffects: false (default)
10
20
  # # bad
11
21
  # def some_method
@@ -41,8 +51,13 @@ module RuboCop
41
51
  # do_something(some_array)
42
52
  # end
43
53
  class Void < Base
54
+ extend AutoCorrector
55
+
56
+ include RangeHelp
57
+
44
58
  OP_MSG = 'Operator `%<op>s` used in void context.'
45
59
  VAR_MSG = 'Variable `%<var>s` used in void context.'
60
+ CONST_MSG = 'Constant `%<var>s` used in void context.'
46
61
  LIT_MSG = 'Literal `%<lit>s` used in void context.'
47
62
  SELF_MSG = '`self` used in void context.'
48
63
  EXPRESSION_MSG = '`%<expression>s` used in void context.'
@@ -59,15 +74,16 @@ module RuboCop
59
74
  shuffle slice sort sort_by squeeze strip sub
60
75
  succ swapcase tr tr_s transform_values
61
76
  unicode_normalize uniq upcase].freeze
62
- METHODS_REPLACABLE_BY_EACH = %i[collect map].freeze
77
+ METHODS_REPLACEABLE_BY_EACH = %i[collect map].freeze
63
78
 
64
79
  NONMUTATING_METHODS = (NONMUTATING_METHODS_WITH_BANG_VERSION +
65
- METHODS_REPLACABLE_BY_EACH).freeze
80
+ METHODS_REPLACEABLE_BY_EACH).freeze
66
81
 
67
82
  def on_block(node)
68
83
  return unless node.body && !node.body.begin_type?
69
84
  return unless in_void_context?(node.body)
70
85
 
86
+ check_void_op(node.body) { node.method?(:each) }
71
87
  check_expression(node.body)
72
88
  end
73
89
 
@@ -83,11 +99,18 @@ module RuboCop
83
99
  def check_begin(node)
84
100
  expressions = *node
85
101
  expressions.pop unless in_void_context?(node)
86
- expressions.each { |expr| check_expression(expr) }
102
+ expressions.each do |expr|
103
+ check_void_op(expr) do
104
+ block_node = node.each_ancestor(:block).first
105
+
106
+ block_node&.method?(:each)
107
+ end
108
+
109
+ check_expression(expr)
110
+ end
87
111
  end
88
112
 
89
113
  def check_expression(expr)
90
- check_void_op(expr)
91
114
  check_literal(expr)
92
115
  check_var(expr)
93
116
  check_self(expr)
@@ -97,45 +120,74 @@ module RuboCop
97
120
  check_nonmutating(expr)
98
121
  end
99
122
 
100
- def check_void_op(node)
123
+ def check_void_op(node, &block)
101
124
  return unless node.send_type? && OPERATORS.include?(node.method_name)
125
+ return if block && yield(node)
102
126
 
103
- add_offense(node.loc.selector, message: format(OP_MSG, op: node.method_name))
127
+ add_offense(node.loc.selector,
128
+ message: format(OP_MSG, op: node.method_name)) do |corrector|
129
+ autocorrect_void_op(corrector, node)
130
+ end
104
131
  end
105
132
 
106
133
  def check_var(node)
107
134
  return unless node.variable? || node.const_type?
108
135
 
109
- add_offense(node.loc.name, message: format(VAR_MSG, var: node.loc.name.source))
136
+ if node.const_type?
137
+ template = node.special_keyword? ? VAR_MSG : CONST_MSG
138
+
139
+ offense_range = node
140
+ message = format(template, var: node.source)
141
+ else
142
+ offense_range = node.loc.name
143
+ message = format(VAR_MSG, var: node.loc.name.source)
144
+ end
145
+
146
+ add_offense(offense_range, message: message) do |corrector|
147
+ autocorrect_void_expression(corrector, node)
148
+ end
110
149
  end
111
150
 
112
151
  def check_literal(node)
113
- return if !node.literal? || node.xstr_type? || node.range_type?
152
+ return if !entirely_literal?(node) || node.xstr_type? || node.range_type?
114
153
 
115
- add_offense(node, message: format(LIT_MSG, lit: node.source))
154
+ add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
155
+ autocorrect_void_expression(corrector, node)
156
+ end
116
157
  end
117
158
 
118
159
  def check_self(node)
119
160
  return unless node.self_type?
120
161
 
121
- add_offense(node, message: SELF_MSG)
162
+ add_offense(node, message: SELF_MSG) do |corrector|
163
+ autocorrect_void_expression(corrector, node)
164
+ end
122
165
  end
123
166
 
124
167
  def check_void_expression(node)
125
168
  return unless node.defined_type? || node.lambda_or_proc?
126
169
 
127
- add_offense(node, message: format(EXPRESSION_MSG, expression: node.source))
170
+ add_offense(node, message: format(EXPRESSION_MSG, expression: node.source)) do |corrector|
171
+ autocorrect_void_expression(corrector, node)
172
+ end
128
173
  end
129
174
 
130
175
  def check_nonmutating(node)
131
- return unless node.respond_to?(:method_name)
176
+ return if !node.send_type? && !node.block_type? && !node.numblock_type?
132
177
 
133
178
  method_name = node.method_name
134
179
  return unless NONMUTATING_METHODS.include?(method_name)
135
180
 
136
- suggestion = METHODS_REPLACABLE_BY_EACH.include?(method_name) ? 'each' : "#{method_name}!"
181
+ suggestion = if METHODS_REPLACEABLE_BY_EACH.include?(method_name)
182
+ 'each'
183
+ else
184
+ "#{method_name}!"
185
+ end
137
186
  add_offense(node,
138
- message: format(NONMUTATING_MSG, method: method_name, suggest: suggestion))
187
+ message: format(NONMUTATING_MSG, method: method_name,
188
+ suggest: suggestion)) do |corrector|
189
+ autocorrect_nonmutating_send(corrector, node, suggestion)
190
+ end
139
191
  end
140
192
 
141
193
  def in_void_context?(node)
@@ -145,6 +197,44 @@ module RuboCop
145
197
 
146
198
  VOID_CONTEXT_TYPES.include?(parent.type) && parent.void_context?
147
199
  end
200
+
201
+ def autocorrect_void_op(corrector, node)
202
+ if node.arguments.empty?
203
+ corrector.replace(node, node.receiver.source)
204
+ else
205
+ corrector.replace(
206
+ range_with_surrounding_space(range: node.loc.selector, side: :both,
207
+ newlines: false),
208
+ "\n"
209
+ )
210
+ end
211
+ end
212
+
213
+ def autocorrect_void_expression(corrector, node)
214
+ corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
215
+ end
216
+
217
+ def autocorrect_nonmutating_send(corrector, node, suggestion)
218
+ send_node = if node.send_type?
219
+ node
220
+ else
221
+ node.send_node
222
+ end
223
+ corrector.replace(send_node.loc.selector, suggestion)
224
+ end
225
+
226
+ def entirely_literal?(node)
227
+ case node.type
228
+ when :array
229
+ node.each_value.all? { |value| entirely_literal?(value) }
230
+ when :hash
231
+ return false unless node.each_key.all? { |key| entirely_literal?(key) }
232
+
233
+ node.each_value.all? { |value| entirely_literal?(value) }
234
+ else
235
+ node.literal?
236
+ end
237
+ end
148
238
  end
149
239
  end
150
240
  end
@@ -10,9 +10,9 @@ module RuboCop
10
10
  #
11
11
  # Interpreting ABC size:
12
12
  #
13
- # * <= 17 satisfactory
14
- # * 18..30 unsatisfactory
15
- # * > 30 dangerous
13
+ # * ``<= 17`` satisfactory
14
+ # * `18..30` unsatisfactory
15
+ # * `>` 30 dangerous
16
16
  #
17
17
  # You can have repeated "attributes" calls count as a single "branch".
18
18
  # For this purpose, attributes are any method with no argument; no attempt
@@ -12,6 +12,7 @@ module RuboCop
12
12
  # Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
13
13
  # will be counted as one line regardless of its actual size.
14
14
  #
15
+ # NOTE: This cop does not apply for `Struct` definitions.
15
16
  #
16
17
  # NOTE: The `ExcludedMethods` configuration is deprecated and only kept
17
18
  # for backwards compatibility. Please use `AllowedMethods` and `AllowedPatterns`
@@ -40,7 +41,6 @@ module RuboCop
40
41
  # )
41
42
  # end # 6 points
42
43
  #
43
- # NOTE: This cop does not apply for `Struct` definitions.
44
44
  class BlockLength < Base
45
45
  include CodeLength
46
46
  include AllowedMethods
@@ -44,7 +44,7 @@ module RuboCop
44
44
  def consider_node?(node)
45
45
  return true if NESTING_BLOCKS.include?(node.type)
46
46
 
47
- count_blocks? && node.block_type?
47
+ count_blocks? && (node.block_type? || node.numblock_type?)
48
48
  end
49
49
 
50
50
  def message(max)
@@ -11,6 +11,8 @@ module RuboCop
11
11
  # Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
12
12
  # will be counted as one line regardless of its actual size.
13
13
  #
14
+ # NOTE: This cop also applies for `Struct` definitions.
15
+ #
14
16
  # @example CountAsOne: ['array', 'heredoc', 'method_call']
15
17
  #
16
18
  # class Foo
@@ -34,8 +36,6 @@ module RuboCop
34
36
  # )
35
37
  # end # 6 points
36
38
  #
37
- #
38
- # NOTE: This cop also applies for `Struct` definitions.
39
39
  class ClassLength < Base
40
40
  include CodeLength
41
41
 
@@ -43,6 +43,12 @@ module RuboCop
43
43
  check_code_length(node)
44
44
  end
45
45
 
46
+ def on_sclass(node)
47
+ return if node.each_ancestor(:class).any?
48
+
49
+ on_class(node)
50
+ end
51
+
46
52
  def on_casgn(node)
47
53
  parent = node.parent
48
54
 
@@ -54,7 +54,7 @@ module RuboCop
54
54
  alias on_defs on_def
55
55
 
56
56
  def on_block(node)
57
- return unless node.send_node.method?(:define_method)
57
+ return unless node.method?(:define_method)
58
58
 
59
59
  check_code_length(node)
60
60
  end
@@ -117,8 +117,7 @@ module RuboCop
117
117
  end
118
118
 
119
119
  def capturing_variable?(name)
120
- # TODO: Remove `Symbol#to_s` after supporting only Ruby >= 2.7.
121
- name && !name.to_s.start_with?('_')
120
+ name && !name.start_with?('_')
122
121
  end
123
122
 
124
123
  def branch?(node)
@@ -63,7 +63,7 @@ module RuboCop
63
63
  types
64
64
  end
65
65
 
66
- def code_length(node)
66
+ def code_length(node) # rubocop:disable Metrics/MethodLength
67
67
  if classlike_node?(node)
68
68
  classlike_code_length(node)
69
69
  elsif heredoc_node?(node)
@@ -72,7 +72,14 @@ module RuboCop
72
72
  body = extract_body(node)
73
73
  return 0 unless body
74
74
 
75
- body.source.each_line.count { |line| !irrelevant_line?(line) }
75
+ source =
76
+ if node_with_heredoc?(body)
77
+ source_from_node_with_heredoc(body)
78
+ else
79
+ body.source.lines
80
+ end
81
+
82
+ source.count { |line| !irrelevant_line?(line) }
76
83
  end
77
84
  end
78
85
 
@@ -138,7 +145,7 @@ module RuboCop
138
145
 
139
146
  def extract_body(node)
140
147
  case node.type
141
- when :class, :module, :block, :numblock, :def, :defs
148
+ when :class, :module, :sclass, :block, :numblock, :def, :defs
142
149
  node.body
143
150
  when :casgn
144
151
  _scope, _name, value = *node
@@ -175,6 +182,27 @@ module RuboCop
175
182
  def another_args?(node)
176
183
  node.call_type? && node.arguments.count > 1
177
184
  end
185
+
186
+ def node_with_heredoc?(node)
187
+ node.each_descendant(:str, :dstr).any? { |descendant| heredoc_node?(descendant) }
188
+ end
189
+
190
+ def source_from_node_with_heredoc(node)
191
+ last_line = -1
192
+ node.each_descendant do |descendant|
193
+ next unless descendant.source
194
+
195
+ descendant_last_line =
196
+ if heredoc_node?(descendant)
197
+ descendant.loc.heredoc_end.line
198
+ else
199
+ descendant.last_line
200
+ end
201
+
202
+ last_line = [last_line, descendant_last_line].max
203
+ end
204
+ @processed_source[(node.first_line - 1)..(last_line - 1)]
205
+ end
178
206
  end
179
207
  end
180
208
  end
@@ -16,7 +16,7 @@ module RuboCop
16
16
  # The token that makes up a disable comment.
17
17
  # The allowed specification for comments after `# rubocop: disable` is
18
18
  # `DepartmentName/CopName` or` all`.
19
- DISABLING_COPS_CONTENT_TOKEN = %r{[A-z]+/[A-z]+|all}.freeze
19
+ DISABLING_COPS_CONTENT_TOKEN = %r{[A-Za-z]+/[A-Za-z]+|all}.freeze
20
20
 
21
21
  def on_new_investigation
22
22
  processed_source.comments.each do |comment|
@@ -67,7 +67,7 @@ module RuboCop
67
67
  end
68
68
 
69
69
  def contain_unexpected_character_for_department_name?(name)
70
- name.match?(%r{[^A-z/, ]})
70
+ name.match?(%r{[^A-Za-z/, ]})
71
71
  end
72
72
 
73
73
  def qualified_legacy_cop_name(cop_name)
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # This module encapsulates the ability to allow certain receivers in a cop.
6
+ module AllowedReceivers
7
+ def allowed_receiver?(receiver)
8
+ receiver_name = receiver_name(receiver)
9
+
10
+ allowed_receivers.include?(receiver_name)
11
+ end
12
+
13
+ def receiver_name(receiver)
14
+ if receiver.receiver && !receiver.receiver.const_type?
15
+ return receiver_name(receiver.receiver)
16
+ end
17
+
18
+ if receiver.send_type?
19
+ if receiver.receiver
20
+ "#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
21
+ else
22
+ receiver.method_name.to_s
23
+ end
24
+ else
25
+ receiver.source
26
+ end
27
+ end
28
+
29
+ def allowed_receivers
30
+ cop_config.fetch('AllowedReceivers', [])
31
+ end
32
+ end
33
+ end
34
+ end
@@ -218,7 +218,7 @@ module RuboCop
218
218
 
219
219
  # @api private
220
220
  def already_on_multiple_lines?(node)
221
- return node.first_line != node.arguments.last.last_line if node.def_type?
221
+ return node.first_line != node.last_argument.last_line if node.def_type?
222
222
 
223
223
  !node.single_line?
224
224
  end
@@ -25,7 +25,7 @@ module RuboCop
25
25
  def comments_contain_disables?(node, cop_name)
26
26
  disabled_ranges = processed_source.disabled_line_ranges[cop_name]
27
27
 
28
- return unless disabled_ranges
28
+ return false unless disabled_ranges
29
29
 
30
30
  node_range = node.source_range.line...find_end_line(node)
31
31
 
@@ -62,21 +62,29 @@ module RuboCop
62
62
  # Returns the end line of a node, which might be a comment and not part of the AST
63
63
  # End line is considered either the line at which another node starts, or
64
64
  # the line at which the parent node ends.
65
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
65
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
66
66
  def find_end_line(node)
67
- if node.if_type? && node.else?
68
- node.loc.else.line
69
- elsif node.if_type? && node.ternary?
70
- node.else_branch.loc.line
67
+ if node.if_type?
68
+ if node.else?
69
+ node.loc.else.line
70
+ elsif node.ternary?
71
+ node.else_branch.loc.line
72
+ elsif node.elsif?
73
+ node.each_ancestor(:if).find(&:if?).loc.end.line
74
+ end
75
+ elsif node.block_type? || node.numblock_type?
76
+ node.loc.end.line
71
77
  elsif (next_sibling = node.right_sibling) && next_sibling.is_a?(AST::Node)
72
78
  next_sibling.loc.line
73
79
  elsif (parent = node.parent)
74
- parent.loc.end ? parent.loc.end.line : parent.loc.line
75
- else
76
- node.loc.end.line
77
- end
80
+ if parent.loc.respond_to?(:end) && parent.loc.end
81
+ parent.loc.end.line
82
+ else
83
+ parent.loc.line
84
+ end
85
+ end || node.loc.end.line
78
86
  end
79
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
87
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
80
88
  end
81
89
  end
82
90
  end
@@ -18,6 +18,7 @@ module RuboCop
18
18
  alternative_styles.each do |alternative|
19
19
  return unexpected_style_detected(alternative) if valid_name?(node, name, alternative)
20
20
  end
21
+ unrecognized_style_detected
21
22
  end
22
23
 
23
24
  def valid_name?(node, name, given_style = style)