rubocop 1.84.2 → 1.86.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +99 -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 +7 -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_resolver.rb +2 -1
  16. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
  17. data/lib/rubocop/config_store.rb +1 -1
  18. data/lib/rubocop/config_validator.rb +1 -1
  19. data/lib/rubocop/cop/autocorrect_logic.rb +2 -1
  20. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  21. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -5
  22. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  23. data/lib/rubocop/cop/correctors.rb +28 -0
  24. data/lib/rubocop/cop/documentation.rb +2 -3
  25. data/lib/rubocop/cop/exclude_limit.rb +31 -5
  26. data/lib/rubocop/cop/gemspec/require_mfa.rb +4 -4
  27. data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
  28. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  30. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -2
  31. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  32. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -2
  34. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  35. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
  36. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
  37. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
  38. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
  39. data/lib/rubocop/cop/layout/end_alignment.rb +6 -3
  40. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  41. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  42. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  43. data/lib/rubocop/cop/layout/line_length.rb +5 -3
  44. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
  45. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  46. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +53 -3
  47. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  48. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  50. data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
  51. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
  52. data/lib/rubocop/cop/lint/constant_reassignment.rb +59 -9
  53. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  54. data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
  55. data/lib/rubocop/cop/lint/duplicate_methods.rb +55 -8
  56. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  57. data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
  58. data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
  59. data/lib/rubocop/cop/lint/empty_when.rb +8 -1
  60. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  61. data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
  62. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -1
  63. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  64. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -13
  65. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +0 -9
  66. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +23 -6
  67. data/lib/rubocop/cop/lint/require_relative_self_path.rb +2 -0
  68. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
  69. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
  70. data/lib/rubocop/cop/lint/syntax.rb +25 -1
  71. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
  72. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  73. data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
  74. data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
  75. data/lib/rubocop/cop/lint/useless_assignment.rb +4 -9
  76. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
  77. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
  78. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +35 -9
  79. data/lib/rubocop/cop/lint/void.rb +32 -12
  80. data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
  81. data/lib/rubocop/cop/migration/department_name.rb +12 -1
  82. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  83. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  84. data/lib/rubocop/cop/mixin/configurable_max.rb +6 -5
  85. data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
  86. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
  87. data/lib/rubocop/cop/mixin.rb +85 -0
  88. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  89. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  90. data/lib/rubocop/cop/offense.rb +8 -0
  91. data/lib/rubocop/cop/registry.rb +39 -37
  92. data/lib/rubocop/cop/security/eval.rb +15 -2
  93. data/lib/rubocop/cop/style/access_modifier_declarations.rb +14 -2
  94. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
  95. data/lib/rubocop/cop/style/alias.rb +4 -1
  96. data/lib/rubocop/cop/style/and_or.rb +1 -0
  97. data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
  98. data/lib/rubocop/cop/style/array_join.rb +4 -2
  99. data/lib/rubocop/cop/style/ascii_comments.rb +6 -3
  100. data/lib/rubocop/cop/style/attr.rb +5 -2
  101. data/lib/rubocop/cop/style/bare_percent_literals.rb +3 -1
  102. data/lib/rubocop/cop/style/begin_block.rb +3 -1
  103. data/lib/rubocop/cop/style/block_delimiters.rb +25 -33
  104. data/lib/rubocop/cop/style/case_equality.rb +4 -0
  105. data/lib/rubocop/cop/style/class_and_module_children.rb +10 -2
  106. data/lib/rubocop/cop/style/collection_compact.rb +36 -16
  107. data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
  108. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
  109. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -4
  110. data/lib/rubocop/cop/style/copyright.rb +22 -11
  111. data/lib/rubocop/cop/style/date_time.rb +2 -2
  112. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +6 -1
  113. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  114. data/lib/rubocop/cop/style/each_with_object.rb +2 -0
  115. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  116. data/lib/rubocop/cop/style/empty_class_definition.rb +43 -20
  117. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  118. data/lib/rubocop/cop/style/encoding.rb +7 -1
  119. data/lib/rubocop/cop/style/end_block.rb +3 -1
  120. data/lib/rubocop/cop/style/endless_method.rb +8 -3
  121. data/lib/rubocop/cop/style/file_open.rb +84 -0
  122. data/lib/rubocop/cop/style/for.rb +3 -0
  123. data/lib/rubocop/cop/style/format_string_token.rb +29 -2
  124. data/lib/rubocop/cop/style/global_vars.rb +5 -2
  125. data/lib/rubocop/cop/style/guard_clause.rb +9 -6
  126. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +21 -5
  127. data/lib/rubocop/cop/style/hash_lookup_method.rb +19 -7
  128. data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
  129. data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
  130. data/lib/rubocop/cop/style/if_inside_else.rb +16 -7
  131. data/lib/rubocop/cop/style/if_unless_modifier.rb +14 -3
  132. data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
  133. data/lib/rubocop/cop/style/inline_comment.rb +4 -1
  134. data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
  135. data/lib/rubocop/cop/style/magic_comment_format.rb +2 -2
  136. data/lib/rubocop/cop/style/map_join.rb +123 -0
  137. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -3
  138. data/lib/rubocop/cop/style/module_member_existence_check.rb +7 -14
  139. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -1
  140. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  141. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  142. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  143. data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
  144. data/lib/rubocop/cop/style/not.rb +2 -0
  145. data/lib/rubocop/cop/style/numeric_literals.rb +3 -2
  146. data/lib/rubocop/cop/style/one_class_per_file.rb +115 -0
  147. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  148. data/lib/rubocop/cop/style/parallel_assignment.rb +4 -0
  149. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
  150. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  151. data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
  152. data/lib/rubocop/cop/style/proc.rb +3 -2
  153. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  154. data/lib/rubocop/cop/style/reduce_to_hash.rb +200 -0
  155. data/lib/rubocop/cop/style/redundant_begin.rb +3 -3
  156. data/lib/rubocop/cop/style/redundant_each.rb +3 -3
  157. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  158. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
  159. data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
  160. data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
  161. data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -22
  162. data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -1
  163. data/lib/rubocop/cop/style/redundant_return.rb +3 -1
  164. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  165. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
  166. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +114 -0
  167. data/lib/rubocop/cop/style/regexp_literal.rb +29 -0
  168. data/lib/rubocop/cop/style/safe_navigation.rb +7 -7
  169. data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
  170. data/lib/rubocop/cop/style/select_by_range.rb +197 -0
  171. data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
  172. data/lib/rubocop/cop/style/semicolon.rb +2 -0
  173. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  174. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
  175. data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
  176. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -2
  177. data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
  178. data/lib/rubocop/cop/style/symbol_proc.rb +7 -6
  179. data/lib/rubocop/cop/style/tally_method.rb +181 -0
  180. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  181. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
  182. data/lib/rubocop/cop/style/while_until_modifier.rb +16 -0
  183. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  184. data/lib/rubocop/cop/team.rb +86 -35
  185. data/lib/rubocop/cop/variable_force/branch.rb +2 -2
  186. data/lib/rubocop/directive_comment.rb +2 -1
  187. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -2
  188. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  189. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  190. data/lib/rubocop/formatter/simple_text_formatter.rb +0 -2
  191. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  192. data/lib/rubocop/formatter.rb +22 -21
  193. data/lib/rubocop/lsp/diagnostic.rb +1 -0
  194. data/lib/rubocop/lsp/routes.rb +10 -3
  195. data/lib/rubocop/lsp/runtime.rb +1 -2
  196. data/lib/rubocop/mcp/server.rb +200 -0
  197. data/lib/rubocop/options.rb +17 -4
  198. data/lib/rubocop/path_util.rb +14 -2
  199. data/lib/rubocop/plugin/loader.rb +1 -1
  200. data/lib/rubocop/result_cache.rb +22 -10
  201. data/lib/rubocop/rspec/cop_helper.rb +8 -0
  202. data/lib/rubocop/rspec/shared_contexts.rb +32 -2
  203. data/lib/rubocop/runner.rb +78 -51
  204. data/lib/rubocop/server/cache.rb +5 -7
  205. data/lib/rubocop/server/core.rb +2 -0
  206. data/lib/rubocop/target_finder.rb +14 -7
  207. data/lib/rubocop/target_ruby.rb +18 -12
  208. data/lib/rubocop/version.rb +2 -2
  209. data/lib/rubocop.rb +21 -96
  210. metadata +25 -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,85 @@
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 :AnnotationComment, 'rubocop/cop/mixin/annotation_comment'
32
+ autoload :EmptyParameter, 'rubocop/cop/mixin/empty_parameter'
33
+ autoload :EndKeywordAlignment, 'rubocop/cop/mixin/end_keyword_alignment'
34
+ autoload :EndlessMethodRewriter, 'rubocop/cop/mixin/endless_method_rewriter'
35
+ autoload :EnforceSuperclass, 'rubocop/cop/mixin/enforce_superclass'
36
+ autoload :FirstElementLineBreak, 'rubocop/cop/mixin/first_element_line_break'
37
+ autoload :FrozenStringLiteral, 'rubocop/cop/mixin/frozen_string_literal'
38
+ autoload :GemDeclaration, 'rubocop/cop/mixin/gem_declaration'
39
+ autoload :GemspecHelp, 'rubocop/cop/mixin/gemspec_help'
40
+ autoload :HashAlignmentStyles, 'rubocop/cop/mixin/hash_alignment_styles'
41
+ autoload :HashSubset, 'rubocop/cop/mixin/hash_subset'
42
+ autoload :HashTransformMethod, 'rubocop/cop/mixin/hash_transform_method'
43
+ autoload :IntegerNode, 'rubocop/cop/mixin/integer_node'
44
+ autoload :Interpolation, 'rubocop/cop/mixin/interpolation'
45
+ autoload :LineLengthHelp, 'rubocop/cop/mixin/line_length_help'
46
+ autoload :MatchRange, 'rubocop/cop/mixin/match_range'
47
+ autoload :HashShorthandSyntax, 'rubocop/cop/mixin/hash_shorthand_syntax'
48
+ autoload :MethodComplexity, 'rubocop/cop/mixin/method_complexity'
49
+ autoload :MethodPreference, 'rubocop/cop/mixin/method_preference'
50
+ autoload :MinBodyLength, 'rubocop/cop/mixin/min_body_length'
51
+ autoload :MinBranchesCount, 'rubocop/cop/mixin/min_branches_count'
52
+ autoload :MultilineElementIndentation, 'rubocop/cop/mixin/multiline_element_indentation'
53
+ autoload :MultilineElementLineBreaks, 'rubocop/cop/mixin/multiline_element_line_breaks'
54
+ autoload :MultilineExpressionIndentation, 'rubocop/cop/mixin/multiline_expression_indentation'
55
+ autoload :MultilineLiteralBraceLayout, 'rubocop/cop/mixin/multiline_literal_brace_layout'
56
+ autoload :NegativeConditional, 'rubocop/cop/mixin/negative_conditional'
57
+ autoload :Heredoc, 'rubocop/cop/mixin/heredoc'
58
+ autoload :NilMethods, 'rubocop/cop/mixin/nil_methods'
59
+ autoload :OnNormalIfUnless, 'rubocop/cop/mixin/on_normal_if_unless'
60
+ autoload :OrderedGemNode, 'rubocop/cop/mixin/ordered_gem_node'
61
+ autoload :Parentheses, 'rubocop/cop/mixin/parentheses'
62
+ autoload :PercentArray, 'rubocop/cop/mixin/percent_array'
63
+ autoload :PercentLiteral, 'rubocop/cop/mixin/percent_literal'
64
+ autoload :PrecedingFollowingAlignment, 'rubocop/cop/mixin/preceding_following_alignment'
65
+ autoload :PreferredDelimiters, 'rubocop/cop/mixin/preferred_delimiters'
66
+ autoload :RationalLiteral, 'rubocop/cop/mixin/rational_literal'
67
+ autoload :RequireLibrary, 'rubocop/cop/mixin/require_library'
68
+ autoload :RescueNode, 'rubocop/cop/mixin/rescue_node'
69
+ autoload :SafeAssignment, 'rubocop/cop/mixin/safe_assignment'
70
+ autoload :SpaceAfterPunctuation, 'rubocop/cop/mixin/space_after_punctuation'
71
+ autoload :SpaceBeforePunctuation, 'rubocop/cop/mixin/space_before_punctuation'
72
+ autoload :SurroundingSpace, 'rubocop/cop/mixin/surrounding_space'
73
+ autoload :StatementModifier, 'rubocop/cop/mixin/statement_modifier'
74
+ autoload :StringHelp, 'rubocop/cop/mixin/string_help'
75
+ autoload :StringLiteralsHelp, 'rubocop/cop/mixin/string_literals_help'
76
+ autoload :SymbolHelp, 'rubocop/cop/mixin/symbol_help'
77
+ autoload :TargetRubyVersion, 'rubocop/cop/mixin/target_ruby_version'
78
+ autoload :TrailingBody, 'rubocop/cop/mixin/trailing_body'
79
+ autoload :TrailingComma, 'rubocop/cop/mixin/trailing_comma'
80
+ autoload :UncommunicativeName, 'rubocop/cop/mixin/uncommunicative_name'
81
+ autoload :VisibilityHelp, 'rubocop/cop/mixin/visibility_help'
82
+ autoload :CommentsHelp, 'rubocop/cop/mixin/comments_help'
83
+ autoload :DefNode, 'rubocop/cop/mixin/def_node'
84
+ end
85
+ end
@@ -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
@@ -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,18 @@ 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 = {}
55
54
 
