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

Fix Bundler/GemComment offense detection with restrictive_version_specifiers #10149

Merged
merged 1 commit into from Oct 7, 2021
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
@@ -0,0 +1 @@
* [#10149](https://github.com/rubocop/rubocop/pull/10149): Fix `Bundler/GemComment` where it would not detect an offense in some cases when `OnlyFor` is set to `restrictive_version_specifiers`. ([@Drowze][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/bundler/gem_comment.rb
Expand Up @@ -88,7 +88,7 @@ class GemComment < Base
CHECKED_OPTIONS_CONFIG = 'OnlyFor'
VERSION_SPECIFIERS_OPTION = 'version_specifiers'
RESTRICTIVE_VERSION_SPECIFIERS_OPTION = 'restrictive_version_specifiers'
RESTRICTIVE_VERSION_PATTERN = /<|~>/.freeze
RESTRICTIVE_VERSION_PATTERN = /\A\s*(?:<|~>|\d|=)/.freeze
RESTRICT_ON_SEND = %i[gem].freeze

def on_send(node)
Expand Down Expand Up @@ -152,8 +152,8 @@ def version_specified_gem?(node)
def restrictive_version_specified_gem?(node)
return unless version_specified_gem?(node)

node.arguments
.any? { |arg| arg&.str_type? && RESTRICTIVE_VERSION_PATTERN.match?(arg.to_s) }
node.arguments[1..-1]
.any? { |arg| arg&.str_type? && RESTRICTIVE_VERSION_PATTERN.match?(arg.value) }
end

def contains_checked_options?(node)
Expand Down
29 changes: 28 additions & 1 deletion spec/rubocop/cop/bundler/gem_comment_spec.rb
Expand Up @@ -166,7 +166,34 @@
end
end

context 'when a gem is uncommented and has a version specifier' do
context 'when a gem is uncommented and has a non-minimum version specifier with a leading space' do
it 'registers an offense' do
expect_offense(<<-GEM, 'Gemfile')
gem 'rubocop', ' ~> 12.0'
^^^^^^^^^^^^^^^^^^^^^^^^^ Missing gem description comment.
GEM
end
end

context 'when a gem is uncommented and has a version specifier without operator' do
it 'registers an offense' do
expect_offense(<<-GEM, 'Gemfile')
gem 'rubocop', '12.0'
^^^^^^^^^^^^^^^^^^^^^ Missing gem description comment.
GEM
end
end

context 'when a gem is uncommented and has a frozen version specifier' do
it 'registers an offense' do
expect_offense(<<-GEM, 'Gemfile')
gem 'rubocop', '= 12.0'
^^^^^^^^^^^^^^^^^^^^^^^ Missing gem description comment.
GEM
end
end

context 'when a gem is uncommented and has a pessimistic version specifier' do
it 'registers an offense' do
expect_offense(<<-GEM, 'Gemfile')
gem 'rubocop', '~> 12.0'
Expand Down