@@ -14,6 +14,23 @@ import (
1414 "gotest.tools/v3/assert"
1515)
1616
17+ type publishDiagnosticsCall = struct {
18+ Ctx context.Context
19+ Params * lsproto.PublishDiagnosticsParams
20+ }
21+
22+ // filterDiagnosticsByURI returns all PublishDiagnostics calls matching the given URI,
23+ // starting from the given index.
24+ func filterDiagnosticsByURI (calls []publishDiagnosticsCall , uri lsproto.DocumentUri , from int ) []publishDiagnosticsCall {
25+ var result []publishDiagnosticsCall
26+ for i := from ; i < len (calls ); i ++ {
27+ if calls [i ].Params .Uri == uri {
28+ result = append (result , calls [i ])
29+ }
30+ }
31+ return result
32+ }
33+
1734// These tests explicitly verify ProgramUpdateKind using subtests with shared helpers.
1835func TestProjectProgramUpdateKind (t * testing.T ) {
1936 t .Parallel ()
@@ -359,6 +376,51 @@ func TestPushDiagnostics(t *testing.T) {
359376 }
360377 assert .Assert (t , hasGlobalDiag , "expected a 'Cannot find global' diagnostic on tsconfig.json, got: %v" , lastTsconfigCall .Params .Diagnostics )
361378 })
379+
380+ t .Run ("cleans tsconfig diagnostics after TS files close and restores them after TS file is reopened" , func (t * testing.T ) {
381+ t .Parallel ()
382+ files := map [string ]any {
383+ "/src/tsconfig.json" : `{"compilerOptions": {"baseUrl": "."}}` ,
384+ "/src/index.ts" : "export const x = 1;" ,
385+ }
386+ session , utils := projecttestutil .Setup (files )
387+ uri := lsproto .DocumentUri ("file:///src/index.ts" )
388+ session .DidOpenFile (context .Background (), uri , 1 , files ["/src/index.ts" ].(string ), lsproto .LanguageKindTypeScript )
389+ _ , err := session .GetLanguageService (context .Background (), uri )
390+ assert .NilError (t , err )
391+ session .WaitForBackgroundTasks ()
392+
393+ calls := utils .Client ().PublishDiagnosticsCalls ()
394+ tsconfigCalls := filterDiagnosticsByURI (calls , "file:///src/tsconfig.json" , 0 )
395+ assert .Assert (t , len (tsconfigCalls ) > 0 , "expected PublishDiagnostics call for tsconfig.json after opening file" )
396+ assert .Equal (t , len (tsconfigCalls [0 ].Params .Diagnostics ), 1 , "expected one diagnostic on tsconfig.json after opening file" )
397+
398+ callsBeforeClose := len (calls )
399+
400+ session .DidCloseFile (context .Background (), uri )
401+ session .WaitForBackgroundTasks ()
402+
403+ // Cleans up diagnostics after close
404+ calls = utils .Client ().PublishDiagnosticsCalls ()
405+ clearCalls := filterDiagnosticsByURI (calls , "file:///src/tsconfig.json" , callsBeforeClose )
406+ assert .Assert (t , len (clearCalls ) > 0 , "expected PublishDiagnostics call for tsconfig.json after project close" )
407+ lastClearCall := clearCalls [len (clearCalls )- 1 ]
408+ assert .Equal (t , len (lastClearCall .Params .Diagnostics ), 0 , "expected empty diagnostics after project close" )
409+
410+ callsBeforeReopen := len (calls )
411+
412+ session .DidOpenFile (context .Background (), uri , 2 , files ["/src/index.ts" ].(string ), lsproto .LanguageKindTypeScript )
413+ _ , err = session .GetLanguageService (context .Background (), uri )
414+ assert .NilError (t , err )
415+ session .WaitForBackgroundTasks ()
416+
417+ // Restores diagnostics after reopen
418+ calls = utils .Client ().PublishDiagnosticsCalls ()
419+ reopenedCalls := filterDiagnosticsByURI (calls , "file:///src/tsconfig.json" , callsBeforeReopen )
420+ assert .Assert (t , len (reopenedCalls ) > 0 , "expected PublishDiagnostics call for tsconfig.json after reopening file" )
421+ lastReopenedCall := reopenedCalls [len (reopenedCalls )- 1 ]
422+ assert .Equal (t , len (lastReopenedCall .Params .Diagnostics ), 1 , "expected one diagnostic on tsconfig.json after reopening file" )
423+ })
362424}
363425
364426func TestDisplayName (t * testing.T ) {
0 commit comments