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 #9803] Fix Bundler/GemVersion cop not respecting git tags #9807

Closed
wants to merge 1 commit into from
Closed
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/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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use a more neutral name here - e.g. commit_ref?.

(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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to change for the VCS? I doubt someone would use :tag or :ref without them and this will probably result in some mistake. Haven't tried it, though.

(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