-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
104 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
79 changes: 79 additions & 0 deletions
79
lib/rubocop/cop/sorbet/rbi_versioning/forbid_overlapping_and_annotations.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,79 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Checks that RBI gem version annotations do not contain "and" statements with | ||
# overlapping gem versions. | ||
# | ||
# @example | ||
# # bad | ||
# # @version > 3.2.3, > 4.5 | ||
# | ||
# # bad | ||
# # @version > 1.0, <= 2.0, = 1.5 | ||
# | ||
# # good | ||
# # @version < 1.0, >= 2.0 | ||
# | ||
class ForbidOverlappingAndAnnotations < Base | ||
include RBIVersionAnnotationHelper | ||
|
||
MSG = "Some message here" | ||
|
||
def on_new_investigation | ||
rbi_version_annotations.each do |comment| | ||
ranges = [] | ||
versions(comment).each do |version| | ||
ranges += version_to_ranges(version) | ||
end | ||
|
||
overlap = ranges.combination(2).any? do |range_1, range_2| | ||
range_1.cover?(range_2) || range_2.cover?(range_1) | ||
end | ||
|
||
add_offense(comment) if overlap | ||
end | ||
end | ||
|
||
private | ||
|
||
def version_to_ranges(version_string) | ||
operator, version = version_string.strip.split(" ") | ||
# TODO: make more specific | ||
raise if operator.nil? || version.nil? | ||
|
||
begin | ||
gem_version = Gem::Version.new(version) | ||
rescue ArgumentError | ||
# TODO: do something here | ||
return | ||
end | ||
|
||
case operator | ||
when "=" | ||
[Range.new(gem_version, gem_version)] | ||
when "!=" | ||
prev_version_down = Gem::Version.new(gem_version.to_s + "-pre") | ||
next_version_up = Gem::Version.new(gem_version.to_s + ".1") | ||
[Range.new(nil, prev_version_down), Range.new(next_version_up, nil)] | ||
when ">" | ||
next_version_up = Gem::Version.new(gem_version.to_s + ".1") | ||
[Range.new(next_version_up, nil)] | ||
when ">=" | ||
[Range.new(gem_version, nil)] | ||
when "<" | ||
[Range.new(nil, gem_version, true)] # exclude ending value | ||
when "<=" | ||
[Range.new(nil, gem_version)] # include ending value | ||
when "~>" | ||
[Range.new(gem_version, gem_version.bump)] | ||
else | ||
# TODO: make more specific | ||
raise "INVALID OPERATOR" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
71 changes: 0 additions & 71 deletions
71
lib/rubocop/cop/sorbet/rbi_versioning/no_overlapping_and_annotations.rb
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
lib/rubocop/cop/sorbet/rbi_versioning/rbi_version_annotation_helper.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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
module RBIVersionAnnotationHelper | ||
VERSION_PREFIX = "# @version " | ||
|
||
def rbi_version_annotations | ||
processed_source.comments.select do |comment| | ||
version_annotation?(comment) | ||
end | ||
end | ||
|
||
private | ||
|
||
def version_annotation?(comment) | ||
comment.text.start_with?(VERSION_PREFIX) | ||
end | ||
|
||
def versions(comment) | ||
comment.text.delete_prefix(VERSION_PREFIX).split(", ") | ||
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
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
49 changes: 49 additions & 0 deletions
49
spec/rubocop/cop/sorbet/rbi_versioning/forbid_overlapping_and_annotations_spec.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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::ForbidOverlappingAndAnnotations, :config) do | ||
it "does not register an offense when comment is not a version annotation" do | ||
expect_no_offenses(<<~RUBY) | ||
# a random comment | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when versions do not overlap" do | ||
expect_no_offenses(<<~RUBY) | ||
# @version < 1.0, >= 2.0 | ||
RUBY | ||
end | ||
|
||
it "registers an offense when one version is contained within a range" do | ||
expect_offense(<<~RUBY) | ||
# @version < 1.0, = 0.4.0 | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Some message here | ||
RUBY | ||
end | ||
|
||
it "registers an offense when one version is contained within one of a few ranges" do | ||
expect_offense(<<~RUBY) | ||
# @version > 5.0, < 1.0, = 0.4 | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Some message here | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when one version partially overlaps with another" do | ||
expect_no_offenses(<<~RUBY) | ||
# @version > 5.0, < 7.0 | ||
RUBY | ||
end | ||
|
||
it "registers an offense when twiddle-waka operator is used" do | ||
expect_offense(<<~RUBY) | ||
# @version ~> 3.6, = 3.6.8 | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Some message here | ||
RUBY | ||
end | ||
|
||
it "registers an offense when not-equal operator is used" do | ||
expect_offense(<<~RUBY) | ||
# @version != 4.0, <= 3.8 | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Some message here | ||
RUBY | ||
end | ||
end |
21 changes: 0 additions & 21 deletions
21
spec/rubocop/cop/sorbet/rbi_versioning/no_overlapping_and_annotations_spec.rb
This file was deleted.
Oops, something went wrong.