From 19ca8eb80ab48b5dcb83fad892a1820e8fb5abab Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:38:56 -0700 Subject: [PATCH] Use comparePaths via helper --- src/compiler/moduleSpecifiers.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index f4b925eabfaad..40261c7e8e35f 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -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/ @@ -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;