From decc3cd671125acc66e1a2fd3941167985e5c145 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 12 Aug 2019 16:05:15 -0700 Subject: [PATCH] Elide /index from auto-import when using rootDirs compiler option (#32828) --- src/compiler/moduleSpecifiers.ts | 8 ++++--- .../importNameCodeFixNewImportRootDirs1.ts | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/importNameCodeFixNewImportRootDirs1.ts diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index fbed429e312..8c96199b7bf 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -114,7 +114,7 @@ namespace ts.moduleSpecifiers { function getLocalModuleSpecifier(moduleFileName: string, { getCanonicalFileName, sourceDirectory }: Info, compilerOptions: CompilerOptions, { ending, relativePreference }: Preferences): string { const { baseUrl, paths, rootDirs } = compilerOptions; - const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName) || + const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) || removeExtensionAndIndexPostFix(ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), ending, compilerOptions); if (!baseUrl || relativePreference === RelativePreference.Relative) { return relativePath; @@ -248,7 +248,7 @@ namespace ts.moduleSpecifiers { } } - function tryGetModuleNameFromRootDirs(rootDirs: ReadonlyArray, moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string): string | undefined { + function tryGetModuleNameFromRootDirs(rootDirs: ReadonlyArray, moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string, ending: Ending, compilerOptions: CompilerOptions): string | undefined { const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, rootDirs, getCanonicalFileName); if (normalizedTargetPath === undefined) { return undefined; @@ -256,7 +256,9 @@ namespace ts.moduleSpecifiers { const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, rootDirs, getCanonicalFileName); const relativePath = normalizedSourcePath !== undefined ? ensurePathIsNonModuleName(getRelativePathFromDirectory(normalizedSourcePath, normalizedTargetPath, getCanonicalFileName)) : normalizedTargetPath; - return removeFileExtension(relativePath); + return getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs + ? removeExtensionAndIndexPostFix(relativePath, ending, compilerOptions) + : removeFileExtension(relativePath); } function tryGetModuleNameAsNodeModule(moduleFileName: string, { getCanonicalFileName, sourceDirectory }: Info, host: ModuleSpecifierResolutionHost, options: CompilerOptions): string | undefined { diff --git a/tests/cases/fourslash/importNameCodeFixNewImportRootDirs1.ts b/tests/cases/fourslash/importNameCodeFixNewImportRootDirs1.ts new file mode 100644 index 00000000000..dfac047ae19 --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixNewImportRootDirs1.ts @@ -0,0 +1,22 @@ +/// + +// @Filename: a/f1.ts +//// [|foo/*0*/();|] + +// @Filename: a/b/index.ts +//// export function foo() {}; + +// @Filename: tsconfig.json +//// { +//// "compilerOptions": { +//// "rootDirs": [ +//// "a" +//// ] +//// } +//// } + +verify.importFixAtPosition([ +`import { foo } from "./b"; + +foo();` +]);