mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 10:33:54 +00:00
feat(unstable): allow bare specifier for builtin node module (#20728)
closes #20566
This commit is contained in:
parent
8d9fef3b89
commit
fb73eb1e9d
17 changed files with 319 additions and 57 deletions
|
@ -970,6 +970,8 @@ pub enum DenoDiagnostic {
|
|||
ResolutionError(deno_graph::ResolutionError),
|
||||
/// Invalid `node:` specifier.
|
||||
InvalidNodeSpecifier(ModuleSpecifier),
|
||||
/// Bare specifier is used for `node:` specifier
|
||||
BareNodeSpecifier(String),
|
||||
}
|
||||
|
||||
impl DenoDiagnostic {
|
||||
|
@ -1003,6 +1005,7 @@ impl DenoDiagnostic {
|
|||
}
|
||||
}
|
||||
Self::InvalidNodeSpecifier(_) => "resolver-error",
|
||||
Self::BareNodeSpecifier(_) => "import-node-prefix-missing",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1174,6 +1177,7 @@ impl DenoDiagnostic {
|
|||
.map(|specifier| json!({ "specifier": specifier }))
|
||||
),
|
||||
Self::InvalidNodeSpecifier(specifier) => (lsp::DiagnosticSeverity::ERROR, format!("Unknown Node built-in module: {}", specifier.path()), None),
|
||||
Self::BareNodeSpecifier(specifier) => (lsp::DiagnosticSeverity::WARNING, format!("\"{}\" is resolved to \"node:{}\". If you want to use a built-in Node module, add a \"node:\" prefix.", specifier, specifier), Some(json!({ "specifier": specifier }))),
|
||||
};
|
||||
lsp::Diagnostic {
|
||||
range: *range,
|
||||
|
@ -1189,6 +1193,7 @@ impl DenoDiagnostic {
|
|||
|
||||
fn diagnose_resolution(
|
||||
snapshot: &language_server::StateSnapshot,
|
||||
dependency_key: &str,
|
||||
resolution: &Resolution,
|
||||
is_dynamic: bool,
|
||||
maybe_assert_type: Option<&str>,
|
||||
|
@ -1253,6 +1258,20 @@ fn diagnose_resolution(
|
|||
if !deno_node::is_builtin_node_module(module_name) {
|
||||
diagnostics
|
||||
.push(DenoDiagnostic::InvalidNodeSpecifier(specifier.clone()));
|
||||
} else if module_name == dependency_key {
|
||||
let mut is_mapped = false;
|
||||
if let Some(import_map) = &snapshot.maybe_import_map {
|
||||
if let Resolution::Ok(resolved) = &resolution {
|
||||
if import_map.resolve(module_name, &resolved.specifier).is_ok() {
|
||||
is_mapped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// show diagnostics for bare node specifiers that aren't mapped by import map
|
||||
if !is_mapped {
|
||||
diagnostics
|
||||
.push(DenoDiagnostic::BareNodeSpecifier(module_name.to_string()));
|
||||
}
|
||||
} else if let Some(npm_resolver) = snapshot
|
||||
.npm
|
||||
.as_ref()
|
||||
|
@ -1329,6 +1348,7 @@ fn diagnose_dependency(
|
|||
diagnostics.extend(
|
||||
diagnose_resolution(
|
||||
snapshot,
|
||||
dependency_key,
|
||||
if dependency.maybe_code.is_none() {
|
||||
&dependency.maybe_type
|
||||
} else {
|
||||
|
@ -1362,6 +1382,7 @@ fn diagnose_dependency(
|
|||
diagnostics.extend(
|
||||
diagnose_resolution(
|
||||
snapshot,
|
||||
dependency_key,
|
||||
&dependency.maybe_type,
|
||||
dependency.is_dynamic,
|
||||
dependency.maybe_attribute_type.as_deref(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue