Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow #ruby for Ruby version comment style on LeadingCommentSpace #7577

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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][])

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -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.
Expand Down
35 changes: 33 additions & 2 deletions lib/rubocop/cop/layout/leading_comment_space.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions manual/cops_layout.md
Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cop/layout/leading_comment_space_spec.rb
Expand Up @@ -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