Sha256: 7bbd471113d0316acdbcf45cf2f60a08e9be20fed6ba96827e349cf8aaa2035b

Contents?: true

Size: 1.88 KB

Versions: 22

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # If you try to render content along with a non-content status code (100-199, 204, 205, or 304),
      # it will be dropped from the response.
      #
      # This cop checks for uses of `render` which specify both body content and a non-content status.
      #
      # @example
      #   # bad
      #   render 'foo', status: :continue
      #   render status: 100, plain: 'Ruby!'
      #
      #   # good
      #   head :continue
      #   head 100
      class UnusedRenderContent < Base
        include RangeHelp

        MSG = 'Do not specify body content for a response with a non-content status code'
        RESTRICT_ON_SEND = %i[render].freeze
        NON_CONTENT_STATUS_CODES = Set[*100..199, 204, 205, 304] & ::Rack::Utils::SYMBOL_TO_STATUS_CODE.values
        NON_CONTENT_STATUSES = Set[
          *::Rack::Utils::SYMBOL_TO_STATUS_CODE.invert.fetch_values(*NON_CONTENT_STATUS_CODES)
        ]
        BODY_OPTIONS = Set[
          :action,
          :body,
          :content_type,
          :file,
          :html,
          :inline,
          :json,
          :js,
          :layout,
          :plain,
          :raw,
          :template,
          :text,
          :xml
        ]

        def_node_matcher :non_content_status?, <<~PATTERN
          (pair
            (sym :status)
            {(sym NON_CONTENT_STATUSES) (int NON_CONTENT_STATUS_CODES)}
          )
        PATTERN

        def_node_matcher :unused_render_content?, <<~PATTERN
          (send nil? :render {
            (hash <#non_content_status? $(pair (sym BODY_OPTIONS) _) ...>) |
            $({str sym} _) (hash <#non_content_status? ...>)
          })
        PATTERN

        def on_send(node)
          unused_render_content?(node) do |unused_content_node|
            add_offense(unused_content_node)
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 5 rubygems

Version Path
rubocop-rails-2.30.1 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.30.0 lib/rubocop/cop/rails/unused_render_content.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-rails-2.29.1/lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.29.1 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.29.0 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/unused_render_content.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/unused_render_content.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/unused_render_content.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/unused_render_content.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/unused_render_content.rb