Sha256: 7d95dfc09ed7fe732a4ce9697bcc91b2016a27b95986438990e61a48cd41c898

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require "unparser"

module RuboCop
  module Cop
    module Lint
      # @example
      #   # bad
      #   vcr: { record_mode: :once, tag: :foo }
      #   vcr: { record_mode: :none }
      #
      #   # good
      #   vcr: { tag: :foo }
      #   vcr: true
      class NoVCRRecording < RuboCop::Cop::Base
        extend AutoCorrector

        MSG = "Do not set :record option in VCR"

        def_node_search :is_only_setting_record_option?, <<~PATTERN
          (pair
            (sym :vcr)
            (hash
              (pair
                (sym :record)
                ...
              )
            )
          )
        PATTERN
        def_node_search :is_setting_record_option?, <<~PATTERN
          (pair
            (sym :vcr)
            (hash
              <
                (pair
                  (sym :record)
                  ...
                )
                ...
              >
            )
          )
        PATTERN

        def on_pair(node)
          return unless is_only_setting_record_option?(node) || is_setting_record_option?(node)

          add_offense(node) do |corrector|
            if is_only_setting_record_option?(node)
              corrector.replace(node, "vcr: true")
            elsif is_setting_record_option?(node)
              corrector.replace(node, remove_record_option(node))
            end
          end
        end

        private

        def remove_record_option(top_pair_node)
          extend AST::Sexp

          _vcr_key, hash = top_pair_node.children
          other_options = hash.children.reject do |pair|
            key, _value = pair.children
            key == s(:sym, :record)
          end

          Unparser.unparse(s(:pair, s(:sym, :vcr), s(:hash, *other_options)))
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rf-stylez-1.2.5 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.2.4 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.2.3 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.2.1 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.1.2 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.1.1 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.1.0 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.0.3 lib/rubocop/cop/lint/no_vcr_recording.rb
rf-stylez-1.0.2 lib/rubocop/cop/lint/no_vcr_recording.rb