rubocop 1.84.2 → 1.85.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 (116) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +83 -4
  3. data/config/obsoletion.yml +5 -0
  4. data/lib/rubocop/cli/command/mcp.rb +19 -0
  5. data/lib/rubocop/cli.rb +6 -3
  6. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
  7. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  8. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  9. data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
  10. data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
  11. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  12. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  13. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  14. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
  15. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
  16. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
  17. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  18. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  19. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  20. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
  21. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  22. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  23. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  24. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  25. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  26. data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
  27. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  28. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  29. data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
  30. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -1
  31. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +0 -9
  32. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +7 -6
  33. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
  34. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  35. data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
  36. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  37. data/lib/rubocop/cop/lint/void.rb +32 -12
  38. data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
  39. data/lib/rubocop/cop/migration/department_name.rb +12 -1
  40. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  41. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +1 -1
  42. data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
  43. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
  44. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  45. data/lib/rubocop/cop/security/eval.rb +15 -2
  46. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
  47. data/lib/rubocop/cop/style/alias.rb +4 -1
  48. data/lib/rubocop/cop/style/array_join.rb +4 -2
  49. data/lib/rubocop/cop/style/ascii_comments.rb +5 -2
  50. data/lib/rubocop/cop/style/attr.rb +5 -2
  51. data/lib/rubocop/cop/style/bare_percent_literals.rb +3 -1
  52. data/lib/rubocop/cop/style/begin_block.rb +3 -1
  53. data/lib/rubocop/cop/style/block_delimiters.rb +2 -2
  54. data/lib/rubocop/cop/style/case_equality.rb +4 -0
  55. data/lib/rubocop/cop/style/class_and_module_children.rb +10 -2
  56. data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
  57. data/lib/rubocop/cop/style/copyright.rb +1 -1
  58. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  59. data/lib/rubocop/cop/style/each_with_object.rb +2 -0
  60. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  61. data/lib/rubocop/cop/style/empty_class_definition.rb +21 -20
  62. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  63. data/lib/rubocop/cop/style/encoding.rb +7 -1
  64. data/lib/rubocop/cop/style/end_block.rb +3 -1
  65. data/lib/rubocop/cop/style/endless_method.rb +8 -3
  66. data/lib/rubocop/cop/style/file_open.rb +63 -0
  67. data/lib/rubocop/cop/style/for.rb +3 -0
  68. data/lib/rubocop/cop/style/format_string_token.rb +29 -2
  69. data/lib/rubocop/cop/style/global_vars.rb +4 -1
  70. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +21 -5
  71. data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
  72. data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
  73. data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -3
  74. data/lib/rubocop/cop/style/inline_comment.rb +4 -1
  75. data/lib/rubocop/cop/style/map_join.rb +123 -0
  76. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -1
  77. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  78. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  79. data/lib/rubocop/cop/style/not.rb +2 -0
  80. data/lib/rubocop/cop/style/numeric_literals.rb +2 -1
  81. data/lib/rubocop/cop/style/one_class_per_file.rb +95 -0
  82. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  83. data/lib/rubocop/cop/style/parallel_assignment.rb +4 -0
  84. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
  85. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  86. data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
  87. data/lib/rubocop/cop/style/proc.rb +3 -2
  88. data/lib/rubocop/cop/style/reduce_to_hash.rb +169 -0
  89. data/lib/rubocop/cop/style/redundant_begin.rb +3 -3
  90. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  91. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
  92. data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
  93. data/lib/rubocop/cop/style/redundant_parentheses.rb +6 -3
  94. data/lib/rubocop/cop/style/redundant_return.rb +3 -1
  95. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +104 -0
  96. data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
  97. data/lib/rubocop/cop/style/select_by_range.rb +197 -0
  98. data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
  99. data/lib/rubocop/cop/style/semicolon.rb +2 -0
  100. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  101. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
  102. data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
  103. data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
  104. data/lib/rubocop/cop/style/tally_method.rb +181 -0
  105. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  106. data/lib/rubocop/cop/variable_force/branch.rb +2 -2
  107. data/lib/rubocop/directive_comment.rb +2 -1
  108. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  109. data/lib/rubocop/lsp/diagnostic.rb +1 -0
  110. data/lib/rubocop/mcp/server.rb +174 -0
  111. data/lib/rubocop/options.rb +10 -1
  112. data/lib/rubocop/server/cache.rb +5 -7
  113. data/lib/rubocop/target_ruby.rb +18 -12
  114. data/lib/rubocop/version.rb +1 -1
  115. data/lib/rubocop.rb +14 -0
  116. metadata +34 -3
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Identifies places where `max_by { ... }`, `min_by { ... }`, or
7
+ # `minmax_by { ... }` can be replaced by `max`, `min`, or `minmax`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # array.max_by { |x| x }
12
+ # array.min_by { |x| x }
13
+ # array.minmax_by { |x| x }
14
+ #
15
+ # # good
16
+ # array.max
17
+ # array.min
18
+ # array.minmax
19
+ class RedundantMinMaxBy < Base
20
+ include RangeHelp
21
+ extend AutoCorrector
22
+
23
+ MSG_BLOCK = 'Use `%<replacement>s` instead of `%<original>s { |%<var>s| %<var>s }`.'
24
+ MSG_NUMBLOCK = 'Use `%<replacement>s` instead of `%<original>s { _1 }`.'
25
+ MSG_ITBLOCK = 'Use `%<replacement>s` instead of `%<original>s { it }`.'
26
+
27
+ REPLACEMENTS = { max_by: 'max', min_by: 'min', minmax_by: 'minmax' }.freeze
28
+
29
+ def on_block(node)
30
+ redundant_minmax_by_block(node) do |send, var_name|
31
+ register_offense(send, node, message_block(send, var_name))
32
+ end
33
+ end
34
+
35
+ def on_numblock(node)
36
+ redundant_minmax_by_numblock(node) do |send|
37
+ register_offense(send, node, message_numblock(send))
38
+ end
39
+ end
40
+
41
+ def on_itblock(node)
42
+ redundant_minmax_by_itblock(node) do |send|
43
+ register_offense(send, node, message_itblock(send))
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ # @!method redundant_minmax_by_block(node)
50
+ def_node_matcher :redundant_minmax_by_block, <<~PATTERN
51
+ (block $(call _ {:max_by :min_by :minmax_by}) (args (arg $_x)) (lvar _x))
52
+ PATTERN
53
+
54
+ # @!method redundant_minmax_by_numblock(node)
55
+ def_node_matcher :redundant_minmax_by_numblock, <<~PATTERN
56
+ (numblock $(call _ {:max_by :min_by :minmax_by}) 1 (lvar :_1))
57
+ PATTERN
58
+
59
+ # @!method redundant_minmax_by_itblock(node)
60
+ def_node_matcher :redundant_minmax_by_itblock, <<~PATTERN
61
+ (itblock $(call _ {:max_by :min_by :minmax_by}) _ (lvar :it))
62
+ PATTERN
63
+
64
+ def register_offense(send, node, message)
65
+ range = offense_range(send, node)
66
+
67
+ add_offense(range, message: message) do |corrector|
68
+ corrector.replace(range, REPLACEMENTS[send.method_name])
69
+ end
70
+ end
71
+
72
+ def offense_range(send, node)
73
+ range_between(send.loc.selector.begin_pos, node.loc.end.end_pos)
74
+ end
75
+
76
+ def message_block(send, var_name)
77
+ method = send.method_name
78
+ format(MSG_BLOCK, replacement: REPLACEMENTS[method], original: method, var: var_name)
79
+ end
80
+
81
+ def message_numblock(send)
82
+ method = send.method_name
83
+ format(MSG_NUMBLOCK, replacement: REPLACEMENTS[method], original: method)
84
+ end
85
+
86
+ def message_itblock(send)
87
+ method = send.method_name
88
+ format(MSG_ITBLOCK, replacement: REPLACEMENTS[method], original: method)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -155,6 +155,9 @@ module RuboCop
155
155
  return 'a literal' if node.literal? && disallowed_literal?(begin_node, node)
