rubocop 1.84.2 → 1.87.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +106 -16
  3. data/config/obsoletion.yml +5 -0
  4. data/lib/rubocop/cache_config.rb +1 -1
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +28 -2
  6. data/lib/rubocop/cli/command/list_enabled_cops_for.rb +40 -0
  7. data/lib/rubocop/cli/command/mcp.rb +19 -0
  8. data/lib/rubocop/cli/command/show_cops.rb +2 -2
  9. data/lib/rubocop/cli/command/show_docs_url.rb +4 -8
  10. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  11. data/lib/rubocop/cli.rb +9 -7
  12. data/lib/rubocop/comment_config.rb +12 -15
  13. data/lib/rubocop/config.rb +14 -10
  14. data/lib/rubocop/config_finder.rb +1 -1
  15. data/lib/rubocop/config_loader.rb +17 -2
  16. data/lib/rubocop/config_loader_resolver.rb +13 -4
  17. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
  18. data/lib/rubocop/config_store.rb +2 -2
  19. data/lib/rubocop/config_validator.rb +1 -1
  20. data/lib/rubocop/cop/autocorrect_logic.rb +2 -1
  21. data/lib/rubocop/cop/base.rb +8 -2
  22. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  23. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -5
  24. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +33 -2
  25. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  26. data/lib/rubocop/cop/correctors.rb +28 -0
  27. data/lib/rubocop/cop/documentation.rb +2 -3
  28. data/lib/rubocop/cop/exclude_limit.rb +31 -5
  29. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  30. data/lib/rubocop/cop/gemspec/require_mfa.rb +5 -5
  31. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -3
  32. data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
  33. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
  34. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  35. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -2
  36. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  37. data/lib/rubocop/cop/layout/begin_end_alignment.rb +1 -1
  38. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  39. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  40. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +23 -7
  41. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  42. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
  43. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
  44. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
  45. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
  46. data/lib/rubocop/cop/layout/end_alignment.rb +8 -5
  47. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  48. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  49. data/lib/rubocop/cop/layout/indentation_width.rb +12 -0
  50. data/lib/rubocop/cop/layout/line_length.rb +5 -3
  51. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
  52. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  53. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +53 -3
  54. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  55. data/lib/rubocop/cop/layout/redundant_line_break.rb +3 -1
  56. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  57. data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
  58. data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -1
  59. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
  60. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  61. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
  62. data/lib/rubocop/cop/lint/constant_reassignment.rb +93 -11
  63. data/lib/rubocop/cop/lint/constant_resolution.rb +6 -6
  64. data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
  65. data/lib/rubocop/cop/lint/deprecated_constants.rb +1 -1
  66. data/lib/rubocop/cop/lint/duplicate_methods.rb +55 -8
  67. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  68. data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
  69. data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
  70. data/lib/rubocop/cop/lint/empty_when.rb +8 -1
  71. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  72. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  73. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -1
  74. data/lib/rubocop/cop/lint/multiple_comparison.rb +2 -2
  75. data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
  76. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +4 -2
  77. data/lib/rubocop/cop/lint/number_conversion.rb +6 -6
  78. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
  79. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -13
  80. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +2 -11
  81. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +23 -6
  82. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +3 -3
  83. data/lib/rubocop/cop/lint/require_relative_self_path.rb +3 -1
  84. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
  85. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
  86. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  87. data/lib/rubocop/cop/lint/syntax.rb +25 -1
  88. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
  89. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +1 -1
  90. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  91. data/lib/rubocop/cop/lint/unreachable_code.rb +2 -2
  92. data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
  93. data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
  94. data/lib/rubocop/cop/lint/useless_assignment.rb +4 -9
  95. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
  96. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
  97. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +1 -1
  98. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +35 -9
  99. data/lib/rubocop/cop/lint/void.rb +32 -12
  100. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  101. data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
  102. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  103. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +1 -1
  104. data/lib/rubocop/cop/migration/department_name.rb +12 -1
  105. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  106. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  107. data/lib/rubocop/cop/mixin/configurable_max.rb +6 -5
  108. data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
  109. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
  110. data/lib/rubocop/cop/mixin/project_index_help.rb +48 -0
  111. data/lib/rubocop/cop/mixin.rb +86 -0
  112. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  113. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  114. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  115. data/lib/rubocop/cop/naming/predicate_method.rb +2 -2
  116. data/lib/rubocop/cop/naming/predicate_prefix.rb +1 -1
  117. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  118. data/lib/rubocop/cop/offense.rb +8 -0
  119. data/lib/rubocop/cop/registry.rb +62 -38
  120. data/lib/rubocop/cop/security/eval.rb +15 -2
  121. data/lib/rubocop/cop/security/io_methods.rb +1 -1
  122. data/lib/rubocop/cop/style/access_modifier_declarations.rb +14 -2
  123. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
  124. data/lib/rubocop/cop/style/alias.rb +14 -2
  125. data/lib/rubocop/cop/style/and_or.rb +1 -0
  126. data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
  127. data/lib/rubocop/cop/style/array_join.rb +4 -2
  128. data/lib/rubocop/cop/style/ascii_comments.rb +6 -3
  129. data/lib/rubocop/cop/style/attr.rb +5 -2
  130. data/lib/rubocop/cop/style/bare_percent_literals.rb +3 -1
  131. data/lib/rubocop/cop/style/begin_block.rb +3 -1
  132. data/lib/rubocop/cop/style/block_delimiters.rb +25 -33
  133. data/lib/rubocop/cop/style/case_equality.rb +4 -0
  134. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  135. data/lib/rubocop/cop/style/class_and_module_children.rb +18 -2
  136. data/lib/rubocop/cop/style/collection_compact.rb +36 -16
  137. data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
  138. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
  139. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -4
  140. data/lib/rubocop/cop/style/copyright.rb +22 -11
  141. data/lib/rubocop/cop/style/date_time.rb +2 -2
  142. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  143. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +6 -1
  144. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  145. data/lib/rubocop/cop/style/each_with_object.rb +2 -0
  146. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  147. data/lib/rubocop/cop/style/empty_class_definition.rb +43 -20
  148. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  149. data/lib/rubocop/cop/style/encoding.rb +7 -1
  150. data/lib/rubocop/cop/style/end_block.rb +3 -1
  151. data/lib/rubocop/cop/style/endless_method.rb +8 -3
  152. data/lib/rubocop/cop/style/file_open.rb +84 -0
  153. data/lib/rubocop/cop/style/file_write.rb +18 -16
  154. data/lib/rubocop/cop/style/for.rb +3 -0
  155. data/lib/rubocop/cop/style/format_string.rb +4 -3
  156. data/lib/rubocop/cop/style/format_string_token.rb +29 -2
  157. data/lib/rubocop/cop/style/global_vars.rb +5 -2
  158. data/lib/rubocop/cop/style/guard_clause.rb +9 -6
  159. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +21 -5
  160. data/lib/rubocop/cop/style/hash_conversion.rb +1 -1
  161. data/lib/rubocop/cop/style/hash_lookup_method.rb +19 -7
  162. data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
  163. data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
  164. data/lib/rubocop/cop/style/if_inside_else.rb +16 -7
  165. data/lib/rubocop/cop/style/if_unless_modifier.rb +14 -3
  166. data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
  167. data/lib/rubocop/cop/style/inline_comment.rb +4 -1
  168. data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
  169. data/lib/rubocop/cop/style/magic_comment_format.rb +3 -3
  170. data/lib/rubocop/cop/style/map_join.rb +123 -0
  171. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -3
  172. data/lib/rubocop/cop/style/min_max_comparison.rb +1 -1
  173. data/lib/rubocop/cop/style/module_member_existence_check.rb +7 -14
  174. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -1
  175. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  176. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  177. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  178. data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
  179. data/lib/rubocop/cop/style/not.rb +2 -0
  180. data/lib/rubocop/cop/style/numeric_literals.rb +3 -2
  181. data/lib/rubocop/cop/style/one_class_per_file.rb +115 -0
  182. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  183. data/lib/rubocop/cop/style/parallel_assignment.rb +4 -0
  184. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
  185. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  186. data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
  187. data/lib/rubocop/cop/style/proc.rb +3 -2
  188. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  189. data/lib/rubocop/cop/style/reduce_to_hash.rb +200 -0
  190. data/lib/rubocop/cop/style/redundant_array_constructor.rb +2 -2
  191. data/lib/rubocop/cop/style/redundant_begin.rb +3 -3
  192. data/lib/rubocop/cop/style/redundant_constant_base.rb +5 -5
  193. data/lib/rubocop/cop/style/redundant_each.rb +3 -3
  194. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  195. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
  196. data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
  197. data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
  198. data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -22
  199. data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -1
  200. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +2 -2
  201. data/lib/rubocop/cop/style/redundant_return.rb +3 -1
  202. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  203. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
  204. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +114 -0
  205. data/lib/rubocop/cop/style/regexp_literal.rb +31 -2
  206. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -3
  207. data/lib/rubocop/cop/style/safe_navigation.rb +7 -7
  208. data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
  209. data/lib/rubocop/cop/style/select_by_range.rb +197 -0
  210. data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
  211. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  212. data/lib/rubocop/cop/style/semicolon.rb +2 -0
  213. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  214. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
  215. data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
  216. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -2
  217. data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
  218. data/lib/rubocop/cop/style/struct_inheritance.rb +13 -0
  219. data/lib/rubocop/cop/style/symbol_proc.rb +7 -6
  220. data/lib/rubocop/cop/style/tally_method.rb +181 -0
  221. data/lib/rubocop/cop/style/top_level_method_definition.rb +2 -2
  222. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  223. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
  224. data/lib/rubocop/cop/style/unless_logical_operators.rb +3 -3
  225. data/lib/rubocop/cop/style/while_until_modifier.rb +16 -0
  226. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  227. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  228. data/lib/rubocop/cop/team.rb +86 -35
  229. data/lib/rubocop/cop/variable_force/branch.rb +2 -2
  230. data/lib/rubocop/directive_comment.rb +2 -1
  231. data/lib/rubocop/file_patterns.rb +9 -1
  232. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -2
  233. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  234. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  235. data/lib/rubocop/formatter/simple_text_formatter.rb +0 -2
  236. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  237. data/lib/rubocop/formatter.rb +22 -21
  238. data/lib/rubocop/lsp/diagnostic.rb +1 -0
  239. data/lib/rubocop/lsp/routes.rb +10 -3
  240. data/lib/rubocop/lsp/runtime.rb +1 -2
  241. data/lib/rubocop/mcp/server.rb +200 -0
  242. data/lib/rubocop/options.rb +35 -4
  243. data/lib/rubocop/path_util.rb +14 -2
  244. data/lib/rubocop/plugin/loader.rb +1 -1
  245. data/lib/rubocop/project_index_loader.rb +66 -0
  246. data/lib/rubocop/result_cache.rb +22 -10
  247. data/lib/rubocop/rspec/cop_helper.rb +8 -0
  248. data/lib/rubocop/rspec/shared_contexts.rb +32 -2
  249. data/lib/rubocop/runner.rb +124 -53
  250. data/lib/rubocop/server/cache.rb +5 -7
  251. data/lib/rubocop/server/core.rb +2 -0
  252. data/lib/rubocop/target_finder.rb +14 -7
  253. data/lib/rubocop/target_ruby.rb +18 -12
  254. data/lib/rubocop/version.rb +21 -3
  255. data/lib/rubocop.rb +22 -96
  256. metadata +27 -5
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'hash_transform_method/autocorrection'
4
+
3
5
  module RuboCop
