Sha256: b7e3c6f536cb15691c6a17f7c46e94a1f68d8a1f7065c6e4eb3e9dda443f2c73

Contents?: true

Size: 1.5 KB

Versions: 41

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

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

      private

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

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

        preceding_lines.any? do |comment|
          !annotation?(comment) &&
            !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 do |line|
          line.loc.line < node.loc.line
        end
      end

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

      def rubocop_directive_comment?(comment)
        CommentConfig::COMMENT_DIRECTIVE_REGEXP.match?(comment.text)
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 4 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/documentation_comment.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/documentation_comment.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/documentation_comment.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/documentation_comment.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/documentation_comment.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.11.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.10.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.9.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.9.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.8.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.8.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.7.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.6.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.6.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.5.2 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.5.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.5.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.4.2 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.4.1 lib/rubocop/cop/mixin/documentation_comment.rb