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] Update Bundler/GemVersion to check commit references #9885

Merged
merged 3 commits into from Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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,@timlkelly][])
timlkelly marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 38 additions & 4 deletions lib/rubocop/cop/bundler/gem_version.rb
Expand Up @@ -3,8 +3,8 @@
module RuboCop
module Cop
module Bundler
# Enforce that Gem version specifications are either required
# or forbidden.
# Enforce that Gem version specifications or a commit reference (branch,
# ref, or tag) are either required or forbidden.
#
# @example EnforcedStyle: required (default)
# # bad
Expand All @@ -19,6 +19,15 @@ module Bundler
# # good
# gem 'rubocop', '>= 1.5.0', '< 1.10.0'
#
# # good
# gem 'rubocop', branch: 'feature-branch'
#
# # good
# gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
#
# # good
# gem 'rubocop', tag: 'v1.17.0'
#
# @example EnforcedStyle: forbidden
# # good
# gem 'rubocop'
Expand All @@ -32,6 +41,15 @@ module Bundler
# # bad
# gem 'rubocop', '>= 1.5.0', '< 1.10.0'
#
# # bad
# gem 'rubocop', branch: 'feature-branch'
#
# # bad
# gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
#
# # bad
# gem 'rubocop', tag: 'v1.17.0'
#
class GemVersion < Base
include ConfigurableEnforcedStyle
include GemDeclaration
Expand All @@ -45,6 +63,11 @@ class GemVersion < Base
(send nil? :gem <(str #version_specification?) ...>)
PATTERN

# @!method includes_commit_reference?(node)
def_node_matcher :includes_commit_reference?, <<~PATTERN
(send nil? :gem <(hash <(pair (sym {:branch :ref :tag}) (str _)) ...>) ...>)
PATTERN

def on_send(node)
return unless gem_declaration?(node)
return if allowed_gem?(node)
Expand Down Expand Up @@ -78,8 +101,19 @@ 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)
return unless required_style?

!includes_version_specification?(node) && !includes_commit_reference?(node)
end

def forbidden_offense?(node)
return unless forbidden_style?

includes_version_specification?(node) || includes_commit_reference?(node)
end

def forbidden_style?
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/bundler/gem_version_spec.rb
Expand Up @@ -24,6 +24,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', branch: 'feature-branch'
gem 'rubocop', ref: 'b3f37bc7f'
gem 'rubocop', tag: 'v1'
RUBY
end

Expand Down Expand Up @@ -52,6 +55,12 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', '>= 1.5.0', '< 1.10.0', git: 'https://github.com/rubocop/rubocop'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', branch: 'feature-branch'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', ref: 'b3f37bc7f'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', tag: 'v1'
^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
RUBY
end

Expand Down