Skip to content

Commit

Permalink
fix: do not error for specifier with version requirement and @ symbol…
Browse files Browse the repository at this point in the history
… in path (#40)
  • Loading branch information
dsherret authored Dec 9, 2024
1 parent d482456 commit d8d67b3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/jsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ impl JsrDepPackageReq {

#[cfg(test)]
mod test {
use crate::package::PackageReqReferenceInvalidWithVersionParseError;

use super::*;

#[test]
Expand Down Expand Up @@ -290,6 +292,18 @@ mod test {
assert_eq!(req_ref.req().version_req.to_string(), "^1.0.0");
assert_eq!(req_ref.sub_path(), Some("mod.ts"));
}
{
assert_eq!(
JsrPackageReqReference::from_str("jsr:@std/testing/bdd@1").unwrap_err(),
PackageReqReferenceParseError::InvalidPathWithVersion(Box::new(
PackageReqReferenceInvalidWithVersionParseError {
kind: PackageKind::Jsr,
current: "@std/testing/bdd@1".to_string(),
suggested: "@std/testing@1/bdd".to_string(),
}
)),
);
}
}

#[test]
Expand Down
16 changes: 15 additions & 1 deletion src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn parse_npm_version(text: &str) -> Result<Version, NpmVersionParseError> {
.map_err(|err| NpmVersionParseError { source: err })
}

#[derive(Error, Debug, Clone, JsError)]
#[derive(Error, Debug, Clone, JsError, PartialEq, Eq)]
#[class(type)]
#[error("Invalid version requirement")]
pub struct NpmVersionReqParseError {
Expand Down Expand Up @@ -1374,6 +1374,20 @@ mod tests {
)),
_ => unreachable!(),
}

// path with `@` shouldn't error
assert_eq!(
NpmPackageReqReference::from_str("npm:package@^5.0.0-beta.35/@some/path")
.unwrap(),
NpmPackageReqReference::new(PackageReqReference {
req: PackageReq {
name: "package".to_string(),
version_req: VersionReq::parse_from_specifier("^5.0.0-beta.35")
.unwrap(),
},
sub_path: Some("@some/path".to_string()),
}),
);
}

#[test]
Expand Down
30 changes: 16 additions & 14 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl PackageKind {
}
}

#[derive(Error, Debug, Clone, JsError)]
#[derive(Error, Debug, Clone, JsError, PartialEq, Eq)]
pub enum PackageReqReferenceParseError {
#[class(type)]
#[error("Not {} specifier", .0.scheme_with_colon())]
Expand All @@ -48,7 +48,7 @@ pub enum PackageReqReferenceParseError {
InvalidPathWithVersion(Box<PackageReqReferenceInvalidWithVersionParseError>),
}

#[derive(Error, Debug, Clone, JsError)]
#[derive(Error, Debug, Clone, JsError, PartialEq, Eq)]
#[class(type)]
#[error("Invalid package specifier '{specifier}'")]
pub struct PackageReqReferenceInvalidParseError {
Expand All @@ -57,9 +57,9 @@ pub struct PackageReqReferenceInvalidParseError {
pub source: PackageReqPartsParseError,
}

#[derive(Error, Debug, Clone, JsError)]
#[derive(Error, Debug, Clone, JsError, PartialEq, Eq)]
#[class(type)]
#[error("Invalid package specifier '{0}{1}'. Did you mean to write '{0}{2}'?", .kind.scheme_with_colon(), current, suggested)]
#[error("Invalid package specifier '{0}{1}'. Did you mean to write '{0}{2}'? If not, add a version requirement to the specifier.", .kind.scheme_with_colon(), current, suggested)]
pub struct PackageReqReferenceInvalidWithVersionParseError {
pub kind: PackageKind,
pub current: String,
Expand Down Expand Up @@ -109,15 +109,17 @@ impl PackageReqReference {
};

if let Some(sub_path) = &sub_path {
if let Some(at_index) = sub_path.rfind('@') {
let (new_sub_path, version) = sub_path.split_at(at_index);
return Err(PackageReqReferenceParseError::InvalidPathWithVersion(
Box::new(PackageReqReferenceInvalidWithVersionParseError {
kind,
current: format!("{req}/{sub_path}"),
suggested: format!("{req}{version}/{new_sub_path}"),
}),
));
if req.version_req.version_text() == "*" {
if let Some(at_index) = sub_path.rfind('@') {
let (new_sub_path, version) = sub_path.split_at(at_index);
return Err(PackageReqReferenceParseError::InvalidPathWithVersion(
Box::new(PackageReqReferenceInvalidWithVersionParseError {
kind,
current: format!("{req}/{sub_path}"),
suggested: format!("{req}{version}/{new_sub_path}"),
}),
));
}
}
}

Expand All @@ -135,7 +137,7 @@ impl std::fmt::Display for PackageReqReference {
}
}

#[derive(Error, Debug, Clone, JsError)]
#[derive(Error, Debug, Clone, JsError, PartialEq, Eq)]
pub enum PackageReqPartsParseError {
#[class(type)]
#[error("Did not contain a package name")]
Expand Down
2 changes: 1 addition & 1 deletion src/specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::VersionReq;

use crate::is_valid_tag;

#[derive(Error, Debug, Clone, deno_error::JsError)]
#[derive(Error, Debug, Clone, deno_error::JsError, PartialEq, Eq)]
#[class(type)]
#[error("Invalid specifier version requirement")]
pub struct VersionReqSpecifierParseError {
Expand Down

0 comments on commit d8d67b3

Please sign in to comment.