Fix two module specifier ending preference detection issues#53691
Fix two module specifier ending preference detection issues#53691andrewbranch merged 1 commit intomicrosoft:mainfrom
Conversation
| return [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension]; | ||
| return preferredEnding === ModuleSpecifierEnding.JsExtension | ||
| ? [ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Index] | ||
| : [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension]; |
There was a problem hiding this comment.
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.
| return firstDefined(imports, ({ text }) => pathIsRelative(text) ? hasExtension(text) : undefined) || false; | ||
| return firstDefined(imports, ({ text }) => pathIsRelative(text) && !fileExtensionIsOneOf(text, extensionsNotSupportingExtensionlessResolution) | ||
| ? hasExtension(text) | ||
| : undefined) || false; |
There was a problem hiding this comment.
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.
Fixes #52167