Class: Dependabot::NpmAndYarn::Requirement

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

Constant Summary collapse

AND_SEPARATOR =
T.let(/(?<=[a-zA-Z0-9*])\s+(?:&+\s+)?(?!\s*[|-])/, Regexp)
OR_SEPARATOR =
T.let(/(?<=[a-zA-Z0-9*])\s*\|+/, Regexp)
NAME_AT_VERSION_SPLIT =
T.let(/(?<=\w)@/, Regexp)
PATTERN_RAW =
T.let("\\s*(#{quoted})?\\s*(#{version_pattern})\\s*".freeze, String)
PATTERN =
T.let(/\A#{PATTERN_RAW}\z/, Regexp)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Returns a new instance of Requirement.



98
99
100
101
102
103
104
# File 'lib/dependabot/npm_and_yarn/requirement.rb', line 98

def initialize(*requirements)
  requirements = requirements.flatten
                             .flat_map { |req_string| T.must(req_string).split(",").map(&:strip) }
                             .flat_map { |req_string| convert_js_constraint_to_ruby_constraint(req_string) }

  super
end

Class Method Details

.parse(obj) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dependabot/npm_and_yarn/requirement.rb', line 33

def self.parse(obj)
  return ["=", nil] if obj.is_a?(String) && Version::VERSION_TAGS.include?(obj.strip)
  return ["=", NpmAndYarn::Version.new(obj.to_s)] if obj.is_a?(Gem::Version)

  unless (matches = PATTERN.match(obj.to_s))
    msg = "Illformed requirement [#{obj.inspect}]"
    raise BadRequirementError, msg
  end

  return DefaultRequirement if matches[1] == ">=" && matches[2] == "0"

  [matches[1] || "=", NpmAndYarn::Version.new(T.must(matches[2]))]
end

.parse_dep_string(dep_string) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dependabot/npm_and_yarn/requirement.rb', line 64

def self.parse_dep_string(dep_string)
  stripped = dep_string.strip
  return nil if stripped.empty?

  parts = stripped.split(NAME_AT_VERSION_SPLIT, 2)
  name = T.must(parts[0])
  constraint = parts[1]

  return nil if constraint.nil? || constraint.strip.empty?

  constraint = constraint.strip
  version = extract_version(constraint)

  {
    name: name,
    normalised_name: name,
    version: version,
    requirement: constraint,
    extras: nil
  }
end

.requirements_array(requirement_string) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dependabot/npm_and_yarn/requirement.rb', line 50

def self.requirements_array(requirement_string)
  return [new(nil)] if requirement_string.nil?

  # Removing parentheses is technically wrong but they are extremely
  # rarely used.
  # TODO: Handle complicated parenthesised requirements
  requirement_string = requirement_string.gsub(/[()]/, "")
  requirement_string.strip.split(OR_SEPARATOR).map do |req_string|
    requirements = req_string.strip.split(AND_SEPARATOR)
    new(requirements)
  end
end