56
55
  @enrollment_queue = cops
57
56
  @options = options
58
57
 
59
58
  @enabled_cache = {}.compare_by_identity
60
59
  @disabled_cache = {}.compare_by_identity
60
+ @warnings = {}
61
61
  end
62
62
 
63
63
  def enlist(cop)
@@ -71,22 +71,17 @@ module RuboCop
71
71
  # @return [Array<Symbol>] list of departments for current cops.
72
72
  def departments
73
73
  clear_enrollment_queue
74
- @departments.keys
74
+ @departments.to_a
75
75
  end
76
76
 
77
77
  # @return [Registry] Cops for that specific department.
78
78
  def with_department(department)
79
- clear_enrollment_queue
80
- with(@departments.fetch(department, []))
79
+ with(cops.select { |cop| cop.department == department })
81
80
  end
82
81
 
83
82
  # @return [Registry] Cops not for a specific department.
84
83
  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)
84
+ with(cops.reject { |cop| cop.department == department })
90
85
  end
91
86
 
92
87
  # @return [Boolean] Checks if given name is department
@@ -132,7 +127,7 @@ module RuboCop
132
127
  # @return [String] Qualified cop name
133
128
  def qualified_cop_name(name, path, warn: true)
134
129
  badge = Badge.parse(name)
