Skip to content

Commit

Permalink
Merge pull request #10149 from Drowze/bundler-gemcomment-frozen
Browse files Browse the repository at this point in the history
Fix Bundler/GemComment offense detection with restrictive_version_specifiers
  • Loading branch information
koic committed Oct 7, 2021
2 parents f7a3088 + babdd6a commit 589c6e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
@@ -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

0 comments on commit 589c6e8

Please sign in to comment.