Sha256: 3f26c096027a35e8021e4f0f048b090a6d42f0381513822af37321f1ed1442f6

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Rubocop
  module Cop
    # Prevents usage of 'redirect_to' in actions 'destroy' and 'destroy_all'
    # without specifying 'status'.
    #
    # @example
    #   # bad
    #
    #   def destroy
    #     redirect_to root_path
    #   end
    #
    #   def destroy_all
    #     redirect_to root_path, alert: 'Oh no!'
    #   end
    #
    #   # good
    #
    #   def destroy
    #     redirect_to root_path, status: 302
    #   end
    #
    #   def destroy_all
    #     redirect_to root_path, alert: 'Oh no!', status: 302
    #   end
    #
    #   def show
    #     redirect_to root_path
    #   end
    #
    # See https://gitlab.com/gitlab-org/gitlab-ce/issues/31840
    class RedirectWithStatus < RuboCop::Cop::Base
      MSG = 'Do not use "redirect_to" without "status" in "%<name>s" action.'

      RESTRICT_ON_SEND = %i[redirect_to].freeze

      ACTIONS = %i[destroy destroy_all].to_set.freeze

      # @!method redirect_to_with_status?(node)
      def_node_matcher :redirect_to_with_status?, <<~PATTERN
        (send nil? :redirect_to ...
          (hash <(pair (sym :status) _) ...>)
        )
      PATTERN

      def on_send(node)
        return if redirect_to_with_status?(node)

        node.each_ancestor(:def) do |def_node|
          next unless ACTIONS.include?(def_node.method_name)

          message = format(MSG, name: def_node.method_name)
          add_offense(node.loc.selector, message: message)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gitlab-styles-13.0.1 lib/rubocop/cop/redirect_with_status.rb
gitlab-styles-13.0.0 lib/rubocop/cop/redirect_with_status.rb
gitlab-styles-11.0.0 lib/rubocop/cop/redirect_with_status.rb
gitlab-styles-10.1.0 lib/rubocop/cop/redirect_with_status.rb
gitlab-styles-10.0.0 lib/rubocop/cop/redirect_with_status.rb