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 #7669] Add Bundler/GemVersion cop #9727
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
74b5bfb
[Fix #7669] Add Bundler/GemVersionDeclaration Cop
timlkelly 03a855e
Change SupportedStyles prohibited to forbidden
timlkelly e197968
Rename cop to GemVersion
timlkelly adfbdc1
Prefer specification over delcaration terminology
timlkelly e534b59
Prefer AllowedGems over IgnoredGems
timlkelly 96299da
Include .gemfile and gems.rb file types
timlkelly 90db0f7
Extract gem_declaration?
timlkelly 14cf5c5
Update VERSION_SPECIFICATION_REGEX
timlkelly File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#7669](https://github.com/rubocop/rubocop/issues/7669): New cop `Bundler/GemVersion` requires or forbids specifying gem versions. ([@timlkelly][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Bundler | ||
# Enforce that Gem version specifications are either required | ||
# or forbidden. | ||
# | ||
# @example EnforcedStyle: required (default) | ||
# # bad | ||
# gem 'rubocop' | ||
# | ||
# # good | ||
# gem 'rubocop', '~> 1.12' | ||
# | ||
# # good | ||
# gem 'rubocop', '>= 1.10.0' | ||
# | ||
# # good | ||
# gem 'rubocop', '>= 1.5.0', '< 1.10.0' | ||
# | ||
# @example EnforcedStyle: forbidden | ||
# # good | ||
# gem 'rubocop' | ||
# | ||
# # bad | ||
# gem 'rubocop', '~> 1.12' | ||
# | ||
# # bad | ||
# gem 'rubocop', '>= 1.10.0' | ||
# | ||
# # bad | ||
# gem 'rubocop', '>= 1.5.0', '< 1.10.0' | ||
# | ||
class GemVersion < Base | ||
include ConfigurableEnforcedStyle | ||
include GemDeclaration | ||
|
||
REQUIRED_MSG = 'Gem version specification is required.' | ||
FORBIDDEN_MSG = 'Gem version specification is forbidden.' | ||
VERSION_SPECIFICATION_REGEX = /^\s*[~<>=]*\s*[0-9.]+/.freeze | ||
|
||
# @!method includes_version_specification?(node) | ||
def_node_matcher :includes_version_specification?, <<~PATTERN | ||
(send nil? :gem <(str #version_specification?) ...>) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless gem_declaration?(node) | ||
return if allowed_gem?(node) | ||
|
||
if offense?(node) | ||
add_offense(node) | ||
opposite_style_detected | ||
else | ||
correct_style_detected | ||
end | ||
end | ||
|
||
private | ||
|
||
def allowed_gem?(node) | ||
allowed_gems.include?(node.first_argument.value) | ||
end | ||
|
||
def allowed_gems | ||
Array(cop_config['AllowedGems']) | ||
end | ||
|
||
def message(range) | ||
gem_specification = range.source | ||
|
||
if required_style? | ||
format(REQUIRED_MSG, gem_specification: gem_specification) | ||
elsif forbidden_style? | ||
format(FORBIDDEN_MSG, gem_specification: gem_specification) | ||
end | ||
end | ||
|
||
def offense?(node) | ||
(required_style? && !includes_version_specification?(node)) || | ||
(forbidden_style? && includes_version_specification?(node)) | ||
end | ||
|
||
def forbidden_style? | ||
style == :forbidden | ||
end | ||
|
||
def required_style? | ||
style == :required | ||
end | ||
|
||
def version_specification?(expression) | ||
expression.match?(VERSION_SPECIFICATION_REGEX) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Common functionality for checking gem declarations. | ||
module GemDeclaration | ||
extend NodePattern::Macros | ||
|
||
# @!method gem_declaration?(node) | ||
def_node_matcher :gem_declaration?, '(send nil? :gem str ...)' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Bundler::GemVersion, :config do | ||
context 'when EnforcedStyle is set to required (default)' do | ||
let(:cop_config) do | ||
{ | ||
'EnforcedStyle' => 'required', | ||
'AllowedGems' => ['rspec'] | ||
} | ||
end | ||
|
||
it 'flags gems that do not specify a version' do | ||
expect_offense(<<~RUBY) | ||
gem 'rubocop' | ||
^^^^^^^^^^^^^ Gem version specification is required. | ||
gem 'rubocop', require: false | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is required. | ||
RUBY | ||
end | ||
|
||
it 'does not flag gems with a specified version' do | ||
expect_no_offenses(<<~RUBY) | ||
gem 'rubocop', '>=1.10.0' | ||
gem 'rubocop', '~> 1' | ||
gem 'rubocop', '~> 1.12', require: false | ||
gem 'rubocop', '>= 1.5.0', '< 1.10.0', git: 'https://github.com/rubocop/rubocop' | ||
RUBY | ||
end | ||
|
||
it 'does not flag gems included in AllowedGems metadata' do | ||
expect_no_offenses(<<~RUBY) | ||
gem 'rspec' | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when EnforcedStyle is set to forbidden' do | ||
let(:cop_config) do | ||
{ | ||
'EnforcedStyle' => 'forbidden', | ||
'AllowedGems' => ['rspec'] | ||
} | ||
end | ||
|
||
it 'flags gems that specify a gem version' do | ||
expect_offense(<<~RUBY) | ||
gem 'rubocop', '~> 1' | ||
^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden. | ||
gem 'rubocop', '>=1.10.0' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden. | ||
gem 'rubocop', '~> 1.12', require: false | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden. | ||
gem 'rubocop', '>= 1.5.0', '< 1.10.0', git: 'https://github.com/rubocop/rubocop' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden. | ||
RUBY | ||
end | ||
|
||
it 'does not flag gems without a specified version' do | ||
expect_no_offenses(<<~RUBY) | ||
gem 'rubocop' | ||
gem 'rubocop', require: false | ||
RUBY | ||
end | ||
|
||
it 'does not flag gems included in AllowedGems metadata' do | ||
expect_no_offenses(<<~RUBY) | ||
gem 'rspec', '~> 3.10' | ||
RUBY | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that other
Bundler
cops also include*.gemfile
andgems.rb
. I've personally never worked with this format of gem file, so I was unsure what exactly to test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format is exactly the same, it's just a different filename.