@@ -1481,6 +1481,15 @@ namespace ts {
1481
1481
version : string ,
1482
1482
scriptKind ?: ScriptKind ) : SourceFile ;
1483
1483
1484
+ acquireDocumentWithKey (
1485
+ fileName : string ,
1486
+ path : Path ,
1487
+ compilationSettings : CompilerOptions ,
1488
+ key : DocumentRegistryBucketKey ,
1489
+ scriptSnapshot : IScriptSnapshot ,
1490
+ version : string ,
1491
+ scriptKind ?: ScriptKind ) : SourceFile ;
1492
+
1484
1493
/**
1485
1494
* Request an updated version of an already existing SourceFile with a given fileName
1486
1495
* and compilationSettings. The update will in-turn call updateLanguageServiceSourceFile
@@ -1500,6 +1509,16 @@ namespace ts {
1500
1509
version : string ,
1501
1510
scriptKind ?: ScriptKind ) : SourceFile ;
1502
1511
1512
+ updateDocumentWithKey (
1513
+ fileName : string ,
1514
+ path : Path ,
1515
+ compilationSettings : CompilerOptions ,
1516
+ key : DocumentRegistryBucketKey ,
1517
+ scriptSnapshot : IScriptSnapshot ,
1518
+ version : string ,
1519
+ scriptKind ?: ScriptKind ) : SourceFile ;
1520
+
1521
+ getKeyForCompilationSettings ( settings : CompilerOptions ) : DocumentRegistryBucketKey ;
1503
1522
/**
1504
1523
* Informs the DocumentRegistry that a file is not needed any longer.
1505
1524
*
@@ -1511,9 +1530,13 @@ namespace ts {
1511
1530
*/
1512
1531
releaseDocument ( fileName : string , compilationSettings : CompilerOptions ) : void ;
1513
1532
1533
+ releaseDocumentWithKey ( path : Path , key : DocumentRegistryBucketKey ) : void ;
1534
+
1514
1535
reportStats ( ) : string ;
1515
1536
}
1516
1537
1538
+ export type DocumentRegistryBucketKey = string & { __bucketKey : any } ;
1539
+
1517
1540
// TODO: move these to enums
1518
1541
export namespace ScriptElementKind {
1519
1542
export const unknown = "" ;
@@ -1783,11 +1806,13 @@ namespace ts {
1783
1806
1784
1807
public getOrCreateEntry ( fileName : string ) : HostFileInformation {
1785
1808
const path = toPath ( fileName , this . currentDirectory , this . getCanonicalFileName ) ;
1786
- if ( this . contains ( path ) ) {
1787
- return this . getEntry ( path ) ;
1788
- }
1809
+ return this . getOrCreateEntryByPath ( fileName , path ) ;
1810
+ }
1789
1811
1790
- return this . createEntry ( fileName , path ) ;
1812
+ public getOrCreateEntryByPath ( fileName : string , path : Path ) : HostFileInformation {
1813
+ return this . contains ( path )
1814
+ ? this . getEntry ( path )
1815
+ : this . createEntry ( fileName , path ) ;
1791
1816
}
1792
1817
1793
1818
public getRootFileNames ( ) : string [ ] {
@@ -2041,12 +2066,11 @@ namespace ts {
2041
2066
const buckets : Map < FileMap < DocumentRegistryEntry > > = { } ;
2042
2067
const getCanonicalFileName = createGetCanonicalFileName ( ! ! useCaseSensitiveFileNames ) ;
2043
2068
2044
- function getKeyFromCompilationSettings ( settings : CompilerOptions ) : string {
2045
- return "_" + settings . target + "|" + settings . module + "|" + settings . noResolve + "|" + settings . jsx + + "|" + settings . allowJs ;
2069
+ function getKeyForCompilationSettings ( settings : CompilerOptions ) : DocumentRegistryBucketKey {
2070
+ return < DocumentRegistryBucketKey > `_ ${ settings . target } | ${ settings . module } | ${ settings . noResolve } | ${ settings . jsx } | ${ settings . allowJs } | ${ settings . baseUrl } | ${ settings . typesRoot } | ${ settings . typesSearchPaths } | ${ JSON . stringify ( settings . rootDirs ) } | ${ JSON . stringify ( settings . paths ) } ` ;
2046
2071
}
2047
2072
2048
- function getBucketForCompilationSettings ( settings : CompilerOptions , createIfMissing : boolean ) : FileMap < DocumentRegistryEntry > {
2049
- const key = getKeyFromCompilationSettings ( settings ) ;
2073
+ function getBucketForCompilationSettings ( key : DocumentRegistryBucketKey , createIfMissing : boolean ) : FileMap < DocumentRegistryEntry > {
2050
2074
let bucket = lookUp ( buckets , key ) ;
2051
2075
if ( ! bucket && createIfMissing ) {
2052
2076
buckets [ key ] = bucket = createFileMap < DocumentRegistryEntry > ( ) ;
@@ -2075,23 +2099,36 @@ namespace ts {
2075
2099
}
2076
2100
2077
2101
function acquireDocument ( fileName : string , compilationSettings : CompilerOptions , scriptSnapshot : IScriptSnapshot , version : string , scriptKind ?: ScriptKind ) : SourceFile {
2078
- return acquireOrUpdateDocument ( fileName , compilationSettings , scriptSnapshot , version , /*acquiring*/ true , scriptKind ) ;
2102
+ const path = toPath ( fileName , currentDirectory , getCanonicalFileName ) ;
2103
+ const key = getKeyForCompilationSettings ( compilationSettings ) ;
2104
+ return acquireDocumentWithKey ( fileName , path , compilationSettings , key , scriptSnapshot , version , scriptKind ) ;
2105
+ }
2106
+
2107
+ function acquireDocumentWithKey ( fileName : string , path : Path , compilationSettings : CompilerOptions , key : DocumentRegistryBucketKey , scriptSnapshot : IScriptSnapshot , version : string , scriptKind ?: ScriptKind ) : SourceFile {
2108
+ return acquireOrUpdateDocument ( fileName , path , compilationSettings , key , scriptSnapshot , version , /*acquiring*/ true , scriptKind ) ;
2079
2109
}
2080
2110
2081
2111
function updateDocument ( fileName : string , compilationSettings : CompilerOptions , scriptSnapshot : IScriptSnapshot , version : string , scriptKind ?: ScriptKind ) : SourceFile {
2082
- return acquireOrUpdateDocument ( fileName , compilationSettings , scriptSnapshot , version , /*acquiring*/ false , scriptKind ) ;
2112
+ const path = toPath ( fileName , currentDirectory , getCanonicalFileName ) ;
2113
+ const key = getKeyForCompilationSettings ( compilationSettings ) ;
2114
+ return updateDocumentWithKey ( fileName , path , compilationSettings , key , scriptSnapshot , version , scriptKind ) ;
2115
+ }
2116
+
2117
+ function updateDocumentWithKey ( fileName : string , path : Path , compilationSettings : CompilerOptions , key : DocumentRegistryBucketKey , scriptSnapshot : IScriptSnapshot , version : string , scriptKind ?: ScriptKind ) : SourceFile {
2118
+ return acquireOrUpdateDocument ( fileName , path , compilationSettings , key , scriptSnapshot , version , /*acquiring*/ false , scriptKind ) ;
2083
2119
}
2084
2120
2085
2121
function acquireOrUpdateDocument (
2086
2122
fileName : string ,
2123
+ path : Path ,
2087
2124
compilationSettings : CompilerOptions ,
2125
+ key : DocumentRegistryBucketKey ,
2088
2126
scriptSnapshot : IScriptSnapshot ,
2089
2127
version : string ,
2090
2128
acquiring : boolean ,
2091
2129
scriptKind ?: ScriptKind ) : SourceFile {
2092
2130
2093
- const bucket = getBucketForCompilationSettings ( compilationSettings , /*createIfMissing*/ true ) ;
2094
- const path = toPath ( fileName , currentDirectory , getCanonicalFileName ) ;
2131
+ const bucket = getBucketForCompilationSettings ( key , /*createIfMissing*/ true ) ;
2095
2132
let entry = bucket . get ( path ) ;
2096
2133
if ( ! entry ) {
2097
2134
Debug . assert ( acquiring , "How could we be trying to update a document that the registry doesn't have?" ) ;
@@ -2129,10 +2166,14 @@ namespace ts {
2129
2166
}
2130
2167
2131
2168
function releaseDocument ( fileName : string , compilationSettings : CompilerOptions ) : void {
2132
- const bucket = getBucketForCompilationSettings ( compilationSettings , /*createIfMissing*/ false ) ;
2133
- Debug . assert ( bucket !== undefined ) ;
2134
-
2135
2169
const path = toPath ( fileName , currentDirectory , getCanonicalFileName ) ;
2170
+ const key = getKeyForCompilationSettings ( compilationSettings ) ;
2171
+ return releaseDocumentWithKey ( path , key ) ;
2172
+ }
2173
+
2174
+ function releaseDocumentWithKey ( path : Path , key : DocumentRegistryBucketKey ) : void {
2175
+ const bucket = getBucketForCompilationSettings ( key , /*createIfMissing*/ false ) ;
2176
+ Debug . assert ( bucket !== undefined ) ;
2136
2177
2137
2178
const entry = bucket . get ( path ) ;
2138
2179
entry . languageServiceRefCount -- ;
@@ -2145,9 +2186,13 @@ namespace ts {
2145
2186
2146
2187
return {
2147
2188
acquireDocument,
2189
+ acquireDocumentWithKey,
2148
2190
updateDocument,
2191
+ updateDocumentWithKey,
2149
2192
releaseDocument,
2150
- reportStats
2193
+ releaseDocumentWithKey,
2194
+ reportStats,
2195
+ getKeyForCompilationSettings
2151
2196
} ;
2152
2197
}
2153
2198
@@ -2858,6 +2903,7 @@ namespace ts {
2858
2903
// Now create a new compiler
2859
2904
const compilerHost : CompilerHost = {
2860
2905
getSourceFile : getOrCreateSourceFile ,
2906
+ getSourceFileByPath : getOrCreateSourceFileByPath ,
2861
2907
getCancellationToken : ( ) => cancellationToken ,
2862
2908
getCanonicalFileName,
2863
2909
useCaseSensitiveFileNames : ( ) => useCaseSensitivefileNames ,
@@ -2893,15 +2939,17 @@ namespace ts {
2893
2939
} ;
2894
2940
}
2895
2941
2942
+ const documentRegistryBucketKey = documentRegistry . getKeyForCompilationSettings ( newSettings ) ;
2896
2943
const newProgram = createProgram ( hostCache . getRootFileNames ( ) , newSettings , compilerHost , program ) ;
2897
2944
2898
2945
// Release any files we have acquired in the old program but are
2899
2946
// not part of the new program.
2900
2947
if ( program ) {
2901
2948
const oldSourceFiles = program . getSourceFiles ( ) ;
2949
+ const oldSettingsKey = documentRegistry . getKeyForCompilationSettings ( oldSettings ) ;
2902
2950
for ( const oldSourceFile of oldSourceFiles ) {
2903
2951
if ( ! newProgram . getSourceFile ( oldSourceFile . fileName ) || changesInCompilationSettingsAffectSyntax ) {
2904
- documentRegistry . releaseDocument ( oldSourceFile . fileName , oldSettings ) ;
2952
+ documentRegistry . releaseDocumentWithKey ( oldSourceFile . path , oldSettingsKey ) ;
2905
2953
}
2906
2954
}
2907
2955
}
@@ -2918,11 +2966,15 @@ namespace ts {
2918
2966
return ;
2919
2967
2920
2968
function getOrCreateSourceFile ( fileName : string ) : SourceFile {
2969
+ return getOrCreateSourceFileByPath ( fileName , toPath ( fileName , currentDirectory , getCanonicalFileName ) ) ;
2970
+ }
2971
+
2972
+ function getOrCreateSourceFileByPath ( fileName : string , path : Path ) : SourceFile {
2921
2973
Debug . assert ( hostCache !== undefined ) ;
2922
2974
// The program is asking for this file, check first if the host can locate it.
2923
2975
// If the host can not locate the file, then it does not exist. return undefined
2924
2976
// to the program to allow reporting of errors for missing files.
2925
- const hostFileInformation = hostCache . getOrCreateEntry ( fileName ) ;
2977
+ const hostFileInformation = hostCache . getOrCreateEntryByPath ( fileName , path ) ;
2926
2978
if ( ! hostFileInformation ) {
2927
2979
return undefined ;
2928
2980
}
@@ -2932,7 +2984,7 @@ namespace ts {
2932
2984
// can not be reused. we have to dump all syntax trees and create new ones.
2933
2985
if ( ! changesInCompilationSettingsAffectSyntax ) {
2934
2986
// Check if the old program had this file already
2935
- const oldSourceFile = program && program . getSourceFile ( fileName ) ;
2987
+ const oldSourceFile = program && program . getSourceFileByPath ( path ) ;
2936
2988
if ( oldSourceFile ) {
2937
2989
// We already had a source file for this file name. Go to the registry to
2938
2990
// ensure that we get the right up to date version of it. We need this to
@@ -2959,16 +3011,16 @@ namespace ts {
2959
3011
// We do not support the scenario where a host can modify a registered
2960
3012
// file's script kind, i.e. in one project some file is treated as ".ts"
2961
3013
// and in another as ".js"
2962
- Debug . assert ( hostFileInformation . scriptKind === oldSourceFile . scriptKind , "Registered script kind (" + oldSourceFile . scriptKind + ") should match new script kind (" + hostFileInformation . scriptKind + ") for file: " + fileName ) ;
3014
+ Debug . assert ( hostFileInformation . scriptKind === oldSourceFile . scriptKind , "Registered script kind (" + oldSourceFile . scriptKind + ") should match new script kind (" + hostFileInformation . scriptKind + ") for file: " + path ) ;
2963
3015
2964
- return documentRegistry . updateDocument ( fileName , newSettings , hostFileInformation . scriptSnapshot , hostFileInformation . version , hostFileInformation . scriptKind ) ;
3016
+ return documentRegistry . updateDocumentWithKey ( fileName , path , newSettings , documentRegistryBucketKey , hostFileInformation . scriptSnapshot , hostFileInformation . version , hostFileInformation . scriptKind ) ;
2965
3017
}
2966
3018
2967
3019
// We didn't already have the file. Fall through and acquire it from the registry.
2968
3020
}
2969
3021
2970
3022
// Could not find this file in the old program, create a new SourceFile for it.
2971
- return documentRegistry . acquireDocument ( fileName , newSettings , hostFileInformation . scriptSnapshot , hostFileInformation . version , hostFileInformation . scriptKind ) ;
3023
+ return documentRegistry . acquireDocumentWithKey ( fileName , path , newSettings , documentRegistryBucketKey , hostFileInformation . scriptSnapshot , hostFileInformation . version , hostFileInformation . scriptKind ) ;
2972
3024
}
2973
3025
2974
3026
function sourceFileUpToDate ( sourceFile : SourceFile ) : boolean {
0 commit comments