2024-01-23 10:37:43 -05:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std ::borrow ::Cow ;
2024-01-24 16:24:52 -05:00
use std ::path ::PathBuf ;
2024-01-23 10:37:43 -05:00
use std ::sync ::Arc ;
use std ::sync ::Mutex ;
2024-02-08 20:40:26 -05:00
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 ;
2024-01-23 10:37:43 -05:00
use deno_ast ::swc ::common ::util ::take ::Take ;
2024-07-17 23:06:30 -04:00
use deno_ast ::ParseDiagnostic ;
2024-02-29 06:54:57 -05:00
use deno_ast ::SourcePos ;
2024-05-27 21:35:08 -04:00
use deno_ast ::SourceRange ;
2024-02-29 06:54:57 -05:00
use deno_ast ::SourceRanged ;
2024-02-08 20:40:26 -05:00
use deno_ast ::SourceTextInfo ;
2024-01-23 10:37:43 -05:00
use deno_core ::anyhow ::anyhow ;
use deno_core ::error ::AnyError ;
use deno_graph ::FastCheckDiagnostic ;
2024-05-14 10:30:09 -04:00
use deno_semver ::Version ;
2024-01-24 16:24:52 -05:00
use lsp_types ::Url ;
2024-01-23 10:37:43 -05:00
2024-02-27 10:13:16 -05:00
use super ::unfurl ::SpecifierUnfurlerDiagnostic ;
2024-01-23 10:37:43 -05:00
#[ derive(Clone, Default) ]
pub struct PublishDiagnosticsCollector {
diagnostics : Arc < Mutex < Vec < PublishDiagnostic > > > ,
}
impl PublishDiagnosticsCollector {
2024-02-08 20:40:26 -05:00
pub fn print_and_error ( & self ) -> Result < ( ) , AnyError > {
2024-01-23 10:37:43 -05:00
let mut errors = 0 ;
2024-02-19 10:28:41 -05:00
let mut has_slow_types_errors = false ;
2024-02-29 06:54:57 -05:00
let mut diagnostics = self . diagnostics . lock ( ) . unwrap ( ) . take ( ) ;
diagnostics . sort_by_cached_key ( | d | d . sorting_key ( ) ) ;
2024-01-23 10:37:43 -05:00
for diagnostic in diagnostics {
2024-06-05 11:04:16 -04:00
log ::error! ( " {} " , diagnostic . display ( ) ) ;
2024-01-23 10:37:43 -05:00
if matches! ( diagnostic . level ( ) , DiagnosticLevel ::Error ) {
errors + = 1 ;
}
2024-01-31 21:16:52 -05:00
if matches! ( diagnostic , PublishDiagnostic ::FastCheck ( .. ) ) {
2024-02-19 10:28:41 -05:00
has_slow_types_errors = true ;
2024-01-31 21:16:52 -05:00
}
2024-01-23 10:37:43 -05:00
}
if errors > 0 {
2024-02-19 10:28:41 -05:00
if has_slow_types_errors {
2024-05-08 22:45:06 -04:00
log ::error! (
2024-02-19 10:28:41 -05:00
" This package contains errors for slow types. Fixing these errors will: \n "
2024-01-31 21:16:52 -05:00
) ;
2024-05-08 22:45:06 -04:00
log ::error! (
2024-02-19 10:28:41 -05:00
" 1. Significantly improve your package users' type checking performance. "
) ;
2024-05-08 22:45:06 -04:00
log ::error! ( " 2. Improve the automatic documentation generation. " ) ;
log ::error! ( " 3. Enable automatic .d.ts generation for Node.js. " ) ;
log ::error! (
2024-02-19 10:28:41 -05:00
" \n Don't want to bother? You can choose to skip this step by "
) ;
2024-05-08 22:45:06 -04:00
log ::error! ( " providing the --allow-slow-types flag. \n " ) ;
2024-01-31 21:16:52 -05:00
}
2024-01-23 10:37:43 -05:00
Err ( anyhow! (
" Found {} problem{} " ,
errors ,
if errors = = 1 { " " } else { " s " }
) )
} else {
Ok ( ( ) )
}
}
pub fn push ( & self , diagnostic : PublishDiagnostic ) {
self . diagnostics . lock ( ) . unwrap ( ) . push ( diagnostic ) ;
}
}
pub enum PublishDiagnostic {
2024-01-24 08:49:33 -05:00
FastCheck ( FastCheckDiagnostic ) ,
2024-02-27 10:13:16 -05:00
SpecifierUnfurl ( SpecifierUnfurlerDiagnostic ) ,
2024-01-24 16:59:18 -05:00
InvalidPath {
path : PathBuf ,
message : String ,
} ,
DuplicatePath {
path : PathBuf ,
} ,
UnsupportedFileType {
specifier : Url ,
kind : String ,
} ,
InvalidExternalImport {
kind : String ,
imported : Url ,
2024-02-08 20:40:26 -05:00
text_info : SourceTextInfo ,
2024-01-24 16:59:18 -05:00
referrer : deno_graph ::Range ,
} ,
2024-02-29 06:54:57 -05:00
UnsupportedJsxTsx {
specifier : Url ,
} ,
2024-03-21 17:42:23 -04:00
ExcludedModule {
specifier : Url ,
} ,
2024-05-14 10:30:09 -04:00
MissingConstraint {
specifier : Url ,
specifier_text : String ,
resolved_version : Option < Version > ,
text_info : SourceTextInfo ,
referrer : deno_graph ::Range ,
} ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives {
specifier : Url ,
text_info : SourceTextInfo ,
range : SourceRange ,
} ,
2024-07-17 23:06:30 -04:00
SyntaxError ( ParseDiagnostic ) ,
2024-07-22 15:46:37 -04:00
MissingLicense {
/// This only exists because diagnostics require a location.
expected_path : PathBuf ,
} ,
2024-02-29 06:54:57 -05:00
}
impl PublishDiagnostic {
fn sorting_key ( & self ) -> ( String , String , Option < SourcePos > ) {
let loc = self . location ( ) ;
let ( specifier , source_pos ) = match loc {
DiagnosticLocation ::Module { specifier } = > ( specifier . to_string ( ) , None ) ,
DiagnosticLocation ::Path { path } = > ( path . display ( ) . to_string ( ) , None ) ,
DiagnosticLocation ::ModulePosition {
specifier ,
source_pos ,
text_info ,
} = > (
specifier . to_string ( ) ,
Some ( match source_pos {
DiagnosticSourcePos ::SourcePos ( s ) = > s ,
DiagnosticSourcePos ::ByteIndex ( index ) = > {
text_info . range ( ) . start ( ) + index
}
DiagnosticSourcePos ::LineAndCol { line , column } = > {
text_info . line_start ( line ) + column
}
} ) ,
) ,
} ;
( self . code ( ) . to_string ( ) , specifier , source_pos )
}
2024-01-23 10:37:43 -05:00
}
impl Diagnostic for PublishDiagnostic {
fn level ( & self ) -> DiagnosticLevel {
2024-01-24 16:59:18 -05:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match self {
2024-01-24 16:59:18 -05:00
FastCheck ( FastCheckDiagnostic ::UnsupportedJavaScriptEntrypoint {
..
} ) = > DiagnosticLevel ::Warning ,
FastCheck ( _ ) = > DiagnosticLevel ::Error ,
2024-02-27 10:13:16 -05:00
SpecifierUnfurl ( _ ) = > DiagnosticLevel ::Warning ,
2024-01-24 16:59:18 -05:00
InvalidPath { .. } = > DiagnosticLevel ::Error ,
DuplicatePath { .. } = > DiagnosticLevel ::Error ,
UnsupportedFileType { .. } = > DiagnosticLevel ::Warning ,
InvalidExternalImport { .. } = > DiagnosticLevel ::Error ,
2024-02-29 06:54:57 -05:00
UnsupportedJsxTsx { .. } = > DiagnosticLevel ::Warning ,
2024-03-21 17:42:23 -04:00
ExcludedModule { .. } = > DiagnosticLevel ::Error ,
2024-05-14 10:30:09 -04:00
MissingConstraint { .. } = > DiagnosticLevel ::Error ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives { .. } = > DiagnosticLevel ::Error ,
2024-07-17 23:06:30 -04:00
SyntaxError { .. } = > DiagnosticLevel ::Error ,
2024-07-22 15:46:37 -04:00
// todo(#24676): make this an error in Deno 1.46
MissingLicense { .. } = > DiagnosticLevel ::Warning ,
2024-01-23 10:37:43 -05:00
}
}
2024-02-08 20:40:26 -05:00
fn code ( & self ) -> Cow < '_ , str > {
2024-01-24 16:59:18 -05:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match & self {
2024-01-24 16:59:18 -05:00
FastCheck ( diagnostic ) = > diagnostic . code ( ) ,
2024-02-27 10:13:16 -05:00
SpecifierUnfurl ( diagnostic ) = > Cow ::Borrowed ( diagnostic . code ( ) ) ,
2024-02-08 20:40:26 -05:00
InvalidPath { .. } = > Cow ::Borrowed ( " invalid-path " ) ,
DuplicatePath { .. } = > Cow ::Borrowed ( " case-insensitive-duplicate-path " ) ,
UnsupportedFileType { .. } = > Cow ::Borrowed ( " unsupported-file-type " ) ,
InvalidExternalImport { .. } = > Cow ::Borrowed ( " invalid-external-import " ) ,
2024-02-29 06:54:57 -05:00
UnsupportedJsxTsx { .. } = > Cow ::Borrowed ( " unsupported-jsx-tsx " ) ,
2024-03-21 17:42:23 -04:00
ExcludedModule { .. } = > Cow ::Borrowed ( " excluded-module " ) ,
2024-05-14 10:30:09 -04:00
MissingConstraint { .. } = > Cow ::Borrowed ( " missing-constraint " ) ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives { .. } = > {
Cow ::Borrowed ( " banned-triple-slash-directives " )
}
2024-07-17 23:06:30 -04:00
SyntaxError { .. } = > Cow ::Borrowed ( " syntax-error " ) ,
2024-07-22 15:46:37 -04:00
MissingLicense { .. } = > Cow ::Borrowed ( " missing-license " ) ,
2024-01-23 10:37:43 -05:00
}
}
2024-02-08 20:40:26 -05:00
fn message ( & self ) -> Cow < '_ , str > {
2024-01-24 16:59:18 -05:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match & self {
2024-02-08 20:40:26 -05:00
FastCheck ( diagnostic ) = > diagnostic . message ( ) ,
2024-02-27 10:13:16 -05:00
SpecifierUnfurl ( diagnostic ) = > Cow ::Borrowed ( diagnostic . message ( ) ) ,
2024-01-24 16:59:18 -05:00
InvalidPath { message , .. } = > Cow ::Borrowed ( message . as_str ( ) ) ,
DuplicatePath { .. } = > {
2024-01-24 16:24:52 -05:00
Cow ::Borrowed ( " package path is a case insensitive duplicate of another path in the package " )
}
2024-01-24 16:59:18 -05:00
UnsupportedFileType { kind , .. } = > {
Cow ::Owned ( format! ( " unsupported file type ' {kind} ' " ) )
2024-01-24 16:24:52 -05:00
}
2024-01-24 16:59:18 -05:00
InvalidExternalImport { kind , .. } = > Cow ::Owned ( format! ( " invalid import to a {kind} specifier " ) ) ,
2024-02-29 06:54:57 -05:00
UnsupportedJsxTsx { .. } = > Cow ::Borrowed ( " JSX and TSX files are currently not supported " ) ,
2024-03-21 17:42:23 -04:00
ExcludedModule { .. } = > Cow ::Borrowed ( " module in package's module graph was excluded from publishing " ) ,
2024-05-14 10:30:09 -04:00
MissingConstraint { specifier , .. } = > Cow ::Owned ( format! ( " specifier ' {} ' is missing a version constraint " , specifier ) ) ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives { .. } = > Cow ::Borrowed ( " triple slash directives that modify globals are not allowed " ) ,
2024-07-17 23:06:30 -04:00
SyntaxError ( diagnostic ) = > diagnostic . message ( ) ,
2024-07-22 15:46:37 -04:00
MissingLicense { .. } = > Cow ::Borrowed ( " missing license file " ) ,
2024-01-23 10:37:43 -05:00
}
}
fn location ( & self ) -> DiagnosticLocation {
2024-05-14 10:30:09 -04:00
fn from_referrer_range < ' a > (
referrer : & ' a deno_graph ::Range ,
text_info : & ' a SourceTextInfo ,
) -> DiagnosticLocation < ' a > {
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 ,
} ,
}
}
2024-01-24 16:59:18 -05:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match & self {
2024-02-08 20:40:26 -05:00
FastCheck ( diagnostic ) = > diagnostic . location ( ) ,
2024-02-27 10:13:16 -05:00
SpecifierUnfurl ( diagnostic ) = > match diagnostic {
SpecifierUnfurlerDiagnostic ::UnanalyzableDynamicImport {
2024-01-24 08:49:33 -05:00
specifier ,
2024-02-08 20:40:26 -05:00
text_info ,
2024-01-24 08:49:33 -05:00
range ,
2024-01-24 16:24:52 -05:00
} = > DiagnosticLocation ::ModulePosition {
2024-01-24 08:49:33 -05:00
specifier : Cow ::Borrowed ( specifier ) ,
2024-02-08 20:40:26 -05:00
text_info : Cow ::Borrowed ( text_info ) ,
2024-01-24 08:49:33 -05:00
source_pos : DiagnosticSourcePos ::SourcePos ( range . start ) ,
} ,
} ,
2024-01-24 16:59:18 -05:00
InvalidPath { path , .. } = > {
2024-01-24 16:24:52 -05:00
DiagnosticLocation ::Path { path : path . clone ( ) }
}
2024-01-24 16:59:18 -05:00
DuplicatePath { path , .. } = > {
2024-01-24 16:24:52 -05:00
DiagnosticLocation ::Path { path : path . clone ( ) }
}
2024-01-24 16:59:18 -05:00
UnsupportedFileType { specifier , .. } = > DiagnosticLocation ::Module {
specifier : Cow ::Borrowed ( specifier ) ,
} ,
2024-02-08 20:40:26 -05:00
InvalidExternalImport {
referrer ,
text_info ,
..
2024-05-14 10:30:09 -04:00
} = > from_referrer_range ( referrer , text_info ) ,
2024-02-29 06:54:57 -05:00
UnsupportedJsxTsx { specifier } = > DiagnosticLocation ::Module {
specifier : Cow ::Borrowed ( specifier ) ,
} ,
2024-03-21 17:42:23 -04:00
ExcludedModule { specifier } = > DiagnosticLocation ::Module {
specifier : Cow ::Borrowed ( specifier ) ,
} ,
2024-05-14 10:30:09 -04:00
MissingConstraint {
referrer ,
text_info ,
..
} = > from_referrer_range ( referrer , text_info ) ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives {
specifier ,
range ,
text_info ,
} = > DiagnosticLocation ::ModulePosition {
specifier : Cow ::Borrowed ( specifier ) ,
source_pos : DiagnosticSourcePos ::SourcePos ( range . start ) ,
text_info : Cow ::Borrowed ( text_info ) ,
} ,
2024-07-17 23:06:30 -04:00
SyntaxError ( diagnostic ) = > diagnostic . location ( ) ,
2024-07-22 15:46:37 -04:00
MissingLicense { expected_path } = > DiagnosticLocation ::Path {
path : expected_path . clone ( ) ,
} ,
2024-01-23 10:37:43 -05:00
}
}
fn snippet ( & self ) -> Option < DiagnosticSnippet < '_ > > {
2024-05-14 10:30:09 -04:00
fn from_range < ' a > (
text_info : & ' a SourceTextInfo ,
referrer : & ' a deno_graph ::Range ,
) -> Option < DiagnosticSnippet < ' a > > {
if referrer . start . line = = 0 & & referrer . start . character = = 0 {
return None ; // no range, probably a jsxImportSource import
}
Some ( DiagnosticSnippet {
source : Cow ::Borrowed ( text_info ) ,
2024-06-05 11:04:16 -04:00
highlights : vec ! [ DiagnosticSnippetHighlight {
2024-05-14 10:30:09 -04:00
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 ( ) ) ,
2024-06-05 11:04:16 -04:00
} ] ,
2024-05-14 10:30:09 -04:00
} )
}
2024-03-21 17:42:23 -04:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match & self {
2024-03-21 17:42:23 -04:00
FastCheck ( diagnostic ) = > diagnostic . snippet ( ) ,
SpecifierUnfurl ( diagnostic ) = > match diagnostic {
2024-02-27 10:13:16 -05:00
SpecifierUnfurlerDiagnostic ::UnanalyzableDynamicImport {
2024-02-08 20:40:26 -05:00
text_info ,
2024-01-24 08:49:33 -05:00
range ,
2024-02-08 20:40:26 -05:00
..
2024-01-24 08:49:33 -05:00
} = > Some ( DiagnosticSnippet {
2024-02-08 20:40:26 -05:00
source : Cow ::Borrowed ( text_info ) ,
2024-06-05 11:04:16 -04:00
highlights : vec ! [ DiagnosticSnippetHighlight {
2024-01-24 08:49:33 -05:00
style : DiagnosticSnippetHighlightStyle ::Warning ,
range : DiagnosticSourceRange {
start : DiagnosticSourcePos ::SourcePos ( range . start ) ,
end : DiagnosticSourcePos ::SourcePos ( range . end ) ,
} ,
description : Some ( " the unanalyzable dynamic import " . into ( ) ) ,
2024-06-05 11:04:16 -04:00
} ] ,
2024-01-24 08:49:33 -05:00
} ) ,
} ,
2024-03-21 17:42:23 -04:00
InvalidPath { .. } = > None ,
DuplicatePath { .. } = > None ,
UnsupportedFileType { .. } = > None ,
InvalidExternalImport {
2024-02-08 20:40:26 -05:00
referrer ,
text_info ,
..
2024-05-14 10:30:09 -04:00
} = > from_range ( text_info , referrer ) ,
2024-03-21 17:42:23 -04:00
UnsupportedJsxTsx { .. } = > None ,
ExcludedModule { .. } = > None ,
2024-05-14 10:30:09 -04:00
MissingConstraint {
referrer ,
text_info ,
..
} = > from_range ( text_info , referrer ) ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives {
range , text_info , ..
} = > Some ( DiagnosticSnippet {
source : Cow ::Borrowed ( text_info ) ,
2024-06-05 11:04:16 -04:00
highlights : vec ! [ DiagnosticSnippetHighlight {
2024-05-27 21:35:08 -04:00
style : DiagnosticSnippetHighlightStyle ::Error ,
range : DiagnosticSourceRange {
start : DiagnosticSourcePos ::SourcePos ( range . start ) ,
end : DiagnosticSourcePos ::SourcePos ( range . end ) ,
} ,
description : Some ( " the triple slash directive " . into ( ) ) ,
2024-06-05 11:04:16 -04:00
} ] ,
2024-05-27 21:35:08 -04:00
} ) ,
2024-07-17 23:06:30 -04:00
SyntaxError ( diagnostic ) = > diagnostic . snippet ( ) ,
2024-07-22 15:46:37 -04:00
MissingLicense { .. } = > None ,
2024-01-23 10:37:43 -05:00
}
}
2024-02-08 20:40:26 -05:00
fn hint ( & self ) -> Option < Cow < '_ , str > > {
2024-03-21 17:42:23 -04:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match & self {
2024-03-21 17:42:23 -04:00
FastCheck ( diagnostic ) = > diagnostic . hint ( ) ,
SpecifierUnfurl ( _ ) = > None ,
InvalidPath { .. } = > Some (
2024-02-08 20:40:26 -05:00
Cow ::Borrowed ( " rename or remove the file, or add it to 'publish.exclude' in the config file " ) ,
2024-01-24 16:24:52 -05:00
) ,
2024-03-21 17:42:23 -04:00
DuplicatePath { .. } = > Some (
2024-02-08 20:40:26 -05:00
Cow ::Borrowed ( " rename or remove the file " ) ,
2024-01-24 16:24:52 -05:00
) ,
2024-03-21 17:42:23 -04:00
UnsupportedFileType { .. } = > Some (
2024-02-08 20:40:26 -05:00
Cow ::Borrowed ( " remove the file, or add it to 'publish.exclude' in the config file " ) ,
2024-01-24 16:24:52 -05:00
) ,
2024-03-21 17:42:23 -04:00
InvalidExternalImport { .. } = > Some ( Cow ::Borrowed ( " replace this import with one from jsr or npm, or vendor the dependency into your package " ) ) ,
UnsupportedJsxTsx { .. } = > None ,
ExcludedModule { .. } = > Some (
2024-04-24 14:52:05 -04:00
Cow ::Borrowed ( " remove the module from 'exclude' and/or 'publish.exclude' in the config file or use 'publish.exclude' with a negative glob to unexclude from gitignore " ) ,
2024-03-21 17:42:23 -04:00
) ,
2024-05-14 10:30:09 -04:00
MissingConstraint { specifier_text , .. } = > {
Some ( Cow ::Borrowed ( if specifier_text . starts_with ( " jsr: " ) | | specifier_text . starts_with ( " npm: " ) {
" specify a version constraint for the specifier "
} else {
" specify a version constraint for the specifier in the import map "
} ) )
} ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives { .. } = > Some (
Cow ::Borrowed ( " remove the triple slash directive " ) ,
) ,
2024-07-17 23:06:30 -04:00
SyntaxError ( diagnostic ) = > diagnostic . hint ( ) ,
2024-07-22 15:46:37 -04:00
MissingLicense { .. } = > Some (
Cow ::Borrowed ( " add a LICENSE file to the package and ensure it is not ignored from being published " ) ,
) ,
2024-01-23 10:37:43 -05:00
}
}
fn snippet_fixed ( & self ) -> Option < DiagnosticSnippet < '_ > > {
2024-03-21 17:42:23 -04:00
use PublishDiagnostic ::* ;
2024-03-03 23:25:28 -05:00
match & self {
2024-03-21 17:42:23 -04:00
InvalidExternalImport { imported , .. } = > {
2024-03-03 23:25:28 -05:00
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 : Cow ::Owned ( replacement ) ,
2024-06-05 11:04:16 -04:00
highlights : vec ! [ DiagnosticSnippetHighlight {
2024-03-03 23:25:28 -05:00
style : DiagnosticSnippetHighlightStyle ::Hint ,
range : DiagnosticSourceRange {
start : DiagnosticSourcePos ::SourcePos ( start ) ,
end : DiagnosticSourcePos ::SourcePos ( end ) ,
} ,
description : Some ( " try this specifier " . into ( ) ) ,
2024-06-05 11:04:16 -04:00
} ] ,
2024-03-03 23:25:28 -05:00
} )
}
None = > None ,
}
}
2024-07-17 23:06:30 -04:00
SyntaxError ( diagnostic ) = > diagnostic . snippet_fixed ( ) ,
FastCheck ( _ )
| SpecifierUnfurl ( _ )
| InvalidPath { .. }
| DuplicatePath { .. }
| UnsupportedFileType { .. }
| UnsupportedJsxTsx { .. }
| ExcludedModule { .. }
| MissingConstraint { .. }
2024-07-22 15:46:37 -04:00
| BannedTripleSlashDirectives { .. }
| MissingLicense { .. } = > None ,
2024-03-03 23:25:28 -05:00
}
2024-01-23 10:37:43 -05:00
}
fn info ( & self ) -> Cow < '_ , [ Cow < '_ , str > ] > {
2024-03-21 17:42:23 -04:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match & self {
2024-03-21 17:42:23 -04:00
FastCheck ( diagnostic ) = > {
2024-02-08 20:40:26 -05:00
diagnostic . info ( )
2024-01-23 10:37:43 -05:00
}
2024-03-21 17:42:23 -04:00
SpecifierUnfurl ( diagnostic ) = > match diagnostic {
2024-02-27 10:13:16 -05:00
SpecifierUnfurlerDiagnostic ::UnanalyzableDynamicImport { .. } = > Cow ::Borrowed ( & [
Cow ::Borrowed ( " after publishing this package, imports from the local import map / package.json do not work " ) ,
2024-01-24 08:49:33 -05:00
Cow ::Borrowed ( " dynamic imports that can not be analyzed at publish time will not be rewritten automatically " ) ,
2024-02-27 10:13:16 -05:00
Cow ::Borrowed ( " make sure the dynamic import is resolvable at runtime without an import map / package.json " )
2024-01-24 08:49:33 -05:00
] ) ,
} ,
2024-03-21 17:42:23 -04:00
InvalidPath { .. } = > Cow ::Borrowed ( & [
2024-01-24 16:24:52 -05:00
Cow ::Borrowed ( " to portably support all platforms, including windows, the allowed characters in package paths are limited " ) ,
] ) ,
2024-03-21 17:42:23 -04:00
DuplicatePath { .. } = > Cow ::Borrowed ( & [
2024-01-24 16:24:52 -05:00
Cow ::Borrowed ( " to support case insensitive file systems, no two package paths may differ only by case " ) ,
] ) ,
2024-03-21 17:42:23 -04:00
UnsupportedFileType { .. } = > Cow ::Borrowed ( & [
2024-01-24 16:24:52 -05:00
Cow ::Borrowed ( " only files and directories are supported " ) ,
Cow ::Borrowed ( " the file was ignored and will not be published " )
] ) ,
2024-03-21 17:42:23 -04:00
InvalidExternalImport { imported , .. } = > Cow ::Owned ( vec! [
2024-01-24 16:59:18 -05:00
Cow ::Owned ( format! ( " the import was resolved to ' {} ' " , imported ) ) ,
Cow ::Borrowed ( " this specifier is not allowed to be imported on jsr " ) ,
Cow ::Borrowed ( " jsr only supports importing `jsr:`, `npm:`, and `data:` specifiers " ) ,
] ) ,
2024-03-21 17:42:23 -04:00
UnsupportedJsxTsx { .. } = > Cow ::Owned ( vec! [
2024-02-29 06:54:57 -05:00
Cow ::Borrowed ( " follow https://github.com/jsr-io/jsr/issues/24 for updates " ) ,
2024-03-21 17:42:23 -04:00
] ) ,
ExcludedModule { .. } = > Cow ::Owned ( vec! [
Cow ::Borrowed ( " excluded modules referenced via a package export will error at runtime due to not existing in the package " ) ,
2024-05-14 10:30:09 -04:00
] ) ,
MissingConstraint { resolved_version , .. } = > Cow ::Owned ( vec! [
Cow ::Owned ( format! (
" the specifier resolved to version {} today, but will resolve to a different " ,
resolved_version . as_ref ( ) . map ( | v | v . to_string ( ) ) . unwrap_or_else ( | | " <unresolved> " . to_string ( ) ) ) ,
) ,
Cow ::Borrowed ( " major version if one is published in the future and potentially break " ) ,
] ) ,
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives { .. } = > Cow ::Borrowed ( & [
Cow ::Borrowed ( " instead instruct the user of your package to specify these directives " ) ,
Cow ::Borrowed ( " or set their 'lib' compiler option appropriately " ) ,
] ) ,
2024-07-17 23:06:30 -04:00
SyntaxError ( diagnostic ) = > diagnostic . info ( ) ,
2024-07-22 15:46:37 -04:00
MissingLicense { .. } = > Cow ::Borrowed ( & [ ] ) ,
2024-01-23 10:37:43 -05:00
}
}
2024-02-08 20:40:26 -05:00
fn docs_url ( & self ) -> Option < Cow < '_ , str > > {
2024-03-21 17:42:23 -04:00
use PublishDiagnostic ::* ;
2024-01-23 10:37:43 -05:00
match & self {
2024-03-21 17:42:23 -04:00
FastCheck ( diagnostic ) = > diagnostic . docs_url ( ) ,
SpecifierUnfurl ( diagnostic ) = > match diagnostic {
2024-02-27 10:13:16 -05:00
SpecifierUnfurlerDiagnostic ::UnanalyzableDynamicImport { .. } = > None ,
2024-01-24 08:49:33 -05:00
} ,
2024-03-21 17:42:23 -04:00
InvalidPath { .. } = > {
2024-02-08 20:40:26 -05:00
Some ( Cow ::Borrowed ( " https://jsr.io/go/invalid-path " ) )
2024-01-24 16:24:52 -05:00
}
2024-03-21 17:42:23 -04:00
DuplicatePath { .. } = > Some ( Cow ::Borrowed (
2024-02-08 20:40:26 -05:00
" https://jsr.io/go/case-insensitive-duplicate-path " ,
) ) ,
2024-03-21 17:42:23 -04:00
UnsupportedFileType { .. } = > {
2024-02-08 20:40:26 -05:00
Some ( Cow ::Borrowed ( " https://jsr.io/go/unsupported-file-type " ) )
2024-01-24 16:59:18 -05:00
}
2024-03-21 17:42:23 -04:00
InvalidExternalImport { .. } = > {
2024-02-08 20:40:26 -05:00
Some ( Cow ::Borrowed ( " https://jsr.io/go/invalid-external-import " ) )
2024-01-24 16:59:18 -05:00
}
2024-03-21 17:42:23 -04:00
UnsupportedJsxTsx { .. } = > None ,
ExcludedModule { .. } = > {
Some ( Cow ::Borrowed ( " https://jsr.io/go/excluded-module " ) )
}
2024-05-14 10:30:09 -04:00
MissingConstraint { .. } = > {
Some ( Cow ::Borrowed ( " https://jsr.io/go/missing-constraint " ) )
}
2024-05-27 21:35:08 -04:00
BannedTripleSlashDirectives { .. } = > Some ( Cow ::Borrowed (
" https://jsr.io/go/banned-triple-slash-directives " ,
) ) ,
2024-07-17 23:06:30 -04:00
SyntaxError ( diagnostic ) = > diagnostic . docs_url ( ) ,
2024-07-22 15:46:37 -04:00
MissingLicense { .. } = > {
Some ( Cow ::Borrowed ( " https://jsr.io/go/missing-license " ) )
}
2024-01-23 10:37:43 -05:00
}
}
}