Skip to content

Commit

Permalink
Compare RuboCop version against strings
Browse files Browse the repository at this point in the history
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
sambostock committed Feb 14, 2024
1 parent 8616e05 commit e8e8718
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
28 changes: 28 additions & 0 deletions lib/rubocop/shopify/gem_version_string_comparable_backport.rb
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
13 changes: 6 additions & 7 deletions rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<%
require "rubocop/shopify/gem_version_string_comparable_backport"
rubocop_version = Gem.loaded_specs.fetch("rubocop").version
v1_57 = Gem::Version.new("1.57")
v1_58 = Gem::Version.new("1.58")
v1_59 = Gem::Version.new("1.59")
%>

inherit_mode:
Expand Down Expand Up @@ -190,15 +189,15 @@ Lint/IncompatibleIoSelectWithFiberScheduler:
Lint/InterpolationCheck:
Enabled: false

<% if rubocop_version >= v1_59 %>
<% if rubocop_version >= "1.59" %>
Lint/ItWithoutArgumentsInBlock:
Enabled: true
<% end %>

Lint/LambdaWithoutLiteralBlock:
Enabled: false

<% if rubocop_version >= v1_58 %>
<% if rubocop_version >= "1.58" %>
Lint/LiteralAssignmentInCondition:
Enabled: true
<% end %>
Expand Down Expand Up @@ -811,7 +810,7 @@ Style/SelectByRegexp:
Style/SingleArgumentDig:
Enabled: false

<% if rubocop_version >= v1_57 %>
<% if rubocop_version >= "1.57" %>
Style/SingleLineDoEndBlock:
Enabled: true
<% end %>
Expand Down Expand Up @@ -840,7 +839,7 @@ Style/StringLiteralsInInterpolation:
Style/StructInheritance:
Enabled: false

<% if rubocop_version >= v1_58 %>
<% if rubocop_version >= "1.58" %>
Style/SuperWithArgsParentheses:
Enabled: true
<% end %>
Expand Down

0 comments on commit e8e8718

Please sign in to comment.