Sha256: 168c02bf8ff76d331a90c734a51b4bfd003c4686448f21bd7be5cd8b23be2171

Contents?: true

Size: 1.38 KB

Versions: 13

Compression:

Stored size: 1.38 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 = [].freeze
      AMENDMENTS = %(Metrics/BlockLength)
      COP_DOC_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/'

      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  - ')
          .gsub('"~"', '~')
      end

      private

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

          replace_nil(unified[cop])
          unified[cop].merge!(descriptions.fetch(cop))
          unified[cop]['Reference'] = reference(cop)
        end
      end

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

      def replace_nil(config)
        config.each do |key, value|
          config[key] = '~' if value.nil?
        end
      end

      def reference(cop)
        COP_DOC_BASE_URL + cop
      end

      attr_reader :config, :descriptions
    end
  end
end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.3.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.2.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.1.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.0.5 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.0.4 lib/rubocop/rspec/config_formatter.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.0.3 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.0.2 lib/rubocop/rspec/config_formatter.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.0.1 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.0.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-3.0.0.pre lib/rubocop/rspec/config_formatter.rb