Skip to content

Fix two module specifier ending preference detection issues #53691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ function getPreferences(
return [ModuleSpecifierEnding.JsExtension];
}
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Classic) {
return [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension];
return preferredEnding === ModuleSpecifierEnding.JsExtension
? [ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Index]
: [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension];
Comment on lines -150 to +152
Copy link
Member Author

@andrewbranch andrewbranch Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change alone fixes the issue reported by @jespertheend in #52167, which was different from the OP’s issue. When --moduleResolution is classic (the unfortunate default of --module esnext), we simply weren’t respecting the inferred preference. classic resolution doesn’t support dropping /index.js suffixes, which is why it gets its own if block here that never returns ModuleSpecifierEnding.Minimal.

}
switch (preferredEnding) {
case ModuleSpecifierEnding.JsExtension: return [ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Minimal, ModuleSpecifierEnding.Index];
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9094,6 +9094,8 @@ const allSupportedExtensionsWithJson: readonly Extension[][] = [...allSupportedE
export const supportedDeclarationExtensions: readonly Extension[] = [Extension.Dts, Extension.Dcts, Extension.Dmts];
/** @internal */
export const supportedTSImplementationExtensions: readonly Extension[] = [Extension.Ts, Extension.Cts, Extension.Mts, Extension.Tsx];
/** @internal */
export const extensionsNotSupportingExtensionlessResolution: readonly Extension[] = [Extension.Mts, Extension.Dmts, Extension.Mjs, Extension.Cts, Extension.Dcts, Extension.Cjs];

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

/** @internal */
export function usesExtensionsOnImports({ imports }: SourceFile, hasExtension: (text: string) => boolean = or(hasJSFileExtension, hasTSFileExtension)): boolean {
return firstDefined(imports, ({ text }) => pathIsRelative(text) ? hasExtension(text) : undefined) || false;
return firstDefined(imports, ({ text }) => pathIsRelative(text) && !fileExtensionIsOneOf(text, extensionsNotSupportingExtensionlessResolution)
? hasExtension(text)
: undefined) || false;
Comment on lines -9159 to +9163
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change, and the similar one below, fixes the main issue of #52167. This code is used to detect a preference for module specifier endings from existing imports in a file. Previously, any relative-path module specifier that included an extension was taken as an indication that the user prefers including extensions. However, only .js/.ts extensions can ever be omitted—if an extension included .mjs or .cjs, for example, that cannot indicate a preference of the user because that file extension was required to make the import resolve. So we need to skip those if we’re looking for clues as to what the user prefers; we can only infer a preference if the user had a choice.

If you look at the (video) repro in #52167, one of the existing imports in the big file had a .mjs extension, which caused a new import added in that file to include an extension. When the user tried the same auto-import in an empty file, the import excluded the optional extension, because that’s the default in absence of any evidence that the user wants extensions.

}

/** @internal */
Expand Down Expand Up @@ -9197,6 +9201,10 @@ export function getModuleSpecifierEndingPreference(preference: UserPreferences["
emptyArray;
for (const specifier of specifiers) {
if (pathIsRelative(specifier)) {
if (fileExtensionIsOneOf(specifier, extensionsNotSupportingExtensionlessResolution)) {
// These extensions are not optional, so do not indicate a preference.
continue;
}
if (hasTSFileExtension(specifier)) {
return ModuleSpecifierEnding.TsExtension;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/fourslash/importNameCodeFixInferEndingPreference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />

// @module: esnext
// @moduleResolution: bundler

// @Filename: /a.mts
//// export {};

// @Filename: /b.ts
//// export {};

// @Filename: /c.ts
//// export const c = 0;

// @Filename: /main.ts
//// import {} from "./a.mjs";
//// import {} from "./b";
////
//// c/**/;

verify.importFixModuleSpecifiers("", ["./c"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />

// @module: esnext
// @checkJs: true
// @allowJs: true
// @noEmit: true

// @Filename: /a.js
//// export const a = 0;

// @Filename: /b.js
//// export const b = 0;

// @Filename: /c.js
//// import { a } from "./a.js";
////
//// b/**/;

verify.importFixModuleSpecifiers("", ["./b.js"]);