-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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[][]; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 If you look at the (video) repro in #52167, one of the existing imports in the big file had a |
||
} | ||
|
||
/** @internal */ | ||
|
@@ -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; | ||
} | ||
|
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"]); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
isclassic
(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 returnsModuleSpecifierEnding.Minimal
.