-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a cop to validate gem version annotations in RBI files #199
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions
27
lib/rubocop/cop/sorbet/rbi_versioning/gem_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 GemVersionAnnotationHelper | ||
VERSION_PREFIX = "# @version" | ||
|
||
def gem_version_annotations | ||
processed_source.comments.select do |comment| | ||
gem_version_annotation?(comment) | ||
end | ||
end | ||
|
||
private | ||
|
||
def gem_version_annotation?(comment) | ||
comment.text.start_with?(VERSION_PREFIX) | ||
end | ||
|
||
def gem_versions(comment) | ||
comment.text.delete_prefix(VERSION_PREFIX).split(/, ?/).map(&:strip) | ||
end | ||
end | ||
end | ||
end | ||
end |
62 changes: 62 additions & 0 deletions
62
lib/rubocop/cop/sorbet/rbi_versioning/valid_gem_version_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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Checks that gem versions in RBI annotations are properly formatted per the Bundler gem specification. | ||
# | ||
# @example | ||
# # bad | ||
# # @version > not a version number | ||
# | ||
# # good | ||
# # @version = 1 | ||
# | ||
# # good | ||
# # @version > 1.2.3 | ||
# | ||
# # good | ||
# # @version <= 4.3-preview | ||
# | ||
class ValidGemVersionAnnotations < RuboCop::Cop::Base | ||
include GemVersionAnnotationHelper | ||
|
||
MSG = "Invalid gem version(s) detected: %<versions>s" | ||
VALID_OPERATORS = ["=", "!=", ">", ">=", "<", "<=", "~>"] | ||
|
||
def on_new_investigation | ||
gem_version_annotations.each do |comment| | ||
gem_versions = gem_versions(comment) | ||
|
||
if gem_versions.empty? | ||
message = format(MSG, versions: "empty version") | ||
add_offense(comment, message: message) | ||
break | ||
end | ||
|
||
invalid_versions = gem_versions.reject do |version| | ||
valid_version?(version) | ||
end | ||
|
||
unless invalid_versions.empty? | ||
message = format(MSG, versions: invalid_versions.map(&:strip).join(", ")) | ||
add_offense(comment, message: message) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def valid_version?(version_string) | ||
parts = version_string.strip.split(" ") | ||
operator, version = parts | ||
|
||
return false unless operator && parts | ||
return false unless VALID_OPERATORS.include?(operator) | ||
|
||
Gem::Version.correct?(version) | ||
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
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
78 changes: 78 additions & 0 deletions
78
spec/rubocop/cop/sorbet/rbi_versioning/valid_gem_version_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,78 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::ValidGemVersionAnnotations, :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 comment is a valid version annotation" do | ||
expect_no_offenses(<<~RUBY) | ||
# @version = 1.3.4-prerelease | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when comment uses AND version annotations" do | ||
expect_no_offenses(<<~RUBY) | ||
# @version > 1, < 3.5 | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when comment uses OR version annotations" do | ||
expect_no_offenses(<<~RUBY) | ||
# @version > 1.3.6 | ||
# @version <= 4 | ||
RUBY | ||
end | ||
|
||
it "registers an offense for an empty version annotation" do | ||
expect_offense(<<~RUBY) | ||
# @version | ||
^^^^^^^^^^ Invalid gem version(s) detected: empty version | ||
RUBY | ||
end | ||
|
||
it "registers an offense for an annotation with no operator" do | ||
expect_offense(<<~RUBY) | ||
# @version blah | ||
^^^^^^^^^^^^^^^ Invalid gem version(s) detected: blah | ||
RUBY | ||
end | ||
|
||
it "registers an offense when gem version is not formatted correctly" do | ||
egiurleo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect_offense(<<~RUBY) | ||
# @version = blah | ||
^^^^^^^^^^^^^^^^^ Invalid gem version(s) detected: = blah | ||
RUBY | ||
end | ||
|
||
it "registers an offense when one gem version out of the list is not formatted correctly" do | ||
expect_offense(<<~RUBY) | ||
# @version < 3.2, > 4, ~> five | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid gem version(s) detected: ~> five | ||
RUBY | ||
end | ||
|
||
it "registers an offense when one gem version is not formatted correctly in an OR" do | ||
expect_offense(<<~RUBY) | ||
# @version < 3.2, > 4 | ||
# @version ~> five | ||
^^^^^^^^^^^^^^^^^^ Invalid gem version(s) detected: ~> five | ||
RUBY | ||
end | ||
|
||
it "registers an offense for multiple incorrectly formatted versions" do | ||
expect_offense(<<~RUBY) | ||
# @version < 3.2, ~> five, = blah | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid gem version(s) detected: ~> five, = blah | ||
RUBY | ||
end | ||
|
||
it "registers an offense if operator is invalid" do | ||
expect_offense(<<~RUBY) | ||
# @version << 3.2 | ||
^^^^^^^^^^^^^^^^^ Invalid gem version(s) detected: << 3.2 | ||
RUBY | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this cop will be enabled in
rbi-central
. Have we considered having the default case not error to make the onboarding easier (both here and inrbi
logic)? Or do we want to manually add versions to existing annotations?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is only if someone writes
# @version
but then doesn't actually provide any version numbers! Any code without any comment or a different comment will be left alone :)