|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + # Common functionality for Rails/IndexBy and Rails/IndexWith |
| 6 | + module IndexMethod # rubocop:disable Metrics/ModuleLength |
| 7 | + def on_block(node) |
| 8 | + on_bad_each_with_object(node) do |*match| |
| 9 | + handle_possible_offense(node, match, 'each_with_object') |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + def on_send(node) |
| 14 | + on_bad_map_to_h(node) do |*match| |
| 15 | + handle_possible_offense(node, match, 'map { ... }.to_h') |
| 16 | + end |
| 17 | + |
| 18 | + on_bad_hash_brackets_map(node) do |*match| |
| 19 | + handle_possible_offense(node, match, 'Hash[map { ... }]') |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + def on_csend(node) |
| 24 | + on_bad_map_to_h(node) do |*match| |
| 25 | + handle_possible_offense(node, match, 'map { ... }.to_h') |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def autocorrect(node) |
| 30 | + lambda do |corrector| |
| 31 | + correction = prepare_correction(node) |
| 32 | + execute_correction(corrector, node, correction) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + private |
| 37 | + |
| 38 | + # @abstract Implemented with `def_node_matcher` |
| 39 | + def on_bad_each_with_object(_node) |
| 40 | + raise NotImplementedError |
| 41 | + end |
| 42 | + |
| 43 | + # @abstract Implemented with `def_node_matcher` |
| 44 | + def on_bad_map_to_h(_node) |
| 45 | + raise NotImplementedError |
| 46 | + end |
| 47 | + |
| 48 | + # @abstract Implemented with `def_node_matcher` |
| 49 | + def on_bad_hash_brackets_map(_node) |
| 50 | + raise NotImplementedError |
| 51 | + end |
| 52 | + |
| 53 | + def handle_possible_offense(node, match, match_desc) |
| 54 | + captures = extract_captures(match) |
| 55 | + |
| 56 | + return if captures.noop_transformation? |
| 57 | + |
| 58 | + add_offense( |
| 59 | + node, |
| 60 | + message: "Prefer `#{new_method_name}` over `#{match_desc}`." |
| 61 | + ) |
| 62 | + end |
| 63 | + |
| 64 | + def extract_captures(match) |
| 65 | + argname, body_expr = *match |
| 66 | + Captures.new(argname, body_expr) |
| 67 | + end |
| 68 | + |
| 69 | + def new_method_name |
| 70 | + raise NotImplementedError |
| 71 | + end |
| 72 | + |
| 73 | + def prepare_correction(node) |
| 74 | + if (match = on_bad_each_with_object(node)) |
| 75 | + Autocorrection.from_each_with_object(node, match) |
| 76 | + elsif (match = on_bad_map_to_h(node)) |
| 77 | + Autocorrection.from_map_to_h(node, match) |
| 78 | + elsif (match = on_bad_hash_brackets_map(node)) |
| 79 | + Autocorrection.from_hash_brackets_map(node, match) |
| 80 | + else |
| 81 | + raise 'unreachable' |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def execute_correction(corrector, node, correction) |
| 86 | + correction.strip_prefix_and_suffix(node, corrector) |
| 87 | + correction.set_new_method_name(new_method_name, corrector) |
| 88 | + |
| 89 | + captures = extract_captures(correction.match) |
| 90 | + correction.set_new_arg_name(captures.transformed_argname, corrector) |
| 91 | + correction.set_new_body_expression( |
| 92 | + captures.transforming_body_expr, |
| 93 | + corrector |
| 94 | + ) |
| 95 | + end |
| 96 | + |
| 97 | + # Internal helper class to hold match data |
| 98 | + Captures = Struct.new( |
| 99 | + :transformed_argname, |
| 100 | + :transforming_body_expr |
| 101 | + ) do |
| 102 | + def noop_transformation? |
| 103 | + transforming_body_expr.lvar_type? && |
| 104 | + transforming_body_expr.children == [transformed_argname] |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + # Internal helper class to hold autocorrect data |
| 109 | + Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do # rubocop:disable Metrics/BlockLength |
| 110 | + def self.from_each_with_object(node, match) |
| 111 | + new(match, node, 0, 0) |
| 112 | + end |
| 113 | + |
| 114 | + def self.from_map_to_h(node, match) |
| 115 | + strip_trailing_chars = node.parent&.block_type? ? 0 : '.to_h'.length |
| 116 | + new(match, node.children.first, 0, strip_trailing_chars) |
| 117 | + end |
| 118 | + |
| 119 | + def self.from_hash_brackets_map(node, match) |
| 120 | + new(match, node.children.last, 'Hash['.length, ']'.length) |
| 121 | + end |
| 122 | + |
| 123 | + def strip_prefix_and_suffix(node, corrector) |
| 124 | + expression = node.loc.expression |
| 125 | + corrector.remove_leading(expression, leading) |
| 126 | + corrector.remove_trailing(expression, trailing) |
| 127 | + end |
| 128 | + |
| 129 | + def set_new_method_name(new_method_name, corrector) |
| 130 | + range = block_node.send_node.loc.selector |
| 131 | + if (send_end = block_node.send_node.loc.end) |
| 132 | + # If there are arguments (only true in the `each_with_object` case) |
| 133 | + range = range.begin.join(send_end) |
| 134 | + end |
| 135 | + corrector.replace(range, new_method_name) |
| 136 | + end |
| 137 | + |
| 138 | + def set_new_arg_name(transformed_argname, corrector) |
| 139 | + corrector.replace( |
| 140 | + block_node.arguments.loc.expression, |
| 141 | + "|#{transformed_argname}|" |
| 142 | + ) |
| 143 | + end |
| 144 | + |
| 145 | + def set_new_body_expression(transforming_body_expr, corrector) |
| 146 | + corrector.replace( |
| 147 | + block_node.body.loc.expression, |
| 148 | + transforming_body_expr.loc.expression.source |
| 149 | + ) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | +end |
0 commit comments