Class: RuboCop::Cop::Sorbet::ValidGemVersionAnnotations

Inherits:
Base
  • Object
show all
Includes:
GemVersionAnnotationHelper
Defined in:
lib/rubocop/cop/sorbet/rbi_versioning/valid_gem_version_annotations.rb

Overview

Checks that gem versions in RBI annotations are properly formatted per the Bundler gem specification.

Examples:

# bad
# @version > not a version number

# good
# @version = 1

# good
# @version > 1.2.3

# good
# @version <= 4.3-preview

Constant Summary collapse

MSG =
"Invalid gem version(s) detected: %<versions>s"
VALID_OPERATORS =
["=", "!=", ">", ">=", "<", "<=", "~>"]

Constants included from GemVersionAnnotationHelper

GemVersionAnnotationHelper::VERSION_PREFIX

Instance Method Summary collapse

Methods included from GemVersionAnnotationHelper

#gem_version_annotations

Instance Method Details

#on_new_investigationObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/sorbet/rbi_versioning/valid_gem_version_annotations.rb', line 27

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