-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compare RuboCop version against strings
As of Rubygems 3.5.6 (included in Ruby 3.2 onwards, and often available in environments with older Ruby versions where Rubygems has been updated), `Gem::Version#<=>` supports passing in a `String`, instead of needing to create a `Gem::Version` to pass in. If we temporarily add a backport, we can use this approach, which makes the version checks in the YAML+ERB config files more straightforward. The backport is done in a separate file so we can leverage the deduplication `require` provides out of the box, so as not to trigger a method redefinition warning. It also means we'll be able to import it in our upcoming plugin config files, rather than duplicating it.
- Loading branch information
1 parent
8616e05
commit e8e8718
Showing
2 changed files
with
34 additions
and
7 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
lib/rubocop/shopify/gem_version_string_comparable_backport.rb
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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# This is true for Ruby 3.2+, so once support for 3.1 is dropped, we can remove this. | ||
# Until then, some installations may have a recent enough version of RubyGems, but it is not guaranteed. | ||
return if Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.5.6") | ||
|
||
module RuboCop | ||
module Shopify | ||
# Backport rubygems/rubygems#5275, so we can compare `Gem::Version`s directly against `String`s. | ||
# | ||
# Gem::Version.new("1.2.3") > "1.2" | ||
# | ||
# Without this, to support Ruby < 3.2, we would have to create a new `Gem::Version` instance ourselves. | ||
# | ||
# Gem::Version.new("1.2.3") > Gem::Version.new("1.2") | ||
# | ||
# This would get very verbose in our RuboCop config files. | ||
module GemVersionStringComparableBackport | ||
def <=>(other) | ||
return self <=> self.class.new(other) if (String === other) && self.class.correct?(other) | ||
|
||
super | ||
end | ||
|
||
Gem::Version.prepend(self) | ||
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