rubocop 1.84.2 → 1.86.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +91 -15
  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 +1 -1
  6. data/lib/rubocop/cli/command/mcp.rb +19 -0
  7. data/lib/rubocop/cli/command/show_cops.rb +2 -2
  8. data/lib/rubocop/cli/command/show_docs_url.rb +1 -1
  9. data/lib/rubocop/cli.rb +6 -3
  10. data/lib/rubocop/config.rb +14 -10
  11. data/lib/rubocop/config_finder.rb +1 -1
  12. data/lib/rubocop/config_loader_resolver.rb +2 -1
  13. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
  14. data/lib/rubocop/config_store.rb +1 -1
  15. data/lib/rubocop/config_validator.rb +1 -1
  16. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  17. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  18. data/lib/rubocop/cop/documentation.rb +2 -3
  19. data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
  20. data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
  21. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  22. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -2
  23. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  24. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  25. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -2
  26. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  27. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
  28. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
  29. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
  30. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
  31. data/lib/rubocop/cop/layout/end_alignment.rb +6 -3
  32. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  33. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  34. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_length.rb +5 -3
  36. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
  37. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +28 -3
  38. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  39. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  40. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  41. data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
  42. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
  43. data/lib/rubocop/cop/lint/constant_reassignment.rb +59 -9
  44. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  45. data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
  46. data/lib/rubocop/cop/lint/duplicate_methods.rb +55 -8
  47. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  48. data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
  49. data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
  50. data/lib/rubocop/cop/lint/empty_when.rb +8 -1
  51. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  52. data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
  53. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -1
  54. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  55. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +0 -9
  56. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +23 -6
  57. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
  58. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
  59. data/lib/rubocop/cop/lint/syntax.rb +25 -1
  60. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
  61. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  62. data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
  63. data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
  64. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  65. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
  66. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
  67. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +22 -7
  68. data/lib/rubocop/cop/lint/void.rb +32 -12
  69. data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
  70. data/lib/rubocop/cop/migration/department_name.rb +12 -1
  71. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  72. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  73. data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
  74. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
  75. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  76. data/lib/rubocop/cop/registry.rb +20 -13
  77. data/lib/rubocop/cop/security/eval.rb +15 -2
  78. data/lib/rubocop/cop/style/access_modifier_declarations.rb +14 -2
  79. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
  80. data/lib/rubocop/cop/style/alias.rb +4 -1
  81. data/lib/rubocop/cop/style/and_or.rb +1 -0
  82. data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
  83. data/lib/rubocop/cop/style/array_join.rb +4 -2
  84. data/lib/rubocop/cop/style/ascii_comments.rb +6 -3
  85. data/lib/rubocop/cop/style/attr.rb +5 -2
  86. data/lib/rubocop/cop/style/bare_percent_literals.rb +3 -1
  87. data/lib/rubocop/cop/style/begin_block.rb +3 -1
  88. data/lib/rubocop/cop/style/block_delimiters.rb +25 -33
  89. data/lib/rubocop/cop/style/case_equality.rb +4 -0
  90. data/lib/rubocop/cop/style/class_and_module_children.rb +10 -2
  91. data/lib/rubocop/cop/style/collection_compact.rb +36 -16
  92. data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
  93. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
  94. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -4
  95. data/lib/rubocop/cop/style/copyright.rb +1 -1
  96. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  97. data/lib/rubocop/cop/style/each_with_object.rb +2 -0
  98. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  99. data/lib/rubocop/cop/style/empty_class_definition.rb +43 -20
  100. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  101. data/lib/rubocop/cop/style/encoding.rb +7 -1
  102. data/lib/rubocop/cop/style/end_block.rb +3 -1
  103. data/lib/rubocop/cop/style/endless_method.rb +8 -3
  104. data/lib/rubocop/cop/style/file_open.rb +84 -0
  105. data/lib/rubocop/cop/style/for.rb +3 -0
  106. data/lib/rubocop/cop/style/format_string_token.rb +29 -2
  107. data/lib/rubocop/cop/style/global_vars.rb +5 -2
  108. data/lib/rubocop/cop/style/guard_clause.rb +9 -6
  109. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +21 -5
  110. data/lib/rubocop/cop/style/hash_lookup_method.rb +7 -0
  111. data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
  112. data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
  113. data/lib/rubocop/cop/style/if_inside_else.rb +1 -5
  114. data/lib/rubocop/cop/style/if_unless_modifier.rb +14 -3
  115. data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
  116. data/lib/rubocop/cop/style/inline_comment.rb +4 -1
  117. data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
  118. data/lib/rubocop/cop/style/magic_comment_format.rb +2 -2
  119. data/lib/rubocop/cop/style/map_join.rb +123 -0
  120. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -3
  121. data/lib/rubocop/cop/style/module_member_existence_check.rb +1 -11
  122. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -1
  123. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  124. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  125. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  126. data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
  127. data/lib/rubocop/cop/style/not.rb +2 -0
  128. data/lib/rubocop/cop/style/numeric_literals.rb +3 -2
  129. data/lib/rubocop/cop/style/one_class_per_file.rb +115 -0
  130. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  131. data/lib/rubocop/cop/style/parallel_assignment.rb +4 -0
  132. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
  133. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  134. data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
  135. data/lib/rubocop/cop/style/proc.rb +3 -2
  136. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  137. data/lib/rubocop/cop/style/reduce_to_hash.rb +184 -0
  138. data/lib/rubocop/cop/style/redundant_begin.rb +3 -3
  139. data/lib/rubocop/cop/style/redundant_each.rb +3 -3
  140. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  141. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
  142. data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
  143. data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
  144. data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -22
  145. data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -1
  146. data/lib/rubocop/cop/style/redundant_return.rb +3 -1
  147. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
  148. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +114 -0
  149. data/lib/rubocop/cop/style/safe_navigation.rb +7 -7
  150. data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
  151. data/lib/rubocop/cop/style/select_by_range.rb +197 -0
  152. data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
  153. data/lib/rubocop/cop/style/semicolon.rb +2 -0
  154. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  155. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
  156. data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
  157. data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
  158. data/lib/rubocop/cop/style/symbol_proc.rb +4 -3
  159. data/lib/rubocop/cop/style/tally_method.rb +181 -0
  160. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  161. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
  162. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  163. data/lib/rubocop/cop/variable_force/branch.rb +2 -2
  164. data/lib/rubocop/directive_comment.rb +2 -1
  165. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  166. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  167. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  168. data/lib/rubocop/formatter/simple_text_formatter.rb +0 -2
  169. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  170. data/lib/rubocop/formatter.rb +22 -21
  171. data/lib/rubocop/lsp/diagnostic.rb +1 -0
  172. data/lib/rubocop/lsp/routes.rb +10 -3
  173. data/lib/rubocop/mcp/server.rb +200 -0
  174. data/lib/rubocop/options.rb +10 -1
  175. data/lib/rubocop/path_util.rb +14 -2
  176. data/lib/rubocop/plugin/loader.rb +1 -1
  177. data/lib/rubocop/result_cache.rb +22 -10
  178. data/lib/rubocop/rspec/cop_helper.rb +8 -0
  179. data/lib/rubocop/rspec/shared_contexts.rb +11 -2
  180. data/lib/rubocop/runner.rb +8 -3
  181. data/lib/rubocop/server/cache.rb +5 -7
  182. data/lib/rubocop/server/core.rb +2 -0
  183. data/lib/rubocop/target_finder.rb +1 -1
  184. data/lib/rubocop/target_ruby.rb +18 -12
  185. data/lib/rubocop/version.rb +2 -2
  186. data/lib/rubocop.rb +14 -0
  187. metadata +22 -5
