@@ -872,7 +872,6 @@ namespace ts {
872
872
function lookupFromPackageJson ( ) : NonNullable < ResolutionMode > {
873
873
const scope = getPackageScopeForPath ( fileName , packageJsonInfoCache , host , options ) ;
874
874
return scope ?. packageJsonContent . type === "module" ? ModuleKind . ESNext : ModuleKind . CommonJS ;
875
-
876
875
}
877
876
}
878
877
@@ -983,7 +982,7 @@ namespace ts {
983
982
return optionsHaveChanges ( program . getCompilerOptions ( ) , newOptions , sourceFileAffectingCompilerOptions ) ;
984
983
}
985
984
986
- function createCreateProgramOptions ( rootNames : readonly string [ ] , options : CompilerOptions , host ?: CompilerHost , oldProgram ?: Program | OldBuildInfoProgram , configFileParsingDiagnostics ?: readonly Diagnostic [ ] ) : CreateProgramOptionsWithOldBuildInfoProgram {
985
+ function createCreateProgramOptions ( rootNames : readonly string [ ] , options : CompilerOptions , host ?: CompilerHost , oldProgram ?: Program | OldBuildInfoProgramConstructor , configFileParsingDiagnostics ?: readonly Diagnostic [ ] ) : CreateProgramOptionsWithOldBuildInfoProgramConstructor {
987
986
return {
988
987
rootNames,
989
988
options,
@@ -993,10 +992,6 @@ namespace ts {
993
992
} ;
994
993
}
995
994
996
- function isOldBuildInfoProgram ( program : Program | OldBuildInfoProgram | undefined ) : program is OldBuildInfoProgram {
997
- return ! ! ( program as OldBuildInfoProgram | undefined ) ?. getResolvedModule ;
998
- }
999
-
1000
995
/**
1001
996
* Create a new 'Program' instance. A Program is an immutable collection of 'SourceFile's and a 'CompilerOptions'
1002
997
* that represent a compilation unit.
@@ -1009,7 +1004,7 @@ namespace ts {
1009
1004
*/
1010
1005
export function createProgram ( createProgramOptions : CreateProgramOptions ) : Program ;
1011
1006
/*@internal */
1012
- export function createProgram ( createProgramOptions : CreateProgramOptionsWithOldBuildInfoProgram ) : Program ; // eslint-disable-line @typescript-eslint/unified-signatures
1007
+ export function createProgram ( createProgramOptions : CreateProgramOptionsWithOldBuildInfoProgramConstructor ) : Program ; // eslint-disable-line @typescript-eslint/unified-signatures
1013
1008
/**
1014
1009
* Create a new 'Program' instance. A Program is an immutable collection of 'SourceFile's and a 'CompilerOptions'
1015
1010
* that represent a compilation unit.
@@ -1025,12 +1020,10 @@ namespace ts {
1025
1020
* @returns A 'Program' object.
1026
1021
*/
1027
1022
export function createProgram ( rootNames : readonly string [ ] , options : CompilerOptions , host ?: CompilerHost , oldProgram ?: Program , configFileParsingDiagnostics ?: readonly Diagnostic [ ] ) : Program ;
1028
- export function createProgram ( rootNamesOrOptions : readonly string [ ] | CreateProgramOptionsWithOldBuildInfoProgram , _options ?: CompilerOptions , _host ?: CompilerHost , _oldProgram ?: Program , _configFileParsingDiagnostics ?: readonly Diagnostic [ ] ) : Program {
1023
+ export function createProgram ( rootNamesOrOptions : readonly string [ ] | CreateProgramOptionsWithOldBuildInfoProgramConstructor , _options ?: CompilerOptions , _host ?: CompilerHost , _oldProgram ?: Program , _configFileParsingDiagnostics ?: readonly Diagnostic [ ] ) : Program {
1029
1024
const createProgramOptions = isArray ( rootNamesOrOptions ) ? createCreateProgramOptions ( rootNamesOrOptions , _options ! , _host , _oldProgram , _configFileParsingDiagnostics ) : rootNamesOrOptions ; // TODO: GH#18217
1030
1025
const { rootNames, options, configFileParsingDiagnostics, projectReferences } = createProgramOptions ;
1031
- let { oldProgram : oldProgramOrOldBuildInfoProgram } = createProgramOptions ;
1032
- let oldProgram = isOldBuildInfoProgram ( oldProgramOrOldBuildInfoProgram ) ? undefined : oldProgramOrOldBuildInfoProgram ;
1033
- let oldBuildInfoProgram = isOldBuildInfoProgram ( oldProgramOrOldBuildInfoProgram ) ? oldProgramOrOldBuildInfoProgram : undefined ;
1026
+ let { oldProgram : oldProgramOrOldBuildInfoProgramConstructor } = createProgramOptions ;
1034
1027
1035
1028
let processingDefaultLibFiles : SourceFile [ ] | undefined ;
1036
1029
let processingOtherFiles : SourceFile [ ] | undefined ;
@@ -1178,8 +1171,9 @@ namespace ts {
1178
1171
loadWithTypeDirectiveCache ( typeReferenceDirectiveNames , containingFile , redirectedReference , containingFileMode , loader ) ;
1179
1172
}
1180
1173
1181
- let oldBuildInfoProgramResolutionHost : OldBuildInfoProgramResolutionHost | undefined ;
1182
- if ( oldBuildInfoProgram ) {
1174
+ let oldProgram = typeof oldProgramOrOldBuildInfoProgramConstructor === "object" ? oldProgramOrOldBuildInfoProgramConstructor : undefined ;
1175
+ let oldBuildInfoProgram : OldBuildInfoProgram | undefined ;
1176
+ if ( ! oldProgram && typeof oldProgramOrOldBuildInfoProgramConstructor === "function" ) {
1183
1177
const state : ModuleResolutionState = {
1184
1178
host,
1185
1179
compilerOptions : options ,
@@ -1192,21 +1186,22 @@ namespace ts {
1192
1186
requestContainingDirectory : undefined ,
1193
1187
reportResolutionDiagnostic : noop
1194
1188
} ;
1195
- oldBuildInfoProgramResolutionHost = {
1189
+ oldBuildInfoProgram = oldProgramOrOldBuildInfoProgramConstructor ( {
1196
1190
fileExists : fileName => host . fileExists ( fileName ) ,
1197
1191
createHash : maybeBind ( host , host . createHash ) ,
1198
1192
getPackageJsonInfo : fileName => getPackageJsonInfo ( getDirectoryPath ( fileName ) , /*onlyRecordFailures*/ false , state ) ,
1199
- } ;
1200
- // Ensure redirected references are verified before using existing cache
1201
- oldBuildInfoProgram . clearRedirectsMap ( ) ;
1202
- moduleResolutionCache ?. setOldResolutionCache ( {
1203
- getResolved : ( dirPath , name , mode , redirectedReference ) =>
1204
- oldBuildInfoProgram ?. getResolvedModule ( oldBuildInfoProgramResolutionHost ! , dirPath , name , mode , redirectedReference )
1205
- } ) ;
1206
- typeReferenceDirectiveResolutionCache ?. setOldResolutionCache ( {
1207
- getResolved : ( dirPath , name , mode , redirectedReference ) =>
1208
- oldBuildInfoProgram ?. getResolvedTypeReferenceDirective ( oldBuildInfoProgramResolutionHost ! , dirPath , name , mode , redirectedReference )
1209
1193
} ) ;
1194
+ if ( oldBuildInfoProgram ) {
1195
+ // Ensure redirected references are verified before using existing cache
1196
+ moduleResolutionCache ?. setOldResolutionCache ( {
1197
+ getResolved : ( dirPath , name , mode , redirectedReference ) =>
1198
+ oldBuildInfoProgram ?. getResolvedModule ( dirPath , name , mode , redirectedReference )
1199
+ } ) ;
1200
+ typeReferenceDirectiveResolutionCache ?. setOldResolutionCache ( {
1201
+ getResolved : ( dirPath , name , mode , redirectedReference ) =>
1202
+ oldBuildInfoProgram ?. getResolvedTypeReferenceDirective ( dirPath , name , mode , redirectedReference )
1203
+ } ) ;
1204
+ }
1210
1205
}
1211
1206
1212
1207
// Map from a stringified PackageId to the source file with that id.
@@ -1380,7 +1375,7 @@ namespace ts {
1380
1375
// unconditionally set oldProgram to undefined to prevent it from being captured in closure
1381
1376
oldProgram = undefined ;
1382
1377
oldBuildInfoProgram = undefined ;
1383
- oldProgramOrOldBuildInfoProgram = undefined ;
1378
+ oldProgramOrOldBuildInfoProgramConstructor = undefined ;
1384
1379
1385
1380
const program : Program = {
1386
1381
getRootFileNames : ( ) => rootNames ,
@@ -1650,7 +1645,6 @@ namespace ts {
1650
1645
const oldResolution = ! oldBuildInfoProgram ?
1651
1646
oldSourceFile ?. resolvedModules ?. get ( moduleName , mode ) :
1652
1647
oldBuildInfoProgram . getResolvedModule (
1653
- oldBuildInfoProgramResolutionHost ! ,
1654
1648
getDirectoryPath ( file . path ) ,
1655
1649
moduleName ,
1656
1650
mode ,
@@ -1824,7 +1818,6 @@ namespace ts {
1824
1818
const oldResolution = ! oldBuildInfoProgram ?
1825
1819
( containingSourceFile ? oldSourceFile ?. resolvedTypeReferenceDirectiveNames : oldProgram ?. getAutomaticTypeDirectiveResolutions ( ) ) ?. get ( typeDirectiveName , mode ) :
1826
1820
oldBuildInfoProgram . getResolvedTypeReferenceDirective (
1827
- oldBuildInfoProgramResolutionHost ! ,
1828
1821
getDirectoryPath ( containingSourceFile ? containingSourceFile . path : toPath ( inferredTypeFile ! ) ) ,
1829
1822
typeDirectiveName ,
1830
1823
mode ,
@@ -1913,13 +1906,13 @@ namespace ts {
1913
1906
function tryReuseStructureFromOldProgram ( ) : StructureIsReused {
1914
1907
// check properties that can affect structure of the program or module resolution strategy
1915
1908
// if any of these properties has changed - structure cannot be reused
1916
- const oldOptions = oldProgramOrOldBuildInfoProgram ?. getCompilerOptions ( ) ;
1909
+ const oldOptions = oldProgram ?. getCompilerOptions ( ) || oldBuildInfoProgram ?. getCompilerOptions ( ) ;
1917
1910
if ( ! oldOptions || changesAffectModuleResolution ( oldOptions , options ) ) {
1918
1911
return StructureIsReused . Not ;
1919
1912
}
1920
1913
1921
1914
const result = tryReuseStructureFromOldProgramWorker ( ) ;
1922
- return options . cacheResolutions && oldProgramOrOldBuildInfoProgram && result === StructureIsReused . Not ?
1915
+ return options . cacheResolutions && ( oldProgram || oldBuildInfoProgram ) && result === StructureIsReused . Not ?
1923
1916
StructureIsReused . SafeModuleCache :
1924
1917
result ;
1925
1918
}
0 commit comments