Skip to content

Commit

Permalink
[Fix rubocop#9803] Fix Bundler/GemVersion cop not respecting git tags
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane authored and timlkelly committed Jun 20, 2021
1 parent 981a8a2 commit cdc3096
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_gem_version_not_respecting_tags.md
@@ -0,0 +1 @@
* [#9803](https://github.com/rubocop/rubocop/pull/9803): Fix `Bundler/GemVersion` cop not respecting git tags. ([@tejasbubane][])
26 changes: 24 additions & 2 deletions lib/rubocop/cop/bundler/gem_version.rb
Expand Up @@ -45,6 +45,21 @@ class GemVersion < Base
(send nil? :gem <(str #version_specification?) ...>)
PATTERN

# @!method with_git_ref?(node)
def_node_matcher :with_git_ref?, <<~PATTERN
(send nil? :gem <(hash <#git? #tag_ref? ...>) ...>)
PATTERN

# @!method tag_ref?(node)
def_node_matcher :tag_ref?, <<~PATTERN
(pair (sym {:tag :ref}) (str _))
PATTERN

# @!method git?(node)
def_node_matcher :git?, <<~PATTERN
(pair (sym {:git :github :bitbucket}) (str _))
PATTERN

def on_send(node)
return unless gem_declaration?(node)
return if allowed_gem?(node)
Expand Down Expand Up @@ -78,8 +93,15 @@ def message(range)
end

def offense?(node)
(required_style? && !includes_version_specification?(node)) ||
(forbidden_style? && includes_version_specification?(node))
required_offense?(node) || forbidden_offense?(node)
end

def required_offense?(node)
required_style? && !includes_version_specification?(node) && !with_git_ref?(node)
end

def forbidden_offense?(node)
forbidden_style? && includes_version_specification?(node)
end

def forbidden_style?
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/bundler/gem_version_spec.rb
Expand Up @@ -15,6 +15,8 @@
^^^^^^^^^^^^^ Gem version specification is required.
gem 'rubocop', require: false
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is required.
gem 'rubocop', tag: '1.2.0'
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is required.
RUBY
end

Expand All @@ -24,6 +26,9 @@
gem 'rubocop', '~> 1'
gem 'rubocop', '~> 1.12', require: false
gem 'rubocop', '>= 1.5.0', '< 1.10.0', git: 'https://github.com/rubocop/rubocop'
gem 'rubocop', github: 'rubocop/rubocop', tag: 'v1'
gem 'rubocop', git: 'https://github.com/rubocop/rubocop', ref: 'b3f37bc7f'
gem 'foobar', bitbucket: 'foo/bar', tag: 'v1'
RUBY
end

Expand Down Expand Up @@ -67,5 +72,13 @@
gem 'rspec', '~> 3.10'
RUBY
end

it 'does not flag gems using git source with tag or ref' do
expect_no_offenses(<<~RUBY)
gem 'rubocop', github: 'rubocop/rubocop', tag: 'v1'
gem 'rubocop', git: 'https://github.com/rubocop/rubocop', ref: 'b3f37bc7f'
gem 'foobar', bitbucket: 'foo/bar', tag: 'v1'
RUBY
end
end
end

0 comments on commit cdc3096

Please sign in to comment.