Skip to content

Commit

Permalink
Fix Gemspec/RequiredRubyVersion version matcher
Browse files Browse the repository at this point in the history
Fixes a bug believed to be introduced by rubocop#10157 where the cop
no longer supported multiple requirements when they were passed as a
`Gem::Requirement` object instead of as an array.
  • Loading branch information
nickpellant committed Jan 11, 2022
1 parent c651461 commit 5cf2af2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_gemspecrequiredrubyversion_version.md
@@ -0,0 +1 @@
* [#10354](https://github.com/rubocop/rubocop/pull/10354): Fix Gemspec/RequiredRubyVersion version matcher when Gem::Requirement.new is used and initialised with multiple requirements. ([@nickpellant][])
13 changes: 10 additions & 3 deletions lib/rubocop/cop/gemspec/required_ruby_version.rb
Expand Up @@ -68,8 +68,11 @@ class RequiredRubyVersion < Base

# @!method defined_ruby_version(node)
def_node_matcher :defined_ruby_version, <<~PATTERN
{$(str _) $(array (str _) (str _))
(send (const (const nil? :Gem) :Requirement) :new $(str _))}
{
$(str _)
$(array (str _) (str _))
(send (const (const nil? :Gem) :Requirement) :new $str+)
}
PATTERN

def on_new_investigation
Expand Down Expand Up @@ -97,7 +100,11 @@ def dynamic_version?(node)
def extract_ruby_version(required_ruby_version)
return unless required_ruby_version

if required_ruby_version.array_type?
if required_ruby_version.is_a?(Array)
required_ruby_version = required_ruby_version.detect do |v|
/[>=]/.match?(v.str_content)
end
elsif required_ruby_version.array_type?
required_ruby_version = required_ruby_version.children.detect do |v|
/[>=]/.match?(v.str_content)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/gemspec/required_ruby_version_spec.rb
Expand Up @@ -38,6 +38,14 @@
RUBY
end

it 'recognizes a Gem::Requirement with multiple requirements and does not register an offense' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0", "<= 2.8")
end
RUBY
end

describe 'false negatives' do
it 'does not register an offense when `required_ruby_version` ' \
'is assigned as a variable (string literal)' do
Expand Down

0 comments on commit 5cf2af2

Please sign in to comment.