diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e0d10fc92b..99c48bf3388 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### New features +* [#7577](https://github.com/rubocop-hq/rubocop/pull/7577): Add `AllowGemfileRubyComment` configuration on `Layout/LeadingCommentSpace`. ([@cetinajero][]) * [#7663](https://github.com/rubocop-hq/rubocop/pull/7663): Add new `Style/HashTransformKeys` and `Style/HashTransformValues` cops. ([@djudd][], [@eugeneius][]) * [#7619](https://github.com/rubocop-hq/rubocop/issues/7619): Support autocorrect of legacy cop names for `Migration/DepartmentName`. ([@koic][]) @@ -4345,8 +4346,9 @@ [@mangara]: https://github.com/mangara [@pirj]: https://github.com/pirj [@pawptart]: https://github.com/pawptart +[@cetinajero]: https://github.com/cetinajero [@gfyoung]: https://github.com/gfyoung [@Tietew]: https://github.com/Tietew [@hanachin]: https://github.com/hanachin [@masarakki]: https://github.com/masarakki -[@djudd]: https://github.com/djudd +[@djudd]: https://github.com/djudd \ No newline at end of file diff --git a/config/default.yml b/config/default.yml index 10fbd0edf67..49163be98d6 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 1ae38bedc1f..50d87f25b57 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 79d30a08215..3b30f5e4452 100644 --- a/spec/rubocop/cop/layout/leading_comment_space_spec.rb +++ b/spec/rubocop/cop/layout/leading_comment_space_spec.rb @@ -156,4 +156,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