4
6
  module Cop
5
7
  # Common functionality for Style/HashTransformKeys and
@@ -27,68 +29,16 @@ module RuboCop
27
29
  end
28
30
  end
29
31
 
30
- # Internal helper class to hold autocorrect data
31
- Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
32
- def self.from_each_with_object(node, match)
33
- new(match, node, 0, 0)
34
- end
35
-
36
- def self.from_hash_brackets_map(node, match)
37
- new(match, node.children.last, 'Hash['.length, ']'.length)
38
- end
39
-
40
- def self.from_map_to_h(node, match)
41
- if node.parent&.block_type? && node.parent.send_node == node
42
- strip_trailing_chars = 0
43
- else
44
- map_range = node.children.first.source_range
45
- node_range = node.source_range
46
- strip_trailing_chars = node_range.end_pos - map_range.end_pos
47
- end
48
-
49
- new(match, node.children.first, 0, strip_trailing_chars)
50
- end
51
-
52
- def self.from_to_h(node, match)
53
- new(match, node, 0, 0)
54
- end
55
-
56
- def strip_prefix_and_suffix(node, corrector)
57
- expression = node.source_range
58
- corrector.remove_leading(expression, leading)
59
- corrector.remove_trailing(expression, trailing)
60
- end
61
-
62
- def set_new_method_name(new_method_name, corrector)
63
- range = block_node.send_node.loc.selector
64
- if (send_end = block_node.send_node.loc.end)
65
- # If there are arguments (only true in the `each_with_object`
66
- # case)
67
- range = range.begin.join(send_end)
68
- end
69
- corrector.replace(range, new_method_name)
70
- end
71
-
72
- def set_new_arg_name(transformed_argname, corrector)
73
- corrector.replace(block_node.arguments, "|#{transformed_argname}|")
74
- end
75
-
76
- def set_new_body_expression(transforming_body_expr, corrector)
77
- body_source = transforming_body_expr.source
78
- if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
79
- body_source = "{ #{body_source} }"
80
- end
81
-
82
- corrector.replace(block_node.body, body_source)
83
- end
84
- end
85
-
86
- # @!method array_receiver?(node)
87
- def_node_matcher :array_receiver?, <<~PATTERN
88
- {(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
32
+ # @!method hash_receiver?(node)
33
+ def_node_matcher :hash_receiver?, <<~PATTERN
34
+ {(hash ...)
35
+ (send _ {:to_h :to_hash :merge :merge! :update :invert :except :tally} ...)
36
+ (block (send _ {:group_by :to_h :tally :transform_keys :transform_keys!
37
+ :transform_values :transform_values!}) ...)
38
+ (block (send _ :each_with_object (hash)) ...)}
89
39
  PATTERN
90
40
 
91
- def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
41
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
92
42
  on_bad_each_with_object(node) do |*match|
93
43
  handle_possible_offense(node, match, 'each_with_object')
94
44
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common helpers for cops that consult the project-wide static-analysis index
6
+ # via `Cop::Base#project_index`.
7
+ #
8
+ # Mixed-in cops gain the `external_dependency_checksum` override that invalidates
9
+ # the `ResultCache` whenever the indexed project files change on disk.
10
+ # To run index-backed analysis, cops should simply check whether `project_index` is non-nil;
11
+ # the runner only exposes a non-nil index when the user opted in via `AllCops/UseProjectIndex`
12
+ # and the underlying gem is available.
13
+ module ProjectIndexHelp
14
+ BUILTIN_DOCUMENT_URI = 'rubydex:built-in'
15
+ FILE_URI_PREFIX = 'file://'
16
+ # Matches the spurious leading slash before a Windows drive letter that
17
+ # remains after stripping `file://` from a `file:///C:/...` URI.
18
+ WINDOWS_DRIVE_PREFIX = %r{\A/(?=[A-Za-z]:[/\\])}.freeze
19
+
20
+ def external_dependency_checksum
21
+ return nil unless project_index
22
+
23
+ @external_dependency_checksum ||= Digest::SHA1.hexdigest(
24
+ project_index_signature.join("\n")
25
+ )
26
+ end
27
+
28
+ private
29
+
30
+ def project_index_signature
31
+ project_index.documents.filter_map do |doc|
32
+ uri = doc.uri
33
+ next if uri == BUILTIN_DOCUMENT_URI
34
+
35
+ path = uri.delete_prefix(FILE_URI_PREFIX).sub(WINDOWS_DRIVE_PREFIX, '')
36
+ mtime, size = begin
37
+ stat = File.stat(path)
38
+ [stat.mtime.to_f, stat.size]
39
+ rescue StandardError
40
+ [0, 0]
41
+ end
42
+
43
+ "#{path}:#{mtime}:#{size}"
44
+ end.sort
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop # rubocop:disable Style/Documentation
5
+ # Autoloads mixin modules included by cops. Mixins are autoloaded to reduce the number of
6
+ # requires because they're used only when the relevant cop class is loaded.
7
+
8
+ autoload :ArrayMinSize, 'rubocop/cop/mixin/array_min_size'
9
+ autoload :ArraySyntax, 'rubocop/cop/mixin/array_syntax'
10
+ autoload :Alignment, 'rubocop/cop/mixin/alignment'
11
+ autoload :AllowedIdentifiers, 'rubocop/cop/mixin/allowed_identifiers'
12
+ autoload :AllowedMethods, 'rubocop/cop/mixin/allowed_methods'
13
+ autoload :AllowedPattern, 'rubocop/cop/mixin/allowed_pattern'
14
+ autoload :AllowedReceivers, 'rubocop/cop/mixin/allowed_receivers'
15
+ autoload :ForbiddenIdentifiers, 'rubocop/cop/mixin/forbidden_identifiers'
16
+ autoload :ForbiddenPattern, 'rubocop/cop/mixin/forbidden_pattern'
17
+ autoload :AutoCorrector, 'rubocop/cop/mixin/auto_corrector' # rubocop:todo Naming/InclusiveLanguage
18
+ autoload :CheckAssignment, 'rubocop/cop/mixin/check_assignment'
19
+ autoload :CheckLineBreakable, 'rubocop/cop/mixin/check_line_breakable'
20
+ autoload :CheckSingleLineSuitability, 'rubocop/cop/mixin/check_single_line_suitability'
21
+ autoload :ConfigurableMax, 'rubocop/cop/mixin/configurable_max'
22
+ autoload :CodeLength, 'rubocop/cop/mixin/code_length'
23
+ autoload :ConfigurableEnforcedStyle, 'rubocop/cop/mixin/configurable_enforced_style'
24
+ autoload :ConfigurableFormatting, 'rubocop/cop/mixin/configurable_formatting'
25
+ autoload :ConfigurableNaming, 'rubocop/cop/mixin/configurable_naming'
26
+ autoload :ConfigurableNumbering, 'rubocop/cop/mixin/configurable_numbering'
27
+ autoload :DigHelp, 'rubocop/cop/mixin/dig_help'
28
+ autoload :DocumentationComment, 'rubocop/cop/mixin/documentation_comment'
29
+ autoload :Duplication, 'rubocop/cop/mixin/duplication'
30
+ autoload :RangeHelp, 'rubocop/cop/mixin/range_help'
31
+ autoload :ProjectIndexHelp, 'rubocop/cop/mixin/project_index_help'
32
+ autoload :AnnotationComment, 'rubocop/cop/mixin/annotation_comment'
33
+ autoload :EmptyParameter, 'rubocop/cop/mixin/empty_parameter'
34
+ autoload :EndKeywordAlignment, 'rubocop/cop/mixin/end_keyword_alignment'
35
+ autoload :EndlessMethodRewriter, 'rubocop/cop/mixin/endless_method_rewriter'
36
+ autoload :EnforceSuperclass, 'rubocop/cop/mixin/enforce_superclass'
37
+ autoload :FirstElementLineBreak, 'rubocop/cop/mixin/first_element_line_break'
38
+ autoload :FrozenStringLiteral, 'rubocop/cop/mixin/frozen_string_literal'
39
+ autoload :GemDeclaration, 'rubocop/cop/mixin/gem_declaration'
40
+ autoload :GemspecHelp, 'rubocop/cop/mixin/gemspec_help'
41
+ autoload :HashAlignmentStyles, 'rubocop/cop/mixin/hash_alignment_styles'
42
+ autoload :HashSubset, 'rubocop/cop/mixin/hash_subset'
43
+ autoload :HashTransformMethod, 'rubocop/cop/mixin/hash_transform_method'
44
+ autoload :IntegerNode, 'rubocop/cop/mixin/integer_node'
45
+ autoload :Interpolation, 'rubocop/cop/mixin/interpolation'
46
+ autoload :LineLengthHelp, 'rubocop/cop/mixin/line_length_help'
47
+ autoload :MatchRange, 'rubocop/cop/mixin/match_range'
48
+ autoload :HashShorthandSyntax, 'rubocop/cop/mixin/hash_shorthand_syntax'
49
+ autoload :MethodComplexity, 'rubocop/cop/mixin/method_complexity'
50
+ autoload :MethodPreference, 'rubocop/cop/mixin/method_preference'
51
+ autoload :MinBodyLength, 'rubocop/cop/mixin/min_body_length'
52
+ autoload :MinBranchesCount, 'rubocop/cop/mixin/min_branches_count'
53
+ autoload :MultilineElementIndentation, 'rubocop/cop/mixin/multiline_element_indentation'
54
+ autoload :MultilineElementLineBreaks, 'rubocop/cop/mixin/multiline_element_line_breaks'
55
+ autoload :MultilineExpressionIndentation, 'rubocop/cop/mixin/multiline_expression_indentation'
56
+ autoload :MultilineLiteralBraceLayout, 'rubocop/cop/mixin/multiline_literal_brace_layout'
57
+ autoload :NegativeConditional, 'rubocop/cop/mixin/negative_conditional'
58
+ autoload :Heredoc, 'rubocop/cop/mixin/heredoc'
59
+ autoload :NilMethods, 'rubocop/cop/mixin/nil_methods'
60
+ autoload :OnNormalIfUnless, 'rubocop/cop/mixin/on_normal_if_unless'
61
+ autoload :OrderedGemNode, 'rubocop/cop/mixin/ordered_gem_node'
62
+ autoload :Parentheses, 'rubocop/cop/mixin/parentheses'
63
+ autoload :PercentArray, 'rubocop/cop/mixin/percent_array'
64
+ autoload :PercentLiteral, 'rubocop/cop/mixin/percent_literal'
65
+ autoload :PrecedingFollowingAlignment, 'rubocop/cop/mixin/preceding_following_alignment'
66
+ autoload :PreferredDelimiters, 'rubocop/cop/mixin/preferred_delimiters'
67
+ autoload :RationalLiteral, 'rubocop/cop/mixin/rational_literal'
68
+ autoload :RequireLibrary, 'rubocop/cop/mixin/require_library'
69
+ autoload :RescueNode, 'rubocop/cop/mixin/rescue_node'
70
+ autoload :SafeAssignment, 'rubocop/cop/mixin/safe_assignment'
71
+ autoload :SpaceAfterPunctuation, 'rubocop/cop/mixin/space_after_punctuation'
72
+ autoload :SpaceBeforePunctuation, 'rubocop/cop/mixin/space_before_punctuation'
73
+ autoload :SurroundingSpace, 'rubocop/cop/mixin/surrounding_space'
74
+ autoload :StatementModifier, 'rubocop/cop/mixin/statement_modifier'
75
+ autoload :StringHelp, 'rubocop/cop/mixin/string_help'
76
+ autoload :StringLiteralsHelp, 'rubocop/cop/mixin/string_literals_help'
77
+ autoload :SymbolHelp, 'rubocop/cop/mixin/symbol_help'
78
+ autoload :TargetRubyVersion, 'rubocop/cop/mixin/target_ruby_version'
79
+ autoload :TrailingBody, 'rubocop/cop/mixin/trailing_body'
80
+ autoload :TrailingComma, 'rubocop/cop/mixin/trailing_comma'
81
+ autoload :UncommunicativeName, 'rubocop/cop/mixin/uncommunicative_name'
82
+ autoload :VisibilityHelp, 'rubocop/cop/mixin/visibility_help'
83
+ autoload :CommentsHelp, 'rubocop/cop/mixin/comments_help'
84
+ autoload :DefNode, 'rubocop/cop/mixin/def_node'
85
+ end
86
+ end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Naming
6
6
  # Makes sure that certain binary operator methods have their
7
- # sole parameter named `other`.
7
+ # sole parameter named `other`.
8
8
  #
9
9
  # @example
10
10
  #
@@ -38,7 +38,7 @@ module RuboCop
38
38
  class BlockParameterName < Base
39
39
  include UncommunicativeName
40
40
 
41
- def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
41
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
42
42
  return unless node.arguments?
43
43
 
44
44
  check(node, node.arguments)
@@ -109,7 +109,7 @@ module RuboCop
109
109
  # @_foo = calculate_expensive_thing
110
110
  # end
111
111
  #
112
- # @example EnforcedStyleForLeadingUnderscores :optional
112
+ # @example EnforcedStyleForLeadingUnderscores: optional
113
113
  # # bad
114
114
  # def foo
115
115
  # @something ||= calculate_expensive_thing
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Checks that predicate methods end with `?` and non-predicate methods do not.
7
7
  #
8
8
  # The names of predicate methods (methods that return a boolean value) should end
9
- # in a question mark. Methods that don't return a boolean, shouldn't
9
+ # in a question mark. Methods that don't return a boolean shouldn't
10
10
  # end in a question mark.
11
11
  #
12
12
  # The cop assesses a predicate method as one that returns boolean values. Likewise,
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # return values are detected.
23
23
  #
24
24
  # The cop also has `AllowedMethods` configuration in order to prevent the cop from
25
- # registering an offense from a method name that does not confirm to the naming
25
+ # registering an offense from a method name that does not conform to the naming
26
26
  # guidelines. By default, `call` is allowed. The cop also has `AllowedPatterns`
27
27
  # configuration to allow method names by regular expression.
28
28
  #
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # they end with a `?`. These methods should be changed to remove the
18
18
  # prefix.
19
19
  #
20
- # When `UseSorbetSigs` set to true (optional), the cop will only report
20
+ # When `UseSorbetSigs` is set to true (optional), the cop will only report
21
21
  # offenses if the method has a Sorbet `sig` with a return type of
22
22
  # `T::Boolean`. Dynamic methods are not supported with this configuration.
23
23
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Naming
6
- # Makes sure that rescued exceptions variables are named as
6
+ # Makes sure that rescued exception variables are named as
7
7
  # expected.
8
8
  #
9
9
  # The `PreferredName` config option takes a `String`. It represents
@@ -98,6 +98,14 @@ module RuboCop
98
98
  freeze
99
99
  end
100
100
 
101
+ def marshal_dump
102
+ [@severity, @location, @message, @cop_name, @status]
103
+ end
104
+
105
+ def marshal_load(array)
106
+ @severity, @location, @message, @cop_name, @status = array
107
+ end
108
+
101
109
  # @api public
102
110
  #
103
111
  # @!attribute [r] correctable?
@@ -16,7 +16,7 @@ module RuboCop
16
16
  end
17
17
 
18
18
  # Registry that tracks all cops by their badge and department.
19
- class Registry
19
+ class Registry # rubocop:disable Metrics/ClassLength
20
20
  include Enumerable
21
21
 
22
22
  def self.all
@@ -46,18 +46,25 @@ module RuboCop
46
46
  global.qualify_badge(badge).first == badge
47
47
  end
48
48
 
49
- attr_reader :options
49
+ attr_reader :options, :warnings
50
50
 
51
51
  def initialize(cops = [], options = {})
52
- @registry = {}
53
- @departments = {}
54
- @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] }
52
+ @departments = Set.new
53
+ @cops_by_badge = {}
54
+ @lazy_loaded_cops_by_badge = {}
55
55
 
56
56
  @enrollment_queue = cops
57
57
  @options = options
58
58
 
59
59
  @enabled_cache = {}.compare_by_identity
60
60
  @disabled_cache = {}.compare_by_identity
61
+ @warnings = {}
62
+ end
63
+
64
+ def lazy_load(cop_name, constant_name)
65
+ badge = Badge.parse(cop_name)
66
+ @departments << badge.department
67
+ @lazy_loaded_cops_by_badge[badge] = constant_name
61
68
  end
62
69
 
63
70
  def enlist(cop)
@@ -71,22 +78,17 @@ module RuboCop
71
78
  # @return [Array<Symbol>] list of departments for current cops.
72
79
  def departments
73
80
  clear_enrollment_queue
74
- @departments.keys
81
+ @departments.to_a
75
82
  end
76
83
 
77
84
  # @return [Registry] Cops for that specific department.
78
85
  def with_department(department)
79
- clear_enrollment_queue
80
- with(@departments.fetch(department, []))
86
+ with(cops.select { |cop| cop.department == department })
81
87
  end
82
88
 
83
89
  # @return [Registry] Cops not for a specific department.
84
90
  def without_department(department)
85
- clear_enrollment_queue
86
- without_department = @departments.dup
87
- without_department.delete(department)
88
-
89
- with(without_department.values.flatten)
91
+ with(cops.reject { |cop| cop.department == department })
90
92
  end
