diff options
Diffstat (limited to 'cli/tools/registry/diagnostics.rs')
-rw-r--r-- | cli/tools/registry/diagnostics.rs | 175 |
1 files changed, 73 insertions, 102 deletions
diff --git a/cli/tools/registry/diagnostics.rs b/cli/tools/registry/diagnostics.rs index e7f947303..aeb5d61e2 100644 --- a/cli/tools/registry/diagnostics.rs +++ b/cli/tools/registry/diagnostics.rs @@ -1,28 +1,25 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. use std::borrow::Cow; -use std::fmt::Display; use std::path::PathBuf; use std::sync::Arc; use std::sync::Mutex; +use deno_ast::diagnostics::Diagnostic; +use deno_ast::diagnostics::DiagnosticLevel; +use deno_ast::diagnostics::DiagnosticLocation; +use deno_ast::diagnostics::DiagnosticSnippet; +use deno_ast::diagnostics::DiagnosticSnippetHighlight; +use deno_ast::diagnostics::DiagnosticSnippetHighlightStyle; +use deno_ast::diagnostics::DiagnosticSourcePos; +use deno_ast::diagnostics::DiagnosticSourceRange; 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; use lsp_types::Url; -use crate::cache::LazyGraphSourceParser; -use crate::diagnostics::Diagnostic; -use crate::diagnostics::DiagnosticLevel; -use crate::diagnostics::DiagnosticLocation; -use crate::diagnostics::DiagnosticSnippet; -use crate::diagnostics::DiagnosticSnippetHighlight; -use crate::diagnostics::DiagnosticSnippetHighlightStyle; -use crate::diagnostics::DiagnosticSnippetSource; -use crate::diagnostics::DiagnosticSourcePos; -use crate::diagnostics::DiagnosticSourceRange; -use crate::diagnostics::SourceTextParsedSourceStore; use crate::util::import_map::ImportMapUnfurlDiagnostic; #[derive(Clone, Default)] @@ -31,16 +28,12 @@ pub struct PublishDiagnosticsCollector { } impl PublishDiagnosticsCollector { - pub fn print_and_error( - &self, - sources: LazyGraphSourceParser, - ) -> Result<(), AnyError> { + pub fn print_and_error(&self) -> Result<(), AnyError> { let mut errors = 0; let mut has_zap_errors = false; let diagnostics = self.diagnostics.lock().unwrap().take(); - let sources = SourceTextParsedSourceStore(sources); for diagnostic in diagnostics { - eprint!("{}", diagnostic.display(&sources)); + eprint!("{}", diagnostic.display()); if matches!(diagnostic.level(), DiagnosticLevel::Error) { errors += 1; } @@ -90,6 +83,7 @@ pub enum PublishDiagnostic { InvalidExternalImport { kind: String, imported: Url, + text_info: SourceTextInfo, referrer: deno_graph::Range, }, } @@ -110,22 +104,22 @@ impl Diagnostic for PublishDiagnostic { } } - fn code(&self) -> impl Display + '_ { + fn code(&self) -> Cow<'_, str> { use PublishDiagnostic::*; match &self { FastCheck(diagnostic) => diagnostic.code(), - ImportMapUnfurl(diagnostic) => diagnostic.code(), - InvalidPath { .. } => "invalid-path", - DuplicatePath { .. } => "case-insensitive-duplicate-path", - UnsupportedFileType { .. } => "unsupported-file-type", - InvalidExternalImport { .. } => "invalid-external-import", + ImportMapUnfurl(diagnostic) => Cow::Borrowed(diagnostic.code()), + InvalidPath { .. } => Cow::Borrowed("invalid-path"), + DuplicatePath { .. } => Cow::Borrowed("case-insensitive-duplicate-path"), + UnsupportedFileType { .. } => Cow::Borrowed("unsupported-file-type"), + InvalidExternalImport { .. } => Cow::Borrowed("invalid-external-import"), } } - fn message(&self) -> impl Display + '_ { + fn message(&self) -> Cow<'_, str> { use PublishDiagnostic::*; match &self { - FastCheck(diagnostic) => Cow::Owned(diagnostic.to_string()) , + FastCheck(diagnostic) => diagnostic.message(), ImportMapUnfurl(diagnostic) => Cow::Borrowed(diagnostic.message()), InvalidPath { message, .. } => Cow::Borrowed(message.as_str()), DuplicatePath { .. } => { @@ -141,21 +135,15 @@ impl Diagnostic for PublishDiagnostic { fn location(&self) -> DiagnosticLocation { use PublishDiagnostic::*; match &self { - FastCheck(diagnostic) => match diagnostic.range() { - Some(range) => DiagnosticLocation::ModulePosition { - specifier: Cow::Borrowed(diagnostic.specifier()), - source_pos: DiagnosticSourcePos::SourcePos(range.range.start), - }, - None => DiagnosticLocation::Module { - specifier: Cow::Borrowed(diagnostic.specifier()), - }, - }, + FastCheck(diagnostic) => diagnostic.location(), ImportMapUnfurl(diagnostic) => match diagnostic { ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport { specifier, + text_info, range, } => DiagnosticLocation::ModulePosition { specifier: Cow::Borrowed(specifier), + text_info: Cow::Borrowed(text_info), source_pos: DiagnosticSourcePos::SourcePos(range.start), }, }, @@ -168,41 +156,31 @@ impl Diagnostic for PublishDiagnostic { UnsupportedFileType { specifier, .. } => DiagnosticLocation::Module { specifier: Cow::Borrowed(specifier), }, - InvalidExternalImport { referrer, .. } => { - DiagnosticLocation::ModulePosition { - specifier: Cow::Borrowed(&referrer.specifier), - source_pos: DiagnosticSourcePos::LineAndCol { - line: referrer.start.line, - column: referrer.start.character, - }, - } - } + InvalidExternalImport { + referrer, + text_info, + .. + } => DiagnosticLocation::ModulePosition { + specifier: Cow::Borrowed(&referrer.specifier), + text_info: Cow::Borrowed(text_info), + source_pos: DiagnosticSourcePos::LineAndCol { + line: referrer.start.line, + column: referrer.start.character, + }, + }, } } fn snippet(&self) -> Option<DiagnosticSnippet<'_>> { match &self { - PublishDiagnostic::FastCheck(diagnostic) => { - diagnostic.range().map(|range| DiagnosticSnippet { - source: DiagnosticSnippetSource::Specifier(Cow::Borrowed( - diagnostic.specifier(), - )), - highlight: DiagnosticSnippetHighlight { - style: DiagnosticSnippetHighlightStyle::Error, - range: DiagnosticSourceRange { - start: DiagnosticSourcePos::SourcePos(range.range.start), - end: DiagnosticSourcePos::SourcePos(range.range.end), - }, - description: diagnostic.range_description().map(Cow::Borrowed), - }, - }) - } + PublishDiagnostic::FastCheck(diagnostic) => diagnostic.snippet(), PublishDiagnostic::ImportMapUnfurl(diagnostic) => match diagnostic { ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport { - specifier, + text_info, range, + .. } => Some(DiagnosticSnippet { - source: DiagnosticSnippetSource::Specifier(Cow::Borrowed(specifier)), + source: Cow::Borrowed(text_info), highlight: DiagnosticSnippetHighlight { style: DiagnosticSnippetHighlightStyle::Warning, range: DiagnosticSourceRange { @@ -216,44 +194,44 @@ impl Diagnostic for PublishDiagnostic { PublishDiagnostic::InvalidPath { .. } => None, PublishDiagnostic::DuplicatePath { .. } => None, PublishDiagnostic::UnsupportedFileType { .. } => None, - PublishDiagnostic::InvalidExternalImport { referrer, .. } => { - Some(DiagnosticSnippet { - source: DiagnosticSnippetSource::Specifier(Cow::Borrowed( - &referrer.specifier, - )), - highlight: DiagnosticSnippetHighlight { - style: DiagnosticSnippetHighlightStyle::Error, - range: DiagnosticSourceRange { - start: DiagnosticSourcePos::LineAndCol { - line: referrer.start.line, - column: referrer.start.character, - }, - end: DiagnosticSourcePos::LineAndCol { - line: referrer.end.line, - column: referrer.end.character, - }, + PublishDiagnostic::InvalidExternalImport { + referrer, + text_info, + .. + } => Some(DiagnosticSnippet { + source: Cow::Borrowed(text_info), + highlight: DiagnosticSnippetHighlight { + style: DiagnosticSnippetHighlightStyle::Error, + range: DiagnosticSourceRange { + start: DiagnosticSourcePos::LineAndCol { + line: referrer.start.line, + column: referrer.start.character, + }, + end: DiagnosticSourcePos::LineAndCol { + line: referrer.end.line, + column: referrer.end.character, }, - description: Some("the specifier".into()), }, - }) - } + description: Some("the specifier".into()), + }, + }), } } - fn hint(&self) -> Option<impl Display + '_> { + fn hint(&self) -> Option<Cow<'_, str>> { match &self { - PublishDiagnostic::FastCheck(diagnostic) => Some(diagnostic.fix_hint()), + PublishDiagnostic::FastCheck(diagnostic) => diagnostic.hint(), PublishDiagnostic::ImportMapUnfurl(_) => None, PublishDiagnostic::InvalidPath { .. } => Some( - "rename or remove the file, or add it to 'publish.exclude' in the config file", + Cow::Borrowed("rename or remove the file, or add it to 'publish.exclude' in the config file"), ), PublishDiagnostic::DuplicatePath { .. } => Some( - "rename or remove the file", + Cow::Borrowed("rename or remove the file"), ), PublishDiagnostic::UnsupportedFileType { .. } => Some( - "remove the file, or add it to 'publish.exclude' in the config file", + Cow::Borrowed("remove the file, or add it to 'publish.exclude' in the config file"), ), - PublishDiagnostic::InvalidExternalImport { .. } => Some("replace this import with one from jsr or npm, or vendor the dependency into your package") + PublishDiagnostic::InvalidExternalImport { .. } => Some(Cow::Borrowed("replace this import with one from jsr or npm, or vendor the dependency into your package")) } } @@ -264,12 +242,7 @@ impl Diagnostic for PublishDiagnostic { fn info(&self) -> Cow<'_, [Cow<'_, str>]> { match &self { PublishDiagnostic::FastCheck(diagnostic) => { - let infos = diagnostic - .additional_info() - .iter() - .map(|s| Cow::Borrowed(*s)) - .collect(); - Cow::Owned(infos) + diagnostic.info() } PublishDiagnostic::ImportMapUnfurl(diagnostic) => match diagnostic { ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport { .. } => Cow::Borrowed(&[ @@ -296,25 +269,23 @@ impl Diagnostic for PublishDiagnostic { } } - fn docs_url(&self) -> Option<impl Display + '_> { + fn docs_url(&self) -> Option<Cow<'_, str>> { match &self { - PublishDiagnostic::FastCheck(diagnostic) => { - Some(format!("https://jsr.io/go/{}", diagnostic.code())) - } + PublishDiagnostic::FastCheck(diagnostic) => diagnostic.docs_url(), PublishDiagnostic::ImportMapUnfurl(diagnostic) => match diagnostic { ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport { .. } => None, }, PublishDiagnostic::InvalidPath { .. } => { - Some("https://jsr.io/go/invalid-path".to_owned()) - } - PublishDiagnostic::DuplicatePath { .. } => { - Some("https://jsr.io/go/case-insensitive-duplicate-path".to_owned()) + Some(Cow::Borrowed("https://jsr.io/go/invalid-path")) } + PublishDiagnostic::DuplicatePath { .. } => Some(Cow::Borrowed( + "https://jsr.io/go/case-insensitive-duplicate-path", + )), PublishDiagnostic::UnsupportedFileType { .. } => { - Some("https://jsr.io/go/unsupported-file-type".to_owned()) + Some(Cow::Borrowed("https://jsr.io/go/unsupported-file-type")) } PublishDiagnostic::InvalidExternalImport { .. } => { - Some("https://jsr.io/go/invalid-external-import".to_owned()) + Some(Cow::Borrowed("https://jsr.io/go/invalid-external-import")) } } } |