Sha256: 9780ef5147ee72f8ad33e2f2eada782c9e16b653e33f0ff51158809859cb7587

Contents?: true

Size: 982 Bytes

Versions: 16

Compression:

Stored size: 982 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 < Cop
        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 do |corrector|
            corrector.replace(node.loc.expression, "response.parsed_body")
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rubocop-discourse-3.1.0 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.0.3 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.0.2 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.0.1 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-3.0 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.6.0 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.5.0 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.4.2 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.4.1 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.4.0 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.3.2 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.3.1 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.3.0 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.2.0 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.1.2 lib/rubocop/cop/discourse/no_json_parse_response.rb
rubocop-discourse-2.1.1 lib/rubocop/cop/discourse/no_json_parse_response.rb