91
93
 
92
94
  # @return [Boolean] Checks if given name is department
@@ -132,7 +134,7 @@ module RuboCop
132
134
  # @return [String] Qualified cop name
133
135
  def qualified_cop_name(name, path, warn: true)
134
136
  badge = Badge.parse(name)
135
- print_warning(name, path) if warn && department_missing?(badge, name)
137
+ print_department_missing_warning(name, path) if warn && department_missing?(badge, name)
136
138
  return name if registered?(badge)
137
139
 
138
140
  potential_badges = qualify_badge(badge)
@@ -148,42 +150,44 @@ module RuboCop
148
150
  !badge.qualified? && unqualified_cop_names.include?(name)
149
151
  end
150
152
 
151
- def print_warning(name, path)
152
- message = "#{path}: Warning: no department given for #{name}."
153
+ def print_department_missing_warning(name, path)
154
+ message = "no department given for #{name}."
153
155
  if path.end_with?('.rb')
154
156
  message += ' Run `rubocop -a --only Migration/DepartmentName` to fix.'
155
157
  end
156
- warn message
158
+ emit_warning(path, message)
157
159
  end
158
160
 
159
161
  def unqualified_cop_names
160
162
  clear_enrollment_queue
161
163
  @unqualified_cop_names ||=
162
- Set.new(@cops_by_cop_name.keys.map { |qn| File.basename(qn) }) <<
163
- 'RedundantCopDisableDirective'
164
+ (@cops_by_badge.keys | @lazy_loaded_cops_by_badge.keys)
165
+ .to_set { |badge| File.basename(badge.to_s) } << 'RedundantCopDisableDirective'
164
166
  end
