7
7
baselineTsserverLogs ,
8
8
openFilesForSession ,
9
9
TestSession ,
10
+ verifyGetErrRequest ,
10
11
} from "../helpers/tsserver" ;
11
12
import {
12
13
createServerHost ,
@@ -282,6 +283,35 @@ describe("unittests:: tsserver:: plugins:: overriding getSupportedCodeFixes", ()
282
283
} ) ;
283
284
284
285
describe ( "unittests:: tsserver:: plugins:: supportedExtensions::" , ( ) => {
286
+ function createGetExternalFiles ( getSession : ( ) => TestSession ) {
287
+ const externalFiles = new Map < ts . server . Project , string [ ] > ( ) ;
288
+ return ( project : ts . server . Project , updateLevel : ts . ProgramUpdateLevel ) => {
289
+ if ( project . projectKind !== ts . server . ProjectKind . Configured ) return [ ] ;
290
+ if ( updateLevel === ts . ProgramUpdateLevel . Update ) {
291
+ const existing = externalFiles . get ( project ) ;
292
+ if ( existing ) {
293
+ getSession ( ) . logger . log ( `getExternalFiles:: Returning cached .vue files` ) ;
294
+ return existing ;
295
+ }
296
+ }
297
+ getSession ( ) . logger . log ( `getExternalFiles:: Getting new list of .vue files` ) ;
298
+ const configFile = project . getProjectName ( ) ;
299
+ const config = ts . readJsonConfigFile ( configFile , project . readFile . bind ( project ) ) ;
300
+ const parseHost : ts . ParseConfigHost = {
301
+ useCaseSensitiveFileNames : project . useCaseSensitiveFileNames ( ) ,
302
+ fileExists : project . fileExists . bind ( project ) ,
303
+ readFile : project . readFile . bind ( project ) ,
304
+ readDirectory : ( ...args ) => {
305
+ args [ 1 ] = [ ".vue" ] ;
306
+ return project . readDirectory ( ...args ) ;
307
+ } ,
308
+ } ;
309
+ const parsed = ts . parseJsonSourceFileConfigFileContent ( config , parseHost , project . getCurrentDirectory ( ) ) ;
310
+ externalFiles . set ( project , parsed . fileNames ) ;
311
+ return parsed . fileNames ;
312
+ } ;
313
+ }
314
+
285
315
it ( "new files with non ts extensions and wildcard matching" , ( ) => {
286
316
const aTs : File = {
287
317
path : "/user/username/projects/myproject/a.ts" ,
@@ -303,7 +333,6 @@ describe("unittests:: tsserver:: plugins:: supportedExtensions::", () => {
303
333
} ) ,
304
334
} ;
305
335
const host = createServerHost ( [ aTs , dTs , bVue , config , libFile ] ) ;
306
- const externalFiles = new Map < ts . server . Project , string [ ] > ( ) ;
307
336
host . require = ( ) => {
308
337
return {
309
338
module : ( ) => ( {
@@ -321,31 +350,7 @@ describe("unittests:: tsserver:: plugins:: supportedExtensions::", () => {
321
350
originalGetScriptSnapshot ( fileName ) ;
322
351
return proxy ;
323
352
} ,
324
- getExternalFiles : ( project : ts . server . Project , updateLevel : ts . ProgramUpdateLevel ) => {
325
- if ( project . projectKind !== ts . server . ProjectKind . Configured ) return [ ] ;
326
- if ( updateLevel === ts . ProgramUpdateLevel . Update ) {
327
- const existing = externalFiles . get ( project ) ;
328
- if ( existing ) {
329
- session . logger . log ( `getExternalFiles:: Returning cached .vue files` ) ;
330
- return existing ;
331
- }
332
- }
333
- session . logger . log ( `getExternalFiles:: Getting new list of .vue files` ) ;
334
- const configFile = project . getProjectName ( ) ;
335
- const config = ts . readJsonConfigFile ( configFile , project . readFile . bind ( project ) ) ;
336
- const parseHost : ts . ParseConfigHost = {
337
- useCaseSensitiveFileNames : project . useCaseSensitiveFileNames ( ) ,
338
- fileExists : project . fileExists . bind ( project ) ,
339
- readFile : project . readFile . bind ( project ) ,
340
- readDirectory : ( ...args ) => {
341
- args [ 1 ] = [ ".vue" ] ;
342
- return project . readDirectory ( ...args ) ;
343
- } ,
344
- } ;
345
- const parsed = ts . parseJsonSourceFileConfigFileContent ( config , parseHost , project . getCurrentDirectory ( ) ) ;
346
- externalFiles . set ( project , parsed . fileNames ) ;
347
- return parsed . fileNames ;
348
- } ,
353
+ getExternalFiles : createGetExternalFiles ( ( ) => session ) ,
349
354
} ) ,
350
355
error : undefined ,
351
356
} ;
@@ -361,4 +366,67 @@ describe("unittests:: tsserver:: plugins:: supportedExtensions::", () => {
361
366
362
367
baselineTsserverLogs ( "plugins" , "new files with non ts extensions with wildcard matching" , session ) ;
363
368
} ) ;
369
+
370
+ it ( "when scriptKind changes for the external file" , ( ) => {
371
+ const aTs : File = {
372
+ path : "/user/username/projects/myproject/a.ts" ,
373
+ content : `export const a = 10;` ,
374
+ } ;
375
+ const bVue : File = {
376
+ path : "/user/username/projects/myproject/b.vue" ,
377
+ content : "bVueFile" ,
378
+ } ;
379
+ const config : File = {
380
+ path : "/user/username/projects/myproject/tsconfig.json" ,
381
+ content : jsonToReadableText ( {
382
+ compilerOptions : { composite : true } ,
383
+ include : [ "*.ts" , "*.vue" ] ,
384
+ } ) ,
385
+ } ;
386
+ const host = createServerHost ( [ aTs , bVue , config , libFile ] ) ;
387
+ let currentVueScriptKind = ts . ScriptKind . TS ;
388
+ host . require = ( ) => {
389
+ return {
390
+ module : ( ) => ( {
391
+ create ( info : ts . server . PluginCreateInfo ) {
392
+ const proxy = Harness . LanguageService . makeDefaultProxy ( info ) ;
393
+ const originalScriptKind = info . languageServiceHost . getScriptKind ! . bind ( info . languageServiceHost ) ;
394
+ info . languageServiceHost . getScriptKind = fileName =>
395
+ fileName === bVue . path ?
396
+ currentVueScriptKind :
397
+ originalScriptKind ( fileName ) ;
398
+ const originalGetScriptSnapshot = info . languageServiceHost . getScriptSnapshot . bind ( info . languageServiceHost ) ;
399
+ info . languageServiceHost . getScriptSnapshot = fileName =>
400
+ fileName === bVue . path ?
401
+ ts . ScriptSnapshot . fromString ( `import { y } from "${ bVue . content } ";` ) : // Change the text so that imports change and we need to reconstruct program
402
+ originalGetScriptSnapshot ( fileName ) ;
403
+ return proxy ;
404
+ } ,
405
+ getExternalFiles : createGetExternalFiles ( ( ) => session ) ,
406
+ } ) ,
407
+ error : undefined ,
408
+ } ;
409
+ } ;
410
+ const session = new TestSession ( { host, globalPlugins : [ "myplugin" ] } ) ;
411
+ openFilesForSession ( [ bVue ] , session ) ;
412
+
413
+ session . executeCommandSeq < ts . server . protocol . UpdateOpenRequest > ( {
414
+ command : ts . server . protocol . CommandTypes . UpdateOpen ,
415
+ arguments : {
416
+ changedFiles : [ {
417
+ fileName : bVue . path ,
418
+ textChanges : [ {
419
+ start : { line : 1 , offset : bVue . content . length + 1 } ,
420
+ end : { line : 1 , offset : bVue . content . length + 1 } ,
421
+ newText : "Updated" ,
422
+ } ] ,
423
+ } ] ,
424
+ } ,
425
+ } ) ;
426
+ bVue . content += "Updated" ;
427
+ currentVueScriptKind = ts . ScriptKind . JS ;
428
+
429
+ verifyGetErrRequest ( { session, files : [ bVue ] } ) ;
430
+ baselineTsserverLogs ( "plugins" , "when scriptKind changes for the external file" , session ) ;
431
+ } ) ;
364
432
} ) ;
0 commit comments