Class: RuboCop::Cop::Momocop::Layout::LeadingCommentSpace

Inherits:
Layout::LeadingCommentSpace
  • Object
show all
Defined in:
lib/rubocop/cop/momocop/layout/leading_comment_space.rb

Overview

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, or rbs-inline typing.

Examples:


# bad
#Some comment

# good
# Some comment

AllowDoxygenCommentStyle: false (default)


# bad

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

AllowDoxygenCommentStyle: true


# good

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

AllowGemfileRubyComment: false (default)


# bad

#ruby=2.7.0
#ruby-gemset=myproject

AllowGemfileRubyComment: true


# good

#ruby=2.7.0
#ruby-gemset=myproject

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/momocop/layout/leading_comment_space.rb', line 54

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)
    next if rbs_inline_comment_style?(comment)

    add_offense(comment) do |corrector|
      expr = comment.source_range

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

#rbs_inline_comment_style?(comment) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubocop/cop/momocop/layout/leading_comment_space.rb', line 70

def rbs_inline_comment_style?(comment)
  comment.text.start_with?('#:')
end