@@ -63,8 +63,6 @@ export class TSService {
63
63
private readonly fileWatchCallbacks = new Map <
64
64
string ,
65
65
{
66
- setupTarget : ( ) => void ;
67
- resetTarget : ( ) => void ;
68
66
update : ( ) => void ;
69
67
}
70
68
> ( ) ;
@@ -76,9 +74,7 @@ export class TSService {
76
74
}
77
75
78
76
public getProgram ( code : string , filePath : string ) : ts . Program {
79
- const normalized = normalizeFileName (
80
- toRealFileName ( filePath , this . extraFileExtensions )
81
- ) ;
77
+ const normalized = normalizeFileName ( filePath ) ;
82
78
const lastTarget = this . currTarget ;
83
79
84
80
const dirMap = new Map < string , { name : string ; path : string } > ( ) ;
@@ -101,19 +97,8 @@ export class TSService {
101
97
. get ( normalizeFileName ( this . tsconfigPath ) )
102
98
?. update ( ) ;
103
99
}
100
+ this . fileWatchCallbacks . get ( normalizeFileName ( targetPath ) ) ?. update ( ) ;
104
101
}
105
- getRefreshTargetFileNames (
106
- lastTarget . filePath ,
107
- this . extraFileExtensions
108
- ) . forEach ( ( vFilePath ) => {
109
- this . fileWatchCallbacks . get ( vFilePath ) ?. resetTarget ( ) ;
110
- } ) ;
111
- getRefreshTargetFileNames (
112
- this . currTarget . filePath ,
113
- this . extraFileExtensions
114
- ) . forEach ( ( vFilePath ) => {
115
- this . fileWatchCallbacks . get ( vFilePath ) ?. setupTarget ( ) ;
116
- } ) ;
117
102
118
103
const program = this . watch . getProgram ( ) . getProgram ( ) ;
119
104
// sets parent pointers in source files
@@ -286,22 +271,17 @@ export class TSService {
286
271
return distinctArray ( ...results ) ;
287
272
} ;
288
273
watchCompilerHost . readFile = ( fileName , ...args ) => {
289
- const realFileName = toRealFileName ( fileName , extraFileExtensions ) ;
290
- const normalized = normalizeFileName ( realFileName ) ;
291
- if ( this . currTarget . filePath === normalized ) {
274
+ const realFileName = getRealFileNameIfExist ( fileName ) ;
275
+ if ( realFileName == null ) {
276
+ return undefined ;
277
+ }
278
+ if ( this . currTarget . filePath === realFileName ) {
292
279
// It is the file currently being parsed.
293
280
return transformExtraFile ( this . currTarget . code , {
294
- filePath : normalized ,
281
+ filePath : realFileName ,
295
282
current : true ,
296
283
} ) ;
297
284
}
298
- if ( isVirtualTSX ( fileName , extraFileExtensions ) ) {
299
- const dts = toExtraDtsFileName ( normalized , extraFileExtensions ) ;
300
- if ( original . fileExists . call ( watchCompilerHost , dts ) ) {
301
- // If the .d.ts file exists, respect it and consider the virtual file not to exist.
302
- return undefined ;
303
- }
304
- }
305
285
306
286
const code = original . readFile . call (
307
287
watchCompilerHost ,
@@ -312,76 +292,52 @@ export class TSService {
312
292
return code ;
313
293
}
314
294
return transformExtraFile ( code , {
315
- filePath : normalized ,
295
+ filePath : realFileName ,
316
296
current : false ,
317
297
} ) ;
318
298
} ;
319
299
// Modify it so that it can be determined that the virtual file actually exists.
320
- watchCompilerHost . fileExists = ( fileName , ...args ) => {
321
- const normalizedFileName = normalizeFileName ( fileName ) ;
300
+ watchCompilerHost . fileExists = ( fileName ) => {
301
+ return getRealFileNameIfExist ( fileName ) != null ;
302
+ } ;
322
303
304
+ const getRealFileNameIfExist = ( fileName : string ) : string | null => {
305
+ const normalizedFileName = normalizeFileName ( fileName ) ;
323
306
// Even if it is actually a file, if it is specified as a directory to the target file,
324
307
// it is assumed that it does not exist as a file.
325
308
if ( this . currTarget . dirMap . has ( normalizedFileName ) ) {
326
- return false ;
309
+ return null ;
327
310
}
328
- const normalizedRealFileName = toRealFileName (
329
- normalizedFileName ,
330
- extraFileExtensions
331
- ) ;
332
- if ( this . currTarget . filePath === normalizedRealFileName ) {
311
+ if ( this . currTarget . filePath === normalizedFileName ) {
333
312
// It is the file currently being parsed.
334
- return true ;
313
+ return normalizedFileName ;
335
314
}
336
- if (
337
- original . fileExists . call (
338
- watchCompilerHost ,
339
- normalizedRealFileName ,
340
- ...args
341
- )
342
- ) {
343
- if ( isVirtualTSX ( fileName , extraFileExtensions ) ) {
344
- if (
345
- original . fileExists . call (
346
- watchCompilerHost ,
347
- toExtraDtsFileName ( normalizedRealFileName , extraFileExtensions ) ,
348
- ...args
349
- )
350
- ) {
315
+ const exists = original . fileExists . call (
316
+ watchCompilerHost ,
317
+ normalizedFileName
318
+ ) ;
319
+ if ( exists ) {
320
+ return normalizedFileName ;
321
+ }
322
+ if ( isVirtualTSX ( normalizedFileName , extraFileExtensions ) ) {
323
+ const real = normalizedFileName . slice ( 0 , - 4 ) ;
324
+ for ( const dts of toExtraDtsFileNames ( real , extraFileExtensions ) ) {
325
+ if ( original . fileExists . call ( watchCompilerHost , dts ) ) {
351
326
// If the d.ts file exists, respect it and consider the virtual file not to exist.
352
- return false ;
327
+ return null ;
353
328
}
354
329
}
355
- return true ;
330
+ if ( original . fileExists . call ( watchCompilerHost , real ) ) {
331
+ return real ;
332
+ }
356
333
}
357
- return false ;
334
+ return null ;
358
335
} ;
359
336
360
- // It keeps a callback to mark the parsed file as changed so that it can be reparsed .
337
+ // It keeps a callback to mark the parsed file as changed so that it can be re-parsed .
361
338
watchCompilerHost . watchFile = ( fileName , callback ) => {
362
339
const normalized = normalizeFileName ( fileName ) ;
363
340
this . fileWatchCallbacks . set ( normalized , {
364
- // The function is called when the file is targeted for parsing.
365
- setupTarget : ( ) => {
366
- if ( isExtraDts ( fileName , extraFileExtensions ) ) {
367
- callback ( fileName , ts . FileWatcherEventKind . Deleted ) ;
368
- } else if ( isVirtualTSX ( fileName , extraFileExtensions ) ) {
369
- callback ( fileName , ts . FileWatcherEventKind . Created ) ;
370
- } else {
371
- callback ( fileName , ts . FileWatcherEventKind . Changed ) ;
372
- }
373
- } ,
374
- // The function is called when the file leaves the target of parsing.
375
- resetTarget : ( ) => {
376
- if ( isExtraDts ( fileName , extraFileExtensions ) ) {
377
- // If the .d.ts file exists, it will take respect.
378
- callback ( fileName , ts . FileWatcherEventKind . Created ) ;
379
- } else if ( isVirtualTSX ( fileName , extraFileExtensions ) ) {
380
- callback ( fileName , ts . FileWatcherEventKind . Deleted ) ;
381
- } else {
382
- callback ( fileName , ts . FileWatcherEventKind . Changed ) ;
383
- }
384
- } ,
385
341
update : ( ) => callback ( fileName , ts . FileWatcherEventKind . Changed ) ,
386
342
} ) ;
387
343
@@ -420,44 +376,31 @@ export class TSService {
420
376
}
421
377
}
422
378
423
- /**
424
- * If the given filename is a extra extension file (.vue),
425
- * return a list of filenames containing virtual filename (.vue.tsx) and type def filename (.vue.d.ts).
426
- */
427
- function getRefreshTargetFileNames (
428
- fileName : string ,
429
- extraFileExtensions : string [ ]
430
- ) {
431
- if ( isExtra ( fileName , extraFileExtensions ) ) {
432
- return [ `${ fileName } .tsx` , `${ fileName } .d.ts` , fileName ] ;
433
- }
434
- return [ fileName ] ;
435
- }
436
-
437
379
/** If the given filename has extra extensions, returns the d.ts filename. */
438
- function toExtraDtsFileName ( fileName : string , extraFileExtensions : string [ ] ) {
439
- if ( isExtra ( fileName , extraFileExtensions ) ) {
440
- return `${ fileName } .d.ts` ;
441
- }
442
- return fileName ;
443
- }
444
-
445
- /** If the given filename is a virtual filename (.vue.tsx), returns the real filename. */
446
- function toRealFileName ( fileName : string , extraFileExtensions : string [ ] ) {
447
- if ( isVirtualTSX ( fileName , extraFileExtensions ) ) {
448
- return fileName . slice ( 0 , - 4 ) ;
380
+ function toExtraDtsFileNames ( fileName : string , extraFileExtensions : string [ ] ) {
381
+ const ext = getExtIfExtra ( fileName , extraFileExtensions ) ;
382
+ if ( ext != null ) {
383
+ return [ `${ fileName } .d.ts` , `${ fileName . slice ( 0 , - ext . length ) } .d${ ext } .ts` ] ;
449
384
}
450
- return fileName ;
385
+ return [ ] ;
451
386
}
452
387
453
388
/** Checks the given filename has extra extension or not. */
454
389
function isExtra ( fileName : string , extraFileExtensions : string [ ] ) : boolean {
390
+ return getExtIfExtra ( fileName , extraFileExtensions ) != null ;
391
+ }
392
+
393
+ /** Gets the file extension if the given file is an extra extension file. */
394
+ function getExtIfExtra (
395
+ fileName : string ,
396
+ extraFileExtensions : string [ ]
397
+ ) : string | null {
455
398
for ( const extraFileExtension of extraFileExtensions ) {
456
399
if ( fileName . endsWith ( extraFileExtension ) ) {
457
- return true ;
400
+ return extraFileExtension ;
458
401
}
459
402
}
460
- return false ;
403
+ return null ;
461
404
}
462
405
463
406
/** Checks the given filename is virtual file tsx or not. */
@@ -473,16 +416,6 @@ function isVirtualTSX(
473
416
return false ;
474
417
}
475
418
476
- /** Checks the given filename has extra extension with d.ts or not. */
477
- function isExtraDts ( fileName : string , extraFileExtensions : string [ ] ) : boolean {
478
- for ( const extraFileExtension of extraFileExtensions ) {
479
- if ( fileName . endsWith ( `${ extraFileExtension } .d.ts` ) ) {
480
- return true ;
481
- }
482
- }
483
- return false ;
484
- }
485
-
486
419
function formatDiagnostics ( diagnostics : ts . Diagnostic [ ] ) {
487
420
return ts . formatDiagnostics ( diagnostics , {
488
421
getCanonicalFileName : ( f ) => f ,
0 commit comments