class RuboCop::Cop::Layout::LeadingCommentSpace

This cop checks whether comments have a leading space after the `#` denoting the start of the comment. The leading space is not required for some RDoc special syntax, like `#++`, `#–`, `#:nodoc`, `=begin`- and `=end` comments, “shebang” directives, or rackup options.

@example

# bad
#Some comment

# good
# Some comment

Constants

MSG

Public Instance Methods

investigate(processed_source) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 22
def investigate(processed_source)
  processed_source.comments.each do |comment|
    next unless comment.text =~ /\A#+[^#\s=:+-]/
    next if comment.loc.line == 1 && allowed_on_first_line?(comment)

    add_offense(comment)
  end
end

Private Instance Methods

allowed_on_first_line?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 40
def allowed_on_first_line?(comment)
  shebang?(comment) || rackup_config_file? && rackup_options?(comment)
end
autocorrect(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 33
def autocorrect(comment)
  expr = comment.loc.expression
  hash_mark = range_between(expr.begin_pos, expr.begin_pos + 1)

  ->(corrector) { corrector.insert_after(hash_mark, ' ') }
end
rackup_config_file?() click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 52
def rackup_config_file?
  File.basename(processed_source.buffer.name).eql?('config.ru')
end
rackup_options?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 48
def rackup_options?(comment)
  comment.text.start_with?('#\\')
end
shebang?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 44
def shebang?(comment)
  comment.text.start_with?('#!')
end