add support for Extension Preference in import#25073
add support for Extension Preference in import#25073Kingwl wants to merge 2 commits intomicrosoft:masterfrom
Conversation
src/compiler/moduleSpecifiers.ts
Outdated
| namespace ts.moduleSpecifiers { | ||
| export interface ModuleSpecifierPreferences { | ||
| importModuleSpecifierPreference?: "relative" | "non-relative"; | ||
| includingJsExtensionOnAutoImports?: boolean; |
There was a problem hiding this comment.
it is not always .js, it can also be .jsx. so the name of the flag should not include Js. also this also applies to all LS operations and not only auto-imports for completions. i would just call it includeExtensionInImports
There was a problem hiding this comment.
in src/compiler/moduleSpecifiers L400 removeExtensionAndIndexPostFix
i saw it only handle the .js Extension, should i make a change for that to support jsx?
There was a problem hiding this comment.
There is also a minor problem in importNameCodeFix_jsExtension test case:
why could it import a file with .js extension even if that was a ts file,
There was a problem hiding this comment.
That was supported intentionally in #8895.
src/compiler/moduleSpecifiers.ts
Outdated
| function usesJsExtensionOnImports({ imports }: SourceFile): boolean { | ||
| return firstDefined(imports, ({ text }) => pathIsRelative(text) ? fileExtensionIs(text, Extension.Js) : undefined) || false; | ||
| function usesJsExtensionOnImports({ imports }: SourceFile, preferences: ModuleSpecifierPreferences): boolean { | ||
| return firstDefined(imports, ({ text }) => pathIsRelative(text) ? fileExtensionIs(text, Extension.Js) : undefined) || !!preferences.includingJsExtensionOnAutoImports; |
There was a problem hiding this comment.
we need to handle .jsx here as well.
also please add a test to it.
There was a problem hiding this comment.
I think an explicit (non-undefined) preference should always take priority over looking through the source file for an existing import.
|
@Andy-MS can you please review |
04f3e1a to
4d57419
Compare
|
i have try track the issue to understand that why the js or jsx extension been append after filename |
src/compiler/moduleSpecifiers.ts
Outdated
| includeExtensionInImports?: Extension.Js | Extension.Jsx; | ||
| } | ||
|
|
||
| // Note: fromSourceFile is just for usesJsExtensionOnImports |
There was a problem hiding this comment.
From #25074 (comment) -- maybe we shouldn't take fromSourceFile as input and just use includeExtensionInImports for that.
src/compiler/moduleSpecifiers.ts
Outdated
| namespace ts.moduleSpecifiers { | ||
| export interface ModuleSpecifierPreferences { | ||
| importModuleSpecifierPreference?: "relative" | "non-relative"; | ||
| includeExtensionInImports?: Extension.Js | Extension.Jsx; |
There was a problem hiding this comment.
It seems wrong for this to be configurable as always .js or .jsx. I think the right policy is: if the file you're importing from is a .ts or .js file you should import with .js (if the setting is on), if the file is .tsx or .jsx you should import with .jsx (if the setting is on). Then all the user has to set is: yes add extensions, or no don't add extensions.
There was a problem hiding this comment.
Actually, .tsx files are typically compiled to .js, right? So it would still make sense to use a .js import always? @mhegazy
There was a problem hiding this comment.
Actually, .tsx files are typically compiled to .js, right?
depends on the setting.. --JSX preserve will result in .jsx, --JSX react/--JSX react-native will result in .js.
d3d34b9 to
607daf9
Compare
|
@Andy-MS can you take another look. |
src/compiler/moduleSpecifiers.ts
Outdated
| return addJsExtension | ||
| ? noExtension + ".js" | ||
| const actualExtension = tryGetActualExtension(fileName, compilerOptions); | ||
| return (preferences.includeExtensionInImports && actualExtension) |
There was a problem hiding this comment.
If includeExtensionInImports is undefined (so not explicitly set), we should still be doing what the usesJsExtensionOnImports function did - context based method of determining which way we should go based on what's already there.
b449ff0 to
29d7c73
Compare
|
How to config the tsconfig? |
29d7c73 to
b24489f
Compare
|
@Andy-MS should i close this? |
|
@RyanCavanaugh is the milestone means i need to update the branch?🤦🏻♂️ |
|
update soon🚲 |
|
Seems already implements |
Fixes #24779