Sha256: 93bc5ab69af3dd5425c46f15582dd172fc6d98f4aef93c3c57b79a8517ffb260

Contents?: true

Size: 1.71 KB

Versions: 28

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Looks for uses of block comments (=begin...=end).
      #
      # @example
      #   # bad
      #   =begin
      #   Multiple lines
      #   of comments...
      #   =end
      #
      #   # good
      #   # Multiple lines
      #   # of comments...
      #
      class BlockComments < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Do not use block comments.'
        BEGIN_LENGTH = "=begin\n".length
        END_LENGTH = "\n=end".length

        def on_new_investigation
          processed_source.comments.each do |comment|
            next unless comment.document?

            add_offense(comment) do |corrector|
              eq_begin, eq_end, contents = parts(comment)

              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
        end

        private

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

        def eq_end_part(comment, expr)
          if comment.text.chomp == comment.text
            range_between(expr.end_pos - END_LENGTH - 1, expr.end_pos - 2)
          else
            range_between(expr.end_pos - END_LENGTH, expr.end_pos)
          end
        end
      end
    end
  end
end

Version data entries

28 entries across 24 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/block_comments.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/block_comments.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/block_comments.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/block_comments.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/block_comments.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/block_comments.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/block_comments.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/block_comments.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/block_comments.rb
rubocop-1.41.1 lib/rubocop/cop/style/block_comments.rb
rubocop-1.41.0 lib/rubocop/cop/style/block_comments.rb
rubocop-1.40.0 lib/rubocop/cop/style/block_comments.rb
rubocop-1.39.0 lib/rubocop/cop/style/block_comments.rb
rubocop-1.38.0 lib/rubocop/cop/style/block_comments.rb
rubocop-1.37.1 lib/rubocop/cop/style/block_comments.rb
rubocop-1.37.0 lib/rubocop/cop/style/block_comments.rb
rubocop-1.36.0 lib/rubocop/cop/style/block_comments.rb
rubocop-1.35.1 lib/rubocop/cop/style/block_comments.rb
rubocop-1.35.0 lib/rubocop/cop/style/block_comments.rb
rubocop-1.34.1 lib/rubocop/cop/style/block_comments.rb