From 66668f14368b51f4bdb787e64b20c7ca409237a0 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 31 Jul 2021 00:28:14 +0900 Subject: [PATCH] Make `Layout/LeadingCommentSpace` aware of `#:nodoc` This PR makes `Layout/LeadingCommentSpace` aware of `#:nodoc`. The following mention is the trigger. https://twitter.com/kamipo/status/1421031611588485121 It seems that there was no definitive comment on whether to allow `#:nodoc:`. - https://github.com/rubocop/rubocop/issues/277 - https://github.com/rubocop/rubocop/commit/db79146 I've look RDoc doc's example of Ruby 3.0 and it looks to include leading comment space. ```ruby module MyModule # :nodoc: class Input end end module OtherModule # :nodoc: all class Output end end ``` https://docs.ruby-lang.org/en/3.0.0/RDoc/Markup.html#class-RDoc::Markup-label-Controlling+what+is+documented I also considered making it an option for the presence or absence of leading comment space in `#:nodoc:`, but I think it would be better to consistent it that space is included when possible as the role of cop. --- ...ut_leading_comment_space_aware_of_nodoc.md | 1 + .../cop/layout/leading_comment_space.rb | 2 +- .../cop/layout/leading_comment_space_spec.rb | 20 +++++++++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 changelog/change_make_layout_leading_comment_space_aware_of_nodoc.md diff --git a/changelog/change_make_layout_leading_comment_space_aware_of_nodoc.md b/changelog/change_make_layout_leading_comment_space_aware_of_nodoc.md new file mode 100644 index 00000000000..052046bcbba --- /dev/null +++ b/changelog/change_make_layout_leading_comment_space_aware_of_nodoc.md @@ -0,0 +1 @@ +* [#9964](https://github.com/rubocop/rubocop/pull/9964): Make `Layout/LeadingCommentSpace` aware of `#:nodoc`. ([@koic][]) diff --git a/lib/rubocop/cop/layout/leading_comment_space.rb b/lib/rubocop/cop/layout/leading_comment_space.rb index ae822360909..7cca1b14291 100644 --- a/lib/rubocop/cop/layout/leading_comment_space.rb +++ b/lib/rubocop/cop/layout/leading_comment_space.rb @@ -57,7 +57,7 @@ class LeadingCommentSpace < Base def on_new_investigation processed_source.comments.each do |comment| - next unless /\A#+[^#\s=:+-]/.match?(comment.text) + 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) diff --git a/spec/rubocop/cop/layout/leading_comment_space_spec.rb b/spec/rubocop/cop/layout/leading_comment_space_spec.rb index 77d1a0ee9d4..d17a66d00c5 100644 --- a/spec/rubocop/cop/layout/leading_comment_space_spec.rb +++ b/spec/rubocop/cop/layout/leading_comment_space_spec.rb @@ -135,12 +135,20 @@ end end - it 'accepts rdoc syntax' do - expect_no_offenses(<<~RUBY) - #++ - #-- - #:nodoc: - RUBY + describe 'RDoc syntax' do + it 'does not register an offense when using `#++` or `#--`' do + expect_no_offenses(<<~RUBY) + #++ + #-- + RUBY + end + + it 'registers an offense when starting `:`' do + expect_offense(<<~RUBY) + #:nodoc: + ^^^^^^^^ Missing space after `#`. + RUBY + end end it 'accepts sprockets directives' do