Skip to content

Commit 287ee41

Browse files
authored
Fix two module specifier ending preference detection issues (#53691)
1 parent 68d8be4 commit 287ee41

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ function getPreferences(
147147
return [ModuleSpecifierEnding.JsExtension];
148148
}
149149
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Classic) {
150-
return [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension];
150+
return preferredEnding === ModuleSpecifierEnding.JsExtension
151+
? [ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Index]
152+
: [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension];
151153
}
152154
switch (preferredEnding) {
153155
case ModuleSpecifierEnding.JsExtension: return [ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Minimal, ModuleSpecifierEnding.Index];

src/compiler/utilities.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9094,6 +9094,8 @@ const allSupportedExtensionsWithJson: readonly Extension[][] = [...allSupportedE
90949094
export const supportedDeclarationExtensions: readonly Extension[] = [Extension.Dts, Extension.Dcts, Extension.Dmts];
90959095
/** @internal */
90969096
export const supportedTSImplementationExtensions: readonly Extension[] = [Extension.Ts, Extension.Cts, Extension.Mts, Extension.Tsx];
9097+
/** @internal */
9098+
export const extensionsNotSupportingExtensionlessResolution: readonly Extension[] = [Extension.Mts, Extension.Dmts, Extension.Mjs, Extension.Cts, Extension.Dcts, Extension.Cjs];
90979099

90989100
/** @internal */
90999101
export function getSupportedExtensions(options?: CompilerOptions): readonly Extension[][];
@@ -9156,7 +9158,9 @@ export const enum ModuleSpecifierEnding {
91569158

91579159
/** @internal */
91589160
export function usesExtensionsOnImports({ imports }: SourceFile, hasExtension: (text: string) => boolean = or(hasJSFileExtension, hasTSFileExtension)): boolean {
9159-
return firstDefined(imports, ({ text }) => pathIsRelative(text) ? hasExtension(text) : undefined) || false;
9161+
return firstDefined(imports, ({ text }) => pathIsRelative(text) && !fileExtensionIsOneOf(text, extensionsNotSupportingExtensionlessResolution)
9162+
? hasExtension(text)
9163+
: undefined) || false;
91609164
}
91619165

91629166
/** @internal */
@@ -9197,6 +9201,10 @@ export function getModuleSpecifierEndingPreference(preference: UserPreferences["
91979201
emptyArray;
91989202
for (const specifier of specifiers) {
91999203
if (pathIsRelative(specifier)) {
9204+
if (fileExtensionIsOneOf(specifier, extensionsNotSupportingExtensionlessResolution)) {
9205+
// These extensions are not optional, so do not indicate a preference.
9206+
continue;
9207+
}
92009208
if (hasTSFileExtension(specifier)) {
92019209
return ModuleSpecifierEnding.TsExtension;
92029210
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: esnext
4+
// @moduleResolution: bundler
5+
6+
// @Filename: /a.mts
7+
//// export {};
8+
9+
// @Filename: /b.ts
10+
//// export {};
11+
12+
// @Filename: /c.ts
13+
//// export const c = 0;
14+
15+
// @Filename: /main.ts
16+
//// import {} from "./a.mjs";
17+
//// import {} from "./b";
18+
////
19+
//// c/**/;
20+
21+
verify.importFixModuleSpecifiers("", ["./c"]);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: esnext
4+
// @checkJs: true
5+
// @allowJs: true
6+
// @noEmit: true
7+
8+
// @Filename: /a.js
9+
//// export const a = 0;
10+
11+
// @Filename: /b.js
12+
//// export const b = 0;
13+
14+
// @Filename: /c.js
15+
//// import { a } from "./a.js";
16+
////
17+
//// b/**/;
18+
19+
verify.importFixModuleSpecifiers("", ["./b.js"]);

0 commit comments

Comments
 (0)