Ensure paths-based resolution does not generate module specifiers with .. in the middle (#53957)

This commit is contained in:
Andrew Branch
2023-04-21 15:04:16 -07:00
committed by GitHub
parent 36b632552d
commit 818c9806d4
2 changed files with 44 additions and 4 deletions

View File

@@ -459,7 +459,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
}
const baseDirectory = getNormalizedAbsolutePath(getPathsBasePath(compilerOptions, host) || baseUrl!, host.getCurrentDirectory());
const relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseDirectory, getCanonicalFileName);
const relativeToBaseUrl = getRelativePathIfInSameVolume(moduleFileName, baseDirectory, getCanonicalFileName);
if (!relativeToBaseUrl) {
return pathsOnly ? undefined : relativePath;
}
@@ -773,7 +773,7 @@ function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<rea
validateEnding({ ending, value })
) {
const matchedStar = value.substring(prefix.length, value.length - suffix.length);
return key.replace("*", matchedStar);
return pathIsRelative(matchedStar) ? undefined : key.replace("*", matchedStar);
}
}
}
@@ -1038,7 +1038,7 @@ function tryGetAnyFileFromPath(host: ModuleSpecifierResolutionHost, path: string
function getPathsRelativeToRootDirs(path: string, rootDirs: readonly string[], getCanonicalFileName: GetCanonicalFileName): string[] | undefined {
return mapDefined(rootDirs, rootDir => {
const relativePath = getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName);
const relativePath = getRelativePathIfInSameVolume(path, rootDir, getCanonicalFileName);
return relativePath !== undefined && isPathRelativeToParent(relativePath) ? undefined : relativePath;
});
}
@@ -1133,7 +1133,7 @@ export function tryGetJSExtensionForFile(fileName: string, options: CompilerOpti
}
}
function getRelativePathIfInDirectory(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
function getRelativePathIfInSameVolume(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
return isRootedDiskPath(relativePath) ? undefined : relativePath;
}

View File

@@ -0,0 +1,40 @@
/// <reference path="../fourslash.ts" />
// @Filename: /project/packages/common/package.json
//// {
//// "name": "@company/common",
//// "version": "1.0.0",
//// "main": "./lib/index.tsx"
//// }
// @Filename: /project/packages/common/lib/index.tsx
//// export function Tooltip {};
// @Filename: /project/packages/app/package.json
//// {
//// "name": "@company/app",
//// "version": "1.0.0",
//// "dependencies": {
//// "@company/common": "1.0.0"
//// }
//// }
// @Filename: /project/packages/app/tsconfig.json
//// {
//// "compilerOptions": {
//// "composite": true,
//// "module": "esnext",
//// "moduleResolution": "bundler",
//// "paths": {
//// "@/*": ["./*"]
//// }
//// }
//// }
// @Filename: /project/packages/app/lib/index.ts
//// Tooltip/**/
// @link: /project/packages/common -> /project/node_modules/@company/common
goTo.marker("");
verify.importFixModuleSpecifiers("", ["@company/common"]);