-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
add support for Extension Preference in import #25073
Conversation
src/compiler/moduleSpecifiers.ts
Outdated
@@ -3,11 +3,12 @@ | |||
namespace ts.moduleSpecifiers { | |||
export interface ModuleSpecifierPreferences { | |||
importModuleSpecifierPreference?: "relative" | "non-relative"; | |||
includingJsExtensionOnAutoImports?: boolean; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to handle .jsx
here as well.
also please add a test to it.
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.
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 @mhegazy @andy-ms could you give some suggestion please😢 |
src/compiler/moduleSpecifiers.ts
Outdated
@@ -3,11 +3,12 @@ | |||
namespace ts.moduleSpecifiers { | |||
export interface ModuleSpecifierPreferences { | |||
importModuleSpecifierPreference?: "relative" | "non-relative"; | |||
includeExtensionInImports?: Extension.Js | Extension.Jsx; | |||
} | |||
|
|||
// Note: fromSourceFile is just for usesJsExtensionOnImports |
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.
From #25074 (comment) -- maybe we shouldn't take fromSourceFile
as input and just use includeExtensionInImports
for that.
src/compiler/moduleSpecifiers.ts
Outdated
@@ -3,11 +3,12 @@ | |||
namespace ts.moduleSpecifiers { | |||
export interface ModuleSpecifierPreferences { | |||
importModuleSpecifierPreference?: "relative" | "non-relative"; | |||
includeExtensionInImports?: Extension.Js | Extension.Jsx; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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