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
@example AllowRBSInlineAnnotation: false (default)
# bad include Enumerable #[Integer] attr_reader :name #: String attr_reader :age #: Integer?
@example AllowRBSInlineAnnotation: true
# good include Enumerable #[Integer] attr_reader :name #: String attr_reader :age #: Integer?
@example AllowSteepAnnotation: false (default)
# bad [1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer] list << n end name = 'John' #: String
@example AllowSteepAnnotation: true
# good [1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer] list << n end name = 'John' #: String
Constants
- MSG
Public Instance Methods
on_new_investigation()
click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 95 def on_new_investigation # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity 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_annotation?(comment) next if steep_annotation?(comment) add_offense(comment) do |corrector| expr = comment.source_range 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 134 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 142 def allow_gemfile_ruby_comment? cop_config['AllowGemfileRubyComment'] end
allow_rbs_inline_annotation?()
click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 158 def allow_rbs_inline_annotation? cop_config['AllowRBSInlineAnnotation'] end
allow_steep_annotation?()
click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 166 def allow_steep_annotation? cop_config['AllowSteepAnnotation'] end
allowed_on_first_line?(comment)
click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 118 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 138 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 146 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 154 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 114 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 130 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 126 def rackup_options?(comment) comment.text.start_with?('#\\') end
rbs_inline_annotation?(comment)
click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 162 def rbs_inline_annotation?(comment) allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]/) end
ruby_comment_in_gemfile?(comment)
click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 150 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 122 def shebang?(comment) comment.text.start_with?('#!') end
steep_annotation?(comment)
click to toggle source
# File lib/rubocop/cop/layout/leading_comment_space.rb, line 170 def steep_annotation?(comment) allow_steep_annotation? && comment.text.start_with?(/#[$:]/) end