Skip to content

Commit decc3cd

Browse files
authored
Elide /index from auto-import when using rootDirs compiler option (microsoft#32828)
1 parent 4ab85bb commit decc3cd

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace ts.moduleSpecifiers {
114114
function getLocalModuleSpecifier(moduleFileName: string, { getCanonicalFileName, sourceDirectory }: Info, compilerOptions: CompilerOptions, { ending, relativePreference }: Preferences): string {
115115
const { baseUrl, paths, rootDirs } = compilerOptions;
116116

117-
const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName) ||
117+
const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) ||
118118
removeExtensionAndIndexPostFix(ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), ending, compilerOptions);
119119
if (!baseUrl || relativePreference === RelativePreference.Relative) {
120120
return relativePath;
@@ -248,15 +248,17 @@ namespace ts.moduleSpecifiers {
248248
}
249249
}
250250

251-
function tryGetModuleNameFromRootDirs(rootDirs: ReadonlyArray<string>, moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string): string | undefined {
251+
function tryGetModuleNameFromRootDirs(rootDirs: ReadonlyArray<string>, moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string, ending: Ending, compilerOptions: CompilerOptions): string | undefined {
252252
const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, rootDirs, getCanonicalFileName);
253253
if (normalizedTargetPath === undefined) {
254254
return undefined;
255255
}
256256

257257
const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, rootDirs, getCanonicalFileName);
258258
const relativePath = normalizedSourcePath !== undefined ? ensurePathIsNonModuleName(getRelativePathFromDirectory(normalizedSourcePath, normalizedTargetPath, getCanonicalFileName)) : normalizedTargetPath;
259-
return removeFileExtension(relativePath);
259+
return getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs
260+
? removeExtensionAndIndexPostFix(relativePath, ending, compilerOptions)
261+
: removeFileExtension(relativePath);
260262
}
261263

262264
function tryGetModuleNameAsNodeModule(moduleFileName: string, { getCanonicalFileName, sourceDirectory }: Info, host: ModuleSpecifierResolutionHost, options: CompilerOptions): string | undefined {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: a/f1.ts
4+
//// [|foo/*0*/();|]
5+
6+
// @Filename: a/b/index.ts
7+
//// export function foo() {};
8+
9+
// @Filename: tsconfig.json
10+
//// {
11+
//// "compilerOptions": {
12+
//// "rootDirs": [
13+
//// "a"
14+
//// ]
15+
//// }
16+
//// }
17+
18+
verify.importFixAtPosition([
19+
`import { foo } from "./b";
20+
21+
foo();`
22+
]);

0 commit comments

Comments
 (0)