165
167
 
166
168
  def qualify_badge(badge)
167
169
  clear_enrollment_queue
168
170
  @departments
169
- .map { |department, _| badge.with_department(department) }
171
+ .map { |department| badge.with_department(department) }
170
172
  .select { |potential_badge| registered?(potential_badge) }
171
173
  end
172
174
 
173
175
  # @return [Hash{String => Array<Class>}]
174
176
  def to_h
175
177
  clear_enrollment_queue
176
- @cops_by_cop_name
178
+ load_all_lazy_cops
179
+ @cops_by_badge.to_h { |_badge, cop| [cop.cop_name, [cop]] }
177
180
  end
178
181
 
179
182
  def cops
180
183
  clear_enrollment_queue
181
- @registry.values
184
+ load_all_lazy_cops
185
+ @cops_by_badge.values
182
186
  end
183
187
 
184
188
  def length
185
189
  clear_enrollment_queue
186
- @registry.size
190
+ @cops_by_badge.size + @lazy_loaded_cops_by_badge.size
187
191
  end
188
192
 
189
193
  def enabled(config)
@@ -218,7 +222,8 @@ module RuboCop
218
222
  end
219
223
 
220
224
  def names
221
- cops.map(&:cop_name)
225
+ clear_enrollment_queue
226
+ @cops_by_badge.keys.map(&:to_s) | @lazy_loaded_cops_by_badge.keys.map(&:to_s)
222
227
  end
