Class: RuboCop::Cop::Heatwave::RedundantRedirectSeeOther

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/heatwave/redundant_redirect_see_other.rb

Overview

Flags status: :see_other (or status: 303) on redirect_to and
redirect_back_or_to calls. ApplicationController#redirect_to
auto-promotes the status for any POST/PUT/PATCH/DELETE redirect, so
the explicit option is redundant noise.

If you legitimately need a 303 on a non-mutating verb (e.g. forcing
a GET-after-GET to land via See Other) silence with # rubocop:disable
and add a comment explaining why.

Examples:

# bad
redirect_to thing_path(@thing), status: :see_other
redirect_back_or_to root_path, status: :see_other, alert: 'No'

# good
redirect_to thing_path(@thing)
redirect_back_or_to root_path, alert: 'No'

Constant Summary collapse

MSG =
'`status: :see_other` is redundant — ApplicationController#redirect_to ' \
'auto-applies it for mutating verbs.'
REDIRECT_METHODS =
%i[
  redirect_to
  redirect_back
  redirect_back_or_to
  redirect_to_return_path_or_default
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/heatwave/redundant_redirect_see_other.rb', line 46

def on_send(node)
  return unless redirect_call(node)

  hash_arg = node.arguments.last
  return unless hash_arg.is_a?(RuboCop::AST::HashNode)

  offending = hash_arg.pairs.find { |pair| see_other_pair(pair) }
  return unless offending

  add_offense(offending) do |corrector|
    remove_pair(corrector, hash_arg, offending)
  end
end

#redirect_call(node) ⇒ Object



37
38
39
# File 'lib/rubocop/cop/heatwave/redundant_redirect_see_other.rb', line 37

def_node_matcher :redirect_call, <<~PATTERN
  (send nil? {:redirect_to :redirect_back :redirect_back_or_to :redirect_to_return_path_or_default} ...)
PATTERN

#see_other_pair(node) ⇒ Object



42
43
44
# File 'lib/rubocop/cop/heatwave/redundant_redirect_see_other.rb', line 42

def_node_matcher :see_other_pair, <<~PATTERN
  (pair (sym :status) {(sym :see_other) (int 303)})
PATTERN