Class: Dependabot::NpmAndYarn::Version

Inherits:
Version
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/npm_and_yarn/version.rb

Constant Summary collapse

VERSION_TAGS =

These are possible npm versioning tags that can be used in place of a version. See docs.npmjs.com/cli/v10/commands/npm-dist-tag#purpose for more details.

T.let(
  [
    "alpha", # Alpha version, early testing phase
    "beta",         # Beta version, more stable than alpha
    "canary",       # Canary version, often used for cutting-edge builds
    "dev",          # Development version, ongoing development
    "experimental", # Experimental version, unstable and new features
    "latest",       # Latest stable version, used by npm to identify the current version of a package
    "legacy",       # Legacy version, older version maintained for compatibility
    "next",         # Next version, used by some projects to identify the upcoming version
    "nightly",      # Nightly build, daily builds often including latest changes
    "rc",           # Release candidate, potential final version
    "release",      # General release version
    "stable"        # Stable version, thoroughly tested and stable
  ].freeze.map(&:freeze),
  T::Array[String]
)
VERSION_PATTERN =
T.let(Gem::Version::VERSION_PATTERN + '(\+[0-9a-zA-Z\-.]+)?', String)
ANCHORED_VERSION_PATTERN =
/\A\s*(#{VERSION_PATTERN})?\s*\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dependabot/npm_and_yarn/version.rb', line 67

def initialize(version)
  version = clean_version(version)

  @version_string = T.let(version.to_s, String)

  @build_info = T.let(nil, T.nilable(String))

  version, @build_info = version.to_s.split("+") if version.to_s.include?("+")

  super(T.must(version))
end

Instance Attribute Details

#build_infoObject (readonly)

Returns the value of attribute build_info.



20
21
22
# File 'lib/dependabot/npm_and_yarn/version.rb', line 20

def build_info
  @build_info
end

Class Method Details

.correct?(version) ⇒ Boolean



46
47
48
49
50
51
52
# File 'lib/dependabot/npm_and_yarn/version.rb', line 46

def self.correct?(version)
  version = version.gsub(/^v/, "") if version.is_a?(String)

  return false if version.nil?

  version.to_s.match?(ANCHORED_VERSION_PATTERN)
end

.new(version) ⇒ Object



98
99
100
# File 'lib/dependabot/npm_and_yarn/version.rb', line 98

def self.new(version)
  T.cast(super, Dependabot::NpmAndYarn::Version)
end

.semver_for(version) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/dependabot/npm_and_yarn/version.rb', line 55

def self.semver_for(version)
  # The next two lines are to guard against improperly formatted
  # versions in a lockfile, such as an empty string or additional
  # characters. NPM/yarn fixes these when running an update, so we can
  # safely ignore these versions.
  return if version == ""
  return unless correct?(version)

  version
end

Instance Method Details

#backwards_compatible_with?(other) ⇒ Boolean



118
119
120
121
122
123
124
125
# File 'lib/dependabot/npm_and_yarn/version.rb', line 118

def backwards_compatible_with?(other)
  case major
  when 0
    self == other
  else
    major == other.major && minor >= other.minor
  end
end

#clean_version(version) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dependabot/npm_and_yarn/version.rb', line 80

def clean_version(version)
  # Check if version is a string before attempting to match
  if version.is_a?(String)
    # Matches @ followed by x.y.z (digits separated by dots)
    if (match = version.match(/@(\d+\.\d+\.\d+)/))
      version = match[1] # Just "4.5.3"

    # Extract version in case the output contains Corepack verbose data
    elsif version.include?("Corepack")
      version = T.must(T.must(version.tr("\n", " ").match(/(\d+\.\d+\.\d+)/))[-1])
    end
    version = version&.gsub(/^v/, "")
  end

  version
end

#inspectObject



133
134
135
# File 'lib/dependabot/npm_and_yarn/version.rb', line 133

def inspect
  "#<#{self.class} #{@version_string}>"
end

#majorObject



103
104
105
# File 'lib/dependabot/npm_and_yarn/version.rb', line 103

def major
  @major ||= T.let(segments[0].to_i, T.nilable(Integer))
end

#minorObject



108
109
110
# File 'lib/dependabot/npm_and_yarn/version.rb', line 108

def minor
  @minor ||= T.let(segments[1].to_i, T.nilable(Integer))
end

#patchObject



113
114
115
# File 'lib/dependabot/npm_and_yarn/version.rb', line 113

def patch
  @patch ||= T.let(segments[2].to_i, T.nilable(Integer))
end

#to_sObject



128
129
130
# File 'lib/dependabot/npm_and_yarn/version.rb', line 128

def to_s
  @version_string
end