@@ -4,6 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  module Metrics
6
6
  # Checks for excessive nesting of conditional and looping constructs.
7
+ # Deeply nested code is harder to read, understand, and maintain.
8
+ # Extracting nested logic into methods improves clarity.
7
9
  #
8
10
  # You can configure if blocks are considered using the `CountBlocks` and `CountModifierForms`
9
11
  # options. When both are set to `false` (the default) blocks and modifier forms are not
@@ -11,6 +13,27 @@ module RuboCop
11
13
  # calculation as well.
12
14
  #
13
15
  # The maximum level of nesting allowed is configurable.
16
+ #
17
+ # @example Max: 3 (default)
18
+ # # bad
19
+ # if condition1
20
+ # if condition2
21
+ # if condition3
22
+ # if condition4
23
+ # do_something
24
+ # end
25
+ # end
26
+ # end
27
+ # end
28
+ #
29
+ # # good
30
+ # if condition1
31
+ # if condition2
32
+ # if condition3
33
+ # do_something
34
+ # end
35
+ # end
36
+ # end
14
37
  class BlockNesting < Base
15
38
  NESTING_BLOCKS = %i[case case_match if while while_post until until_post for resbody].freeze
16
39
 
@@ -1,10 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Lint/RedundantCopDisableDirective needs to be disabled so as
4
+ # to be able to provide examples of rubocop:disable comments.
5
+ # rubocop:disable Lint/RedundantCopDisableDirective
3
6
  module RuboCop
4
7
  module Cop
5
8
  module Migration
6
- # Check that cop names in rubocop:disable comments are given with
9
+ # Checks that cop names in rubocop:disable comments are given with
7
10
  # department name.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # # rubocop:disable AbcSize
15
+ #
16
+ # # good
17
+ # # rubocop:disable Metrics/AbcSize
8
18
  class DepartmentName < Base
9
19
  include RangeHelp
10
20
  extend AutoCorrector
@@ -79,3 +89,4 @@ module RuboCop
79
89
  end
80
90
  end
81
91
  end
92
+ # rubocop:enable Lint/RedundantCopDisableDirective
@@ -222,7 +222,7 @@ module RuboCop
222
222
  def already_on_multiple_lines?(node)
223
223
  return node.first_line != node.last_argument.last_line if node.any_def_type?
224
224
 
225
- !node.single_line?
225
+ node.multiline?
226
226
  end
227
227
 
228
228
  def chained_to_heredoc?(node)
@@ -38,9 +38,9 @@ module RuboCop
38
38
  end
39
39
 
