1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

feat(publish): suggest importing jsr:@std/ for deno.land/std urls (#25046)

This commit is contained in:
David Sherret 2024-08-16 10:42:11 -04:00 committed by GitHub
parent 57cd2951f1
commit 4d1b263e91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 9 deletions

View file

@ -149,14 +149,62 @@ pub async fn get_package(
}
pub fn get_jsr_alternative(imported: &Url) -> Option<String> {
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\""),
);
}
}

View file

@ -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