@@ -2,6 +2,9 @@ import * as ts from "../../_namespaces/ts";
2
2
import {
3
3
jsonToReadableText ,
4
4
} from "../helpers" ;
5
+ import {
6
+ compilerOptionsToConfigJson ,
7
+ } from "../helpers/contents" ;
5
8
import {
6
9
ensureErrorFreeBuild ,
7
10
} from "../helpers/solutionBuilder" ;
@@ -27,13 +30,10 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
27
30
it ( "create configured project without file list" , ( ) => {
28
31
const configFile : File = {
29
32
path : "/a/b/tsconfig.json" ,
30
- content : `
31
- {
32
- "compilerOptions": {},
33
- "exclude": [
34
- "e"
35
- ]
36
- }` ,
33
+ content : jsonToReadableText ( {
34
+ compilerOptions : { } ,
35
+ exclude : [ "e" ] ,
36
+ } ) ,
37
37
} ;
38
38
const file1 : File = {
39
39
path : "/a/b/c/f1.ts" ,
@@ -57,11 +57,10 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
57
57
it ( "create configured project with the file list" , ( ) => {
58
58
const configFile : File = {
59
59
path : "/a/b/tsconfig.json" ,
60
- content : `
61
- {
62
- "compilerOptions": {},
63
- "include": ["*.ts"]
64
- }` ,
60
+ content : jsonToReadableText ( {
61
+ compilerOptions : { } ,
62
+ include : [ "*.ts" ] ,
63
+ } ) ,
65
64
} ;
66
65
const file1 : File = {
67
66
path : "/a/b/f1.ts" ,
@@ -85,9 +84,7 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
85
84
it ( "add and then remove a config file in a folder with loose files" , ( ) => {
86
85
const configFile : File = {
87
86
path : `/user/username/projects/myproject/tsconfig.json` ,
88
- content : `{
89
- "files": ["commonFile1.ts"]
90
- }` ,
87
+ content : jsonToReadableText ( { files : [ "commonFile1.ts" ] } ) ,
91
88
} ;
92
89
const commonFile1 : File = {
93
90
path : `/user/username/projects/myproject/commonFile1.ts` ,
@@ -101,6 +98,7 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
101
98
const host = createServerHost ( [ libFile , commonFile1 , commonFile2 ] ) ;
102
99
103
100
const session = new TestSession ( host ) ;
101
+ // 1: when both files are open
104
102
openFilesForSession ( [ commonFile1 , commonFile2 ] , session ) ;
105
103
106
104
// Add a tsconfig file
@@ -111,9 +109,139 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
111
109
host . deleteFile ( configFile . path ) ;
112
110
host . runQueuedTimeoutCallbacks ( ) ; // Refresh inferred projects
113
111
112
+ // Add a tsconfig file
113
+ host . writeFile ( configFile . path , configFile . content ) ;
114
+ host . runQueuedTimeoutCallbacks ( ) ; // load configured project from disk + ensureProjectsForOpenFiles
115
+
116
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
117
+ // Check status when all files are closed
118
+ closeFilesForSession ( [ commonFile1 , commonFile2 , "/random/random.ts" ] , session ) ;
119
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
120
+
121
+ // 2: when file is opened while config file is deleted
122
+ closeFilesForSession ( [ "/random/random.ts" ] , session ) ;
123
+ openFilesForSession ( [ commonFile1 ] , session ) ;
124
+
125
+ // remove the tsconfig file
126
+ host . deleteFile ( configFile . path ) ;
127
+ openFilesForSession ( [ commonFile2 ] , session ) ;
128
+
129
+ // Add a tsconfig file
130
+ host . writeFile ( configFile . path , configFile . content ) ;
131
+ host . runQueuedTimeoutCallbacks ( ) ; // load configured project from disk + ensureProjectsForOpenFiles
132
+
133
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
134
+ // Check status when all files are closed
135
+ closeFilesForSession ( [ commonFile1 , commonFile2 , "/random/random.ts" ] , session ) ;
136
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
137
+
114
138
baselineTsserverLogs ( "configuredProjects" , "add and then remove a config file in a folder with loose files" , session ) ;
115
139
} ) ;
116
140
141
+ it ( "add and then remove a config file when parent folder has config file" , ( ) => {
142
+ const configFile : File = {
143
+ path : `/user/username/projects/myproject/folder/tsconfig.json` ,
144
+ content : jsonToReadableText ( { files : [ "commonFile1.ts" ] } ) ,
145
+ } ;
146
+ const parentConfigFile : File = {
147
+ path : `/user/username/projects/myproject/tsconfig.json` ,
148
+ content : jsonToReadableText ( { files : [ "folder/commonFile2.ts" ] } ) ,
149
+ } ;
150
+ const commonFile1 : File = {
151
+ path : `/user/username/projects/myproject/folder/commonFile1.ts` ,
152
+ content : "let x = 1" ,
153
+ } ;
154
+ const commonFile2 : File = {
155
+ path : `/user/username/projects/myproject/folder/commonFile2.ts` ,
156
+ content : "let y = 1" ,
157
+ } ;
158
+
159
+ const host = createServerHost ( [ libFile , commonFile1 , commonFile2 , configFile , parentConfigFile ] ) ;
160
+
161
+ const session = new TestSession ( host ) ;
162
+
163
+ // 1: When config file is deleted and then another file is opened
164
+ openFilesForSession ( [ commonFile1 ] , session ) ;
165
+
166
+ // remove the tsconfig file
167
+ host . deleteFile ( configFile . path ) ;
168
+ openFilesForSession ( [ commonFile2 ] , session ) ;
169
+
170
+ // Add a tsconfig file
171
+ host . writeFile ( configFile . path , configFile . content ) ;
172
+ host . runQueuedTimeoutCallbacks ( ) ;
173
+
174
+ // Check the state after files collected
175
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
176
+
177
+ // Check status when all files are closed
178
+ closeFilesForSession ( [ commonFile1 , commonFile2 , "/random/random.ts" ] , session ) ;
179
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
180
+
181
+ // 2: When both files are open and config file is deleted
182
+ closeFilesForSession ( [ "/random/random.ts" ] , session ) ;
183
+ openFilesForSession ( [ commonFile1 , commonFile2 ] , session ) ;
184
+
185
+ // remove the tsconfig file
186
+ host . deleteFile ( configFile . path ) ;
187
+ host . runQueuedTimeoutCallbacks ( ) ;
188
+
189
+ // Add a tsconfig file
190
+ host . writeFile ( configFile . path , configFile . content ) ;
191
+ host . runQueuedTimeoutCallbacks ( ) ; // load configured project from disk + ensureProjectsForOpenFiles
192
+
193
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
194
+
195
+ closeFilesForSession ( [ commonFile1 , commonFile2 , "/random/random.ts" ] , session ) ;
196
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
197
+
198
+ // 3: Check when both files are closed when config file is deleted
199
+ closeFilesForSession ( [ "/random/random.ts" ] , session ) ;
200
+ openFilesForSession ( [ commonFile1 ] , session ) ;
201
+
202
+ // remove the tsconfig file
203
+ host . deleteFile ( configFile . path ) ;
204
+ openFilesForSession ( [ commonFile2 ] , session ) ;
205
+
206
+ // State after open files are closed
207
+ closeFilesForSession ( [ commonFile1 , commonFile2 ] , session ) ;
208
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
209
+
210
+ // 4: Check when both files are closed one by one when file is deleted
211
+ host . writeFile ( configFile . path , configFile . content ) ;
212
+ closeFilesForSession ( [ "/random/random.ts" ] , session ) ;
213
+ openFilesForSession ( [ commonFile1 ] , session ) ;
214
+
215
+ // remove the tsconfig file
216
+ host . deleteFile ( configFile . path ) ;
217
+ openFilesForSession ( [ commonFile2 ] , session ) ;
218
+
219
+ // State after open files are closed
220
+ closeFilesForSession ( [ commonFile1 ] , session ) ;
221
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
222
+
223
+ closeFilesForSession ( [ commonFile2 , "random/random.ts" ] , session ) ;
224
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
225
+
226
+ // 5: Check when both files are closed one by one when file is deleted order changed
227
+ host . writeFile ( configFile . path , configFile . content ) ;
228
+ closeFilesForSession ( [ "/random/random.ts" ] , session ) ;
229
+ openFilesForSession ( [ commonFile1 ] , session ) ;
230
+
231
+ // remove the tsconfig file
232
+ host . deleteFile ( configFile . path ) ;
233
+ openFilesForSession ( [ commonFile2 ] , session ) ;
234
+
235
+ // State after open files are closed
236
+ closeFilesForSession ( [ commonFile2 ] , session ) ;
237
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
238
+
239
+ closeFilesForSession ( [ commonFile1 , "random/random.ts" ] , session ) ;
240
+ openFilesForSession ( [ { file : "/random/random.ts" , content : "export const y = 10;" } ] , session ) ;
241
+
242
+ baselineTsserverLogs ( "configuredProjects" , "add and then remove a config file when parent folder has config file" , session ) ;
243
+ } ) ;
244
+
117
245
it ( "add new files to a configured project without file list" , ( ) => {
118
246
const configFile : File = {
119
247
path : "/a/b/tsconfig.json" ,
@@ -132,13 +260,13 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
132
260
it ( "should ignore non-existing files specified in the config file" , ( ) => {
133
261
const configFile : File = {
134
262
path : "/a/b/tsconfig.json" ,
135
- content : ` {
136
- " compilerOptions" : {},
137
- " files" : [
138
- "commonFile1.ts",
139
- "commonFile3.ts"
140
- ]
141
- }` ,
263
+ content : jsonToReadableText ( {
264
+ compilerOptions : { } ,
265
+ files : [
266
+ "commonFile1.ts" ,
267
+ "commonFile3.ts" ,
268
+ ] ,
269
+ } ) ,
142
270
} ;
143
271
const host = createServerHost ( [ commonFile1 , commonFile2 , configFile ] ) ;
144
272
const session = new TestSession ( host ) ;
@@ -168,10 +296,10 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
168
296
it ( "files explicitly excluded in config file" , ( ) => {
169
297
const configFile : File = {
170
298
path : "/a/b/tsconfig.json" ,
171
- content : ` {
172
- " compilerOptions" : {},
173
- " exclude" : ["/a/c"]
174
- }` ,
299
+ content : jsonToReadableText ( {
300
+ compilerOptions : { } ,
301
+ exclude : [ "/a/c" ] ,
302
+ } ) ,
175
303
} ;
176
304
const excludedFile1 : File = {
177
305
path : "/a/c/excluedFile1.ts" ,
@@ -204,12 +332,10 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
204
332
} ;
205
333
const configFile : File = {
206
334
path : "/a/b/tsconfig.json" ,
207
- content : `{
208
- "compilerOptions": {
209
- "moduleResolution": "node"
210
- },
211
- "files": ["${ file1 . path } "]
212
- }` ,
335
+ content : jsonToReadableText ( {
336
+ compilerOptions : compilerOptionsToConfigJson ( { moduleResolution : ts . ModuleResolutionKind . Node10 } ) ,
337
+ files : [ file1 . path ] ,
338
+ } ) ,
213
339
} ;
214
340
const files = [ file1 , nodeModuleFile , classicModuleFile , configFile , randomFile ] ;
215
341
const host = createServerHost ( files ) ;
@@ -218,12 +344,10 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
218
344
219
345
host . writeFile (
220
346
configFile . path ,
221
- `{
222
- "compilerOptions": {
223
- "moduleResolution": "classic"
224
- },
225
- "files": ["${ file1 . path } "]
226
- }` ,
347
+ jsonToReadableText ( {
348
+ compilerOptions : compilerOptionsToConfigJson ( { moduleResolution : ts . ModuleResolutionKind . Classic } ) ,
349
+ files : [ file1 . path ] ,
350
+ } ) ,
227
351
) ;
228
352
host . runQueuedTimeoutCallbacks ( ) ;
229
353
@@ -244,12 +368,12 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
244
368
} ;
245
369
const configFile : File = {
246
370
path : "/a/b/tsconfig.json" ,
247
- content : ` {
248
- " compilerOptions" : {
249
- " target" : "es6"
250
- },
251
- " files" : [ "main.ts" ]
252
- }` ,
371
+ content : jsonToReadableText ( {
372
+ compilerOptions : {
373
+ target : "es6" ,
374
+ } ,
375
+ files : [ "main.ts" ] ,
376
+ } ) ,
253
377
} ;
254
378
const host = createServerHost ( [ file1 , file2 , configFile ] ) ;
255
379
const session = new TestSession ( host ) ;
@@ -262,13 +386,13 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
262
386
it ( "should tolerate config file errors and still try to build a project" , ( ) => {
263
387
const configFile : File = {
264
388
path : "/a/b/tsconfig.json" ,
265
- content : ` {
266
- " compilerOptions" : {
267
- " target" : "es6",
268
- " allowAnything" : true
269
- },
270
- " someOtherProperty" : {}
271
- }` ,
389
+ content : jsonToReadableText ( {
390
+ compilerOptions : {
391
+ target : "es6" ,
392
+ allowAnything : true ,
393
+ } ,
394
+ someOtherProperty : { } ,
395
+ } ) ,
272
396
} ;
273
397
const host = createServerHost ( [ commonFile1 , commonFile2 , libFile , configFile ] ) ;
274
398
const session = new TestSession ( host ) ;
@@ -287,12 +411,12 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
287
411
} ;
288
412
const configFile : File = {
289
413
path : "/a/b/tsconfig.json" ,
290
- content : ` {
291
- " compilerOptions" : {
292
- " target" : "es6"
293
- },
294
- " files" : [ "main.ts", "main2.ts" ]
295
- }` ,
414
+ content : jsonToReadableText ( {
415
+ compilerOptions : {
416
+ target : "es6" ,
417
+ } ,
418
+ files : [ "main.ts" , "main2.ts" ] ,
419
+ } ) ,
296
420
} ;
297
421
const host = createServerHost ( [ file1 , file2 , configFile , libFile ] ) ;
298
422
const session = new TestSession ( { host, useSingleInferredProject : true } ) ;
@@ -309,12 +433,12 @@ describe("unittests:: tsserver:: ConfiguredProjects", () => {
309
433
} ;
310
434
const configFile : File = {
311
435
path : "/a/b/tsconfig.json" ,
312
- content : ` {
313
- " compilerOptions" : {
314
- " target" : "es6"
315
- },
316
- " files" : [ "main.ts" ]
317
- }` ,
436
+ content : jsonToReadableText ( {
437
+ compilerOptions : {
438
+ target : "es6" ,
439
+ } ,
440
+ files : [ "main.ts" ] ,
441
+ } ) ,
318
442
} ;
319
443
const host = createServerHost ( [ file1 , configFile , libFile ] ) ;
320
444
const session = new TestSession ( { host, useSingleInferredProject : true } ) ;
0 commit comments