Skip to content

Commit a99c04e

Browse files
committed
Make the failedLookuplocations to be readonly array
1 parent 6bf9133 commit a99c04e

4 files changed

Lines changed: 26 additions & 21 deletions

File tree

src/compiler/resolutionCache.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace ts {
1313
resolveModuleNames(moduleNames: string[], containingFile: string, logChanges: boolean): ResolvedModuleFull[];
1414
resolveTypeReferenceDirectives(typeDirectiveNames: string[], containingFile: string): ResolvedTypeReferenceDirective[];
1515

16-
invalidateResolutionOfDeletedFile(filePath: Path): void;
16+
invalidateResolutionOfFile(filePath: Path): void;
1717
invalidateResolutionOfChangedFailedLookupLocation(failedLookupLocation: string): void;
1818

1919
createHasInvalidatedResolution(): HasInvalidatedResolution;
@@ -22,7 +22,7 @@ namespace ts {
2222
}
2323

2424
interface NameResolutionWithFailedLookupLocations {
25-
readonly failedLookupLocations: string[];
25+
readonly failedLookupLocations: ReadonlyArray<string>;
2626
isInvalidated?: boolean;
2727
}
2828

@@ -40,9 +40,12 @@ namespace ts {
4040
getGlobalCache?: () => string | undefined): ResolutionCache {
4141

4242
let host: ModuleResolutionHost;
43-
let filesWithChangedSetOfUnresolvedImports: Path[];
44-
let filesWithInvalidatedResolutions: Map<true>;
43+
let filesWithChangedSetOfUnresolvedImports: Path[] | undefined;
44+
let filesWithInvalidatedResolutions: Map<true> | undefined;
4545

46+
// The resolvedModuleNames and resolvedTypeReferenceDirectives are the cache of resolutions per file.
47+
// The key in the map is source file's path.
48+
// The values are Map of resolutions with key being name lookedup.
4649
const resolvedModuleNames = createMap<Map<ResolvedModuleWithFailedLookupLocations>>();
4750
const resolvedTypeReferenceDirectives = createMap<Map<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>>();
4851

@@ -54,7 +57,7 @@ namespace ts {
5457
finishRecordingFilesWithChangedResolutions,
5558
resolveModuleNames,
5659
resolveTypeReferenceDirectives,
57-
invalidateResolutionOfDeletedFile,
60+
invalidateResolutionOfFile,
5861
invalidateResolutionOfChangedFailedLookupLocation,
5962
createHasInvalidatedResolution,
6063
clear
@@ -65,6 +68,7 @@ namespace ts {
6568
}
6669

6770
function clear() {
71+
// Close all the watches for failed lookup locations, irrespective of refcounts for them since this is to clear the cache
6872
clearMap(failedLookupLocationsWatches, (failedLookupLocationPath, failedLookupLocationWatcher) => {
6973
log(`Watcher: FailedLookupLocations: Status: ForceClose: LocationPath: ${failedLookupLocationPath}, refCount: ${failedLookupLocationWatcher.refCount}`);
7074
failedLookupLocationWatcher.fileWatcher.close();
@@ -103,7 +107,7 @@ namespace ts {
103107
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
104108
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, projectName, compilerOptions, host, globalCache);
105109
if (resolvedModule) {
106-
return { resolvedModule, failedLookupLocations: primaryResult.failedLookupLocations.concat(failedLookupLocations) };
110+
return { resolvedModule, failedLookupLocations: addRange(primaryResult.failedLookupLocations as Array<string>, failedLookupLocations) };
107111
}
108112
}
109113

@@ -229,20 +233,20 @@ namespace ts {
229233
}
230234

231235
type FailedLookupLocationAction = (failedLookupLocation: string, failedLookupLocationPath: Path, containingFile: string, name: string) => void;
232-
function withFailedLookupLocations(failedLookupLocations: string[], containingFile: string, name: string, fn: FailedLookupLocationAction) {
236+
function withFailedLookupLocations(failedLookupLocations: ReadonlyArray<string>, containingFile: string, name: string, fn: FailedLookupLocationAction) {
233237
forEach(failedLookupLocations, failedLookupLocation => {
234238
fn(failedLookupLocation, toPath(failedLookupLocation), containingFile, name);
235239
});
236240
}
237241

238-
function updateFailedLookupLocationWatches(containingFile: string, name: string, existingFailedLookupLocations: string[], failedLookupLocations: string[]) {
242+
function updateFailedLookupLocationWatches(containingFile: string, name: string, existingFailedLookupLocations: ReadonlyArray<string> | undefined, failedLookupLocations: ReadonlyArray<string>) {
239243
if (failedLookupLocations) {
240244
if (existingFailedLookupLocations) {
241-
const existingWatches = arrayToMap(existingFailedLookupLocations, failedLookupLocation => toPath(failedLookupLocation));
245+
const existingWatches = arrayToMap(existingFailedLookupLocations, toPath);
242246
for (const failedLookupLocation of failedLookupLocations) {
243247
const failedLookupLocationPath = toPath(failedLookupLocation);
244248
if (existingWatches && existingWatches.has(failedLookupLocationPath)) {
245-
// still has same failed lookup location, keep the was
249+
// still has same failed lookup location, keep the watch
246250
existingWatches.delete(failedLookupLocationPath);
247251
}
248252
else {
@@ -272,15 +276,15 @@ namespace ts {
272276
cache: Map<Map<T>>,
273277
getResult: (s: T) => R,
274278
getResultFileName: (result: R) => string | undefined) {
275-
cache.forEach((value, path: Path) => {
279+
cache.forEach((value, path) => {
276280
if (path === deletedFilePath) {
277281
cache.delete(path);
278282
value.forEach((resolution, name) => {
279283
withFailedLookupLocations(resolution.failedLookupLocations, path, name, closeFailedLookupLocationWatcher);
280284
});
281285
}
282286
else if (value) {
283-
value.forEach((resolution, __name) => {
287+
value.forEach(resolution => {
284288
if (resolution && !resolution.isInvalidated) {
285289
const result = getResult(resolution);
286290
if (result) {
@@ -298,10 +302,10 @@ namespace ts {
298302
function invalidateResolutionCacheOfChangedFailedLookupLocation<T extends NameResolutionWithFailedLookupLocations>(
299303
failedLookupLocation: string,
300304
cache: Map<Map<T>>) {
301-
cache.forEach((value, containingFile: Path) => {
305+
cache.forEach((value, containingFile) => {
302306
if (value) {
303-
value.forEach((resolution, __name) => {
304-
if (resolution && !resolution.isInvalidated && contains(resolution.failedLookupLocations, failedLookupLocation)) {
307+
value.forEach(resolution => {
308+
if (resolution && !resolution.isInvalidated && some(resolution.failedLookupLocations, location => toPath(location) === failedLookupLocation)) {
305309
// Mark the file as needing re-evaluation of module resolution instead of using it blindly.
306310
resolution.isInvalidated = true;
307311
(filesWithInvalidatedResolutions || (filesWithInvalidatedResolutions = createMap<true>())).set(containingFile, true);
@@ -311,7 +315,7 @@ namespace ts {
311315
});
312316
}
313317

314-
function invalidateResolutionOfDeletedFile(filePath: Path) {
318+
function invalidateResolutionOfFile(filePath: Path) {
315319
invalidateResolutionCacheOfDeletedFile(filePath, resolvedModuleNames, m => m.resolvedModule, r => r.resolvedFileName);
316320
invalidateResolutionCacheOfDeletedFile(filePath, resolvedTypeReferenceDirectives, m => m.resolvedTypeReferenceDirective, r => r.resolvedFileName);
317321
}

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4011,7 +4011,7 @@ namespace ts {
40114011
export interface ResolvedModuleWithFailedLookupLocations {
40124012
readonly resolvedModule: ResolvedModuleFull | undefined;
40134013
/* @internal */
4014-
readonly failedLookupLocations: string[];
4014+
readonly failedLookupLocations: ReadonlyArray<string>;
40154015
/*@internal*/
40164016
isInvalidated?: boolean;
40174017
}
@@ -4025,7 +4025,7 @@ namespace ts {
40254025

40264026
export interface ResolvedTypeReferenceDirectiveWithFailedLookupLocations {
40274027
readonly resolvedTypeReferenceDirective: ResolvedTypeReferenceDirective;
4028-
readonly failedLookupLocations: string[];
4028+
readonly failedLookupLocations: ReadonlyArray<string>;
40294029
/*@internal*/
40304030
isInvalidated?: boolean;
40314031
}

src/compiler/watchedProgram.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ namespace ts {
437437
if (hostSourceFile !== undefined) {
438438
if (!isString(hostSourceFile)) {
439439
hostSourceFile.fileWatcher.close();
440-
resolutionCache.invalidateResolutionOfDeletedFile(path);
440+
resolutionCache.invalidateResolutionOfFile(path);
441441
}
442442
sourceFilesCache.delete(path);
443443
}
@@ -461,6 +461,7 @@ namespace ts {
461461
}
462462
else if (hostSourceFileInfo.sourceFile === oldSourceFile) {
463463
sourceFilesCache.delete(oldSourceFile.path);
464+
resolutionCache.invalidateResolutionOfFile(oldSourceFile.path);
464465
}
465466
}
466467
}
@@ -528,7 +529,7 @@ namespace ts {
528529
if (hostSourceFile) {
529530
// Update the cache
530531
if (eventKind === FileWatcherEventKind.Deleted) {
531-
resolutionCache.invalidateResolutionOfDeletedFile(path);
532+
resolutionCache.invalidateResolutionOfFile(path);
532533
if (!isString(hostSourceFile)) {
533534
hostSourceFile.fileWatcher.close();
534535
sourceFilesCache.set(path, (hostSourceFile.version++).toString());

src/server/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ namespace ts.server {
547547
if (this.isRoot(info)) {
548548
this.removeRoot(info);
549549
}
550-
this.resolutionCache.invalidateResolutionOfDeletedFile(info.path);
550+
this.resolutionCache.invalidateResolutionOfFile(info.path);
551551
this.cachedUnresolvedImportsPerFile.remove(info.path);
552552

553553
if (detachFromProject) {

0 commit comments

Comments
 (0)