Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
201 additions
and
3 deletions.
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 |