Sha256: 1159160b3003b31dee55d4b9aa7e5e69084bb0baffca5a461d382496bf8e51fe

Contents?: true

Size: 1.55 KB

Versions: 167

Compression:

Stored size: 1.55 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.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

167 entries across 166 versions & 18 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.73.2 lib/rubocop/cop/mixin/documentation_comment.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.73.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.73.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.72.2 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.72.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.72.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.71.2 lib/rubocop/cop/mixin/documentation_comment.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.71.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.71.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.70.0 lib/rubocop/cop/mixin/documentation_comment.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.69.2 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.69.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.69.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.68.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.67.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.66.1 lib/rubocop/cop/mixin/documentation_comment.rb