Sha256: 0b2d021fe6f44096330164728ec0a476a6947d46f3b85fb664e6877bfbcb1f4d

Contents?: true

Size: 1.69 KB

Versions: 8

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpecRails
    # 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_rails_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
        RSPEC_RAILS_COP_CLASS_NAME = 'RuboCop::Cop::RSpec::Base'
        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 rspec_rails_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 == RSPEC_RAILS_COP_CLASS_NAME ||
            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

8 entries across 8 versions & 2 rubygems

Version Path
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec_rails-2.30.0/lib/rubocop/rspec_rails/description_extractor.rb
rubocop-rspec_rails-2.30.0 lib/rubocop/rspec_rails/description_extractor.rb
rubocop-rspec_rails-2.29.1 lib/rubocop/rspec_rails/description_extractor.rb
rubocop-rspec_rails-2.29.0 lib/rubocop/rspec_rails/description_extractor.rb
rubocop-rspec_rails-2.28.3 lib/rubocop/rspec_rails/description_extractor.rb
rubocop-rspec_rails-2.28.2 lib/rubocop/rspec_rails/description_extractor.rb
rubocop-rspec_rails-2.28.1 lib/rubocop/rspec_rails/description_extractor.rb
rubocop-rspec_rails-2.28.0 lib/rubocop/rspec_rails/description_extractor.rb