Skip to content

Commit

Permalink
Add AllowGemfileRubyComment option to Layout/LeadingCommentSpace
Browse files Browse the repository at this point in the history
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'
```
  • Loading branch information
cetinajero committed Jan 9, 2020
1 parent 1a6ef08 commit 6eb457f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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][])
Expand Down Expand Up @@ -4324,3 +4328,4 @@
[@mangara]: https://github.com/mangara
[@pirj]: https://github.com/pirj
[@pawptart]: https://github.com/pawptart
[@cetinajero]: https://github.com/cetinajero
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 @@ -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

0 comments on commit 6eb457f

Please sign in to comment.