Sha256: 2ff3eb97fbdb134de9ec82a790b3748556a8396bc9f7011738a94f9bebcb6474

Contents?: true

Size: 958 Bytes

Versions: 5

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Discourse
      # Use `response.parsed_body` instead of `JSON.parse(response.body)` in specs.
      #
      # @example
      #   # bad
      #   expect(::JSON.parse(response.body)).to eq({})
      #
      #   # good
      #   expect(response.parsed_body).to eq({})
      class NoJsonParseResponse < Base
        MSG = "Use `response.parsed_body` instead of `JSON.parse(response.body)` in specs."

        def_node_matcher :json_parse_body?, <<-MATCHER
          (send
            (const {cbase nil?} :JSON) :parse
            (send
              (send nil? :response) :body))
        MATCHER

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

          add_offense(node, message: MSG)
        end

        def autocorrect(node)
          lambda { |corrector| corrector.replace(node.loc.expression, "response.parsed_body") }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-discourse-3.8.6 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.8.5 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.8.4 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.8.3 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.8.2 lib/rubocop/cop/discourse/no_json_parse_response.rb