Sha256: 3cb6c3932d9079fc05b8c2bd99d996f7c307cdc6148473da6353f9ca96cd5101

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

# encoding: utf-8

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.'

        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)

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

        def parts(comment)
          expr = comment.loc.expression
          eq_begin = expr.resize("=begin\n".length)
          eq_end = Parser::Source::Range.new(expr.source_buffer,
                                             expr.end_pos - "\n=end".length,
                                             expr.end_pos)
          contents = Parser::Source::Range.new(expr.source_buffer,
                                               eq_begin.end_pos,
                                               eq_end.begin_pos)
          [eq_begin, eq_end, contents]
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/style/block_comments.rb
rubocop-0.30.0 lib/rubocop/cop/style/block_comments.rb
rubocop-0.29.1 lib/rubocop/cop/style/block_comments.rb
rubocop-0.29.0 lib/rubocop/cop/style/block_comments.rb
rubocop-0.28.0 lib/rubocop/cop/style/block_comments.rb
rubocop-0.27.1 lib/rubocop/cop/style/block_comments.rb
rubocop-0.27.0 lib/rubocop/cop/style/block_comments.rb