40
40
  def safe_to_split?(node)
41
- node.each_descendant(:if, :case, :kwbegin, :any_def).none? &&
41
+ node.each_descendant(:if, :case, :kwbegin, :any_def, :rescue, :ensure).none? &&
42
42
  node.each_descendant(:dstr, :str).none? { |n| n.heredoc? || n.value.include?("\n") } &&
43
- node.each_descendant(:begin, :sym).none? { |b| !b.single_line? }
43
+ node.each_descendant(:begin, :sym).none?(&:multiline?)
44
44
  end
45
45
  end
46
46
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module HashTransformMethod
6
+ # Internal helper class to hold autocorrect data
7
+ Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
8
+ def self.from_each_with_object(node, match)
9
+ new(match, node, 0, 0)
10
+ end
11
+
12
+ def self.from_hash_brackets_map(node, match)
13
+ new(match, node.children.last, 'Hash['.length, ']'.length)
14
+ end
15
+
16
+ def self.from_map_to_h(node, match)
17
+ if node.parent&.block_type? && node.parent.send_node == node
18
+ strip_trailing_chars = 0
19
+ else
20
+ map_range = node.children.first.source_range
21
+ node_range = node.source_range
22
+ strip_trailing_chars = node_range.end_pos - map_range.end_pos
23
+ end
24
+
25
+ new(match, node.children.first, 0, strip_trailing_chars)
26
+ end
27
+
28
+ def self.from_to_h(node, match)
29
+ new(match, node, 0, 0)
30
+ end
31
+
32
+ def strip_prefix_and_suffix(node, corrector)
33
+ expression = node.source_range
34
+ corrector.remove_leading(expression, leading)
35
+ corrector.remove_trailing(expression, trailing)
36
+ end
37
+
38
+ def set_new_method_name(new_method_name, corrector)
39
+ range = block_node.send_node.loc.selector
40
+ if (send_end = block_node.send_node.loc.end)
41
+ # If there are arguments (only true in the `each_with_object`
42
+ # case)
43
+ range = range.begin.join(send_end)
44
+ end
45
+ corrector.replace(range, new_method_name)
46
+ end
47
+
48
+ def set_new_arg_name(transformed_argname, corrector)
49
+ corrector.replace(block_node.arguments, "|#{transformed_argname}|")
50
+ end
51
+
52
+ def set_new_body_expression(transforming_body_expr, corrector)
53
+ body_source = transforming_body_expr.source
54
+ if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
55
+ body_source = "{ #{body_source} }"
56
+ end
57
+
58
+ corrector.replace(block_node.body, body_source)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -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
@@ -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)
@@ -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,7 +46,7 @@ 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
52
  @registry = {}
@@ -58,6 +58,7 @@ module RuboCop
58
58
 
59
59
  @enabled_cache = {}.compare_by_identity
60
60
  @disabled_cache = {}.compare_by_identity
61
+ @warnings = {}
61
62
  end
62
63
 
63
64
  def enlist(cop)
@@ -132,7 +133,7 @@ module RuboCop
132
133
  # @return [String] Qualified cop name
133
134
  def qualified_cop_name(name, path, warn: true)
134
135
  badge = Badge.parse(name)
135
- print_warning(name, path) if warn && department_missing?(badge, name)
136
+ print_department_missing_warning(name, path) if warn && department_missing?(badge, name)
136
137
  return name if registered?(badge)
137
138
 
138
139
  potential_badges = qualify_badge(badge)
@@ -148,12 +149,12 @@ module RuboCop
148
149
  !badge.qualified? && unqualified_cop_names.include?(name)
149
150
  end
150
151
 
151
- def print_warning(name, path)
152
- message = "#{path}: Warning: no department given for #{name}."
152
+ def print_department_missing_warning(name, path)
153
+ message = "no department given for #{name}."
153
154
  if path.end_with?('.rb')
154
155
  message += ' Run `rubocop -a --only Migration/DepartmentName` to fix.'
155
156
  end
156
- warn message
157
+ emit_warning(path, message)
157
158
  end
158
159
 
159
160
  def unqualified_cop_names
@@ -274,6 +275,10 @@ module RuboCop
274
275
  attr_reader :global
275
276
  end
276
277
 
278
+ def warnings?(path)
279
+ @warnings[path]
280
+ end
281
+
277
282
  private
278
283
 
279
284
  def initialize_copy(reg)
@@ -297,18 +302,20 @@ module RuboCop
297
302
  end
298
303
 
299
304
  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
305
+ if warn && !given_badge.match?(real_badge)
306
+ emit_warning(source_path,
307
+ "#{given_badge} has the wrong namespace - " \
308
+ "replace it with #{given_badge.with_department(real_badge.department)}")
307
309
  end
308
310
 
309
311
  real_badge.to_s
310
312
  end
311
313
 
314
+ def emit_warning(path, message)
315
+ Registry.global.warnings[path] = true
316
+ warn "#{PathUtil.smart_path(path)}: Warning: #{message}"
317
+ end
318
+
312
319
  def registered?(badge)
313
320
  clear_enrollment_queue
314
321
  @registry.key?(badge)
@@ -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