156
156
  return 'a variable' if node.variable?
157
157
  return 'a constant' if node.const_type?
158
+ if begin_node.parent&.any_block_type? && begin_node.parent.body == begin_node
159
+ return 'block body'
160
+ end
158
161
  if node.assignment? && (begin_node.parent.nil? || begin_node.parent.begin_type?)
159
162
  return 'an assignment'
160
163
  end
@@ -308,10 +311,10 @@ module RuboCop
308
311
  end
309
312
 
310
313
  def singular_parenthesized_parent?(begin_node)
311
- return true unless begin_node.parent
312
- return false if begin_node.parent.type?(:splat, :kwsplat)
314
+ return true unless (parent = begin_node.parent)
315
+ return false if parent.type?(:splat, :kwsplat)
313
316
 
314
- parentheses?(begin_node) && begin_node.parent.children.one?
317
+ parent.children.one?
315
318
  end
316
319
 
317
320
  def only_begin_arg?(args)
@@ -3,7 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for redundant `return` expressions.
6
+ # Checks for redundant `return` expressions. Ruby methods
7
+ # implicitly return the value of the last evaluated expression,
8
+ # so an explicit `return` at the end of a method body is unnecessary.
7
9
  #
8
10
  # @example
9
11
  # # These bad cases should be extended to handle methods whose body is
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for redundant `keyword_init` option for `Struct.new`.
7
+ #
8
+ # Since Ruby 3.2, `keyword_init` in `Struct.new` defaults to `nil` behavior.
9
+ # Therefore, this cop detects and autocorrects redundant `keyword_init: nil`
10
+ # and `keyword_init: true` in `Struct.new`.
11
+ #
12
+ # @safety
13
+ # This autocorrect is unsafe because when the value of `keyword_init` changes
14
+ # from `true` to `nil`, the return value of `Struct#keyword_init?` changes.
15
+ #
16
+ # @example
17
+ #
18
+ # # bad
19
+ # Struct.new(:foo, keyword_init: nil)
20
+ # Struct.new(:foo, keyword_init: true)
21
+ #
22
+ # # good
23
+ # Struct.new(:foo)
24
+ #
25
+ class RedundantStructKeywordInit < Base
26
+ extend AutoCorrector
27
+ extend TargetRubyVersion
28
+
29
+ MSG = 'Remove the redundant `keyword_init: %<value>s`.'
30
+ RESTRICT_ON_SEND = %i[new].freeze
31
+
32
+ minimum_target_ruby_version 3.2
33
+
34
+ # @!method struct_new?(node)
35
+ def_node_matcher :struct_new?, <<~PATTERN
36
+ (call (const {nil? cbase} :Struct) :new ...)
37
+ PATTERN
38
+
39
+ # @!method keyword_init?(node)
40
+ def_node_matcher :keyword_init?, <<~PATTERN
41
+ {#redundant_keyword_init? #keyword_init_false?}
42
+ PATTERN
43
+
44
+ # @!method redundant_keyword_init?(node)
45
+ def_node_matcher :redundant_keyword_init?, <<~PATTERN
46
+ (pair (sym :keyword_init) {(true) (nil)})
47
+ PATTERN
48
+
49
+ # @!method keyword_init_false?(node)
50
+ def_node_matcher :keyword_init_false?, <<~PATTERN
51
+ (pair (sym :keyword_init) (false))
52
+ PATTERN
53
+
54
+ def on_send(node)
55
+ return if !struct_new?(node) || node.arguments.none? || !node.last_argument.hash_type?
56
+
57
+ keyword_init_nodes = select_keyword_init_nodes(node)
58
+ return if keyword_init_nodes.any? { |node| keyword_init_false?(node) }
59
+
60
+ redundant_keyword_init_nodes = select_redundant_keyword_init_nodes(keyword_init_nodes)
61
+
62
+ redundant_keyword_init_nodes.each do |redundant_keyword_init|
63
+ register_offense(redundant_keyword_init)
64
+ end
65
+ end
66
+ alias on_csend on_send
67
+
68
+ private
69
+
70
+ def select_keyword_init_nodes(node)
71
+ node.last_argument.pairs.select do |pair|
72
+ keyword_init?(pair)
73
+ end
74
+ end
75
+
76
+ def select_redundant_keyword_init_nodes(keyword_init_nodes)
77
+ keyword_init_nodes.select do |keyword_init_node|
78
+ redundant_keyword_init?(keyword_init_node)
79
+ end
80
+ end
81
+
82
+ def register_offense(keyword_init)
83
+ message = format(MSG, value: keyword_init.value.source)
84
+
85
+ add_offense(keyword_init, message: message) do |corrector|
86
+ range = range(keyword_init)
87
+
88
+ corrector.remove(range)
89
+ end
90
+ end
91
+
92
+ def range(redundant_keyword_init)
93
+ if redundant_keyword_init.parent.left_siblings.last.is_a?(AST::Node)
94
+ beginning_of_range = redundant_keyword_init.parent.left_siblings.last.source_range.end
95
+
96
+ beginning_of_range.join(redundant_keyword_init.source_range.end)
97
+ else
98
+ redundant_keyword_init
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Looks for places where a subset of an Enumerable (array,
7
+ # range, set, etc.; see note below) is calculated based on a class type
8
+ # check, and suggests `grep` or `grep_v` instead.
9
+ #
10
+ # NOTE: Hashes do not behave as you may expect with `grep`, which
11
+ # means that `hash.grep` is not equivalent to `hash.select`. Although
12
+ # RuboCop is limited by static analysis, this cop attempts to avoid
13
+ # registering an offense when the receiver is a hash (hash literal,
14
+ # `Hash.new`, `Hash#[]`, or `to_h`/`to_hash`).
15
+ #
16
+ # @safety
17
+ # Autocorrection is marked as unsafe because the cop cannot guarantee
18
+ # that the receiver is actually an array by static analysis, so the
19
+ # correction may not be actually equivalent.
20
+ #
21
+ # @example
22
+ # # bad (select or find_all)
23
+ # array.select { |x| x.is_a?(Foo) }
24
+ # array.select { |x| x.kind_of?(Foo) }
25
+ #
26
+ # # bad (reject)
27
+ # array.reject { |x| x.is_a?(Foo) }
28
+ #
29
+ # # bad (negative form)
30
+ # array.reject { |x| !x.is_a?(Foo) }
31
+ #
32
+ # # good
33
+ # array.grep(Foo)
34
+ # array.grep_v(Foo)
35
+ class SelectByKind < Base
36
+ extend AutoCorrector
37
+ include RangeHelp
38
+
39
+ MSG = 'Prefer `%<replacement>s` to `%<original_method>s` with a kind check.'
40
+ RESTRICT_ON_SEND = %i[select filter find_all reject].freeze
41
+ SELECT_METHODS = %i[select filter find_all].freeze
42
+ CLASS_CHECK_METHODS = %i[is_a? kind_of?].to_set.freeze
43
+
44
+ # @!method class_check?(node)
45
+ def_node_matcher :class_check?, <<~PATTERN
46
+ {
47
+ (block call (args (arg $_)) ${(send (lvar _) %CLASS_CHECK_METHODS _)})
48
+ (block call (args (arg $_)) ${(send (send (lvar _) %CLASS_CHECK_METHODS _) :!)})
49
+ (numblock call $1 ${(send (lvar _) %CLASS_CHECK_METHODS _)})
50
+ (numblock call $1 ${(send (send (lvar _) %CLASS_CHECK_METHODS _) :!)})
51
+ (itblock call $_ ${(send (lvar _) %CLASS_CHECK_METHODS _)})
52
+ (itblock call $_ ${(send (send (lvar _) %CLASS_CHECK_METHODS _) :!)})
53
+ }
54
+ PATTERN
55
+
56
+ # Returns true if a node appears to return a hash
57
+ # @!method creates_hash?(node)
58
+ def_node_matcher :creates_hash?, <<~PATTERN
59
+ {
60
+ (call (const _ :Hash) {:new :[]} ...)
61
+ (block (call (const _ :Hash) :new ...) ...)
62
+ (call _ { :to_h :to_hash } ...)
63
+ }
64
+ PATTERN
65
+
66
+ # @!method env_const?(node)
67
+ def_node_matcher :env_const?, <<~PATTERN
68
+ (const {nil? cbase} :ENV)
69
+ PATTERN
70
+
71
+ # @!method calls_lvar?(node, name)
72
+ def_node_matcher :calls_lvar?, <<~PATTERN
73
+ (send (lvar %1) %CLASS_CHECK_METHODS _)
74
+ PATTERN
75
+
76
+ # @!method negated_calls_lvar?(node, name)
77
+ def_node_matcher :negated_calls_lvar?, <<~PATTERN
78
+ (send (send (lvar %1) %CLASS_CHECK_METHODS _) :!)
79
+ PATTERN
80
+
81
+ def on_send(node)
82
+ return unless (block_node = node.block_node)
83
+ return if block_node.body&.begin_type?
84
+ return if receiver_allowed?(block_node.receiver)
85
+ return unless (class_check_send_node = extract_send_node(block_node))
86
+
87
+ replacement = replacement(class_check_send_node, node)
88
+ class_constant = find_class_constant(class_check_send_node)
89
+
90
+ register_offense(node, block_node, class_constant, replacement)
91
+ end
92
+ alias on_csend on_send
93
+
94
+ private
95
+
96
+ def receiver_allowed?(node)
97
+ return false unless node
98
+
99
+ node.hash_type? || creates_hash?(node) || env_const?(node)
100
+ end
101
+
102
+ def replacement(class_check_send_node, node)
103
+ negated = negated?(class_check_send_node)
104
+
105
+ method_name = node.method_name
106
+
107
+ if SELECT_METHODS.include?(method_name)
108
+ negated ? 'grep_v' : 'grep'
109
+ else # reject
110
+ negated ? 'grep' : 'grep_v'
111
+ end
112
+ end
113
+
114
+ def register_offense(node, block_node, class_constant, replacement)
115
+ message = format(MSG, replacement: replacement, original_method: node.method_name)
116
+
117
+ add_offense(block_node, message: message) do |corrector|
118
+ if class_constant
119
+ range = range_between(node.loc.selector.begin_pos, block_node.loc.end.end_pos)
120
+ corrector.replace(range, "#{replacement}(#{class_constant.source})")
121
+ end
122
+ end
123
+ end
124
+
125
+ def extract_send_node(block_node)
126
+ return unless (block_arg_name, class_check_send_node = class_check?(block_node))
127
+
128
+ block_arg_name = :"_#{block_arg_name}" if block_node.numblock_type?
129
+ block_arg_name = :it if block_node.type?(:itblock)
130
+
131
+ inner_node = unwrap_negation(class_check_send_node)
132
+
133
+ if calls_lvar?(inner_node, block_arg_name) ||
134
+ negated_calls_lvar?(class_check_send_node, block_arg_name)
135
+ class_check_send_node
136
+ end
137
+ end
138
+
139
+ def negated?(class_check_send_node)
140
+ class_check_send_node.send_type? && class_check_send_node.method?(:!)
141
+ end
142
+
143
+ def unwrap_negation(node)
144
+ if node.send_type? && node.method?(:!)
145
+ node.receiver
146
+ else
147
+ node
148
+ end
149
+ end
150
+
151
+ def find_class_constant(node)
152
+ inner_node = unwrap_negation(node)
153
+ inner_node.first_argument if inner_node.send_type?
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Looks for places where a subset of an Enumerable (array,
7
+ # range, set, etc.; see note below) is calculated based on a range
8
+ # check, and suggests `grep` or `grep_v` instead.
9
+ #
10
+ # NOTE: Hashes do not behave as you may expect with `grep`, which
11
+ # means that `hash.grep` is not equivalent to `hash.select`. Although
12
+ # RuboCop is limited by static analysis, this cop attempts to avoid
13
+ # registering an offense when the receiver is a hash (hash literal,
14
+ # `Hash.new`, `Hash#[]`, or `to_h`/`to_hash`).
15
+ #
16
+ # @safety
17
+ # Autocorrection is marked as unsafe because the cop cannot guarantee
18
+ # that the receiver is actually an array by static analysis, so the
19
+ # correction may not be actually equivalent.
20
+ #
21
+ # @example
22
+ # # bad (select or find_all)
23
+ # array.select { |x| x.between?(1, 10) }
24
+ # array.select { |x| (1..10).cover?(x) }
25
+ # array.select { |x| (1..10).include?(x) }
26
+ #
27
+ # # bad (reject)
28
+ # array.reject { |x| x.between?(1, 10) }
29
+ #
30
+ # # bad (find or detect)
31
+ # array.find { |x| x.between?(1, 10) }
32
+ # array.detect { |x| (1..10).cover?(x) }
33
+ #
34
+ # # bad (negative form)
35
+ # array.reject { |x| !x.between?(1, 10) }
36
+ # array.find { |x| !(1..10).cover?(x) }
37
+ #
38
+ # # good
39
+ # array.grep(1..10)
40
+ # array.grep_v(1..10)
41
+ # array.grep(1..10).first
42
+ # array.grep_v(1..10).first
43
+ class SelectByRange < Base
44
+ extend AutoCorrector
45
+ include RangeHelp
46
+
47
+ MSG = 'Prefer `%<replacement>s` to `%<original_method>s` with a range check.'
48
+ RESTRICT_ON_SEND = %i[select filter find_all reject find detect].freeze
49
+ SELECT_METHODS = %i[select filter find_all].freeze
50
+ FIND_METHODS = %i[find detect].freeze
51
+
52
+ # @!method range_check?(node)
53
+ # Matches: x.between?(min, max) or (min..max).cover?(x) or (min..max).include?(x)
54
+ def_node_matcher :range_check?, <<~PATTERN
55
+ {
56
+ (block call (args (arg $_)) ${(send (lvar _) :between? _ _)})
57
+ (block call (args (arg $_)) ${(send {range (begin range)} {:cover? :include?} (lvar _))})
58
+ (block call (args (arg $_)) ${(send (send (lvar _) :between? _ _) :!)})
59
+ (block call (args (arg $_)) ${(send (send {range (begin range)} {:cover? :include?} (lvar _)) :!)})
60
+ (block call (args (arg $_)) ${(send (begin (send (lvar _) :between? _ _)) :!)})
61
+ (block call (args (arg $_)) ${(send (begin (send {range (begin range)} {:cover? :include?} (lvar _))) :!)})
62
+ (numblock call $1 ${(send (lvar _) :between? _ _)})
63
+ (numblock call $1 ${(send {range (begin range)} {:cover? :include?} (lvar _))})
64
+ (numblock call $1 ${(send (send (lvar _) :between? _ _) :!)})
65
+ (numblock call $1 ${(send (send {range (begin range)} {:cover? :include?} (lvar _)) :!)})
66
+ (numblock call $1 ${(send (begin (send (lvar _) :between? _ _)) :!)})
67
+ (numblock call $1 ${(send (begin (send {range (begin range)} {:cover? :include?} (lvar _))) :!)})
68
+ (itblock call $_ ${(send (lvar _) :between? _ _)})
69
+ (itblock call $_ ${(send {range (begin range)} {:cover? :include?} (lvar _))})
70
+ (itblock call $_ ${(send (send (lvar _) :between? _ _) :!)})
71
+ (itblock call $_ ${(send (send {range (begin range)} {:cover? :include?} (lvar _)) :!)})
72
+ (itblock call $_ ${(send (begin (send (lvar _) :between? _ _)) :!)})
73
+ (itblock call $_ ${(send (begin (send {range (begin range)} {:cover? :include?} (lvar _))) :!)})
74
+ }
75
+ PATTERN
76
+
77
+ # Returns true if a node appears to return a hash
78
+ # @!method creates_hash?(node)
79
+ def_node_matcher :creates_hash?, <<~PATTERN
80
+ {
81
+ (call (const _ :Hash) {:new :[]} ...)
82
+ (block (call (const _ :Hash) :new ...) ...)
83
+ (call _ { :to_h :to_hash } ...)
84
+ }
85
+ PATTERN
86
+
87
+ # @!method env_const?(node)
88
+ def_node_matcher :env_const?, <<~PATTERN
89
+ (const {nil? cbase} :ENV)
90
+ PATTERN
91
+
92
+ # @!method between_call?(node, name)
93
+ def_node_matcher :between_call?, <<~PATTERN
94
+ (send (lvar %1) :between? _ _)
95
+ PATTERN
96
+
97
+ # @!method range_cover_call?(node, name)
98
+ def_node_matcher :range_cover_call?, <<~PATTERN
99
+ (send {range (begin range)} {:cover? :include?} (lvar %1))
100
+ PATTERN
101
+
102
+ def on_send(node)
103
+ return unless (block_node = node.block_node)
104
+ return if block_node.body&.begin_type?
105
+ return if receiver_allowed?(block_node.receiver)
106
+ return unless (range_check_send_node = extract_send_node(block_node))
107
+
108
+ replacement = replacement(range_check_send_node, node)
109
+ range_literal = find_range(range_check_send_node)
110
+
111
+ register_offense(node, block_node, range_literal, replacement)
112
+ end
113
+ alias on_csend on_send
114
+
115
+ private
116
+
117
+ def receiver_allowed?(node)
118
+ return false unless node
119
+
120
+ node.hash_type? || creates_hash?(node) || env_const?(node)
121
+ end
122
+
123
+ def replacement(range_check_send_node, node)
124
+ negated = negated?(range_check_send_node)
125
+ method_name = node.method_name
126
+
127
+ if SELECT_METHODS.include?(method_name)
128
+ negated ? 'grep_v' : 'grep'
129
+ elsif FIND_METHODS.include?(method_name)
130
+ negated ? 'grep_v(...).first' : 'grep(...).first'
131
+ else # reject
132
+ negated ? 'grep' : 'grep_v'
133
+ end
134
+ end
135
+
136
+ def register_offense(node, block_node, range_literal, replacement)
137
+ message = format(MSG, replacement: replacement, original_method: node.method_name)
138
+
139
+ add_offense(block_node, message: message) do |corrector|
140
+ if range_literal
141
+ range = range_between(node.loc.selector.begin_pos, block_node.loc.end.end_pos)
142
+ grep_method = replacement.include?('grep_v') ? 'grep_v' : 'grep'
143
+ suffix = replacement.include?('.first') ? '.first' : ''
144
+ corrector.replace(range, "#{grep_method}(#{range_literal})#{suffix}")
145
+ end
146
+ end
147
+ end
148
+
149
+ def extract_send_node(block_node)
150
+ return unless (block_arg_name, range_check_send_node = range_check?(block_node))
151
+
152
+ block_arg_name = :"_#{block_arg_name}" if block_node.numblock_type?
153
+ block_arg_name = :it if block_node.type?(:itblock)
154
+
155
+ inner_node = unwrap_negation(range_check_send_node)
156
+
157
+ range_check_send_node if calls_lvar_in_range_check?(inner_node, block_arg_name)
158
+ end
159
+
160
+ def calls_lvar_in_range_check?(node, block_arg_name)
161
+ between_call?(node, block_arg_name) || range_cover_call?(node, block_arg_name)
162
+ end
163
+
164
+ def negated?(range_check_send_node)
165
+ range_check_send_node.send_type? && range_check_send_node.method?(:!)
166
+ end
167
+
168
+ def unwrap_negation(node)
169
+ if node.send_type? && node.method?(:!)
170
+ receiver = node.receiver
171
+ receiver = receiver.children.first if receiver.begin_type?
172
+ receiver
173
+ else
174
+ node
175
+ end
176
+ end
177
+
178
+ def find_range(node)
179
+ inner = unwrap_negation(node)
180
+
181
+ if inner.method?(:between?)
182
+ # x.between?(min, max) -> min..max
183
+ min = inner.first_argument.source
184
+ max = inner.arguments[1].source
185
+ "#{min}..#{max}"
186
+ else
187
+ # (min..max).cover?(x) or (min..max).include?(x)
188
+ receiver = inner.receiver
189
+ # Unwrap begin node from parentheses
190
+ receiver = receiver.children.first if receiver.begin_type?
191
+ receiver.source
192
+ end
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end