From 6eb457f7efe93bd4ea768677bfe2ce71944b16ef Mon Sep 17 00:00:00 2001 From: Edgar Tinajero <24572406+cetinajero@users.noreply.github.com> Date: Thu, 19 Dec 2019 12:41:40 -0700 Subject: [PATCH] Add `AllowGemfileRubyComment` option to `Layout/LeadingCommentSpace` This PR remove the offense for comments that starts with `#ruby` as this style is a valid syntax to write Ruby versions in Gemfile that can be used with bundler, RVM, and others. Example: ```ruby # Specific version (comment) will be used by RVM #ruby=2.7.0 #ruby-gemset=myproject ruby '~> 2.7.0' ``` --- CHANGELOG.md | 5 +++ config/default.yml | 1 + .../cop/layout/leading_comment_space.rb | 35 ++++++++++++++- manual/cops_layout.md | 17 +++++++ .../cop/layout/leading_comment_space_spec.rb | 45 +++++++++++++++++++ 5 files changed, 101 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aa88926e95..1e8f041563b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### New features + +* [#7577](https://github.com/rubocop-hq/rubocop/pull/7577): Add `AllowGemfileRubyComment` configuration on `Layout/LeadingCommentSpace`. ([@cetinajero][]) + ### Changes * [#7636](https://github.com/rubocop-hq/rubocop/issues/7636): Remove `console` from `Lint/Debugger` to prevent false positives. ([@gsamokovarov][]) @@ -4324,3 +4328,4 @@ [@mangara]: https://github.com/mangara [@pirj]: https://github.com/pirj [@pawptart]: https://github.com/pawptart +[@cetinajero]: https://github.com/cetinajero diff --git a/config/default.yml b/config/default.yml index d6f631fe08a..fdb2a6f1250 100644 --- a/config/default.yml +++ b/config/default.yml @@ -794,6 +794,7 @@ Layout/LeadingCommentSpace: VersionAdded: '0.49' VersionChanged: '0.73' AllowDoxygenCommentStyle: false + AllowGemfileRubyComment: false Layout/LeadingEmptyLines: Description: Check for unnecessary blank lines at the beginning of a file. diff --git a/lib/rubocop/cop/layout/leading_comment_space.rb b/lib/rubocop/cop/layout/leading_comment_space.rb index ce33970d7a7..14d2a15b930 100644 --- a/lib/rubocop/cop/layout/leading_comment_space.rb +++ b/lib/rubocop/cop/layout/leading_comment_space.rb @@ -35,6 +35,20 @@ module Layout # # 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 + # class LeadingCommentSpace < Cop include RangeHelp @@ -44,7 +58,8 @@ def investigate(processed_source) processed_source.each_comment do |comment| next unless comment.text =~ /\A#+[^#\s=:+-]/ next if comment.loc.line == 1 && allowed_on_first_line?(comment) - next if allow_doxygen_comment? && doxygen_comment_style?(comment) + next if doxygen_comment_style?(comment) + next if gemfile_ruby_comment?(comment) add_offense(comment) end @@ -80,7 +95,23 @@ def allow_doxygen_comment? end def doxygen_comment_style?(comment) - comment.text.start_with?('#*') + allow_doxygen_comment? && comment.text.start_with?('#*') + end + + def allow_gemfile_ruby_comment? + cop_config['AllowGemfileRubyComment'] + end + + def gemfile? + File.basename(processed_source.file_path).eql?('Gemfile') + end + + def ruby_comment_in_gemfile?(comment) + gemfile? && comment.text.start_with?('#ruby') + end + + def gemfile_ruby_comment?(comment) + allow_gemfile_ruby_comment? && ruby_comment_in_gemfile?(comment) end end end diff --git a/manual/cops_layout.md b/manual/cops_layout.md index 3bb1e07e02a..d9178da9617 100644 --- a/manual/cops_layout.md +++ b/manual/cops_layout.md @@ -2822,12 +2822,29 @@ or rackup options. # Another line of comment #* ``` +#### AllowGemfileRubyComment: false (default) + +```ruby +# bad + +#ruby=2.7.0 +#ruby-gemset=myproject +``` +#### AllowGemfileRubyComment: true + +```ruby +# good + +#ruby=2.7.0 +#ruby-gemset=myproject +``` ### Configurable attributes Name | Default value | Configurable values --- | --- | --- AllowDoxygenCommentStyle | `false` | Boolean +AllowGemfileRubyComment | `false` | Boolean ### References diff --git a/spec/rubocop/cop/layout/leading_comment_space_spec.rb b/spec/rubocop/cop/layout/leading_comment_space_spec.rb index c5a72181446..6031ca8730b 100644 --- a/spec/rubocop/cop/layout/leading_comment_space_spec.rb +++ b/spec/rubocop/cop/layout/leading_comment_space_spec.rb @@ -130,4 +130,49 @@ =end RUBY end + + describe 'Gemfile Ruby comment' do + context 'when config option is disabled' do + let(:cop_config) { { 'AllowGemfileRubyComment' => false } } + + it 'registers an offense when using ruby config as comment' do + expect_offense(<<~'RUBY') + # Specific version (comment) will be used by RVM + #ruby=2.7.0 + ^^^^^^^^^^^ Missing space after `#`. + #ruby-gemset=myproject + ^^^^^^^^^^^^^^^^^^^^^^ Missing space after `#`. + ruby '~> 2.7.0' + RUBY + end + end + + context 'when config option is enabled' do + let(:cop_config) { { 'AllowGemfileRubyComment' => true } } + + context 'file not named Gemfile' do + it 'registers an offense when using ruby config as comment' do + expect_offense(<<~'RUBY', 'test/test_case.rb') + # Specific version (comment) will be used by RVM + #ruby=2.7.0 + ^^^^^^^^^^^ Missing space after `#`. + #ruby-gemset=myproject + ^^^^^^^^^^^^^^^^^^^^^^ Missing space after `#`. + ruby '~> 2.7.0' + RUBY + end + end + + context 'file named Gemfile' do + it 'does not register an offense when using ruby config as comment' do + expect_no_offenses(<<~'RUBY', 'Gemfile') + # Specific version (comment) will be used by RVM + #ruby=2.7.0 + #ruby-gemset=myproject + ruby '~> 2.7.0' + RUBY + end + end + end + end end