Skip to content

Commit

Permalink
Use comparePaths via helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Mar 27, 2024
1 parent 36f39fb commit 19ca8eb
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,10 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
return maybeNonRelative;
}

let nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson(host, getDirectoryPath(modulePath));
let nearestSourcePackageJson = getNearestAncestorDirectoryWithPackageJson(host, sourceDirectory);
nearestTargetPackageJson &&= toPath(nearestTargetPackageJson, projectDirectory, getCanonicalFileName);
nearestSourcePackageJson &&= toPath(nearestSourcePackageJson, projectDirectory, getCanonicalFileName);
if (nearestSourcePackageJson !== nearestTargetPackageJson) {
const nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson(host, getDirectoryPath(modulePath));
const nearestSourcePackageJson = getNearestAncestorDirectoryWithPackageJson(host, sourceDirectory);
const ignoreCase = !hostUsesCaseSensitiveFileNames(host);
if (!packageJsonPathsAreEqual(nearestTargetPackageJson, nearestSourcePackageJson, ignoreCase)) {
// 2. The importing and imported files are part of different packages.
//
// packages/a/
Expand All @@ -572,6 +571,12 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
return isPathRelativeToParent(maybeNonRelative) || countPathComponents(relativePath) < countPathComponents(maybeNonRelative) ? relativePath : maybeNonRelative;
}

function packageJsonPathsAreEqual(a: string | undefined, b: string | undefined, ignoreCase?: boolean) {
if (a === b) return true;
if (a === undefined || b === undefined) return false;
return comparePaths(a, b, ignoreCase) === Comparison.EqualTo;
}

/** @internal */
export function countPathComponents(path: string): number {
let count = 0;
Expand Down

0 comments on commit 19ca8eb

Please sign in to comment.