Skip to content

Commit

Permalink
Fix Npm version range #79 (#82)
Browse files Browse the repository at this point in the history
* Fix Npm version range #79

Reference: #79
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Support - in npm version ranges #83

Reference: #83
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Address review comments

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Use named arguments

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

* Remove space from split

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
  • Loading branch information
TG1999 authored Sep 12, 2022
1 parent 2282b61 commit db4ead6
Show file tree
Hide file tree
Showing 3 changed files with 5,426 additions and 28 deletions.
124 changes: 97 additions & 27 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,40 @@ def from_osv_v1(data, scheme):
"""


def get_allof_constraints(cls, clause):
"""
Return a list of VersionConstraint given an AllOf ``clause``.
"""
if not isinstance(clause, AllOf):
raise ValueError(f"Unknown clause type: {clause!r}")
allof_constraints = []
for constraint in clause.clauses:
comparator = cls.vers_by_native_comparators[constraint.operator]
version = cls.version_class(str(constraint.target))
constraint = VersionConstraint(comparator=comparator, version=version)
allof_constraints.append(constraint)
return allof_constraints


def get_npm_version_constraints_from_semver_npm_spec(string, cls):
"""
Return a VersionConstraint for the provided ``string``.
"""
spec = semantic_version.NpmSpec(string)
clause = spec.clause.simplify()
if isinstance(clause, (AnyOf, AllOf)):
anyof_constraints = []
if isinstance(clause, AnyOf):
for allof_clause in clause.clauses:
anyof_constraints.extend(get_allof_constraints(cls, allof_clause))
elif isinstance(clause, AllOf):
alloc = get_allof_constraints(cls, clause)
anyof_constraints.extend(alloc)
else:
raise ValueError(f"Unknown clause type: {spec!r}")
return anyof_constraints


class NpmVersionRange(VersionRange):
scheme = "npm"
version_class = versions.SemverVersion
Expand All @@ -245,35 +279,71 @@ def from_native(cls, string):

# an NpmSpec handles parsing of both the semver versions and node-semver
# ranges at once
spec = semantic_version.NpmSpec(string)

clause = spec.clause.simplify()
assert isinstance(clause, (AnyOf, AllOf))
anyof_constraints = []
if isinstance(clause, AnyOf):
for allof_clause in clause.clauses:
anyof_constraints.extend(get_allof_constraints(cls, allof_clause))
elif isinstance(clause, AllOf):
alloc = get_allof_constraints(cls, clause)
anyof_constraints.extend(alloc)
else:
raise ValueError(f"Unknown clause type: {spec!r}")

return cls(constraints=anyof_constraints)

if string == "*":
return cls(
constraints=[
VersionConstraint.from_string(string="*", version_class=cls.version_class)
]
)

def get_allof_constraints(cls, clause):
"""
Return a list of VersionConstraint given an AllOf ``clause``.
"""
assert isinstance(clause, AllOf)
allof_constraints = []
for constraint in clause.clauses:
comparator = cls.vers_by_native_comparators[constraint.operator]
version = cls.version_class(str(constraint.target))
constraint = VersionConstraint(comparator=comparator, version=version)
allof_constraints.append(constraint)
return allof_constraints
constraints = []
vrc = cls.version_class
# A constraint item can be a comparator or a version or a version with comparator
# If it's empty continue
# If it's in `vers_by_native_comparators`, append it with the comparator and continue
# If it's a version, make version constraint from the version and use the comparator from the previous item and make comparator empty
# If it's a version with comparator, use split_req to get version and comparator to form constraint and make comparator empty

for range in string.split("||"):
if " - " in range:
constraints.extend(
get_npm_version_constraints_from_semver_npm_spec(string=range, cls=cls)
)
continue
comparator = ""
for constraint in range.split():
cmp = "".join([comparator, constraint])
if cmp in cls.vers_by_native_comparators:
comparator = cls.vers_by_native_comparators[cmp]
continue
if comparator:
if constraint.endswith(".x"):
constraints.extend(
get_npm_version_constraints_from_semver_npm_spec(
string=constraint, cls=cls
)
)
else:
constraint = constraint.lstrip("vV")
constraints.append(
VersionConstraint(comparator=comparator, version=vrc(constraint))
)
else:
if (
constraint.endswith(".x")
or constraint.startswith("~")
or constraint.startswith("^")
):
constraints.extend(
get_npm_version_constraints_from_semver_npm_spec(
string=constraint, cls=cls
)
)
else:
comparator, version_constraint = split_req(
string=constraint,
comparators=cls.vers_by_native_comparators,
default="=",
)
version_constraint = version_constraint.lstrip("vV")
constraints.append(
VersionConstraint(
comparator=comparator, version=vrc(version_constraint)
)
)
comparator = ""
return cls(constraints=constraints)


class GemVersionRange(VersionRange):
Expand Down
Loading

0 comments on commit db4ead6

Please sign in to comment.