55 */
66
77import ts from 'typescript' ;
8+ import { DiagnosticCode } from './diagnostic-code.js' ;
89
910const diagnosticsHost : ts . FormatDiagnosticsHost = {
1011 getCanonicalFileName ( name : string ) {
@@ -18,14 +19,36 @@ const diagnosticsHost: ts.FormatDiagnosticsHost = {
1819 } ,
1920} ;
2021
21- export const createDiagnostic = ( node : ts . Node , message : string ) => ( {
22- file : node . getSourceFile ( ) ,
23- start : node . getStart ( ) ,
24- length : node . getWidth ( ) ,
25- category : ts . DiagnosticCategory . Error ,
26- code : 2323 ,
27- messageText : message ?? '' ,
28- } ) ;
22+ export interface DiagnosticOptions {
23+ node : ts . Node ;
24+ message ?: string | undefined ;
25+ category ?: ts . DiagnosticCategory ;
26+ code ?: DiagnosticCode | undefined ;
27+ }
28+
29+ export const createDiagnostic = ( {
30+ node,
31+ message,
32+ category,
33+ code,
34+ } : DiagnosticOptions ) => {
35+ return {
36+ file : node . getSourceFile ( ) ,
37+ start : node . getStart ( ) ,
38+ length : node . getWidth ( ) ,
39+ category : category ?? ts . DiagnosticCategory . Error ,
40+ code : code ?? DiagnosticCode . UNKNOWN ,
41+ messageText : message ?? '' ,
42+ } ;
43+ } ;
44+
45+ export const logDiagnostics = ( diagnostics : Array < ts . Diagnostic > ) => {
46+ if ( diagnostics . length > 0 ) {
47+ console . log (
48+ ts . formatDiagnosticsWithColorAndContext ( diagnostics , diagnosticsHost )
49+ ) ;
50+ }
51+ } ;
2952
3053export class DiagnosticsError extends Error {
3154 diagnostics : ts . Diagnostic [ ] ;
@@ -40,7 +63,7 @@ export class DiagnosticsError extends Error {
4063 diagnostics = nodeOrDiagnostics ;
4164 } else {
4265 const node = nodeOrDiagnostics as ts . Node ;
43- diagnostics = [ createDiagnostic ( node , message ! ) ] ;
66+ diagnostics = [ createDiagnostic ( { node, message} ) ] ;
4467 message = undefined ;
4568 }
4669 super (
0 commit comments