Sha256: ca63dec47475a4db123e7e635f8a4bbb9b57a690ef029bdd25600a93fba1192c

Contents?: true

Size: 1.56 KB

Versions: 59

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for checking documentation.
    module DocumentationComment
      extend NodePattern::Macros

      private

      def documentation_comment?(node)
        preceding_lines = preceding_lines(node)

        return false unless preceding_comment?(node, preceding_lines.last)

        preceding_lines.any? do |comment|
          !AnnotationComment.new(comment, annotation_keywords).annotation? &&
            !interpreter_directive_comment?(comment) &&
            !rubocop_directive_comment?(comment)
        end
      end

      # The args node1 & node2 may represent a RuboCop::AST::Node
      # or a Parser::Source::Comment. Both respond to #loc.
      def preceding_comment?(node1, node2)
        node1 && node2 && precede?(node2, node1) && comment_line?(node2.loc.expression.source)
      end

      # The args node1 & node2 may represent a RuboCop::AST::Node
      # or a Parser::Source::Comment. Both respond to #loc.
      def precede?(node1, node2)
        node2.loc.line - node1.loc.line == 1
      end

      def preceding_lines(node)
        processed_source.ast_with_comments[node].select { |line| line.loc.line < node.loc.line }
      end

      def interpreter_directive_comment?(comment)
        /^#\s*(frozen_string_literal|encoding):/.match?(comment.text)
      end

      def rubocop_directive_comment?(comment)
        !!DirectiveComment.new(comment).match_captures
      end

      def annotation_keywords
        config.for_cop('Style/CommentAnnotation')['Keywords']
      end
    end
  end
end

Version data entries

59 entries across 53 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/documentation_comment.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/documentation_comment.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/documentation_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/documentation_comment.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.46.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.45.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.45.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.44.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.44.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.43.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.42.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.41.1 lib/rubocop/cop/mixin/documentation_comment.rb