Sha256: d067cb4e514d6b99dd1817f70a58edce1ff5397bba2c8295d863fa089a73a1eb

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'yaml'

module RuboCop
  module RSpec
    # Builds a YAML config file from two config hashes
    class ConfigFormatter
      EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze
      SUBDEPARTMENTS = %(RSpec/Capybara RSpec/FactoryBot RSpec/Rails)
      AMENDMENTS = %(Metrics/BlockLength)
      COP_DOC_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/'

      def initialize(config, descriptions)
        @config       = config
        @descriptions = descriptions
      end

      def dump
        YAML.dump(unified_config)
          .gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1")
          .gsub(*AMENDMENTS, "\n\\0")
          .gsub(/^(\s+)- /, '\1  - ')
      end

      private

      def unified_config
        cops.each_with_object(config.dup) do |cop, unified|
          next if SUBDEPARTMENTS.include?(cop)
          next if AMENDMENTS.include?(cop)

          unified[cop].merge!(descriptions.fetch(cop))
          unified[cop]['Reference'] = COP_DOC_BASE_URL + cop.sub('RSpec/', '')
        end
      end

      def cops
        (descriptions.keys | config.keys).grep(EXTENSION_ROOT_DEPARTMENT)
      end

      attr_reader :config, :descriptions
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/rspec/config_formatter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.12.1 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.12.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.11.1 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.11.0 lib/rubocop/rspec/config_formatter.rb