Skip to content

Commit

Permalink
Merge pull request #8537 from pocke/Allow-a-trailing-comment-as-a-des…
Browse files Browse the repository at this point in the history
…cription-comment-for-Bundler/GemComment

Allow a trailing comment as a description comment for `Bundler/GemComment`
  • Loading branch information
koic committed Aug 14, 2020
2 parents 8661e99 + 5518c1e commit 59fd34b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@

* [#8508](https://github.com/rubocop-hq/rubocop/pull/8508): Fix a false positive for `Style/CaseLikeIf` when conditional contains comparison with a class. Mark `Style/CaseLikeIf` as not safe. ([@fatkodima][])
* [#8534](https://github.com/rubocop-hq/rubocop/issues/8534): Fix `Lint/BinaryOperatorWithIdenticalOperands` for binary operators used as unary operators. ([@marcandre][])
* [#8537](https://github.com/rubocop-hq/rubocop/pull/8537): Allow a trailing comment as a description comment for `Bundler/GemComment`. ([@pocke][])

### Changes

Expand Down
10 changes: 7 additions & 3 deletions lib/rubocop/cop/bundler/gem_comment.rb
Expand Up @@ -70,14 +70,18 @@ class GemComment < Cop
def on_send(node)
return unless gem_declaration?(node)
return if ignored_gem?(node)
return if commented?(node)
return if commented_any_descendant?(node)
return if cop_config[CHECKED_OPTIONS_CONFIG].any? && !checked_options_present?(node)

add_offense(node)
end

private

def commented_any_descendant?(node)
commented?(node) || node.each_descendant.any? { |n| commented?(n) }
end

def commented?(node)
preceding_lines = preceding_lines(node)
preceding_comment?(node, preceding_lines.last)
Expand All @@ -86,12 +90,12 @@ def commented?(node)
# The args node1 & node2 may represent a RuboCop::AST::Node
# or a Parser::Source::Comment. Both respond to #loc.
def precede?(node1, node2)
node2.loc.line - node1.loc.line == 1
node2.loc.line - node1.loc.line <= 1
end

def preceding_lines(node)
processed_source.ast_with_comments[node].select do |line|
line.loc.line < node.loc.line
line.loc.line <= node.loc.line
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/bundler/gem_comment_spec.rb
Expand Up @@ -33,6 +33,14 @@
end
end

context 'and the gem is commented on the same line' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY, 'Gemfile')
gem 'rubocop' # Style-guide enforcer.
RUBY
end
end

context 'and the gem is permitted' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY, 'Gemfile')
Expand Down

0 comments on commit 59fd34b

Please sign in to comment.