Sha256: 82fa904a080fed59e346a0340474209a7115780c3400f6121b78295731167ea6

Contents?: true

Size: 1.52 KB

Versions: 17

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Prefer `response.parsed_body` to `JSON.parse(response.body)`.
      #
      # @safety
      #   This cop is unsafe because Content-Type may not be `application/json`. For example, the proprietary
      #   Content-Type provided by corporate entities such as `application/vnd.github+json` is not supported at
      #   `response.parsed_body` by default, so you still have to use `JSON.parse(response.body)` there.
      #
      # @example
      #   # bad
      #   JSON.parse(response.body)
      #
      #   # good
      #   response.parsed_body
      class ResponseParsedBody < Base
        extend AutoCorrector
        extend TargetRailsVersion

        MSG = 'Prefer `response.parsed_body` to `JSON.parse(response.body)`.'

        RESTRICT_ON_SEND = %i[parse].freeze

        minimum_target_rails_version 5.0

        # @!method json_parse_response_body?(node)
        def_node_matcher :json_parse_response_body?, <<~PATTERN
          (send
            (const {nil? cbase} :JSON)
            :parse
            (send
              (send nil? :response)
              :body
            )
          )
        PATTERN

        def on_send(node)
          return unless json_parse_response_body?(node)

          add_offense(node) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        def autocorrect(corrector, node)
          corrector.replace(node, 'response.parsed_body')
        end
      end
    end
  end
end

Version data entries

17 entries across 16 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.21.1 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.21.0 lib/rubocop/cop/rails/response_parsed_body.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/response_parsed_body.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/response_parsed_body.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/response_parsed_body.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.20.2 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.20.1 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.20.0 lib/rubocop/cop/rails/response_parsed_body.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.19.1 lib/rubocop/cop/rails/response_parsed_body.rb
rubocop-rails-2.19.0 lib/rubocop/cop/rails/response_parsed_body.rb