135
- print_warning(name, path) if warn && department_missing?(badge, name)
130
+ print_department_missing_warning(name, path) if warn && department_missing?(badge, name)
136
131
  return name if registered?(badge)
137
132
 
138
133
  potential_badges = qualify_badge(badge)
@@ -148,42 +143,42 @@ module RuboCop
148
143
  !badge.qualified? && unqualified_cop_names.include?(name)
149
144
  end
150
145
 
151
- def print_warning(name, path)
152
- message = "#{path}: Warning: no department given for #{name}."
146
+ def print_department_missing_warning(name, path)
147
+ message = "no department given for #{name}."
153
148
  if path.end_with?('.rb')
154
149
  message += ' Run `rubocop -a --only Migration/DepartmentName` to fix.'
155
150
  end
156
- warn message
151
+ emit_warning(path, message)
157
152
  end
158
153
 
159
154
  def unqualified_cop_names
160
155
  clear_enrollment_queue
161
156
  @unqualified_cop_names ||=
162
- Set.new(@cops_by_cop_name.keys.map { |qn| File.basename(qn) }) <<
157
+ Set.new(@cops_by_badge.keys.map { |badge| File.basename(badge.to_s) }) <<
163
158
  'RedundantCopDisableDirective'
164
159
  end
165
160
 
