@@ -52,63 +52,72 @@ function testDiagnosticOnExample(
5252 sourceText : string
5353) {
5454 // create the language service with mocked services over a VFS
55- const { program, sourceFile } = createServicesWithMockedVFS ( getHarnessDir ( ) , getExamplesDir ( ) , fileName , sourceText )
56-
57- // create snapshot path
58- const snapshotFilePath = path . join (
59- getSnapshotsSubdir ( "diagnostics" ) ,
60- fileName + ".output"
55+ const { languageService, program, sourceFile } = createServicesWithMockedVFS (
56+ getHarnessDir ( ) ,
57+ getExamplesDir ( ) ,
58+ fileName ,
59+ sourceText
6160 )
6261
63- if ( getHarnessVersion ( ) === "v4" ) {
64- // expect valid initial code
65- const typeDiags = program . getSemanticDiagnostics ( ) . filter ( ( _ ) => _ . source === sourceFile . fileName )
66- const syntaxDiags = program . getSyntacticDiagnostics ( ) . filter ( ( _ ) => _ . source === sourceFile . fileName )
67- const tsDiagsText = [ ...syntaxDiags , ...typeDiags ] . map ( ( diag ) =>
68- diagnosticToLogFormat ( sourceFile , sourceText , diag )
69- ) . join ( "\n\n" )
70- expect ( tsDiagsText ) . toBe ( "" )
71- }
62+ try {
63+ // create snapshot path
64+ const snapshotFilePath = path . join (
65+ getSnapshotsSubdir ( "diagnostics" ) ,
66+ fileName + ".output"
67+ )
7268
73- // attempt to run the diagnostic and get the output
74- return pipe (
75- LSP . getSemanticDiagnosticsWithCodeFixes ( [ diagnostic ] , sourceFile ) ,
76- TypeParser . nanoLayer ,
77- TypeCheckerUtils . nanoLayer ,
78- TypeScriptUtils . nanoLayer ,
79- Nano . provideService ( TypeCheckerApi . TypeCheckerApi , program . getTypeChecker ( ) ) ,
80- Nano . provideService ( TypeScriptApi . TypeScriptProgram , program ) ,
81- Nano . provideService ( TypeScriptApi . TypeScriptApi , ts ) ,
82- Nano . provideService (
83- LanguageServicePluginOptions . LanguageServicePluginOptions ,
84- LanguageServicePluginOptions . parse ( {
85- ...LanguageServicePluginOptions . defaults ,
86- diagnostics : true ,
87- refactors : false ,
88- quickinfo : false ,
89- completions : false ,
90- goto : false ,
91- namespaceImportPackages : [ "effect" ] ,
92- ...configFromSourceComment ( sourceText )
93- } )
94- ) ,
95- Nano . map ( ( { diagnostics } ) => {
96- // sort by start position
97- diagnostics . sort ( ( a , b ) => ( a . start || 0 ) - ( b . start || 0 ) )
98- // create human readable messages
99- return diagnostics . length === 0 ?
100- "// no diagnostics" :
101- diagnostics . map ( ( error ) => diagnosticToLogFormat ( sourceFile , sourceText , error ) )
102- . join ( "\n\n" )
103- } ) ,
104- Nano . unsafeRun ,
105- async ( result ) => {
106- expect ( Result . isSuccess ( result ) , "should run with no error " + result ) . toEqual ( true )
107- await expect ( Result . getOrElse ( result , ( ) => "// no codefixes available" ) ) . toMatchFileSnapshot (
108- snapshotFilePath
109- )
69+ if ( getHarnessVersion ( ) === "v4" ) {
70+ // expect valid initial code
71+ const typeDiags = program . getSemanticDiagnostics ( ) . filter ( ( _ ) => _ . source === sourceFile . fileName )
72+ const syntaxDiags = program . getSyntacticDiagnostics ( ) . filter ( ( _ ) => _ . source === sourceFile . fileName )
73+ const tsDiagsText = [ ...syntaxDiags , ...typeDiags ] . map ( ( diag ) =>
74+ diagnosticToLogFormat ( sourceFile , sourceText , diag )
75+ ) . join ( "\n\n" )
76+ expect ( tsDiagsText ) . toBe ( "" )
11077 }
111- )
78+
79+ // attempt to run the diagnostic and get the output
80+ return pipe (
81+ LSP . getSemanticDiagnosticsWithCodeFixes ( [ diagnostic ] , sourceFile ) ,
82+ TypeParser . nanoLayer ,
83+ TypeCheckerUtils . nanoLayer ,
84+ TypeScriptUtils . nanoLayer ,
85+ Nano . provideService ( TypeCheckerApi . TypeCheckerApi , program . getTypeChecker ( ) ) ,
86+ Nano . provideService ( TypeScriptApi . TypeScriptProgram , program ) ,
87+ Nano . provideService ( TypeScriptApi . TypeScriptApi , ts ) ,
88+ Nano . provideService (
89+ LanguageServicePluginOptions . LanguageServicePluginOptions ,
90+ LanguageServicePluginOptions . parse ( {
91+ ...LanguageServicePluginOptions . defaults ,
92+ diagnostics : true ,
93+ refactors : false ,
94+ quickinfo : false ,
95+ completions : false ,
96+ goto : false ,
97+ namespaceImportPackages : [ "effect" ] ,
98+ ...configFromSourceComment ( sourceText )
99+ } )
100+ ) ,
101+ Nano . map ( ( { diagnostics } ) => {
102+ // sort by start position
103+ diagnostics . sort ( ( a , b ) => ( a . start || 0 ) - ( b . start || 0 ) )
104+ // create human readable messages
105+ return diagnostics . length === 0 ?
106+ "// no diagnostics" :
107+ diagnostics . map ( ( error ) => diagnosticToLogFormat ( sourceFile , sourceText , error ) )
108+ . join ( "\n\n" )
109+ } ) ,
110+ Nano . unsafeRun ,
111+ async ( result ) => {
112+ expect ( Result . isSuccess ( result ) , "should run with no error " + result ) . toEqual ( true )
113+ await expect ( Result . getOrElse ( result , ( ) => "// no codefixes available" ) ) . toMatchFileSnapshot (
114+ snapshotFilePath
115+ )
116+ }
117+ )
118+ } finally {
119+ languageService . dispose ( )
120+ }
112121}
113122
114123function testDiagnosticQuickfixesOnExample (
@@ -117,14 +126,16 @@ function testDiagnosticQuickfixesOnExample(
117126 sourceText : string
118127) {
119128 const promises : Array < Promise < void > > = [ ]
129+ const languageServicesToDispose : Array < ts . LanguageService > = [ ]
120130
121131 // create the language service with mocked services over a VFS
122- const { languageServiceHost, program, sourceFile } = createServicesWithMockedVFS (
132+ const { languageService , languageServiceHost, program, sourceFile } = createServicesWithMockedVFS (
123133 getHarnessDir ( ) ,
124134 getExamplesDir ( ) ,
125135 fileName ,
126136 sourceText
127137 )
138+ languageServicesToDispose . push ( languageService )
128139
129140 // create snapshot path
130141 const snapshotFilePathList = path . join (
@@ -176,17 +187,24 @@ function testDiagnosticQuickfixesOnExample(
176187 codeFix . end + "\n" + applyEdits ( edits , fileName , sourceText )
177188
178189 if ( getHarnessVersion ( ) === "v4" ) {
179- const { program , sourceFile : newSourceFile } = createServicesWithMockedVFS (
190+ const result = createServicesWithMockedVFS (
180191 getHarnessDir ( ) ,
181192 getExamplesDir ( ) ,
182193 fileName ,
183194 finalSource
184195 )
185- const typeDiags = program . getSemanticDiagnostics ( ) . filter ( ( _ ) => _ . source === newSourceFile . fileName )
186- const syntaxDiags = program . getSyntacticDiagnostics ( ) . filter ( ( _ ) => _ . source === newSourceFile . fileName )
196+ languageServicesToDispose . push ( result . languageService )
197+ const typeDiags = result . program . getSemanticDiagnostics ( ) . filter ( ( _ ) =>
198+ _ . source === result . sourceFile . fileName
199+ )
200+ const syntaxDiags = result . program . getSyntacticDiagnostics ( ) . filter ( ( _ ) =>
201+ _ . source === result . sourceFile . fileName
202+ )
187203 const snapshotText = [
188204 finalSource ,
189- ...[ ...syntaxDiags , ...typeDiags ] . map ( ( diag ) => diagnosticToLogFormat ( newSourceFile , finalSource , diag ) )
205+ ...[ ...syntaxDiags , ...typeDiags ] . map ( ( diag ) =>
206+ diagnosticToLogFormat ( result . sourceFile , finalSource , diag )
207+ )
190208 ] . join ( "\n\n" )
191209 promises . push (
192210 expect ( snapshotText ) . toMatchFileSnapshot ( snapshotFilePath )
@@ -224,11 +242,17 @@ function testDiagnosticQuickfixesOnExample(
224242 ) ,
225243 Nano . unsafeRun ,
226244 async ( result ) => {
227- expect ( Result . isSuccess ( result ) , "should run with no error " + result ) . toEqual ( true )
228- await Promise . all ( promises )
229- await expect ( Result . getOrElse ( result , ( ) => "// no codefixes available" ) ) . toMatchFileSnapshot (
230- snapshotFilePathList
231- )
245+ try {
246+ expect ( Result . isSuccess ( result ) , "should run with no error " + result ) . toEqual ( true )
247+ await Promise . all ( promises )
248+ await expect ( Result . getOrElse ( result , ( ) => "// no codefixes available" ) ) . toMatchFileSnapshot (
249+ snapshotFilePathList
250+ )
251+ } finally {
252+ for ( const ls of languageServicesToDispose ) {
253+ ls . dispose ( )
254+ }
255+ }
232256 }
233257 )
234258}
0 commit comments