class RuboCop::Cop::Layout::LeadingCommentSpace

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

@example AllowDoxygenCommentStyle: false (default)

# bad

#**
# Some comment
# Another line of comment
#*

@example AllowDoxygenCommentStyle: true

# good

#**
# Some comment
# Another line of comment
#*

@example AllowGemfileRubyComment: false (default)

# bad

#ruby=2.7.0
#ruby-gemset=myproject

@example AllowGemfileRubyComment: true

# good

#ruby=2.7.0
#ruby-gemset=myproject

Constants

MSG

Public Instance Methods

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

    add_offense(comment) do |corrector|
      expr = comment.loc.expression

      corrector.insert_after(hash_mark(expr), ' ')
    end
  end
end

Private Instance Methods

allow_doxygen_comment?() click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 95
def allow_doxygen_comment?
  cop_config['AllowDoxygenCommentStyle']
end
allow_gemfile_ruby_comment?() click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 103
def allow_gemfile_ruby_comment?
  cop_config['AllowGemfileRubyComment']
end
allowed_on_first_line?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 79
def allowed_on_first_line?(comment)
  shebang?(comment) || (rackup_config_file? && rackup_options?(comment))
end
doxygen_comment_style?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 99
def doxygen_comment_style?(comment)
  allow_doxygen_comment? && comment.text.start_with?('#*')
end
gemfile?() click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 107
def gemfile?
  File.basename(processed_source.file_path).eql?('Gemfile')
end
gemfile_ruby_comment?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 115
def gemfile_ruby_comment?(comment)
  allow_gemfile_ruby_comment? && ruby_comment_in_gemfile?(comment)
end
hash_mark(expr) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 75
def hash_mark(expr)
  range_between(expr.begin_pos, expr.begin_pos + 1)
end
rackup_config_file?() click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 91
def rackup_config_file?
  File.basename(processed_source.file_path).eql?('config.ru')
end
rackup_options?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 87
def rackup_options?(comment)
  comment.text.start_with?('#\\')
end
ruby_comment_in_gemfile?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 111
def ruby_comment_in_gemfile?(comment)
  gemfile? && comment.text.start_with?('#ruby')
end
shebang?(comment) click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 83
def shebang?(comment)
  comment.text.start_with?('#!')
end