223
228
 
224
229
  def cops_for_department(department)
@@ -235,7 +240,8 @@ module RuboCop
235
240
 
236
241
  def sort!
237
242
  clear_enrollment_queue
238
- @registry = @registry.sort_by { |badge, _| badge.cop_name }.to_h
243
+ load_all_lazy_cops
244
+ @cops_by_badge = @cops_by_badge.sort_by { |badge, _cop| badge.cop_name }.to_h
239
245
 
240
246
  self
241
247
  end
@@ -251,7 +257,9 @@ module RuboCop
251
257
  # @param [String] cop_name
252
258
  # @return [Class, nil]
253
259
  def find_by_cop_name(cop_name)
254
- to_h[cop_name].first
260
+ clear_enrollment_queue
261
+ badge = Badge.parse(cop_name)
262
+ @cops_by_badge[badge] || load_lazy_cop(badge)
255
263
  end
256
264
 
257
265
  # When a cop name is given returns a single-element array with the cop class.
@@ -264,6 +272,7 @@ module RuboCop
264
272
 
265
273
  def freeze
266
274
  clear_enrollment_queue
275
+ load_all_lazy_cops
267
276
  unqualified_cop_names # build cache
268
277
  super
269
278
  end
@@ -274,6 +283,10 @@ module RuboCop
274
283
  attr_reader :global
