diff --git a/cli/tools/registry/api.rs b/cli/tools/registry/api.rs index 519800660c..c382aa9ac8 100644 --- a/cli/tools/registry/api.rs +++ b/cli/tools/registry/api.rs @@ -149,14 +149,62 @@ pub async fn get_package( } pub fn get_jsr_alternative(imported: &Url) -> Option { - if !matches!(imported.host_str(), Some("esm.sh")) { - return None; - } - - let mut segments = imported.path_segments().unwrap(); - match segments.next() { - Some("gh") => None, - Some(module) => Some(format!("\"npm:{module}\"")), - None => None, + if matches!(imported.host_str(), Some("esm.sh")) { + let mut segments = imported.path_segments()?; + match segments.next()? { + "gh" => None, + module => Some(format!("\"npm:{module}\"")), + } + } else if imported.as_str().starts_with("https://deno.land/") { + let mut segments = imported.path_segments()?; + let maybe_std = segments.next()?; + if maybe_std != "std" && !maybe_std.starts_with("std@") { + return None; + } + let module = segments.next()?; + let export = segments + .next() + .filter(|s| *s != "mod.ts") + .map(|s| s.strip_suffix(".ts").unwrap_or(s).replace("_", "-")); + Some(format!( + "\"jsr:@std/{}@1{}\"", + module, + export.map(|s| format!("/{}", s)).unwrap_or_default() + )) + } else { + None + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_jsr_alternative() { + #[track_caller] + fn run_test(imported: &str, output: Option<&str>) { + let imported = Url::parse(imported).unwrap(); + let output = output.map(|s| s.to_string()); + assert_eq!(get_jsr_alternative(&imported), output); + } + + run_test("https://esm.sh/ts-morph", Some("\"npm:ts-morph\"")); + run_test( + "https://deno.land/std/path/mod.ts", + Some("\"jsr:@std/path@1\""), + ); + run_test( + "https://deno.land/std/path/join.ts", + Some("\"jsr:@std/path@1/join\""), + ); + run_test( + "https://deno.land/std@0.229.0/path/join.ts", + Some("\"jsr:@std/path@1/join\""), + ); + run_test( + "https://deno.land/std@0.229.0/path/something_underscore.ts", + Some("\"jsr:@std/path@1/something-underscore\""), + ); } } diff --git a/tests/specs/publish/prefer_fast_check_graph/main.out b/tests/specs/publish/prefer_fast_check_graph/main.out index 6c68dde8ac..64296206f0 100644 --- a/tests/specs/publish/prefer_fast_check_graph/main.out +++ b/tests/specs/publish/prefer_fast_check_graph/main.out @@ -9,6 +9,10 @@ error[invalid-external-import]: invalid import to a non-JSR 'https' specifier | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the specifier | = hint: replace this import with one from jsr or npm, or vendor the dependency into your package + | +1 | "jsr:@std/assert@1/assert" + | -------------------------- try this specifier + | info: the import was resolved to 'https://deno.land/std/assert/assert.ts' info: this specifier is not allowed to be imported on jsr