@@ -1670,32 +1670,33 @@ namespace ts {
1670
1670
function resolveTypeReferenceDirectiveNamesReusingOldState ( typeDirectiveNames : readonly FileReference [ ] , containingFile : SourceFile ) : readonly ( ResolvedTypeReferenceDirective | undefined ) [ ] ;
1671
1671
function resolveTypeReferenceDirectiveNamesReusingOldState ( typeDirectiveNames : string [ ] , containingFile : string ) : readonly ( ResolvedTypeReferenceDirective | undefined ) [ ] ;
1672
1672
function resolveTypeReferenceDirectiveNamesReusingOldState ( typeDirectiveNames : string [ ] | readonly FileReference [ ] , containingFile : string | SourceFile ) : readonly ( ResolvedTypeReferenceDirective | undefined ) [ ] {
1673
- // TODO: (shkamat) reuse auto type reference resolutions
1674
- if ( structureIsReused === StructureIsReused . Not || isString ( containingFile ) ) {
1673
+ if ( structureIsReused === StructureIsReused . Not ) {
1675
1674
return resolveTypeReferenceDirectiveNamesWorker ( typeDirectiveNames , containingFile , /*partialResolutionInfo*/ undefined ) ;
1676
1675
}
1677
1676
1678
- const oldSourceFile = oldProgram && oldProgram . getSourceFile ( containingFile . fileName ) ;
1679
- if ( oldSourceFile !== containingFile && containingFile . resolvedTypeReferenceDirectiveNames ) {
1680
- // `file` was created for the new program.
1681
- //
1682
- // We only set `file.resolvedTypeReferenceDirectiveNames` via work from the current function,
1683
- // so it is defined iff we already called the current function on `file`.
1684
- // That call happened no later than the creation of the `file` object,
1685
- // which per above occurred during the current program creation.
1686
- // Since we assume the filesystem does not change during program creation,
1687
- // it is safe to reuse resolutions from the earlier call.
1688
- const result : ResolvedTypeReferenceDirective [ ] = [ ] ;
1689
- for ( const typeDirectiveName of typeDirectiveNames as readonly FileReference [ ] ) {
1690
- // We lower-case all type references because npm automatically lowercases all packages. See GH#9824.
1691
- const resolvedTypeReferenceDirective = containingFile . resolvedTypeReferenceDirectiveNames . get ( typeDirectiveName . fileName . toLowerCase ( ) , typeDirectiveName . resolutionMode || containingFile . impliedNodeFormat ) ! ;
1692
- result . push ( resolvedTypeReferenceDirective ) ;
1677
+ const oldSourceFile = ! isString ( containingFile ) ? oldProgram && oldProgram . getSourceFile ( containingFile . fileName ) : undefined ;
1678
+ if ( ! isString ( containingFile ) ) {
1679
+ if ( oldSourceFile !== containingFile && containingFile . resolvedTypeReferenceDirectiveNames ) {
1680
+ // `file` was created for the new program.
1681
+ //
1682
+ // We only set `file.resolvedTypeReferenceDirectiveNames` via work from the current function,
1683
+ // so it is defined iff we already called the current function on `file`.
1684
+ // That call happened no later than the creation of the `file` object,
1685
+ // which per above occurred during the current program creation.
1686
+ // Since we assume the filesystem does not change during program creation,
1687
+ // it is safe to reuse resolutions from the earlier call.
1688
+ const result : ResolvedTypeReferenceDirective [ ] = [ ] ;
1689
+ for ( const typeDirectiveName of typeDirectiveNames as readonly FileReference [ ] ) {
1690
+ // We lower-case all type references because npm automatically lowercases all packages. See GH#9824.
1691
+ const resolvedTypeReferenceDirective = containingFile . resolvedTypeReferenceDirectiveNames . get ( typeDirectiveName . fileName . toLowerCase ( ) , typeDirectiveName . resolutionMode || containingFile . impliedNodeFormat ) ! ;
1692
+ result . push ( resolvedTypeReferenceDirective ) ;
1693
+ }
1694
+ return result ;
1693
1695
}
1694
- return result ;
1695
1696
}
1696
1697
1697
1698
/** An ordered list of module names for which we cannot recover the resolution. */
1698
- let unknownTypeReferenceDirectiveNames : FileReference [ ] | undefined ;
1699
+ let unknownTypeReferenceDirectiveNames : string [ ] | FileReference [ ] | undefined ;
1699
1700
let unknownTypeReferenceDirectiveNamesIndex : number [ ] | undefined ;
1700
1701
/**
1701
1702
* The indexing of elements in this list matches that of `moduleNames`.
@@ -1705,12 +1706,15 @@ namespace ts {
1705
1706
*/
1706
1707
let result : ( ResolvedTypeReferenceDirective | undefined ) [ ] | undefined ;
1707
1708
let reusedNames : { name : string ; mode : ModuleKind . CommonJS | ModuleKind . ESNext | undefined ; } [ ] | undefined ;
1709
+ const canReuseResolutions = ! isString ( containingFile ) ?
1710
+ containingFile === oldSourceFile && ! hasInvalidatedResolution ( oldSourceFile . path ) :
1711
+ ! hasInvalidatedResolution ( toPath ( containingFile ) ) ;
1708
1712
for ( let i = 0 ; i < typeDirectiveNames . length ; i ++ ) {
1709
- const entry = typeDirectiveNames [ i ] as FileReference ;
1713
+ const entry = typeDirectiveNames [ i ] ;
1710
1714
// If the source file is unchanged and doesnt have invalidated resolution, reuse the module resolutions
1711
- if ( containingFile === oldSourceFile && ! hasInvalidatedResolution ( oldSourceFile . path ) ) {
1712
- const typeDirectiveName = entry . fileName . toLowerCase ( ) ;
1713
- const mode = getModeForFileReference ( entry , oldSourceFile . impliedNodeFormat ) ;
1715
+ if ( canReuseResolutions ) {
1716
+ const typeDirectiveName = ! isString ( entry ) ? entry . fileName . toLowerCase ( ) : entry ;
1717
+ const mode = getModeForFileReference ( entry , oldSourceFile ? .impliedNodeFormat ) ;
1714
1718
const oldResolvedTypeReferenceDirective = getResolvedTypeReferenceDirective ( oldSourceFile , typeDirectiveName , mode ) ;
1715
1719
if ( oldResolvedTypeReferenceDirective ) {
1716
1720
if ( isTraceEnabled ( options , host ) ) {
@@ -1719,7 +1723,7 @@ namespace ts {
1719
1723
Diagnostics . Reusing_resolution_of_type_reference_directive_0_from_1_of_old_program_it_was_successfully_resolved_to_2_with_Package_ID_3 :
1720
1724
Diagnostics . Reusing_resolution_of_type_reference_directive_0_from_1_of_old_program_it_was_successfully_resolved_to_2 ,
1721
1725
typeDirectiveName ,
1722
- getNormalizedAbsolutePath ( containingFile . originalFileName , currentDirectory ) ,
1726
+ ! isString ( containingFile ) ? getNormalizedAbsolutePath ( containingFile . originalFileName , currentDirectory ) : containingFile ,
1723
1727
oldResolvedTypeReferenceDirective . resolvedFileName ,
1724
1728
oldResolvedTypeReferenceDirective . packageId && packageIdToString ( oldResolvedTypeReferenceDirective . packageId )
1725
1729
) ;
@@ -1730,7 +1734,7 @@ namespace ts {
1730
1734
}
1731
1735
}
1732
1736
// Resolution failed in the old program, or resolved to an ambient module for which we can't reuse the result.
1733
- ( unknownTypeReferenceDirectiveNames ??= [ ] ) . push ( entry ) ;
1737
+ ( unknownTypeReferenceDirectiveNames ??= [ ] ) . push ( entry as FileReference & string ) ;
1734
1738
( unknownTypeReferenceDirectiveNamesIndex ??= [ ] ) . push ( i ) ;
1735
1739
}
1736
1740
0 commit comments