166
161
  def qualify_badge(badge)
167
162
  clear_enrollment_queue
168
163
  @departments
169
- .map { |department, _| badge.with_department(department) }
164
+ .map { |department| badge.with_department(department) }
170
165
  .select { |potential_badge| registered?(potential_badge) }
171
166
  end
172
167
 
173
168
  # @return [Hash{String => Array<Class>}]
174
169
  def to_h
175
170
  clear_enrollment_queue
176
- @cops_by_cop_name
171
+ @cops_by_badge.to_h { |_badge, cop| [cop.cop_name, [cop]] }
177
172
  end
178
173
 
179
174
  def cops
180
175
  clear_enrollment_queue
181
- @registry.values
176
+ @cops_by_badge.values
182
177
  end
183
178
 
184
179
  def length
185
180
  clear_enrollment_queue
186
- @registry.size
181
+ @cops_by_badge.size
187
182
  end
188
183
 
189
184
  def enabled(config)
@@ -218,7 +213,8 @@ module RuboCop
218
213
  end
219
214
 
220
215
  def names
221
- cops.map(&:cop_name)
216
+ clear_enrollment_queue
217
+ @cops_by_badge.keys.map(&:to_s)
222
218
  end
223
219
 
224
220
  def cops_for_department(department)
@@ -235,7 +231,7 @@ module RuboCop
235
231
 
