Sha256: f0b7cc9a44960309367fca497c74e3122efa5a0aabce3b35ce0a4b89179c8c74

Contents?: true

Size: 1.85 KB

Versions: 35

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # 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(&:rspec_cop?)
          .map(&:configuration)
          .reduce(:merge)
      end

      private

      attr_reader :code_objects

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

        def initialize(yardoc)
          @yardoc = yardoc
        end

        # Test if the YARD code object documents a concrete rspec cop class
        #
        # @return [Boolean]
        def rspec_cop?
          class_documentation? &&
            rspec_cop_namespace? &&
            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 class_documentation?
          yardoc.type.equal?(:class)
        end

        def rspec_cop_namespace?
          documented_constant.start_with?(RSPEC_NAMESPACE)
        end

        def documented_constant
          yardoc.to_s
        end

        def cop_subclass?
          yardoc.superclass.path == 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

35 entries across 31 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/rspec/description_extractor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/rspec/description_extractor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/rspec/description_extractor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/rspec/description_extractor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/rspec/description_extractor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.15.0 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.14.2 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.14.1 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.14.0 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.13.2 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.13.1 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.13.0 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.12.1 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.12.0 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.11.1 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.11.0 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.10.0 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.9.0 lib/rubocop/rspec/description_extractor.rb
rubocop-rspec-2.8.0 lib/rubocop/rspec/description_extractor.rb