Sha256: 2de652c84f704669ad80dcd66ae334222b372979b8e9247c6f12fa14bd9ad4d6

Contents?: true

Size: 1.37 KB

Versions: 20

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Gitlab
  module Styles
    module Rubocop
      module Cop
        # This cop prevents usage of 'redirect_to' in actions 'destroy' without specifying 'status'.
        # See https://gitlab.com/gitlab-org/gitlab-ce/issues/31840
        class RedirectWithStatus < RuboCop::Cop::Cop
          MSG = 'Do not use "redirect_to" without "status" in "destroy" action'

          def on_def(node)
            return unless in_controller?(node)
            return unless destroy?(node) || destroy_all?(node)

            node.each_descendant(:send) do |def_node|
              next unless redirect_to?(def_node)

              methods = []

              def_node.children.last.each_node(:pair) do |pair|
                methods << pair.children.first.children.first
              end

              add_offense(def_node, location: :selector) unless methods.include?(:status)
            end
          end

          private

          def in_controller?(node)
            node.location.expression.source_buffer.name.end_with?('_controller.rb')
          end

          def destroy?(node)
            node.children.first == :destroy
          end

          def destroy_all?(node)
            node.children.first == :destroy_all
          end

          def redirect_to?(node)
            node.children[1] == :redirect_to
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
gitlab-styles-7.1.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-7.0.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.6.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.5.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.4.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.3.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.2.1 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.2.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.1.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-6.0.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-5.4.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-5.3.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-5.2.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-5.1.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-5.0.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-4.3.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-4.2.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-4.1.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-4.0.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
gitlab-styles-3.4.0 lib/gitlab/styles/rubocop/cop/redirect_with_status.rb