236
232
  def sort!
237
233
  clear_enrollment_queue
238
- @registry = @registry.sort_by { |badge, _| badge.cop_name }.to_h
234
+ @cops_by_badge = @cops_by_badge.sort_by { |badge, _cop| badge.cop_name }.to_h
239
235
 
240
236
  self
241
237
  end
@@ -251,7 +247,9 @@ module RuboCop
251
247
  # @param [String] cop_name
252
248
  # @return [Class, nil]
253
249
  def find_by_cop_name(cop_name)
254
- to_h[cop_name].first
250
+ clear_enrollment_queue
251
+ badge = Badge.parse(cop_name)
252
+ @cops_by_badge[badge]
255
253
  end
256
254
 
257
255
  # When a cop name is given returns a single-element array with the cop class.
@@ -274,6 +272,10 @@ module RuboCop
274
272
  attr_reader :global
275
273
  end
276
274
 
275
+ def warnings?(path)
276
+ @warnings[path]
277
+ end
278
+
277
279
  private
278
280
 
279
281
  def initialize_copy(reg)
@@ -284,10 +286,8 @@ module RuboCop
284
286
  return if @enrollment_queue.empty?
285
287
 
286
288
  @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
289
+ @cops_by_badge[cop.badge] = cop
290
+ @departments << cop.department
291
291
  end
292
292
  @enrollment_queue = []
293
293
  end
@@ -297,21 +297,23 @@ module RuboCop
297
297
  end
298
298
 
299
299
  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
300
+ if warn && !given_badge.match?(real_badge)
301
+ emit_warning(source_path,
302
+ "#{given_badge} has the wrong namespace - " \
303
+ "replace it with #{given_badge.with_department(real_badge.department)}")
307
304
  end
308
305
 
309
306
  real_badge.to_s
310
307
  end
311
308
 
309
+ def emit_warning(path, message)
310
+ Registry.global.warnings[path] = true
311
+ warn "#{PathUtil.smart_path(path)}: Warning: #{message}"
312
+ end
313
+
312
314
  def registered?(badge)
313
315
  clear_enrollment_queue
314
- @registry.key?(badge)
316
+ @cops_by_badge.key?(badge)
315
317
  end
316
318
  end
317
319
  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
@@ -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,7 +4,10 @@ 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
13
  # However, it will always enforce `method_alias` when used `alias`
@@ -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
@@ -424,21 +424,39 @@ module RuboCop
424
424
  end
425
425
 
426
426
  def forwarded_rest_arg
427
- return nil if referenced_rest_arg?
427
+ return @forwarded_rest_arg if defined?(@forwarded_rest_arg)
428
428
 
429
- arguments.find { |arg| forwarded_rest_arg?(arg, @rest_arg_name) }
429
+ @forwarded_rest_arg = if referenced_rest_arg?
430
+ nil
431
+ else
432
+ arguments.find do |arg|
433
+ forwarded_rest_arg?(arg, @rest_arg_name)
434
+ end
435
+ end
430
436
  end
