module RuboCop::Cop::IndexMethod
Common functionality for Rails/IndexBy and Rails/IndexWith
Constants
- Autocorrection
Internal helper class to hold autocorrect data
- Captures
Internal helper class to hold match data
- RESTRICT_ON_SEND
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 9 def on_block(node) on_bad_each_with_object(node) do |*match| handle_possible_offense(node, match, 'each_with_object') end return if target_ruby_version < 2.6 on_bad_to_h(node) do |*match| handle_possible_offense(node, match, 'to_h { ... }') end end
on_csend(node)
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 31 def on_csend(node) on_bad_map_to_h(node) do |*match| handle_possible_offense(node, match, 'map { ... }.to_h') end end
on_send(node)
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 21 def on_send(node) on_bad_map_to_h(node) do |*match| handle_possible_offense(node, match, 'map { ... }.to_h') end on_bad_hash_brackets_map(node) do |*match| handle_possible_offense(node, match, 'Hash[map { ... }]') end end
Private Instance Methods
execute_correction(corrector, node, correction)
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 95 def execute_correction(corrector, node, correction) correction.strip_prefix_and_suffix(node, corrector) correction.set_new_method_name(new_method_name, corrector) captures = extract_captures(correction.match) correction.set_new_arg_name(captures.transformed_argname, corrector) correction.set_new_body_expression( captures.transforming_body_expr, corrector ) end
extract_captures(match)
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 72 def extract_captures(match) argname, body_expr = *match Captures.new(argname, body_expr) end
handle_possible_offense(node, match, match_desc)
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 59 def handle_possible_offense(node, match, match_desc) captures = extract_captures(match) return if captures.noop_transformation? add_offense( node, message: "Prefer `#{new_method_name}` over `#{match_desc}`." ) do |corrector| correction = prepare_correction(node) execute_correction(corrector, node, correction) end end
new_method_name()
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 77 def new_method_name raise NotImplementedError end
on_bad_each_with_object(_node)
click to toggle source
@abstract Implemented with `def_node_matcher`
# File lib/rubocop/cop/mixin/index_method.rb, line 40 def on_bad_each_with_object(_node) raise NotImplementedError end
on_bad_hash_brackets_map(_node)
click to toggle source
@abstract Implemented with `def_node_matcher`
# File lib/rubocop/cop/mixin/index_method.rb, line 55 def on_bad_hash_brackets_map(_node) raise NotImplementedError end
on_bad_map_to_h(_node)
click to toggle source
@abstract Implemented with `def_node_matcher`
# File lib/rubocop/cop/mixin/index_method.rb, line 50 def on_bad_map_to_h(_node) raise NotImplementedError end
on_bad_to_h(_node)
click to toggle source
@abstract Implemented with `def_node_matcher`
# File lib/rubocop/cop/mixin/index_method.rb, line 45 def on_bad_to_h(_node) raise NotImplementedError end
prepare_correction(node)
click to toggle source
# File lib/rubocop/cop/mixin/index_method.rb, line 81 def prepare_correction(node) if (match = on_bad_each_with_object(node)) Autocorrection.from_each_with_object(node, match) elsif (match = on_bad_to_h(node)) Autocorrection.from_to_h(node, match) elsif (match = on_bad_map_to_h(node)) Autocorrection.from_map_to_h(node, match) elsif (match = on_bad_hash_brackets_map(node)) Autocorrection.from_hash_brackets_map(node, match) else raise 'unreachable' end end