Sha256: def5ed5a95ca415ba00a9cda09de7d15e709d93698961f25c0e1658c98a7e2ed

Contents?: true

Size: 1.79 KB

Versions: 49

Compression:

Stored size: 1.79 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.
        keywords_regex = Regexp.new(
          Regexp.union(keywords.sort_by { |w| -w.length }).source,
          Regexp::IGNORECASE
        )
        regex = /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i

        match = comment.text.match(regex)
        return false unless match

        match.captures
      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

49 entries across 43 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/annotation_comment.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/annotation_comment.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/annotation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.40.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.39.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.38.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.37.1 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.37.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.36.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.35.1 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.35.0 lib/rubocop/cop/mixin/annotation_comment.rb
rubocop-1.34.1 lib/rubocop/cop/mixin/annotation_comment.rb