@@ -1545,7 +1545,6 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1545
1545
let commonSourceDirectory : string ;
1546
1546
let typeChecker : TypeChecker ;
1547
1547
let classifiableNames : Set < __String > ;
1548
- const ambientModuleNameToUnmodifiedFileName = new Map < string , string > ( ) ;
1549
1548
let fileReasons = createMultiMap < Path , FileIncludeReason > ( ) ;
1550
1549
let filesWithReferencesProcessed : Set < Path > | undefined ;
1551
1550
let fileReasonsToChain : Map < Path , FileReasonToChainCache > | undefined ;
@@ -2250,52 +2249,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2250
2249
canReuseResolutionsInFile : ( ) =>
2251
2250
containingFile === oldProgram ?. getSourceFile ( containingFile . fileName ) &&
2252
2251
! hasInvalidatedResolutions ( containingFile . path ) ,
2253
- isEntryResolvingToAmbientModule : moduleNameResolvesToAmbientModule ,
2252
+ resolveToOwnAmbientModule : true ,
2254
2253
} ) ;
2255
2254
}
2256
2255
2257
- function moduleNameResolvesToAmbientModule ( moduleName : StringLiteralLike , file : SourceFile ) {
2258
- // We know moduleName resolves to an ambient module provided that moduleName:
2259
- // - is in the list of ambient modules locally declared in the current source file.
2260
- // - resolved to an ambient module in the old program whose declaration is in an unmodified file
2261
- // (so the same module declaration will land in the new program)
2262
- if ( contains ( file . ambientModuleNames , moduleName . text ) ) {
2263
- if ( isTraceEnabled ( options , host ) ) {
2264
- trace ( host , Diagnostics . Module_0_was_resolved_as_locally_declared_ambient_module_in_file_1 , moduleName . text , getNormalizedAbsolutePath ( file . originalFileName , currentDirectory ) ) ;
2265
- }
2266
- return true ;
2267
- }
2268
- else {
2269
- return moduleNameResolvesToAmbientModuleInNonModifiedFile ( moduleName , file ) ;
2270
- }
2271
- }
2272
-
2273
- // If we change our policy of rechecking failed lookups on each program create,
2274
- // we should adjust the value returned here.
2275
- function moduleNameResolvesToAmbientModuleInNonModifiedFile ( moduleName : StringLiteralLike , file : SourceFile ) : boolean {
2276
- const resolutionToFile = oldProgram ?. getResolvedModule ( file , moduleName . text , getModeForUsageLocation ( file , moduleName ) ) ?. resolvedModule ;
2277
- const resolvedFile = resolutionToFile && oldProgram ! . getSourceFile ( resolutionToFile . resolvedFileName ) ;
2278
- if ( resolutionToFile && resolvedFile ) {
2279
- // In the old program, we resolved to an ambient module that was in the same
2280
- // place as we expected to find an actual module file.
2281
- // We actually need to return 'false' here even though this seems like a 'true' case
2282
- // because the normal module resolution algorithm will find this anyway.
2283
- return false ;
2284
- }
2285
-
2286
- // at least one of declarations should come from non-modified source file
2287
- const unmodifiedFile = ambientModuleNameToUnmodifiedFileName . get ( moduleName . text ) ;
2288
-
2289
- if ( ! unmodifiedFile ) {
2290
- return false ;
2291
- }
2292
-
2293
- if ( isTraceEnabled ( options , host ) ) {
2294
- trace ( host , Diagnostics . Module_0_was_resolved_as_ambient_module_declared_in_1_since_this_file_was_not_modified , moduleName . text , unmodifiedFile ) ;
2295
- }
2296
- return true ;
2297
- }
2298
-
2299
2256
function resolveTypeReferenceDirectiveNamesReusingOldState ( typeDirectiveNames : readonly FileReference [ ] , containingFile : SourceFile ) : readonly ResolvedTypeReferenceDirectiveWithFailedLookupLocations [ ] ;
2300
2257
function resolveTypeReferenceDirectiveNamesReusingOldState ( typeDirectiveNames : readonly string [ ] , containingFile : string ) : readonly ResolvedTypeReferenceDirectiveWithFailedLookupLocations [ ] ;
2301
2258
function resolveTypeReferenceDirectiveNamesReusingOldState < T extends string | FileReference > ( typeDirectiveNames : readonly T [ ] , containingFile : string | SourceFile ) : readonly ResolvedTypeReferenceDirectiveWithFailedLookupLocations [ ] {
@@ -2333,7 +2290,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2333
2290
getResolutionFromOldProgram : ( name : string , mode : ResolutionMode ) => Resolution | undefined ;
2334
2291
getResolved : ( oldResolution : Resolution ) => ResolutionWithResolvedFileName | undefined ;
2335
2292
canReuseResolutionsInFile : ( ) => boolean ;
2336
- isEntryResolvingToAmbientModule ?: ( entry : Entry , containingFile : SourceFileOrString ) => boolean ;
2293
+ resolveToOwnAmbientModule ?: true ;
2337
2294
}
2338
2295
2339
2296
function resolveNamesReusingOldState < Entry , SourceFileOrString , SourceFileOrUndefined extends SourceFile | undefined , Resolution > ( {
@@ -2346,10 +2303,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2346
2303
getResolutionFromOldProgram,
2347
2304
getResolved,
2348
2305
canReuseResolutionsInFile,
2349
- isEntryResolvingToAmbientModule ,
2306
+ resolveToOwnAmbientModule ,
2350
2307
} : ResolveNamesReusingOldStateInput < Entry , SourceFileOrString , SourceFileOrUndefined , Resolution > ) : readonly Resolution [ ] {
2351
2308
if ( ! entries . length ) return emptyArray ;
2352
- if ( structureIsReused === StructureIsReused . Not && ( ! isEntryResolvingToAmbientModule || ! containingSourceFile ! . ambientModuleNames . length ) ) {
2309
+ if ( structureIsReused === StructureIsReused . Not && ( ! resolveToOwnAmbientModule || ! containingSourceFile ! . ambientModuleNames . length ) ) {
2353
2310
// If the old program state does not permit reusing resolutions and `file` does not contain locally defined ambient modules,
2354
2311
// the best we can do is fallback to the default logic.
2355
2312
return resolutionWorker (
@@ -2394,14 +2351,27 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2394
2351
continue ;
2395
2352
}
2396
2353
}
2397
- if ( isEntryResolvingToAmbientModule ?.( entry , containingFile ) ) {
2398
- ( result ??= new Array ( entries . length ) ) [ i ] = emptyResolution ;
2399
- }
2400
- else {
2401
- // Resolution failed in the old program, or resolved to an ambient module for which we can't reuse the result.
2402
- ( unknownEntries ??= [ ] ) . push ( entry ) ;
2403
- ( unknownEntryIndices ??= [ ] ) . push ( i ) ;
2354
+ if ( resolveToOwnAmbientModule ) {
2355
+ const name = nameAndModeGetter . getName ( entry ) ;
2356
+ // We know moduleName resolves to an ambient module provided that moduleName:
2357
+ // - is in the list of ambient modules locally declared in the current source file.
2358
+ if ( contains ( containingSourceFile ! . ambientModuleNames , name ) ) {
2359
+ if ( isTraceEnabled ( options , host ) ) {
2360
+ trace (
2361
+ host ,
2362
+ Diagnostics . Module_0_was_resolved_as_locally_declared_ambient_module_in_file_1 ,
2363
+ name ,
2364
+ getNormalizedAbsolutePath ( containingSourceFile ! . originalFileName , currentDirectory ) ,
2365
+ ) ;
2366
+ }
2367
+ ( result ??= new Array ( entries . length ) ) [ i ] = emptyResolution ;
2368
+ continue ;
2369
+ }
2404
2370
}
2371
+
2372
+ // Resolution failed in the old program, or resolved to an ambient module for which we can't reuse the result.
2373
+ ( unknownEntries ??= [ ] ) . push ( entry ) ;
2374
+ ( unknownEntryIndices ??= [ ] ) . push ( i ) ;
2405
2375
}
2406
2376
2407
2377
if ( ! unknownEntries ) return result ! ;
@@ -2586,11 +2556,6 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2586
2556
// add file to the modified list so that we will resolve it later
2587
2557
modifiedSourceFiles . push ( newSourceFile ) ;
2588
2558
}
2589
- else {
2590
- for ( const moduleName of oldSourceFile . ambientModuleNames ) {
2591
- ambientModuleNameToUnmodifiedFileName . set ( moduleName , oldSourceFile . fileName ) ;
2592
- }
2593
- }
2594
2559
2595
2560
// if file has passed all checks it should be safe to reuse it
2596
2561
newSourceFiles . push ( newSourceFile ) ;
0 commit comments