Skip to content

Commit b0552b0

Browse files
author
Andy
authored
Don't include "/index" in import from @types even with classic resolution (#23347)
1 parent 79ad7df commit b0552b0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ namespace ts.codefix {
240240
preferences: UserPreferences,
241241
): ReadonlyArray<NewImportInfo> {
242242
const { baseUrl, paths, rootDirs } = compilerOptions;
243+
const moduleResolutionKind = getEmitModuleResolutionKind(compilerOptions);
243244
const addJsExtension = usesJsExtensionOnImports(sourceFile);
244245
const choicesForEachExportingModule = flatMap<SymbolExportInfo, NewImportInfo[]>(moduleSymbols, ({ moduleSymbol, importKind }) => {
245246
const modulePathsGroups = getAllModulePaths(program, moduleSymbol.valueDeclaration.getSourceFile()).map(moduleFileName => {
@@ -252,7 +253,7 @@ namespace ts.codefix {
252253
return [global];
253254
}
254255

255-
const relativePath = removeExtensionAndIndexPostFix(getRelativePath(moduleFileName, sourceDirectory, getCanonicalFileName), compilerOptions, addJsExtension);
256+
const relativePath = removeExtensionAndIndexPostFix(getRelativePath(moduleFileName, sourceDirectory, getCanonicalFileName), moduleResolutionKind, addJsExtension);
256257
if (!baseUrl || preferences.importModuleSpecifierPreference === "relative") {
257258
return [relativePath];
258259
}
@@ -262,7 +263,7 @@ namespace ts.codefix {
262263
return [relativePath];
263264
}
264265

265-
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(relativeToBaseUrl, compilerOptions, addJsExtension);
266+
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(relativeToBaseUrl, moduleResolutionKind, addJsExtension);
266267
if (paths) {
267268
const fromPaths = tryGetModuleNameFromPaths(removeFileExtension(relativeToBaseUrl), importRelativeToBaseUrl, paths);
268269
if (fromPaths) {
@@ -390,7 +391,8 @@ namespace ts.codefix {
390391
return firstDefined(roots, unNormalizedTypeRoot => {
391392
const typeRoot = toPath(unNormalizedTypeRoot, /*basePath*/ undefined, getCanonicalFileName);
392393
if (startsWith(moduleFileName, typeRoot)) {
393-
return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1), options, addJsExtension);
394+
// For a type definition, we can strip `/index` even with classic resolution.
395+
return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1), ModuleResolutionKind.NodeJs, addJsExtension);
394396
}
395397
});
396398
}
@@ -527,11 +529,11 @@ namespace ts.codefix {
527529
});
528530
}
529531

530-
function removeExtensionAndIndexPostFix(fileName: string, options: CompilerOptions, addJsExtension: boolean): string {
532+
function removeExtensionAndIndexPostFix(fileName: string, moduleResolutionKind: ModuleResolutionKind, addJsExtension: boolean): string {
531533
const noExtension = removeFileExtension(fileName);
532534
return addJsExtension
533535
? noExtension + ".js"
534-
: getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeJs
536+
: moduleResolutionKind === ModuleResolutionKind.NodeJs
535537
? removeSuffix(noExtension, "/index")
536538
: noExtension;
537539
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: classic
4+
5+
// @Filename: /node_modules/@types/foo/index.d.ts
6+
////export const xyz: number;
7+
8+
// @Filename: /a.ts
9+
////[|xyz|]
10+
11+
goTo.file("/a.ts");
12+
verify.importFixAtPosition([
13+
`import { xyz } from "foo";
14+
15+
xyz`
16+
]);

0 commit comments

Comments
 (0)