275
284
  end
276
285
 
286
+ def warnings?(path)
287
+ @warnings[path]
288
+ end
289
+
277
290
  private
278
291
 
279
292
  def initialize_copy(reg)
@@ -284,34 +297,45 @@ module RuboCop
284
297
  return if @enrollment_queue.empty?
285
298
 
286
299
  @enrollment_queue.each do |cop|
287
- @registry[cop.badge] = cop
288
- @departments[cop.department] ||= []
289
- @departments[cop.department] << cop
290
- @cops_by_cop_name[cop.cop_name] << cop
300
+ @cops_by_badge[cop.badge] = cop
301
+ @departments << cop.department
291
302
  end
292
303
  @enrollment_queue = []
293
304
  end
294
305
 
306
+ def load_all_lazy_cops
307
+ @lazy_loaded_cops_by_badge.each_key { |badge| load_lazy_cop(badge) }
308
+ end
309
+
310
+ def load_lazy_cop(badge)
311
+ constant_name = @lazy_loaded_cops_by_badge.delete(badge)
312
+ return unless constant_name
313
+
314
+ @cops_by_badge[badge] = Kernel.const_get(constant_name)
315
+ end
316
+
295
317
  def with(cops)
296
318
  self.class.new(cops)
297
319
  end
298
320
 
299
321
  def resolve_badge(given_badge, real_badge, source_path, warn: true)
300
- unless given_badge.match?(real_badge)
301
- path = PathUtil.smart_path(source_path)
302
-
303
- if warn
304
- warn("#{path}: #{given_badge} has the wrong namespace - " \
305
- "replace it with #{given_badge.with_department(real_badge.department)}")
306
- end
322
+ if warn && !given_badge.match?(real_badge)
323
+ emit_warning(source_path,
324
+ "#{given_badge} has the wrong namespace - " \
325
+ "replace it with #{given_badge.with_department(real_badge.department)}")
307
326
  end
308
327
 
309
328
  real_badge.to_s
310
329
  end
311
330
 
331
+ def emit_warning(path, message)
332
+ Registry.global.warnings[path] = true
333
+ warn "#{PathUtil.smart_path(path)}: Warning: #{message}"
334
+ end
335
+
312
336
  def registered?(badge)
313
337
  clear_enrollment_queue
314
- @registry.key?(badge)
338
+ @cops_by_badge.key?(badge) || @lazy_loaded_cops_by_badge.key?(badge)
315
339
  end
316
340
  end
317
341
  end
@@ -3,15 +3,28 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Security
6
- # Checks for the use of `Kernel#eval` and `Binding#eval`.
6
+ # Checks for the use of `Kernel#eval` and `Binding#eval` with
7
+ # dynamic strings as arguments. Evaluating non-literal strings
8
+ # can enable code injection attacks and makes it difficult to
9
+ # reason about what code will actually be executed.
10
+ #
11
+ # Calls to `eval` with literal strings are not flagged by this cop,
12
+ # as they do not pose the same injection risk.
7
13
  #