431
437
 
432
438
  def forwarded_kwrest_arg
433
- return nil if referenced_kwrest_arg?
439
+ return @forwarded_kwrest_arg if defined?(@forwarded_kwrest_arg)
434
440
 
435
- arguments.filter_map { |arg| extract_forwarded_kwrest_arg(arg, @kwrest_arg_name) }.first
441
+ @forwarded_kwrest_arg = if referenced_kwrest_arg?
442
+ nil
443
+ else
444
+ arguments.filter_map do |arg|
445
+ extract_forwarded_kwrest_arg(arg, @kwrest_arg_name)
446
+ end.first
447
+ end
436
448
  end
437
449
 
438
450
  def forwarded_block_arg
439
- return nil if referenced_block_arg?
440
-
441
- arguments.find { |arg| forwarded_block_arg?(arg, @block_arg_name) }
451
+ return @forwarded_block_arg if defined?(@forwarded_block_arg)
452
+
453
+ @forwarded_block_arg = if referenced_block_arg?
454
+ nil
455
+ else
456
+ arguments.find do |arg|
457
+ forwarded_block_arg?(arg, @block_arg_name)
458
+ end
459
+ end
442
460
  end
443
461
 
444
462
  def classification
@@ -3,9 +3,11 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for uses of "*" as a substitute for _join_.
6
+ # Checks for uses of `*` as a substitute for `Array#join`.
7
+ # Using `join` is clearer about intent and more readable than
8
+ # overloading the `*` operator for string conversion.
7
9
  #
8
- # Not all cases can reliably checked, due to Ruby's dynamic
10
+ # Not all cases can be reliably checked, due to Ruby's dynamic
9
11
  # types, so we consider only cases when the first argument is an
10
12
  # array literal or the second is a string literal.
11
13
  #
@@ -4,8 +4,11 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Checks for non-ascii (non-English) characters
7
- # in comments. You could set an array of allowed non-ascii chars in
8
- # `AllowedChars` attribute (copyright notice "©" by default).
7
+ # in comments. Non-ascii characters can cause issues with
8
+ # portability and encoding across different environments
9
+ # and editors. You could set an array of allowed non-ascii
10
+ # chars in `AllowedChars` attribute (copyright notice "©"
11
+ # by default).
9
12
  #
10
13
  # @example
11
14
  # # bad
@@ -49,7 +52,7 @@ module RuboCop
49
52
  end
50
53
 
51
54
  def allowed_non_ascii_chars
52
- cop_config['AllowedChars'] || []
55
+ @allowed_non_ascii_chars ||= (cop_config['AllowedChars'] || []).freeze
53
56
  end
54
57
  end
55
58
  end
@@ -3,10 +3,13 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for uses of Module#attr.
6
+ # Checks for uses of `Module#attr`. The `attr` method has confusing
7
+ # behavior: with a single argument it creates a reader (like `attr_reader`),
8
+ # but with a second boolean argument it creates an accessor (deprecated in
9
+ # Ruby 1.9). Use `attr_reader` or `attr_accessor` to make intent explicit.
7
10
  #
8
11
  # @example
9
- # # bad - creates a single attribute accessor (deprecated in Ruby 1.9)
12
+ # # bad
10
13
  # attr :something, true
11
14
  # attr :one, :two, :three # behaves as attr_reader
12
15
  #
@@ -3,7 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks if usage of %() or %Q() matches configuration.
6
+ # Checks if usage of `%()` or `%Q()` matches configuration.
7
+ # Consistent use of one style makes the codebase easier
8
+ # to read.
7
9
  #
8
10
  # @example EnforcedStyle: bare_percent (default)
9
11
  # # bad
@@ -3,7 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for BEGIN blocks.
6
+ # Checks for `BEGIN` blocks. They are Perl-style constructs that execute
7
+ # code before the rest of the file is parsed, making the control flow
8
+ # harder to follow and reason about.
7
9
  #
8
10
  # @example
9
11
  # # bad