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 =
/(?<=[a-zA-Z0-9*])\s+(?:&+\s+)?(?!\s*[|-])/
OR_SEPARATOR =
/(?<=[a-zA-Z0-9*])\s*\|+/
PATTERN_RAW =
"\\s*(#{quoted})?\\s*(#{version_pattern})\\s*".freeze
PATTERN =
/\A#{PATTERN_RAW}\z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Returns a new instance of Requirement.



55
56
57
58
59
60
61
# File 'lib/dependabot/npm_and_yarn/requirement.rb', line 55

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

  super(requirements)
end

Class Method Details

.parse(obj) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dependabot/npm_and_yarn/requirement.rb', line 25

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

.requirements_array(requirement_string) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dependabot/npm_and_yarn/requirement.rb', line 42

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