Skip to content

Commit cdc1c9e

Browse files
committed
Clean up and add some comments
1 parent 5c3cb2b commit cdc1c9e

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,41 @@ namespace ts.moduleSpecifiers {
180180
const bundledPkgReference = bundledPackageName ? combinePaths(bundledPackageName, relativeToBaseUrl) : relativeToBaseUrl;
181181
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(bundledPkgReference, ending, compilerOptions);
182182
const fromPaths = paths && tryGetModuleNameFromPaths(removeFileExtension(bundledPkgReference), importRelativeToBaseUrl, paths);
183-
const nonRelative = fromPaths === undefined ? importRelativeToBaseUrl : fromPaths.path;
183+
const nonRelative = fromPaths === undefined ? importRelativeToBaseUrl : fromPaths;
184184

185185
if (relativePreference === RelativePreference.NonRelative) {
186186
return nonRelative;
187187
}
188188

189189
if (relativePreference === RelativePreference.ExternalNonRelative) {
190-
Debug.assertIsDefined(host.getNearestAncestorDirectoryWithPackageJson);
191190
const projectDirectory = host.getCurrentDirectory();
192191
const modulePath = toPath(moduleFileName, projectDirectory, getCanonicalFileName);
193192
const sourceIsInternal = startsWith(sourceDirectory, projectDirectory);
194193
const targetIsInternal = startsWith(modulePath, projectDirectory);
195194
if (sourceIsInternal && !targetIsInternal || !sourceIsInternal && targetIsInternal) {
196195
// 1. The import path crosses the boundary of the tsconfig.json-containing directory.
196+
//
197+
// src/
198+
// tsconfig.json
199+
// index.ts -------
200+
// lib/ | (path crosses tsconfig.json)
201+
// imported.ts <---
202+
//
197203
return nonRelative;
198204
}
199205

200-
const nearestTargetPackageJson = host.getNearestAncestorDirectoryWithPackageJson(getDirectoryPath(modulePath));
201-
const nearestSourcePackageJson = host.getNearestAncestorDirectoryWithPackageJson(sourceDirectory);
206+
const nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson(host, getDirectoryPath(modulePath));
207+
const nearestSourcePackageJson = getNearestAncestorDirectoryWithPackageJson(host, sourceDirectory);
202208
if (nearestSourcePackageJson !== nearestTargetPackageJson) {
203209
// 2. The importing and imported files are part of different packages.
210+
//
211+
// packages/a/
212+
// package.json
213+
// index.ts --------
214+
// packages/b/ | (path crosses package.json)
215+
// package.json |
216+
// component.ts <---
217+
//
204218
return nonRelative;
205219
}
206220

@@ -237,6 +251,15 @@ namespace ts.moduleSpecifiers {
237251
);
238252
}
239253

254+
function getNearestAncestorDirectoryWithPackageJson(host: ModuleSpecifierResolutionHost, fileName: string) {
255+
if (host.getNearestAncestorDirectoryWithPackageJson) {
256+
return host.getNearestAncestorDirectoryWithPackageJson(fileName);
257+
}
258+
return !!forEachAncestorDirectory(fileName, directory => {
259+
return host.fileExists(combinePaths(directory, "package.json")) ? true : undefined;
260+
});
261+
}
262+
240263
export function forEachFileNameOfModule<T>(
241264
importingFileName: string,
242265
importedFileName: string,
@@ -352,7 +375,7 @@ namespace ts.moduleSpecifiers {
352375
}
353376
}
354377

355-
function tryGetModuleNameFromPaths(relativeToBaseUrlWithIndex: string, relativeToBaseUrl: string, paths: MapLike<readonly string[]>): { key: string, path: string } | undefined {
378+
function tryGetModuleNameFromPaths(relativeToBaseUrlWithIndex: string, relativeToBaseUrl: string, paths: MapLike<readonly string[]>): string | undefined {
356379
for (const key in paths) {
357380
for (const patternText of paths[key]) {
358381
const pattern = removeFileExtension(normalizePath(patternText));
@@ -365,11 +388,11 @@ namespace ts.moduleSpecifiers {
365388
endsWith(relativeToBaseUrl, suffix) ||
366389
!suffix && relativeToBaseUrl === removeTrailingDirectorySeparator(prefix)) {
367390
const matchedStar = relativeToBaseUrl.substr(prefix.length, relativeToBaseUrl.length - suffix.length);
368-
return { key, path: key.replace("*", matchedStar) };
391+
return key.replace("*", matchedStar);
369392
}
370393
}
371394
else if (pattern === relativeToBaseUrl || pattern === relativeToBaseUrlWithIndex) {
372-
return { key, path: key };
395+
return key;
373396
}
374397
}
375398
}
@@ -458,7 +481,7 @@ namespace ts.moduleSpecifiers {
458481
versionPaths.paths
459482
);
460483
if (fromPaths !== undefined) {
461-
moduleFileToTry = combinePaths(packageRootPath, fromPaths.path);
484+
moduleFileToTry = combinePaths(packageRootPath, fromPaths);
462485
}
463486
}
464487

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ namespace ts {
301301
/* @internal */
302302
getPackageJsonsVisibleToFile?(fileName: string, rootDir?: string): readonly PackageJsonInfo[];
303303
/* @internal */
304-
getNearestAncestorDirectoryWithPackageJson?(fileName: string, rootDir?: string): string | undefined;
304+
getNearestAncestorDirectoryWithPackageJson?(fileName: string): string | undefined;
305305
/* @internal */
306306
getPackageJsonsForAutoImport?(rootDir?: string): readonly PackageJsonInfo[];
307307
/* @internal */

0 commit comments

Comments
 (0)