8
14
  # @example
9
15
  #
10
16
  # # bad
11
- #
12
17
  # eval(something)
13
18
  # binding.eval(something)
14
19
  # Kernel.eval(something)
20
+ #
21
+ # # good - use safer alternatives
22
+ # obj.public_send(method_name)
23
+ # obj.send(method_name, *args)
24
+ #
25
+ # # good - literal strings are allowed
26
+ # eval("1 + 1")
27
+ # binding.eval("foo")
15
28
  class Eval < Base
16
29
  MSG = 'The use of `eval` is a serious security risk.'
17
30
  RESTRICT_ON_SEND = %i[eval].freeze
@@ -10,7 +10,7 @@ module RuboCop
10
10
  # a subprocess is created in the same way as `Kernel#open`, and its output is returned.
11
11
  # `Kernel#open` may allow unintentional command injection, which is the reason these
12
12
  # `IO` methods are a security risk.
13
- # Consider to use `File.read` to disable the behavior of subprocess invocation.
13
+ # Consider using `File.read` to disable the behavior of subprocess invocation.
14
14
  #
15
15
  # @safety
16
16
  # This cop is unsafe because false positive will occur if the variable passed as
@@ -348,8 +348,20 @@ module RuboCop
348
348
 
349
349
  def remove_modifier_node_within_begin(corrector, modifier_node, begin_node)
350
350
  def_node = begin_node.children[begin_node.children.index(modifier_node) + 1]
351
- range = modifier_node.source_range.begin.join(def_node.source_range.begin)
352
- corrector.remove(range)
351
+ # Stop the removal range at the first comment that precedes the def, if
352
+ # any exist. Without this, comments between the modifier and the def are
353
+ # dropped because they fall inside the removed range.
354
+ end_pos = first_comment_or_node_start(def_node)
355
+ corrector.remove(modifier_node.source_range.begin.join(end_pos))
356
+ end
357
+
358
+ def first_comment_or_node_start(node)
359
+ preceding = processed_source.ast_with_comments[node].select do |comment|
360
+ comment.loc.line < node.loc.line
361
+ end
362
+ return node.source_range.begin if preceding.empty?
363
+
364
+ preceding.first.source_range.begin
353
365
  end
354
366
 
355
367
  def def_source(node, def_nodes)
@@ -4,8 +4,10 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Checks for grouping of accessors in `class` and `module` bodies.
7
- # By default it enforces accessors to be placed in grouped declarations,
8
- # but it can be configured to enforce separating them in multiple declarations.
7
+ # By default it enforces accessors to be placed in grouped
8
+ # declarations, reducing boilerplate. It can also be configured
9
+ # to enforce separating them into individual declarations for
10
+ # easier diffing and per-attribute documentation.
9
11
  #
10
12
  # NOTE: If there is a method call before the accessor method it is always allowed
11
13
  # as it might be intended like Sorbet.
@@ -4,10 +4,13 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Enforces the use of either `#alias` or `#alias_method`
7
- # depending on configuration.
7
+ # depending on configuration. Consistent use of one or the
8
+ # other prevents confusion about their different semantics
9
+ # (e.g., `alias` is resolved at parse time, while `alias_method`
10
+ # is resolved at runtime).
8
11
  # It also flags uses of `alias :symbol` rather than `alias bareword`.
9
12
  #
10
- # However, it will always enforce `method_alias` when used `alias`
13
+ # However, it will always enforce `alias_method` when `alias` is used
11
14
  # in an instance method definition and in a singleton method definition.
12
15
  # If used in a block, always enforce `alias_method`
13
16
  # unless it is an `instance_eval` block.
@@ -42,6 +45,7 @@ module RuboCop
42
45
  return unless node.command?(:alias_method)
43
46
  return unless style == :prefer_alias && alias_keyword_possible?(node)
44
47
  return unless node.arguments.count == 2
48
+ return if alias_method_value_used?(node)
45
49
 
46
50
  msg = format(MSG_ALIAS_METHOD, current: lexical_scope_type(node))
47
51
  add_offense(node.loc.selector, message: msg) do |corrector|
@@ -77,6 +81,14 @@ module RuboCop
77
81
  scope_type(node) != :dynamic && node.arguments.all?(&:sym_type?)
78
82
  end
79
83
 
84
+ # `alias_method` is a method call whose return value can be used
85
+ # (e.g., as an argument to `public`/`module_function`, or as an assignment),
86
+ # but `alias` is a keyword statement that cannot appear in such positions.
87
+ # Detect these positions so the conversion does not produce a syntax error.
88
+ def alias_method_value_used?(node)
89
+ node.argument? || node.parent&.assignment?
90
+ end
91
+
80
92
  def alias_method_possible?(node)
81
93
  scope_type(node) != :instance_eval &&
82
94
  node.children.none?(&:gvar_type?) &&
@@ -128,6 +128,7 @@ module RuboCop
128
128
 
129
129
  def correct_other(node, corrector)
130
130
  return if node.parenthesized_call?
131
+ return if node.children.empty? && node.equal?(node.parent.children.last)
131
132
 
132
133
  corrector.wrap(node, '(', ')')
133
134
  end