diff options
Diffstat (limited to 'cli/lsp')
-rw-r--r-- | cli/lsp/diagnostics.rs | 37 | ||||
-rw-r--r-- | cli/lsp/documents.rs | 52 | ||||
-rw-r--r-- | cli/lsp/registries.rs | 10 |
3 files changed, 66 insertions, 33 deletions
diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index 0bee08c1d..fd4142fd9 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -871,9 +871,9 @@ pub enum DenoDiagnostic { /// remapped to an import map import specifier. ImportMapRemap { from: String, to: String }, /// The import assertion type is incorrect. - InvalidAssertType(String), - /// A module requires an assertion type to be a valid import. - NoAssertType, + InvalidAttributeType(String), + /// A module requires an attribute type to be a valid import. + NoAttributeType, /// A remote module was not found in the cache. NoCache(ModuleSpecifier), /// A remote npm package reference was not found in the cache. @@ -897,8 +897,8 @@ impl DenoDiagnostic { match self { Self::DenoWarn(_) => "deno-warn", Self::ImportMapRemap { .. } => "import-map-remap", - Self::InvalidAssertType(_) => "invalid-assert-type", - Self::NoAssertType => "no-assert-type", + Self::InvalidAttributeType(_) => "invalid-attribute-type", + Self::NoAttributeType => "no-attribute-type", Self::NoCache(_) => "no-cache", Self::NoCacheNpm(_, _) => "no-cache-npm", Self::NoLocal(_) => "no-local", @@ -958,15 +958,15 @@ impl DenoDiagnostic { ..Default::default() } } - "no-assert-type" => lsp::CodeAction { - title: "Insert import assertion.".to_string(), + "no-attribute-type" => lsp::CodeAction { + title: "Insert import attribute.".to_string(), kind: Some(lsp::CodeActionKind::QUICKFIX), diagnostics: Some(vec![diagnostic.clone()]), edit: Some(lsp::WorkspaceEdit { changes: Some(HashMap::from([( specifier.clone(), vec![lsp::TextEdit { - new_text: " assert { type: \"json\" }".to_string(), + new_text: " with { type: \"json\" }".to_string(), range: lsp::Range { start: diagnostic.range.end, end: diagnostic.range.end, @@ -1069,7 +1069,7 @@ impl DenoDiagnostic { "import-map-remap" | "no-cache" | "no-cache-npm" - | "no-assert-type" + | "no-attribute-type" | "redirect" | "import-node-prefix-missing" ) @@ -1084,8 +1084,8 @@ impl DenoDiagnostic { let (severity, message, data) = match self { Self::DenoWarn(message) => (lsp::DiagnosticSeverity::WARNING, message.to_string(), None), Self::ImportMapRemap { from, to } => (lsp::DiagnosticSeverity::HINT, format!("The import specifier can be remapped to \"{to}\" which will resolve it via the active import map."), Some(json!({ "from": from, "to": to }))), - Self::InvalidAssertType(assert_type) => (lsp::DiagnosticSeverity::ERROR, format!("The module is a JSON module and expected an assertion type of \"json\". Instead got \"{assert_type}\"."), None), - Self::NoAssertType => (lsp::DiagnosticSeverity::ERROR, "The module is a JSON module and not being imported with an import assertion. Consider adding `assert { type: \"json\" }` to the import statement.".to_string(), None), + Self::InvalidAttributeType(assert_type) => (lsp::DiagnosticSeverity::ERROR, format!("The module is a JSON module and expected an attribute type of \"json\". Instead got \"{assert_type}\"."), None), + Self::NoAttributeType => (lsp::DiagnosticSeverity::ERROR, "The module is a JSON module and not being imported with an import attribute. Consider adding `with { type: \"json\" }` to the import statement.".to_string(), None), Self::NoCache(specifier) => (lsp::DiagnosticSeverity::ERROR, format!("Uncached or missing remote URL: {specifier}"), Some(json!({ "specifier": specifier }))), Self::NoCacheNpm(pkg_req, specifier) => (lsp::DiagnosticSeverity::ERROR, format!("Uncached or missing npm package: {}", pkg_req), Some(json!({ "specifier": specifier }))), Self::NoLocal(specifier) => (lsp::DiagnosticSeverity::ERROR, format!("Unable to load a local module: {specifier}\n Please check the file path."), None), @@ -1144,15 +1144,16 @@ fn diagnose_resolution( match maybe_assert_type { // The module has the correct assertion type, no diagnostic Some("json") => (), - // The dynamic import statement is missing an assertion type, which + // The dynamic import statement is missing an attribute type, which // we might not be able to statically detect, therefore we will // not provide a potentially incorrect diagnostic. None if is_dynamic => (), // The module has an incorrect assertion type, diagnostic - Some(assert_type) => diagnostics - .push(DenoDiagnostic::InvalidAssertType(assert_type.to_string())), - // The module is missing an assertion type, diagnostic - None => diagnostics.push(DenoDiagnostic::NoAssertType), + Some(assert_type) => diagnostics.push( + DenoDiagnostic::InvalidAttributeType(assert_type.to_string()), + ), + // The module is missing an attribute type, diagnostic + None => diagnostics.push(DenoDiagnostic::NoAttributeType), } } } else if let Ok(pkg_ref) = @@ -1249,7 +1250,7 @@ fn diagnose_dependency( &dependency.maybe_code }, dependency.is_dynamic, - dependency.maybe_assert_type.as_deref(), + dependency.maybe_attribute_type.as_deref(), ) .iter() .flat_map(|diag| { @@ -1278,7 +1279,7 @@ fn diagnose_dependency( snapshot, &dependency.maybe_type, dependency.is_dynamic, - dependency.maybe_assert_type.as_deref(), + dependency.maybe_attribute_type.as_deref(), ) .iter() .map(|diag| diag.to_lsp_diagnostic(&range)), diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index 9c6710714..388f9f2e5 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -32,6 +32,7 @@ use deno_ast::SourceTextInfo; use deno_core::error::custom_error; use deno_core::error::AnyError; use deno_core::futures::future; +use deno_core::futures::FutureExt; use deno_core::parking_lot::Mutex; use deno_core::url; use deno_core::ModuleSpecifier; @@ -45,7 +46,7 @@ use deno_runtime::deno_node::PackageJson; use deno_runtime::permissions::PermissionsContainer; use deno_semver::npm::NpmPackageReqReference; use deno_semver::package::PackageReq; -use indexmap1::IndexMap; +use indexmap::IndexMap; use lsp::Url; use once_cell::sync::Lazy; use package_json::PackageJsonDepsProvider; @@ -1551,24 +1552,53 @@ pub struct OpenDocumentsGraphLoader<'a> { pub open_docs: &'a HashMap<ModuleSpecifier, Document>, } -impl<'a> deno_graph::source::Loader for OpenDocumentsGraphLoader<'a> { - fn load( - &mut self, +impl<'a> OpenDocumentsGraphLoader<'a> { + fn load_from_docs( + &self, specifier: &ModuleSpecifier, - is_dynamic: bool, - ) -> deno_graph::source::LoadFuture { + ) -> Option<deno_graph::source::LoadFuture> { if specifier.scheme() == "file" { if let Some(doc) = self.open_docs.get(specifier) { - return Box::pin(future::ready(Ok(Some( - deno_graph::source::LoadResponse::Module { + return Some( + future::ready(Ok(Some(deno_graph::source::LoadResponse::Module { content: doc.content(), specifier: doc.specifier().clone(), maybe_headers: None, - }, - )))); + }))) + .boxed_local(), + ); } } - self.inner_loader.load(specifier, is_dynamic) + None + } +} + +impl<'a> deno_graph::source::Loader for OpenDocumentsGraphLoader<'a> { + fn registry_url(&self) -> &Url { + self.inner_loader.registry_url() + } + + fn load( + &mut self, + specifier: &ModuleSpecifier, + is_dynamic: bool, + cache_setting: deno_graph::source::CacheSetting, + ) -> deno_graph::source::LoadFuture { + match self.load_from_docs(specifier) { + Some(fut) => fut, + None => self.inner_loader.load(specifier, is_dynamic, cache_setting), + } + } + + fn cache_module_info( + &mut self, + specifier: &deno_ast::ModuleSpecifier, + source: &str, + module_info: &deno_graph::ModuleInfo, + ) { + self + .inner_loader + .cache_module_info(specifier, source, module_info) } } diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index d2044ae66..ea33219e8 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -15,6 +15,7 @@ use super::path_to_regex::Token; use crate::args::CacheSetting; use crate::cache::GlobalHttpCache; use crate::cache::HttpCache; +use crate::file_fetcher::FetchOptions; use crate::file_fetcher::FileFetcher; use crate::http_util::HttpClient; @@ -509,11 +510,12 @@ impl ModuleRegistry { ) -> Result<Vec<RegistryConfiguration>, AnyError> { let fetch_result = self .file_fetcher - .fetch_with_accept( + .fetch_with_options(FetchOptions { specifier, - PermissionsContainer::allow_all(), - Some("application/vnd.deno.reg.v2+json, application/vnd.deno.reg.v1+json;q=0.9, application/json;q=0.8"), - ) + permissions: PermissionsContainer::allow_all(), + maybe_accept: Some("application/vnd.deno.reg.v2+json, application/vnd.deno.reg.v1+json;q=0.9, application/json;q=0.8"), + maybe_cache_setting: None, + }) .await; // if there is an error fetching, we will cache an empty file, so that // subsequent requests they are just an empty doc which will error without |