Skip to content

Commit 362703a

Browse files
authored
Cover more cases for node module auto-import (#54024)
1 parent 2291afc commit 362703a

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
ExportAssignment,
2525
Extension,
2626
extensionFromPath,
27+
extensionsNotSupportingExtensionlessResolution,
2728
fileExtensionIsOneOf,
2829
FileIncludeKind,
2930
firstDefined,
@@ -91,6 +92,7 @@ import {
9192
removeExtension,
9293
removeFileExtension,
9394
removeSuffix,
95+
removeTrailingDirectorySeparator,
9496
ResolutionMode,
9597
resolvePath,
9698
ScriptKind,
@@ -1007,10 +1009,24 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
10071009
// happens very easily in fourslash tests though, since every test file listed gets included. See
10081010
// importNameCodeFix_typesVersions.ts for an example.)
10091011
const mainExportFile = toPath(mainFileRelative, packageRootPath, getCanonicalFileName);
1010-
if (removeFileExtension(mainExportFile) === removeFileExtension(getCanonicalFileName(moduleFileToTry))) {
1012+
const canonicalModuleFileToTry = getCanonicalFileName(moduleFileToTry);
1013+
if (removeFileExtension(mainExportFile) === removeFileExtension(canonicalModuleFileToTry)) {
10111014
// ^ An arbitrary removal of file extension for this comparison is almost certainly wrong
10121015
return { packageRootPath, moduleFileToTry };
10131016
}
1017+
else if (
1018+
packageJsonContent.type !== "module" &&
1019+
!fileExtensionIsOneOf(canonicalModuleFileToTry, extensionsNotSupportingExtensionlessResolution) &&
1020+
startsWith(canonicalModuleFileToTry, mainExportFile) &&
1021+
getDirectoryPath(canonicalModuleFileToTry) === removeTrailingDirectorySeparator(mainExportFile) &&
1022+
removeFileExtension(getBaseFileName(canonicalModuleFileToTry)) === "index"
1023+
) {
1024+
// if mainExportFile is a directory, which contains moduleFileToTry, we just try index file
1025+
// example mainExportFile: `pkg/lib` and moduleFileToTry: `pkg/lib/index`, we can use packageRootPath
1026+
// but this behavior is deprecated for packages with "type": "module", so we only do this for packages without "type": "module"
1027+
// and make sure that the extension on index.{???} is something that supports omitting the extension
1028+
return { packageRootPath, moduleFileToTry };
1029+
}
10141030
}
10151031
}
10161032
else {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /node_modules/pkg/package.json
6+
//// {
7+
//// "name": "pkg",
8+
//// "version": "1.0.0",
9+
//// "main": "lib",
10+
//// "module": "lib"
11+
//// }
12+
13+
// @Filename: /node_modules/pkg/lib/index.js
14+
//// export function foo() {};
15+
16+
// @Filename: /package.json
17+
//// {
18+
//// "dependencies": {
19+
//// "pkg": "*"
20+
//// }
21+
//// }
22+
23+
// @Filename: /index.ts
24+
//// foo/**/
25+
26+
verify.importFixModuleSpecifiers("", ["pkg"]);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /node_modules/pkg/package.json
6+
//// {
7+
//// "name": "pkg",
8+
//// "version": "1.0.0",
9+
//// "main": "lib"
10+
//// }
11+
12+
// @Filename: /node_modules/pkg/lib/index.d.mts
13+
//// export declare function foo(): any;
14+
15+
// @Filename: /package.json
16+
//// {
17+
//// "dependencies": {
18+
//// "pkg": "*"
19+
//// }
20+
//// }
21+
22+
// @Filename: /index.ts
23+
//// foo/**/
24+
25+
verify.importFixModuleSpecifiers("", ["pkg/lib/index.mjs"]);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /node_modules/pkg/package.json
6+
//// {
7+
//// "name": "pkg",
8+
//// "version": "1.0.0",
9+
//// "main": "lib",
10+
//// "type": "module"
11+
//// }
12+
13+
// @Filename: /node_modules/pkg/lib/index.js
14+
//// export function foo() {};
15+
16+
// @Filename: /package.json
17+
//// {
18+
//// "dependencies": {
19+
//// "pkg": "*"
20+
//// }
21+
//// }
22+
23+
// @Filename: /index.ts
24+
//// foo/**/
25+
26+
verify.importFixModuleSpecifiers("", ["pkg/lib"]);

0 commit comments

Comments
 (0)