Sha256: 1aa7c596404b3974607c33919198ae56b181bad1cb5a4e5546dfe2e3c6ec7ca4

Contents?: true

Size: 1.99 KB

Versions: 147

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Representation of an annotation comment in source code (eg. `# TODO: blah blah blah`).
    class AnnotationComment
      extend Forwardable

      attr_reader :comment, :margin, :keyword, :colon, :space, :note

      # @param [Parser::Source::Comment] comment
      # @param [Array<String>] keywords
      def initialize(comment, keywords)
        @comment = comment
        @keywords = keywords
        @margin, @keyword, @colon, @space, @note = split_comment(comment)
      end

      def annotation?
        keyword_appearance? && !just_keyword_of_sentence?
      end

      def correct?(colon:)
        return false unless keyword && space && note
        return false unless keyword == keyword.upcase

        self.colon.nil? == !colon
      end

      # Returns the range bounds for just the annotation
      def bounds
        start = comment.source_range.begin_pos + margin.length
        length = [keyword, colon, space].reduce(0) { |len, elem| len + elem.to_s.length }
        [start, start + length]
      end

      private

      attr_reader :keywords

      def split_comment(comment)
        # Sort keywords by reverse length so that if a keyword is in a phrase
        # but also on its own, both will match properly.
        match = comment.text.match(regex)
        return false unless match

        match.captures
      end

      KEYWORDS_REGEX_CACHE = {} # rubocop:disable Style/MutableConstant
      private_constant :KEYWORDS_REGEX_CACHE

      def regex
        KEYWORDS_REGEX_CACHE[keywords] ||= begin
          keywords_regex = Regexp.new(
            Regexp.union(keywords.sort_by { |w| -w.length }).source,
            Regexp::IGNORECASE
          )
          /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i
        end
      end

      def keyword_appearance?
        keyword && (colon || space)
      end

      def just_keyword_of_sentence?
        keyword == keyword.capitalize && !colon && space && note
      end
    end
  end
end

Version data entries

147 entries across 146 versions & 16 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.65.1 lib/rubocop/cop/mixin/annotation_comment.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.65.0 lib/rubocop/cop/mixin/annotation_comment.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.64.1 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.63.4 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.63.3 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.63.2 lib/rubocop/cop/mixin/annotation_comment.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.63.1 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.63.0 lib/rubocop/cop/mixin/annotation_comment.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.62.1 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.62.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.61.0 lib/rubocop/cop/mixin/annotation_comment.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.60.2 lib/rubocop/cop/mixin/annotation_comment.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/annotation_comment.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/annotation_comment.rb