Sha256: 50cbc0a55f165ea8d7ca4ae38db0e1fd40d7783aad9f99e22f999beef91d3399

Contents?: true

Size: 1.53 KB

Versions: 14

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Capybara
    # Extracts cop descriptions from YARD docstrings
    class DescriptionExtractor
      def initialize(yardocs)
        @code_objects = yardocs.map(&CodeObject.public_method(:new))
      end

      def to_h
        code_objects
          .select(&:cop?)
          .map(&:configuration)
          .reduce(:merge)
      end

      private

      attr_reader :code_objects

      # Decorator of a YARD code object for working with documented cops
      class CodeObject
        RUBOCOP_COP_CLASS_NAME = 'RuboCop::Cop::Base'

        def initialize(yardoc)
          @yardoc = yardoc
        end

        # Test if the YARD code object documents a concrete cop class
        #
        # @return [Boolean]
        def cop?
          cop_subclass? && !abstract?
        end

        # Configuration for the documented cop that would live in default.yml
        #
        # @return [Hash]
        def configuration
          { cop_name => { 'Description' => description } }
        end

        private

        def cop_name
          Object.const_get(documented_constant).cop_name
        end

        def description
          yardoc.docstring.split("\n\n").first.to_s
        end

        def documented_constant
          yardoc.to_s
        end

        def cop_subclass?
          yardoc.superclass.path == RUBOCOP_COP_CLASS_NAME
        end

        def abstract?
          yardoc.tags.any? { |tag| tag.tag_name.eql?('abstract') }
        end

        attr_reader :yardoc
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 6 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.21.0/lib/rubocop/capybara/description_extractor.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-capybara-2.21.0/lib/rubocop/capybara/description_extractor.rb
rubocop-capybara-2.21.0 lib/rubocop/capybara/description_extractor.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.20.0/lib/rubocop/capybara/description_extractor.rb
rubocop-capybara-2.20.0 lib/rubocop/capybara/description_extractor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-capybara-2.18.0/lib/rubocop/capybara/description_extractor.rb
rubocop-capybara-2.19.0 lib/rubocop/capybara/description_extractor.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.18.0/lib/rubocop/capybara/description_extractor.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.18.0/lib/rubocop/capybara/description_extractor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-capybara-2.18.0/lib/rubocop/capybara/description_extractor.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.18.0/lib/rubocop/capybara/description_extractor.rb
rubocop-capybara-2.18.0 lib/rubocop/capybara/description_extractor.rb
rubocop-capybara-2.17.1 lib/rubocop/capybara/description_extractor.rb
rubocop-capybara-2.17.0 lib/rubocop/capybara/description_extractor.rb