@@ -26,7 +26,8 @@ import {
26
26
findTsConfigPath ,
27
27
getNearestWorkspaceUri ,
28
28
hasTsExtensions ,
29
- isSvelteFilePath
29
+ isSvelteFilePath ,
30
+ toVirtualSvelteFilePath
30
31
} from './utils' ;
31
32
import { createProject , ProjectService } from './serviceCache' ;
32
33
import { internalHelpers } from 'svelte2tsx' ;
@@ -974,14 +975,19 @@ async function createLanguageService(
974
975
}
975
976
976
977
const oldProgram = project ?. program ;
977
- const program = languageService . getProgram ( ) ;
978
+ let program : ts . Program | undefined ;
979
+ try {
980
+ program = languageService . getProgram ( ) ;
981
+ } finally {
982
+ // mark as clean even if the update fails, at least we can still try again next time there is a change
983
+ dirty = false ;
984
+ }
978
985
svelteModuleLoader . clearPendingInvalidations ( ) ;
979
986
980
987
if ( project ) {
981
988
project . program = program ;
982
989
}
983
990
984
- dirty = false ;
985
991
compilerHost = undefined ;
986
992
987
993
if ( ! skipSvelteInputCheck ) {
@@ -1376,38 +1382,93 @@ function getOrCreateDocumentRegistry(
1376
1382
1377
1383
registry = ts . createDocumentRegistry ( useCaseSensitiveFileNames , currentDirectory ) ;
1378
1384
1379
- // impliedNodeFormat is always undefined when the svelte source file is created
1380
- // We might patched it later but the registry doesn't know about it
1381
- const releaseDocumentWithKey = registry . releaseDocumentWithKey ;
1382
- registry . releaseDocumentWithKey = (
1385
+ const acquireDocumentWithKey = registry . acquireDocumentWithKey ;
1386
+ registry . acquireDocumentWithKey = (
1387
+ fileName : string ,
1383
1388
path : ts . Path ,
1389
+ compilationSettingsOrHost : ts . CompilerOptions | ts . MinimalResolutionCacheHost ,
1384
1390
key : ts . DocumentRegistryBucketKey ,
1385
- scriptKind : ts . ScriptKind ,
1386
- impliedNodeFormat ?: ts . ResolutionMode
1391
+ scriptSnapshot : ts . IScriptSnapshot ,
1392
+ version : string ,
1393
+ scriptKind ?: ts . ScriptKind ,
1394
+ sourceFileOptions ?: ts . CreateSourceFileOptions | ts . ScriptTarget
1387
1395
) => {
1388
- if ( isSvelteFilePath ( path ) ) {
1389
- releaseDocumentWithKey ( path , key , scriptKind , undefined ) ;
1390
- return ;
1391
- }
1396
+ ensureImpliedNodeFormat ( compilationSettingsOrHost , fileName , sourceFileOptions ) ;
1392
1397
1393
- releaseDocumentWithKey ( path , key , scriptKind , impliedNodeFormat ) ;
1398
+ return acquireDocumentWithKey (
1399
+ fileName ,
1400
+ path ,
1401
+ compilationSettingsOrHost ,
1402
+ key ,
1403
+ scriptSnapshot ,
1404
+ version ,
1405
+ scriptKind ,
1406
+ sourceFileOptions
1407
+ ) ;
1394
1408
} ;
1395
1409
1396
- registry . releaseDocument = (
1410
+ const updateDocumentWithKey = registry . updateDocumentWithKey ;
1411
+ registry . updateDocumentWithKey = (
1397
1412
fileName : string ,
1398
- compilationSettings : ts . CompilerOptions ,
1399
- scriptKind : ts . ScriptKind ,
1400
- impliedNodeFormat ?: ts . ResolutionMode
1413
+ path : ts . Path ,
1414
+ compilationSettingsOrHost : ts . CompilerOptions | ts . MinimalResolutionCacheHost ,
1415
+ key : ts . DocumentRegistryBucketKey ,
1416
+ scriptSnapshot : ts . IScriptSnapshot ,
1417
+ version : string ,
1418
+ scriptKind ?: ts . ScriptKind ,
1419
+ sourceFileOptions ?: ts . CreateSourceFileOptions | ts . ScriptTarget
1401
1420
) => {
1402
- if ( isSvelteFilePath ( fileName ) ) {
1403
- registry ?. releaseDocument ( fileName , compilationSettings , scriptKind , undefined ) ;
1404
- return ;
1405
- }
1421
+ ensureImpliedNodeFormat ( compilationSettingsOrHost , fileName , sourceFileOptions ) ;
1406
1422
1407
- registry ?. releaseDocument ( fileName , compilationSettings , scriptKind , impliedNodeFormat ) ;
1423
+ return updateDocumentWithKey (
1424
+ fileName ,
1425
+ path ,
1426
+ compilationSettingsOrHost ,
1427
+ key ,
1428
+ scriptSnapshot ,
1429
+ version ,
1430
+ scriptKind ,
1431
+ sourceFileOptions
1432
+ ) ;
1408
1433
} ;
1409
1434
1410
1435
documentRegistries . set ( key , registry ) ;
1411
1436
1412
1437
return registry ;
1438
+
1439
+ function ensureImpliedNodeFormat (
1440
+ compilationSettingsOrHost : ts . CompilerOptions | ts . MinimalResolutionCacheHost ,
1441
+ fileName : string ,
1442
+ sourceFileOptions : ts . CreateSourceFileOptions | ts . ScriptTarget | undefined
1443
+ ) {
1444
+ const compilationSettings = getCompilationSettings ( compilationSettingsOrHost ) ;
1445
+ const host : ts . MinimalResolutionCacheHost | undefined =
1446
+ compilationSettingsOrHost === compilationSettings
1447
+ ? undefined
1448
+ : ( compilationSettingsOrHost as ts . MinimalResolutionCacheHost ) ;
1449
+ if (
1450
+ host &&
1451
+ isSvelteFilePath ( fileName ) &&
1452
+ typeof sourceFileOptions === 'object' &&
1453
+ ! sourceFileOptions . impliedNodeFormat
1454
+ ) {
1455
+ const format = ts . getImpliedNodeFormatForFile (
1456
+ toVirtualSvelteFilePath ( fileName ) ,
1457
+ host ?. getCompilerHost ?.( ) ?. getModuleResolutionCache ?.( ) ?. getPackageJsonInfoCache ( ) ,
1458
+ host ,
1459
+ compilationSettings
1460
+ ) ;
1461
+
1462
+ sourceFileOptions . impliedNodeFormat = format ;
1463
+ }
1464
+ }
1465
+
1466
+ function getCompilationSettings (
1467
+ settingsOrHost : ts . CompilerOptions | ts . MinimalResolutionCacheHost
1468
+ ) {
1469
+ if ( typeof settingsOrHost . getCompilationSettings === 'function' ) {
1470
+ return ( settingsOrHost as ts . MinimalResolutionCacheHost ) . getCompilationSettings ( ) ;
1471
+ }
1472
+ return settingsOrHost as ts . CompilerOptions ;
1473
+ }
1413
1474
}
0 commit comments