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 a trailing comment as a description comment for Bundler/GemComment #8537

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
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