class RuboCop::Cop::Layout::LeadingCommentSpace
name = ‘John’ #: String
end
list << n
[1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
# good
@example AllowSteepAnnotation: true
name = ‘John’ #: String
end
list << n
[1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
# bad
@example AllowSteepAnnotation: false (default)
def foo; end
#| ) -> void
#| String
#| Integer,
#: (
attr_reader :age #: Integer?
attr_reader :name #: String
include Enumerable #[Integer]
# good
@example AllowRBSInlineAnnotation: true
def foo; end
#| ) -> void
#| String
#| Integer,
#: (
attr_reader :age #: Integer?
attr_reader :name #: String
include Enumerable #[Integer]
# bad
@example AllowRBSInlineAnnotation: false (default)
#ruby-gemset=myproject
#ruby=2.7.0
# good
@example AllowGemfileRubyComment: true
#ruby-gemset=myproject
#ruby=2.7.0
# bad
@example AllowGemfileRubyComment: false (default)
#*
# Another line of comment
# Some comment
#**
# good
@example AllowDoxygenCommentStyle: true
#*
# Another line of comment
# Some comment
#**
# bad
@example AllowDoxygenCommentStyle: false (default)
# Some comment
# good
#Some comment
# bad
@example
or rackup options.
‘#:nodoc`, `=begin`- and `=end` comments, “shebang” directives,
required for some RDoc special syntax, like `#++`, `#–`,
`#` denoting the start of the comment. The leading space is not
Checks whether comments have a leading space after the
def allow_doxygen_comment?
def allow_doxygen_comment? cop_config['AllowDoxygenCommentStyle'] end
def allow_gemfile_ruby_comment?
def allow_gemfile_ruby_comment? cop_config['AllowGemfileRubyComment'] end
def allow_rbs_inline_annotation?
def allow_rbs_inline_annotation? cop_config['AllowRBSInlineAnnotation'] end
def allow_steep_annotation?
def allow_steep_annotation? cop_config['AllowSteepAnnotation'] end
def allowed_on_first_line?(comment)
def allowed_on_first_line?(comment) shebang?(comment) || (rackup_config_file? && rackup_options?(comment)) end
def doxygen_comment_style?(comment)
def doxygen_comment_style?(comment) allow_doxygen_comment? && comment.text.start_with?('#*') end
def gemfile?
def gemfile? File.basename(processed_source.file_path).eql?('Gemfile') end
def gemfile_ruby_comment?(comment)
def gemfile_ruby_comment?(comment) allow_gemfile_ruby_comment? && ruby_comment_in_gemfile?(comment) end
def hash_mark(expr)
def hash_mark(expr) range_between(expr.begin_pos, expr.begin_pos + 1) end
def on_new_investigation # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
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 shebang_continuation?(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
def rackup_config_file?
def rackup_config_file? File.basename(processed_source.file_path).eql?('config.ru') end
def rackup_options?(comment)
def rackup_options?(comment) comment.text.start_with?('#\\') end
def rbs_inline_annotation?(comment)
def rbs_inline_annotation?(comment) allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]|#\|/) end
def ruby_comment_in_gemfile?(comment)
def ruby_comment_in_gemfile?(comment) gemfile? && comment.text.start_with?('#ruby') end
def shebang?(comment)
def shebang?(comment) comment.text.start_with?('#!') end
def shebang_continuation?(comment)
def shebang_continuation?(comment) return false unless shebang?(comment) return true if comment.loc.line == 1 previous_line_comment = processed_source.comment_at_line(comment.loc.line - 1) return false unless previous_line_comment # If the comment is a shebang but not on the first line, check if the previous # line has a shebang comment that wasn't marked as an offense; if so, this comment # continues the shebang and is acceptable. shebang?(previous_line_comment) && !current_offense_locations.include?(previous_line_comment.source_range) end
def steep_annotation?(comment)
def steep_annotation?(comment) allow_steep_annotation? && comment.text.start_with?(/#[$:]/) end