@@ -9,20 +9,27 @@ import { findPathToModule } from './modules-resolver';
9
9
import { ProcessBasedTsServer } from './process-based-tsserver' ;
10
10
import { CommandTypes , EventName } from './tsp-command-types' ;
11
11
import { getTsserverExecutable } from './utils' ;
12
- import { formatDiagnostic } from './format-diagnostics' ;
12
+ import { formatDiagnostic , Diagnostic } from './format-diagnostics' ;
13
13
14
14
export type TsserverClientOpts = {
15
15
verbose ?: boolean ; // print tsserver events to the console.
16
16
tsServerPath ?: string ; // if not provided, it'll use findTsserverPath() strategies.
17
17
checkTypes ?: CheckTypes ; // whether errors/warnings are monitored and printed to the console.
18
18
printTypeErrors ?: boolean ; // whether print typescript errors to the console.
19
+ aggregateDiagnosticData ?: boolean ; // whether to aggregate diagnostic data instead of printing them to the console.
19
20
} ;
20
21
22
+ export type DiagnosticData = {
23
+ file : string ;
24
+ diagnostic : Diagnostic ;
25
+ formatted : string ;
26
+ }
27
+
21
28
export class TsserverClient {
22
29
private tsServer : ProcessBasedTsServer | null ;
23
30
public lastDiagnostics : ts . server . protocol . DiagnosticEventBody [ ] = [ ] ;
24
31
private serverRunning = false ;
25
-
32
+ public diagnosticData : DiagnosticData [ ] = [ ] ;
26
33
constructor (
27
34
/**
28
35
* absolute root path of the project.
@@ -306,12 +313,24 @@ export class TsserverClient {
306
313
}
307
314
308
315
private publishDiagnostic ( message : ts . server . protocol . DiagnosticEvent ) {
309
- if ( ! message . body ?. diagnostics . length || ! this . options . printTypeErrors ) {
316
+ if ( ! message . body ?. diagnostics . length || ( ! this . options . printTypeErrors && ! this . options . aggregateDiagnosticData ) ) {
310
317
return ;
311
318
}
312
319
this . lastDiagnostics . push ( message . body ) ;
313
320
const file = path . relative ( this . projectPath , message . body . file ) ;
314
- message . body . diagnostics . forEach ( ( diag ) => this . logger . console ( formatDiagnostic ( diag , file ) ) ) ;
321
+ message . body . diagnostics . forEach ( ( diag ) => {
322
+ const formatted = formatDiagnostic ( diag , file ) ;
323
+ if ( this . options . printTypeErrors ) {
324
+ this . logger . console ( formatted ) ;
325
+ }
326
+ if ( this . options . aggregateDiagnosticData ) {
327
+ this . diagnosticData . push ( {
328
+ file,
329
+ diagnostic : diag ,
330
+ formatted,
331
+ } ) ;
332
+ }
333
+ } ) ;
315
334
}
316
335
317
336
/**
0 commit comments