Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(publish): add npm: suggestion for esm.sh specifiers #22343

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions cli/tools/registry/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_runtime::deno_fetch::reqwest;
use lsp_types::Url;
use serde::de::DeserializeOwned;

#[derive(serde::Deserialize)]
Expand Down Expand Up @@ -142,3 +143,16 @@ pub async fn get_package(
let response = client.get(&package_url).send().await?;
Ok(response)
}

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,
}
}
28 changes: 27 additions & 1 deletion cli/tools/registry/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;
use std::sync::Mutex;

use deno_ast::swc::common::util::take::Take;
use deno_ast::SourceTextInfo;
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
use deno_graph::FastCheckDiagnostic;
Expand Down Expand Up @@ -258,7 +259,32 @@ impl Diagnostic for PublishDiagnostic {
}

fn snippet_fixed(&self) -> Option<DiagnosticSnippet<'_>> {
None
match &self {
PublishDiagnostic::InvalidExternalImport { imported, .. } => {
match super::api::get_jsr_alternative(imported) {
Some(replacement) => {
let replacement = SourceTextInfo::new(replacement.into());
let start = replacement.line_start(0);
let end = replacement.line_end(0);
Some(DiagnosticSnippet {
source: DiagnosticSnippetSource::SourceTextInfo(Cow::Owned(
replacement,
)),
dsherret marked this conversation as resolved.
Show resolved Hide resolved
highlight: DiagnosticSnippetHighlight {
style: DiagnosticSnippetHighlightStyle::Hint,
range: DiagnosticSourceRange {
start: DiagnosticSourcePos::SourcePos(start),
end: DiagnosticSourcePos::SourcePos(end),
},
description: Some("try this specifier".into()),
},
})
}
None => None,
}
}
_ => None,
}
}

fn info(&self) -> Cow<'_, [Cow<'_, str>]> {
Expand Down