Sha256: 54ba114e1cee4cdb0ce4e55444a2ec55a0cb897221df984e98b7a5c0f523da03

Contents?: true

Size: 1.46 KB

Versions: 15

Compression:

Stored size: 1.46 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 { |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
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
rubocop-1.19.1 lib/rubocop/cop/mixin/documentation_comment.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.19.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.18.4 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.18.3 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.18.2 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.18.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.18.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.17.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.16.1 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.16.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.15.0 lib/rubocop/cop/mixin/documentation_comment.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.14.0 lib/rubocop/cop/mixin/documentation_comment.rb
rubocop-1.13.0 lib/rubocop/cop/mixin/documentation_comment.rb