class RuboCop::Cop::Style::BlockComments

This cop looks for uses of block comments (=begin…=end).

@example

# bad
=begin
Multiple lines
of comments...
=end

# good
# Multiple lines
# of comments...

Constants

BEGIN_LENGTH
END_LENGTH
MSG

Public Instance Methods

autocorrect(comment) click to toggle source
# File lib/rubocop/cop/style/block_comments.rb, line 34
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
investigate(processed_source) click to toggle source
# File lib/rubocop/cop/style/block_comments.rb, line 26
def investigate(processed_source)
  processed_source.each_comment do |comment|
    next unless comment.document?

    add_offense(comment)
  end
end

Private Instance Methods

eq_end_part(comment, expr) click to toggle source
# File lib/rubocop/cop/style/block_comments.rb, line 60
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
parts(comment) click to toggle source
# File lib/rubocop/cop/style/block_comments.rb, line 52
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