Sha256: 6b32003f789bb6d1a6f5fd30c02ede49afc8d07f90a3bdc911fc5bf2ebd027c1

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'yaml'

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

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

      def dump
        YAML.dump(unified_config)
          .gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1")
          .gsub(/^(\s+)- /, '\1  - ')
          .gsub('"~"', '~')
        # .gsub(*AMENDMENTS, "\n\\0")
      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

6 entries across 6 versions & 4 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.21.0/lib/rubocop/capybara/config_formatter.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-capybara-2.21.0/lib/rubocop/capybara/config_formatter.rb
rubocop-capybara-2.21.0 lib/rubocop/capybara/config_formatter.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.20.0/lib/rubocop/capybara/config_formatter.rb
rubocop-capybara-2.20.0 lib/rubocop/capybara/config_formatter.rb
rubocop-capybara-2.19.0 lib/rubocop/capybara/config_formatter.rb