|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module Rails |
| 6 | + # Checks for uses of `redirect_back(fallback_location: ...)` and |
| 7 | + # suggests using `redirect_back_or_to(...)` instead. |
| 8 | + # |
| 9 | + # `redirect_back(fallback_location: ...)` was soft deprecated in Rails 7.0 and |
| 10 | + # `redirect_back_or_to` was introduced as a replacement. |
| 11 | + # |
| 12 | + # @example |
| 13 | + # # bad |
| 14 | + # redirect_back(fallback_location: root_path) |
| 15 | + # |
| 16 | + # # good |
| 17 | + # redirect_back_or_to(root_path) |
| 18 | + # |
| 19 | + # # bad |
| 20 | + # redirect_back(fallback_location: root_path, allow_other_host: false) |
| 21 | + # |
| 22 | + # # good |
| 23 | + # redirect_back_or_to(root_path, allow_other_host: false) |
| 24 | + # |
| 25 | + class RedirectBackOrTo < Base |
| 26 | + extend AutoCorrector |
| 27 | + extend TargetRailsVersion |
| 28 | + |
| 29 | + minimum_target_rails_version 7.0 |
| 30 | + |
| 31 | + MSG = 'Use `redirect_back_or_to` instead of `redirect_back` with `:fallback_location` keyword argument.' |
| 32 | + RESTRICT_ON_SEND = %i[redirect_back].freeze |
| 33 | + |
| 34 | + def_node_matcher :redirect_back_with_fallback_location, <<~PATTERN |
| 35 | + (send nil? :redirect_back |
| 36 | + (hash <$(pair (sym :fallback_location) $_) ...>) |
| 37 | + ) |
| 38 | + PATTERN |
| 39 | + |
| 40 | + def on_send(node) |
| 41 | + redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value| |
| 42 | + add_offense(node.loc.selector) do |corrector| |
| 43 | + correct_redirect_back(corrector, node, fallback_pair, fallback_value) |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + private |
| 49 | + |
| 50 | + def correct_redirect_back(corrector, node, fallback_pair, fallback_value) |
| 51 | + corrector.replace(node.loc.selector, 'redirect_back_or_to') |
| 52 | + |
| 53 | + hash_arg = node.first_argument |
| 54 | + |
| 55 | + if hash_arg.pairs.one? |
| 56 | + corrector.replace(hash_arg, fallback_value.source) |
| 57 | + else |
| 58 | + remove_fallback_location_pair(corrector, hash_arg, fallback_pair) |
| 59 | + first_pair = hash_arg.pairs.find { |pair| pair != fallback_pair } |
| 60 | + corrector.insert_before(first_pair, "#{fallback_value.source}, ") |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def remove_fallback_location_pair(corrector, hash_node, fallback_pair) |
| 65 | + pairs = hash_node.pairs |
| 66 | + index = pairs.index(fallback_pair) |
| 67 | + |
| 68 | + if pairs.one? |
| 69 | + corrector.remove(fallback_pair) |
| 70 | + elsif index.zero? |
| 71 | + remove_first_pair(corrector, fallback_pair, pairs[1]) |
| 72 | + else |
| 73 | + remove_non_first_pair(corrector, fallback_pair, pairs[index - 1]) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def remove_first_pair(corrector, fallback_pair, next_pair) |
| 78 | + range = fallback_pair.source_range.join(next_pair.source_range.begin) |
| 79 | + corrector.remove(range) |
| 80 | + end |
| 81 | + |
| 82 | + def remove_non_first_pair(corrector, fallback_pair, prev_pair) |
| 83 | + range = prev_pair.source_range.end.join(fallback_pair.source_range.end) |
| 84 | + corrector.remove(range) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments