Sha256: 45c31fc9dcf0a20a2013742fdd113aef90532775aa3a163bf28b30f5da92063b

Contents?: true

Size: 1.71 KB

Versions: 30

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for comments put on the same line as some keywords.
      # These keywords are: `begin`, `class`, `def`, `end`, `module`.
      #
      # Note that some comments
      # (`:nodoc:`, `:yields:`, `rubocop:disable` and `rubocop:todo`)
      # are allowed.
      #
      # @example
      #   # bad
      #   if condition
      #     statement
      #   end # end if
      #
      #   # bad
      #   class X # comment
      #     statement
      #   end
      #
      #   # bad
      #   def x; end # comment
      #
      #   # good
      #   if condition
      #     statement
      #   end
      #
      #   # good
      #   class X # :nodoc:
      #     y
      #   end
      class CommentedKeyword < Cop
        MSG = 'Do not place comments on the same line as the ' \
              '`%<keyword>s` keyword.'

        def investigate(processed_source)
          processed_source.each_comment do |comment|
            add_offense(comment) if offensive?(comment)
          end
        end

        private

        KEYWORDS = %w[begin class def end module].freeze
        ALLOWED_COMMENTS = %w[
          :nodoc:
          :yields:
          rubocop:disable
          rubocop:todo
        ].freeze

        def offensive?(comment)
          line = line(comment)
          KEYWORDS.any? { |word| line =~ /^\s*#{word}\s/ } &&
            ALLOWED_COMMENTS.none? { |c| line =~ /#\s*#{c}/ }
        end

        def message(comment)
          keyword = line(comment).match(/(\S+).*#/)[1]
          format(MSG, keyword: keyword)
        end

        def line(comment)
          comment.location.expression.source_line
        end
      end
    end
  end
end

Version data entries

30 entries across 23 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/commented_keyword.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/commented_keyword.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.85.1 lib/rubocop/cop/style/commented_keyword.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.85.0 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.84.0 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.83.0 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.82.0 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.81.0 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.80.1 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.80.0 lib/rubocop/cop/style/commented_keyword.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/commented_keyword.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.79.0 lib/rubocop/cop/style/commented_keyword.rb
rubocop-0.78.0 lib/rubocop/cop/style/commented_keyword.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/commented_keyword.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/commented_keyword.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/commented_keyword.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/commented_keyword.rb