Skip to content

Commit acc34bd

Browse files
author
Andy
authored
Miscellaneous code cleanup relating to module resolution (#28092)
* Miscellaneous code cleanup relating to module resolution * Revert if condition
1 parent 7c515bf commit acc34bd

File tree

3 files changed

+28
-52
lines changed

3 files changed

+28
-52
lines changed

src/compiler/resolutionCache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace ts {
7979
export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootDirForResolution: string | undefined, logChangesWhenResolvingModule: boolean): ResolutionCache {
8080
let filesWithChangedSetOfUnresolvedImports: Path[] | undefined;
8181
let filesWithInvalidatedResolutions: Map<true> | undefined;
82-
let filesWithInvalidatedNonRelativeUnresolvedImports: Map<ReadonlyArray<string>> | undefined;
82+
let filesWithInvalidatedNonRelativeUnresolvedImports: ReadonlyMap<ReadonlyArray<string>> | undefined;
8383
let allFilesHaveInvalidatedResolution = false;
8484
const nonRelativeExternalModuleResolutions = createMultiMap<ResolutionWithFailedLookupLocations>();
8585

@@ -241,14 +241,14 @@ namespace ts {
241241
}
242242

243243
function resolveNamesWithLocalCache<T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName>(
244-
names: string[],
244+
names: ReadonlyArray<string>,
245245
containingFile: string,
246246
redirectedReference: ResolvedProjectReference | undefined,
247247
cache: Map<Map<T>>,
248248
perDirectoryCacheWithRedirects: CacheWithRedirects<Map<T>>,
249249
loader: (name: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference) => T,
250250
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
251-
reusedNames: string[] | undefined,
251+
reusedNames: ReadonlyArray<string> | undefined,
252252
logChanges: boolean): (R | undefined)[] {
253253

254254
const path = resolutionHost.toPath(containingFile);
@@ -675,7 +675,7 @@ namespace ts {
675675
);
676676
}
677677

678-
function setFilesWithInvalidatedNonRelativeUnresolvedImports(filesMap: Map<ReadonlyArray<string>>) {
678+
function setFilesWithInvalidatedNonRelativeUnresolvedImports(filesMap: ReadonlyMap<ReadonlyArray<string>>) {
679679
Debug.assert(filesWithInvalidatedNonRelativeUnresolvedImports === filesMap || filesWithInvalidatedNonRelativeUnresolvedImports === undefined);
680680
filesWithInvalidatedNonRelativeUnresolvedImports = filesMap;
681681
}

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ interface Array<T> {}`
128128
return s && isString((<FsSymLink>s).symLink);
129129
}
130130

131-
function invokeWatcherCallbacks<T>(callbacks: T[], invokeCallback: (cb: T) => void): void {
131+
function invokeWatcherCallbacks<T>(callbacks: ReadonlyArray<T> | undefined, invokeCallback: (cb: T) => void): void {
132132
if (callbacks) {
133133
// The array copy is made to ensure that even if one of the callback removes the callbacks,
134134
// we dont miss any callbacks following it
@@ -650,15 +650,15 @@ interface Array<T> {}`
650650

651651
// For overriding the methods
652652
invokeWatchedDirectoriesCallback(folderFullPath: string, relativePath: string) {
653-
invokeWatcherCallbacks(this.watchedDirectories.get(this.toPath(folderFullPath))!, cb => this.directoryCallback(cb, relativePath));
653+
invokeWatcherCallbacks(this.watchedDirectories.get(this.toPath(folderFullPath)), cb => this.directoryCallback(cb, relativePath));
654654
}
655655

656656
invokeWatchedDirectoriesRecursiveCallback(folderFullPath: string, relativePath: string) {
657-
invokeWatcherCallbacks(this.watchedDirectoriesRecursive.get(this.toPath(folderFullPath))!, cb => this.directoryCallback(cb, relativePath));
657+
invokeWatcherCallbacks(this.watchedDirectoriesRecursive.get(this.toPath(folderFullPath)), cb => this.directoryCallback(cb, relativePath));
658658
}
659659

660660
private invokeFileWatcher(fileFullPath: string, eventKind: FileWatcherEventKind, useFileNameInCallback?: boolean) {
661-
invokeWatcherCallbacks(this.watchedFiles.get(this.toPath(fileFullPath))!, ({ cb, fileName }) => cb(useFileNameInCallback ? fileName : fileFullPath, eventKind));
661+
invokeWatcherCallbacks(this.watchedFiles.get(this.toPath(fileFullPath)), ({ cb, fileName }) => cb(useFileNameInCallback ? fileName : fileFullPath, eventKind));
662662
}
663663

664664
private getRelativePathToDirectory(directoryFullPath: string, fileFullPath: string) {

src/server/project.ts

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -795,41 +795,6 @@ namespace ts.server {
795795
}
796796
}
797797

798-
/* @internal */
799-
private extractUnresolvedImportsFromSourceFile(file: SourceFile, ambientModules: string[]): ReadonlyArray<string> {
800-
const cached = this.cachedUnresolvedImportsPerFile.get(file.path);
801-
if (cached) {
802-
// found cached result, return
803-
return cached;
804-
}
805-
let unresolvedImports: string[] | undefined;
806-
if (file.resolvedModules) {
807-
file.resolvedModules.forEach((resolvedModule, name) => {
808-
// pick unresolved non-relative names
809-
if (!resolvedModule && !isExternalModuleNameRelative(name) && !isAmbientlyDeclaredModule(name)) {
810-
// for non-scoped names extract part up-to the first slash
811-
// for scoped names - extract up to the second slash
812-
let trimmed = name.trim();
813-
let i = trimmed.indexOf("/");
814-
if (i !== -1 && trimmed.charCodeAt(0) === CharacterCodes.at) {
815-
i = trimmed.indexOf("/", i + 1);
816-
}
817-
if (i !== -1) {
818-
trimmed = trimmed.substr(0, i);
819-
}
820-
(unresolvedImports || (unresolvedImports = [])).push(trimmed);
821-
}
822-
});
823-
}
824-
825-
this.cachedUnresolvedImportsPerFile.set(file.path, unresolvedImports || emptyArray);
826-
return unresolvedImports || emptyArray;
827-
828-
function isAmbientlyDeclaredModule(name: string) {
829-
return ambientModules.some(m => m === name);
830-
}
831-
}
832-
833798
/* @internal */
834799
onFileAddedOrRemoved() {
835800
this.hasAddedorRemovedFiles = true;
@@ -863,15 +828,7 @@ namespace ts.server {
863828
// (can reuse cached imports for files that were not changed)
864829
// 4. compilation settings were changed in the way that might affect module resolution - drop all caches and collect all data from the scratch
865830
if (hasNewProgram || changedFiles.length) {
866-
let result: string[] | undefined;
867-
const ambientModules = this.program.getTypeChecker().getAmbientModules().map(mod => stripQuotes(mod.getName()));
868-
for (const sourceFile of this.program.getSourceFiles()) {
869-
const unResolved = this.extractUnresolvedImportsFromSourceFile(sourceFile, ambientModules);
870-
if (unResolved !== emptyArray) {
871-
(result || (result = [])).push(...unResolved);
872-
}
873-
}
874-
this.lastCachedUnresolvedImportsList = result ? sortAndDeduplicate(result) : emptyArray;
831+
this.lastCachedUnresolvedImportsList = getUnresolvedImports(this.program, this.cachedUnresolvedImportsPerFile);
875832
}
876833

877834
this.projectService.typingsCache.enqueueInstallTypingsForProject(this, this.lastCachedUnresolvedImportsList, hasAddedorRemovedFiles);
@@ -1218,6 +1175,25 @@ namespace ts.server {
12181175
}
12191176
}
12201177

1178+
function getUnresolvedImports(program: Program, cachedUnresolvedImportsPerFile: Map<ReadonlyArray<string>>): SortedReadonlyArray<string> {
1179+
const ambientModules = program.getTypeChecker().getAmbientModules().map(mod => stripQuotes(mod.getName()));
1180+
return sortAndDeduplicate(flatMap(program.getSourceFiles(), sourceFile =>
1181+
extractUnresolvedImportsFromSourceFile(sourceFile, ambientModules, cachedUnresolvedImportsPerFile)));
1182+
}
1183+
function extractUnresolvedImportsFromSourceFile(file: SourceFile, ambientModules: ReadonlyArray<string>, cachedUnresolvedImportsPerFile: Map<ReadonlyArray<string>>): ReadonlyArray<string> {
1184+
return getOrUpdate(cachedUnresolvedImportsPerFile, file.path, () => {
1185+
if (!file.resolvedModules) return emptyArray;
1186+
let unresolvedImports: string[] | undefined;
1187+
file.resolvedModules.forEach((resolvedModule, name) => {
1188+
// pick unresolved non-relative names
1189+
if (!resolvedModule && !isExternalModuleNameRelative(name) && !ambientModules.some(m => m === name)) {
1190+
unresolvedImports = append(unresolvedImports, parsePackageName(name).packageName);
1191+
}
1192+
});
1193+
return unresolvedImports || emptyArray;
1194+
});
1195+
}
1196+
12211197
/**
12221198
* If a file is opened and no tsconfig (or jsconfig) is found,
12231199
* the file and its imports/references are put into an InferredProject.

0 commit comments

Comments
 (0)