Sha256: 887a55feaf86a80c94b0fb8ae8d38aaad6e7a799d92a8e6e8b92f75b0673c890

Contents?: true

Size: 1.36 KB

Versions: 14

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop looks for uses of block comments (=begin...=end).
      class BlockComments < Cop
        MSG = 'Do not use block comments.'.freeze
        BEGIN_LENGTH = "=begin\n".length
        END_LENGTH = "\n=end".length

        def investigate(processed_source)
          processed_source.comments.each do |comment|
            add_offense(comment, :expression) if comment.document?
          end
        end

        private

        def autocorrect(comment)
          eq_begin, eq_end, contents = parts(comment)

          lambda do |corrector|
            corrector.remove(eq_begin)
            unless contents.length.zero?
              corrector.replace(contents,
                                contents.source
                                  .gsub(/\A/, '# ')
                                  .gsub(/\n\n/, "\n#\n")
                                  .gsub(/\n(?=[^#])/, "\n# "))
            end
            corrector.remove(eq_end)
          end
        end

        def parts(comment)
          expr = comment.loc.expression
          eq_begin = expr.resize(BEGIN_LENGTH)
          eq_end = range_between(expr.end_pos - END_LENGTH, expr.end_pos)
          contents = range_between(eq_begin.end_pos, eq_end.begin_pos)
          [eq_begin, eq_end, contents]
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/block_comments.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/block_comments.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/block_comments.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/block_comments.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/block_comments.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/block_comments.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/block_comments.rb
rubocop-0.49.1 lib/rubocop/cop/style/block_comments.rb
rubocop-0.49.0 lib/rubocop/cop/style/block_comments.rb
rubocop-0.48.1 lib/rubocop/cop/style/block_comments.rb
rubocop-0.48.0 lib/rubocop/cop/style/block_comments.rb
rubocop-0.47.1 lib/rubocop/cop/style/block_comments.rb
rubocop-0.47.0 lib/rubocop/cop/style/block_comments.rb
rubocop-0.46.0 lib/rubocop/cop/style/block_comments.rb