Sha256: 327fb9bc2e248c6a43b86aa5253ee502dfb13e75a69aed28cd2f594c88182afb

Contents?: true

Size: 1.99 KB

Versions: 8

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.loc.expression.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

8 entries across 8 versions & 2 rubygems

Version Path
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.46.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.45.1 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.45.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.44.1 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.44.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.43.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.42.0 lib/rubocop/cop/mixin/annotation_comment.rb